ADI2005 commited on
Commit
532154d
β€’
1 Parent(s): 21e69c5

Upload 7 files

Browse files
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:232b9150391e812a5c6ecba4348eb35c649f8c8baa1a390ecea7f8c6f5def965
3
+ size 31307450
2582289.jpg ADDED
3622237.jpg ADDED
592799.jpg ADDED
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb2_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ class_names = ['pizza', 'steak', 'sushi']
12
+
13
+ ### 2. Model and transforms perparation ###
14
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
15
+ num_classes=3)
16
+
17
+ # Load save weights
18
+ effnetb2.load_state_dict(
19
+ torch.load(
20
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
21
+ map_location=torch.device("cpu") # load the model to the CPU
22
+ )
23
+ )
24
+
25
+ ### 3. Predict function ###
26
+
27
+ def predict(img) -> Tuple[Dict, float]:
28
+ # Start a timer
29
+ start_time = timer()
30
+
31
+ # Transform the input image for use with EffNetB2
32
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index
33
+
34
+ # Put model into eval mode, make prediction
35
+ effnetb2.eval()
36
+ with torch.inference_mode():
37
+ # Pass transformed image through the model and turn the prediction logits into probaiblities
38
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
39
+
40
+ # Create a prediction label and prediction probability dictionary
41
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
42
+
43
+ # Calculate pred time
44
+ end_time = timer()
45
+ pred_time = round(end_time - start_time, 4)
46
+
47
+ # Return pred dict and pred time
48
+ return pred_labels_and_probs, pred_time
49
+
50
+ ### 4. Gradio app ###
51
+
52
+ # Create title, description and article
53
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
54
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model to classify images as pizza, steak or sushi."
55
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/#74-building-a-gradio-interface)."
56
+
57
+ # Create example list
58
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
59
+
60
+ # Create the Gradio demo
61
+ demo = gr.Interface(fn=predict, # maps inputs to outputs
62
+ inputs=gr.Image(type="pil"),
63
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
64
+ gr.Number(label="Prediction time (s)")],
65
+ examples=example_list,
66
+ title=title,
67
+ description=description,
68
+ article=article)
69
+
70
+ # Launch the demo!
71
+ demo.launch()
model.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # 1, 2, 3 Create EffNetB2 pretrained weights, transforms and model
9
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
10
+ transforms = weights.transforms()
11
+ model = torchvision.models.efficientnet_b2(weights=weights)
12
+
13
+ # 4. Freeze all layers in the base model
14
+ for param in model.parameters():
15
+ param.requires_grad = False
16
+
17
+ # 5. Change classifier head with random seed for reproducibility
18
+ torch.manual_seed(seed)
19
+ model.classifier = nn.Sequential(
20
+ nn.Dropout(p=0.3, inplace=True),
21
+ nn.Linear(in_features=1408, out_features=num_classes)
22
+ )
23
+
24
+ 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