Spaces:
Sleeping
Sleeping
Commit
·
13f3d4d
1
Parent(s):
a41ef0e
commit FoodVision_Big app file
Browse files- .gitattributes +2 -0
- .idea/.gitignore +8 -0
- .idea/foodvision_mini.iml +10 -0
- .idea/inspectionProfiles/Project_Default.xml +6 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/workspace.xml +43 -0
- 11_pretrained_effnet_feature_extractor_food101.pth +3 -0
- README.md +4 -4
- app.py +171 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +30 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ 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 |
+
10_pretrained_effnetb2_features_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
|
37 |
+
11_pretrained_effnet_feature_extractor_food101.pth filter=lfs diff=lfs merge=lfs -text
|
.idea/.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# Editor-based HTTP Client requests
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
.idea/foodvision_mini.iml
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$">
|
5 |
+
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
6 |
+
</content>
|
7 |
+
<orderEntry type="inheritedJdk" />
|
8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
9 |
+
</component>
|
10 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
5 |
+
</profile>
|
6 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (foodvision_mini)" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/foodvision_mini.iml" filepath="$PROJECT_DIR$/.idea/foodvision_mini.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/workspace.xml
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ChangeListManager">
|
4 |
+
<list default="true" id="874278db-f039-4e0e-8d7a-4102431c0503" name="Changes" comment="" />
|
5 |
+
<option name="SHOW_DIALOG" value="false" />
|
6 |
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
7 |
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
8 |
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
9 |
+
</component>
|
10 |
+
<component name="ProjectId" id="2ZkdZNmEIConALOD57a0VDJ8gas" />
|
11 |
+
<component name="ProjectViewState">
|
12 |
+
<option name="hideEmptyMiddlePackages" value="true" />
|
13 |
+
<option name="showLibraryContents" value="true" />
|
14 |
+
</component>
|
15 |
+
<component name="PropertiesComponent"><![CDATA[{
|
16 |
+
"keyToString": {
|
17 |
+
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
18 |
+
"RunOnceActivity.ShowReadmeOnStart": "true",
|
19 |
+
"WebServerToolWindowFactoryState": "false",
|
20 |
+
"last_opened_file_path": "C:/Users/Dr.Kareem Kamal/Desktop/foodvision_mini",
|
21 |
+
"node.js.detected.package.eslint": "true",
|
22 |
+
"node.js.detected.package.tslint": "true",
|
23 |
+
"node.js.selected.package.eslint": "(autodetect)",
|
24 |
+
"node.js.selected.package.tslint": "(autodetect)",
|
25 |
+
"vue.rearranger.settings.migration": "true"
|
26 |
+
}
|
27 |
+
}]]></component>
|
28 |
+
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
29 |
+
<component name="TaskManager">
|
30 |
+
<task active="true" id="Default" summary="Default task">
|
31 |
+
<changelist id="874278db-f039-4e0e-8d7a-4102431c0503" name="Changes" comment="" />
|
32 |
+
<created>1702970065447</created>
|
33 |
+
<option name="number" value="Default" />
|
34 |
+
<option name="presentableId" value="Default" />
|
35 |
+
<updated>1702970065447</updated>
|
36 |
+
<workItem from="1702970066529" duration="466000" />
|
37 |
+
</task>
|
38 |
+
<servers />
|
39 |
+
</component>
|
40 |
+
<component name="TypeScriptGeneratedFilesManager">
|
41 |
+
<option name="version" value="3" />
|
42 |
+
</component>
|
43 |
+
</project>
|
11_pretrained_effnet_feature_extractor_food101.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d12a8ccfa02ab463a8a3dcb7106c3fc9d5376b8a34932778841a1b5f9ef70e81
|
3 |
+
size 31849786
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title: FoodVision
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.10.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
+
title: FoodVision Mini
|
3 |
+
emoji: 🌖
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.10.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from timeit import default_timer as timer
|
7 |
+
from model import create_effnetb2_model
|
8 |
+
from typing import Tuple , Dict
|
9 |
+
|
10 |
+
# 1.Import and class names setup
|
11 |
+
class_names = ['apple_pie',
|
12 |
+
'baby_back_ribs',
|
13 |
+
'baklava',
|
14 |
+
'beef_carpaccio',
|
15 |
+
'beef_tartare',
|
16 |
+
'beet_salad',
|
17 |
+
'beignets',
|
18 |
+
'bibimbap',
|
19 |
+
'bread_pudding',
|
20 |
+
'breakfast_burrito',
|
21 |
+
'bruschetta',
|
22 |
+
'caesar_salad',
|
23 |
+
'cannoli',
|
24 |
+
'caprese_salad',
|
25 |
+
'carrot_cake',
|
26 |
+
'ceviche',
|
27 |
+
'cheese_plate',
|
28 |
+
'cheesecake',
|
29 |
+
'chicken_curry',
|
30 |
+
'chicken_quesadilla',
|
31 |
+
'chicken_wings',
|
32 |
+
'chocolate_cake',
|
33 |
+
'chocolate_mousse',
|
34 |
+
'churros',
|
35 |
+
'clam_chowder',
|
36 |
+
'club_sandwich',
|
37 |
+
'crab_cakes',
|
38 |
+
'creme_brulee',
|
39 |
+
'croque_madame',
|
40 |
+
'cup_cakes',
|
41 |
+
'deviled_eggs',
|
42 |
+
'donuts',
|
43 |
+
'dumplings',
|
44 |
+
'edamame',
|
45 |
+
'eggs_benedict',
|
46 |
+
'escargots',
|
47 |
+
'falafel',
|
48 |
+
'filet_mignon',
|
49 |
+
'fish_and_chips',
|
50 |
+
'foie_gras',
|
51 |
+
'french_fries',
|
52 |
+
'french_onion_soup',
|
53 |
+
'french_toast',
|
54 |
+
'fried_calamari',
|
55 |
+
'fried_rice',
|
56 |
+
'frozen_yogurt',
|
57 |
+
'garlic_bread',
|
58 |
+
'gnocchi',
|
59 |
+
'greek_salad',
|
60 |
+
'grilled_cheese_sandwich',
|
61 |
+
'grilled_salmon',
|
62 |
+
'guacamole',
|
63 |
+
'gyoza',
|
64 |
+
'hamburger',
|
65 |
+
'hot_and_sour_soup',
|
66 |
+
'hot_dog',
|
67 |
+
'huevos_rancheros',
|
68 |
+
'hummus',
|
69 |
+
'ice_cream',
|
70 |
+
'lasagna',
|
71 |
+
'lobster_bisque',
|
72 |
+
'lobster_roll_sandwich',
|
73 |
+
'macaroni_and_cheese',
|
74 |
+
'macarons',
|
75 |
+
'miso_soup',
|
76 |
+
'mussels',
|
77 |
+
'nachos',
|
78 |
+
'omelette',
|
79 |
+
'onion_rings',
|
80 |
+
'oysters',
|
81 |
+
'pad_thai',
|
82 |
+
'paella',
|
83 |
+
'pancakes',
|
84 |
+
'panna_cotta',
|
85 |
+
'peking_duck',
|
86 |
+
'pho',
|
87 |
+
'pizza',
|
88 |
+
'pork_chop',
|
89 |
+
'poutine',
|
90 |
+
'prime_rib',
|
91 |
+
'pulled_pork_sandwich',
|
92 |
+
'ramen',
|
93 |
+
'ravioli',
|
94 |
+
'red_velvet_cake',
|
95 |
+
'risotto',
|
96 |
+
'samosa',
|
97 |
+
'sashimi',
|
98 |
+
'scallops',
|
99 |
+
'seaweed_salad',
|
100 |
+
'shrimp_and_grits',
|
101 |
+
'spaghetti_bolognese',
|
102 |
+
'spaghetti_carbonara',
|
103 |
+
'spring_rolls',
|
104 |
+
'steak',
|
105 |
+
'strawberry_shortcake',
|
106 |
+
'sushi',
|
107 |
+
'tacos',
|
108 |
+
'takoyaki',
|
109 |
+
'tiramisu',
|
110 |
+
'tuna_tartare',
|
111 |
+
'waffles']
|
112 |
+
|
113 |
+
from torchvision.models._api import WeightsEnum
|
114 |
+
from torch.hub import load_state_dict_from_url
|
115 |
+
def get_state_dict(self, *args, **kwargs):
|
116 |
+
kwargs.pop("check_hash")
|
117 |
+
return load_state_dict_from_url(self.url, *args, **kwargs)
|
118 |
+
WeightsEnum.get_state_dict = get_state_dict
|
119 |
+
|
120 |
+
# 2. Model annd transforms prepration
|
121 |
+
effnetb2_model , effnet_b2_transforms = create_effnetb2_model(num_classes = 101, seed = 42)
|
122 |
+
|
123 |
+
# Load save weights
|
124 |
+
effnetb2_model.load_state_dict(
|
125 |
+
torch.load(
|
126 |
+
f='11_pretrained_effnet_feature_extractor_food101.pth',
|
127 |
+
map_location = torch.device('cpu') # Load the model on CPU
|
128 |
+
)
|
129 |
+
)
|
130 |
+
# 3.prediction function (predict())
|
131 |
+
def predict(img) -> Tuple[Dict,float] :
|
132 |
+
|
133 |
+
start_time = timer()
|
134 |
+
|
135 |
+
image = effnet_b2_transforms(img).unsqueeze(0)
|
136 |
+
|
137 |
+
|
138 |
+
effnetb2_model.eval()
|
139 |
+
with torch.inference_mode():
|
140 |
+
|
141 |
+
pred_probs = torch.softmax(effnetb2_model(image) , dim=1)
|
142 |
+
pred_label_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range (len(class_names))}
|
143 |
+
|
144 |
+
end_time = timer()
|
145 |
+
pred_time = round(end_time - start_time , 4)
|
146 |
+
|
147 |
+
return pred_label_and_probs , pred_time
|
148 |
+
|
149 |
+
### 4. Gradio app - our Gradio interface + launch command
|
150 |
+
|
151 |
+
title = 'FoodVision Big'
|
152 |
+
description = 'An EfficientNetB2 Feature extractor computer vision model to classifiy 101 Food images '
|
153 |
+
article = 'created at PyTorch Model Deployment'
|
154 |
+
|
155 |
+
# Create example list
|
156 |
+
|
157 |
+
example_list = [['examples/'+ example] for example in os.listdir('examples')]
|
158 |
+
example_list
|
159 |
+
|
160 |
+
# create a gradio demo
|
161 |
+
demo = gr.Interface(fn=predict ,
|
162 |
+
inputs=gr.Image(type='pil'),
|
163 |
+
outputs=[gr.Label(num_top_classes = 3 , label= 'prediction'),
|
164 |
+
gr.Number(label= 'Prediction time (s)')],
|
165 |
+
examples = example_list,
|
166 |
+
title = title,
|
167 |
+
description = description,
|
168 |
+
article= article)
|
169 |
+
|
170 |
+
# Launch the demo
|
171 |
+
demo.launch(debug= False)
|
examples/2582289.jpg
ADDED
![]() |
examples/3622237.jpg
ADDED
![]() |
examples/592799.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
from torchvision.models._api import WeightsEnum
|
7 |
+
from torch.hub import load_state_dict_from_url
|
8 |
+
def get_state_dict(self, *args, **kwargs):
|
9 |
+
kwargs.pop("check_hash")
|
10 |
+
return load_state_dict_from_url(self.url, *args, **kwargs)
|
11 |
+
WeightsEnum.get_state_dict = get_state_dict
|
12 |
+
|
13 |
+
def create_effnetb2_model(num_classes : int = 3,
|
14 |
+
seed : int = 42):
|
15 |
+
|
16 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
17 |
+
transform = weights.transforms()
|
18 |
+
model = torchvision.models.efficientnet_b2(weights= weights)
|
19 |
+
|
20 |
+
|
21 |
+
for param in model.parameters():
|
22 |
+
param.requires_grad = False
|
23 |
+
|
24 |
+
torch.manual_seed(seed)
|
25 |
+
model.classifier = torch.nn.Sequential(
|
26 |
+
torch.nn.Dropout(p=0.3, inplace= True),
|
27 |
+
torch.nn.Linear(in_features = 1408,
|
28 |
+
out_features = num_classes)
|
29 |
+
)
|
30 |
+
return model , transform
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.1.0
|
2 |
+
torchvision==0.16.0
|
3 |
+
gradio==4.10.0
|