Victorano commited on
Commit
42f61ce
·
1 Parent(s): 4521eba

gradio demo working fine

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ 07_effnetb0_data_20_percent_10_epochs.pth filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
07_effnetb0_data_20_percent_10_epochs.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7b1d222b609c6e0928d890cc4a17d429727e9623503d5a478b6cf6f50cba69f2
3
+ size 16354890
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_effnetb0_model
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+
10
+ class_names = ["pizza", "steak", "sushi"]
11
+
12
+ effnetb0, effnetb0_transforms = create_effnetb0_model()
13
+
14
+ effnetb0.load_state_dict(torch.load(
15
+ "07_effnetb0_data_20_percent_10_epochs.pth"))
16
+
17
+
18
+ def predict(img) -> Tuple[Dict, float]:
19
+ pred_list = []
20
+
21
+ pred_dict = {}
22
+
23
+ start_time = timer()
24
+
25
+ img = effnetb0_transforms(img).unsqueeze(0)
26
+
27
+ effnetb0.eval()
28
+
29
+ with torch.inference_mode():
30
+ pred_probs = torch.softmax(effnetb0(img), dim=1)
31
+
32
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
33
+ pred_labels_and_probs = {class_names[i]: float(
34
+ pred_probs[0][i]) for i in range(len(class_names))}
35
+
36
+ pred_time = round(timer() - start_time, 4)
37
+
38
+ pred_list.append(pred_dict)
39
+
40
+ return pred_labels_and_probs, pred_time
41
+
42
+
43
+ title = "FoodVision Mini 🍕🥩🍣"
44
+ description = "An EfficientNetB0 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
45
+ article = "Full Source code from scratch [deployment.ipynb](https://github.com/Victoran0/food-vision.git)."
46
+
47
+ # Create examples list from "examples/" directory
48
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
49
+
50
+ demo = gr.Interface(fn=predict, inputs=gr.Image(type='pil'), outputs=[gr.Label(num_top_classes=3, label='Predictions'), gr.Number(
51
+ label="Prediction time (s)")], examples=example_list, title=title, description=description, article=article)
52
+
53
+ 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
+ def create_effnetb0_model(num_classes: int=3, seed: int=42):
7
+ effnetb0_weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
8
+ effnetb0_transforms = effnetb0_weights.transforms()
9
+ effnetb0 = torchvision.models.efficientnet_b0(weights=effnetb0_weights)
10
+
11
+ for param in effnetb0.parameters():
12
+ param.required_grad = False
13
+
14
+ torch.manual_seed(seed)
15
+
16
+ effnetb0.classifier = nn.Sequential(
17
+ nn.Dropout(p=0.3, inplace=True),
18
+ nn.Linear(in_features=1280, out_features=num_classes)
19
+ )
20
+
21
+ return effnetb0, effnetb0_transforms
22
+
playground.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import os
2
+
3
+
4
+ print(os.listdir("C:/Users/User/Data Science/Deep Learning/Pytorch/food-vision/data/pizza_steak_sushi_20_percent"))
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.4.1
2
+ torchvision==0.19.1
3
+ gradio==4.44.0