Manoj779944 commited on
Commit
2533c58
·
1 Parent(s): c669497

Added my Gradio app 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:f38043ceac7cfc1e790ed9dbccfff42c320b7d7b6f7bbfb9ce1249cd329613af
3
+ size 31314554
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as Timer
7
+ from typing import Tuple,Dict
8
+ class_names=["pizza","steak","sushi"]
9
+ effnetb2,effnetb2_transforms=create_effnetb2_model(num_classes=3)
10
+ effnetb2.load_state_dict(
11
+ torch.load(
12
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
13
+ map_location=torch.device("cpu")
14
+ )
15
+ )g
16
+
17
+ def predict(img) -> Tuple[Dict,float]:
18
+ start_time = timer()
19
+ img = effnetb2_transforms(img).unsqueeze(0)
20
+
21
+ effnetb2.eval()
22
+ with torch.inference_mode():
23
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
24
+
25
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
26
+ pred_time = round(timer() - start_time, 5)
27
+ return pred_labels_and_probs, pred_time
28
+
29
+
30
+ title = "FoodVision Mini 🍕🥩🍣"
31
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
32
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
33
+
34
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
35
+
36
+ demo = gr.Interface(fn=predict,
37
+ inputs=gr.Image(type="pil"),
38
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
39
+ gr.Number(label="Prediction time (s)")],
40
+ examples=example_list,
41
+ title=title,
42
+ description=description,
43
+ article=article)
44
+
45
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb2_model(num_classes:int=3,
8
+ seed:int=42):
9
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
10
+ transforms = weights.transforms()
11
+ model = torchvision.models.efficientnet_b2(weights=weights)
12
+
13
+ for param in model.parameters():
14
+ param.requires_grad = False
15
+
16
+ torch.manual_seed(seed)
17
+ model.classifier = nn.Sequential(
18
+ nn.Dropout(p=0.3, inplace=True),
19
+ nn.Linear(in_features=1408, out_features=num_classes),
20
+ )
21
+
22
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==latest
2
+ torchvision==latest
3
+ gradio==3.45.0