Priyanshuchaudhary2425
commited on
Commit
β’
3a4a022
1
Parent(s):
759a397
first update
Browse files- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +73 -0
- model.py +25 -0
- requirements.txt +3 -0
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:a508508ab683517c16a562d715298e6b49d6f478f9f6a2a60e55836ad95a7553
|
3 |
+
size 31273033
|
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
### 1. Imports and class names setup ###
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import torch
|
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 preparation ###
|
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. Predicti function ###
|
26 |
+
def predict(img) -> Tuple[Dict, float]:
|
27 |
+
# Start a timer
|
28 |
+
start_time = timer()
|
29 |
+
|
30 |
+
# Transform the input image for use with EffNetB2
|
31 |
+
img = effnetb2_transforms(img).unsqueeze(0) # uqueeze = add batch dimension on 0th index
|
32 |
+
|
33 |
+
# Put model into eval mode, make prediction
|
34 |
+
effnetb2.eval()
|
35 |
+
with torch.inference_mode():
|
36 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
37 |
+
|
38 |
+
# Create a prediction label and prediction probability dictionary
|
39 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
40 |
+
|
41 |
+
# Calculate pred time
|
42 |
+
end_time = timer()
|
43 |
+
pred_time = round(end_time-start_time, 4)
|
44 |
+
|
45 |
+
# Return pred dict and pred time
|
46 |
+
return pred_labels_and_probs, pred_time
|
47 |
+
|
48 |
+
|
49 |
+
### 4. Gradio app ###
|
50 |
+
|
51 |
+
import gradio as gr
|
52 |
+
|
53 |
+
# Create title, description and article strings
|
54 |
+
title = "FoodVision Mini ππ₯©π£"
|
55 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
56 |
+
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
57 |
+
|
58 |
+
# Create example list
|
59 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
60 |
+
|
61 |
+
|
62 |
+
# Create the Gradio demo
|
63 |
+
demo = gr.Interface(fn=predict, # Maps inputs to outputs
|
64 |
+
inputs=gr.Image(type="pil"),
|
65 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
66 |
+
gr.Number(label="Prediction time (s)")],
|
67 |
+
examples=example_list,
|
68 |
+
title=title,
|
69 |
+
description=description,
|
70 |
+
article=article)
|
71 |
+
|
72 |
+
demo.launch(debug=False, # print errors locally?
|
73 |
+
share=True) # generate a publically shareable URL
|
model.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes:int=3,
|
7 |
+
seed:int=42):
|
8 |
+
|
9 |
+
# 1, 2, 3. Create EffNetB2 pretrained weights, transforms and model
|
10 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
11 |
+
transforms = weights.transforms()
|
12 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
13 |
+
|
14 |
+
# 4. Freeze all layers in base model
|
15 |
+
for param in model.parameters():
|
16 |
+
param.requires_grad = False
|
17 |
+
|
18 |
+
# 5. Change classifier head with random seed for reproducibility
|
19 |
+
torch.manual_seed(seed)
|
20 |
+
model.classifier = nn.Sequential(
|
21 |
+
nn.Dropout(p=0.3, inplace=True),
|
22 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
23 |
+
)
|
24 |
+
|
25 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==4.12.0
|