K1Jamali commited on
Commit
ecc568d
·
verified ·
1 Parent(s): 8986414

Food Vision Mini Uploading from local PC

Browse files
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7feb9cedee2bbb2fa5c5a115293e6db9d5dff1b7e263bbba7eef6f9079832e3c
3
+ size 31305309
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
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
+
9
+ class_names = ["pizza", "steake", "sushi"]
10
+
11
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
12
+
13
+ effnetb2.load_state_dict(
14
+ torch.load(f"09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20.pth",
15
+ map_location=torch.device("cpu"))
16
+ )
17
+
18
+ def predict(img) -> tuple[dict, float]:
19
+ start_time = timer()
20
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th dimension
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
+
27
+ pred_time = round(timer() - start_time, 4)
28
+
29
+ return pred_labels_and_probs, pred_time
30
+
31
+
32
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
33
+
34
+
35
+ title = "FoodVision Mini😊"
36
+ description = "An [EffNetB2 feature extractor](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html) computer vision model to classify images as pizza, steak and sushi"
37
+ article = "Create at [09. PyTorch Model Deployment](http://keivanjamali.com)."
38
+
39
+ demo = gr.Interface(fn=predict,
40
+ inputs=gr.Image(type="pil"),
41
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
42
+ gr.Number(label="Prediction Time (s)")],
43
+ examples=example_list,
44
+ title=title,
45
+ description=description,
46
+ article=article)
47
+
48
+ demo.launch(debug=False, share=True)
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
5
+
6
+ def create_effnetb2_model(num_classes:int=3,
7
+ seed:int=42):
8
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
9
+ transforms = weights.transforms()
10
+ model = torchvision.models.efficientnet_b2(weights=weights)
11
+
12
+ for param in model.parameters():
13
+ param.requires_grad = False
14
+
15
+ torch.manual_seed(seed)
16
+ model.classifier = nn.Sequential(
17
+ nn.Dropout(p=0.3, inplace=True),
18
+ nn.Linear(in_features=1408, out_features=num_classes)
19
+ )
20
+
21
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.0.1
2
+ torchvision==0.15.2
3
+ gradio==4.28.3