KajetanFrackowiak commited on
Commit
fc1ae6e
·
verified ·
1 Parent(s): 680ff78

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +56 -0
  2. model.py +27 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+ class_names = ["pizza", "steak", "sushi"]
10
+
11
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
12
+ num_classes=3
13
+ )
14
+
15
+ effnetb2.load_state_dict(
16
+ torch.load(
17
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
18
+ map_location=torch.device("cpu")
19
+ )
20
+ )
21
+
22
+
23
+ def predict(img) -> Tuple[Dict, float]:
24
+ """Transforms and performs a prediction on img and returns prediction and time taken."""
25
+ start_time = timer()
26
+ img = effnetb2_transforms(img).unsqueeze(0)
27
+
28
+ effnetb2.eval()
29
+ with torch.inference_mode():
30
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
31
+
32
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
33
+ pred_time = round(timer() - start_time, 5)
34
+
35
+ return pred_labels_and_probs, pred_time
36
+
37
+
38
+ title = "FoodVision Mini 🍕🥩🍣"
39
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
40
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
41
+
42
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
43
+
44
+ # Create the Gradio demo
45
+ demo = gr.Interface(fn=predict, # mapping function from input to output
46
+ inputs=gr.Image(type="pil"), # what are the inputs?
47
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
48
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
49
+ # Create examples list from "examples/" directory
50
+ examples=example_list,
51
+ title=title,
52
+ description=description,
53
+ article=article)
54
+
55
+ # Launch the demo!
56
+ demo.launch(share=True)
model.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes:int=3,
7
+ seed:int=42):
8
+ """Creates an EfficientNetB2 feature extractor model and transforms.
9
+
10
+ Args:
11
+ num_classes (int, optional): number of classes in the classifier head.
12
+ Defaults to 3.
13
+ seed (int, optional): random seed value. Defaults to 42.
14
+ """
15
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
16
+ transforms = weights.transforms()
17
+ model = torchvision.models.efficientnet_b2(weights=weights)
18
+
19
+ for param in model.parameters():
20
+ param.requires_grad = False
21
+
22
+ torch.manual_seed(seed)
23
+ model.classifier = nn.Sequential(
24
+ nn.Dropout(p=0.2, inplace=True),
25
+ nn.Linear(in_features=1408, out_features=num_classes),
26
+ )
27
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch>=1.12.0
2
+ torchvision>=0.13.0
3
+ gradio>=3.1.4