Alessandro Goller commited on
Commit
e2567ab
·
1 Parent(s): 3e3a10f

first deploy

Browse files
README.md DELETED
@@ -1,13 +0,0 @@
1
- ---
2
- title: FoodVision Mini
3
- emoji: 👁
4
- colorFrom: purple
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 3.50.2
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_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
+
15
+ # Create model
16
+ model, model_transforms = create_model(
17
+ num_classes=3, # len(class_names) would also work
18
+ )
19
+
20
+ # Load saved weights
21
+ model.load_state_dict(
22
+ torch.load(
23
+ f="pretrained_model.pth",
24
+ map_location=torch.device("cpu"), # load to CPU
25
+ )
26
+ )
27
+
28
+ # Create predict function
29
+ def predict(img) -> Tuple[Dict, float]:
30
+ """Transforms and performs a prediction on img and returns prediction and time taken.
31
+ """
32
+ # Start the timer
33
+ start_time = timer()
34
+
35
+ # Transform the target image and add a batch dimension
36
+ img = model_transforms(img).unsqueeze(0)
37
+
38
+ # Put model into evaluation mode and turn on inference mode
39
+ model.eval()
40
+ with torch.inference_mode():
41
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
42
+ pred_probs = torch.softmax(model(img), dim=1)
43
+
44
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
45
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
46
+
47
+ # Calculate the prediction time
48
+ pred_time = round(timer() - start_time, 5)
49
+
50
+ # Return the prediction dictionary and prediction time
51
+ return pred_labels_and_probs, pred_time
52
+
53
+ ### 4. Gradio app ###
54
+
55
+ # Create title, description and article strings
56
+ title = "FoodVision Mini 🍕🥩🍣"
57
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
58
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
59
+
60
+ # Create examples list from "examples/" directory
61
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
62
+
63
+ # Create the Gradio demo
64
+ demo = gr.Interface(fn=predict, # mapping function from input to output
65
+ inputs=gr.Image(type="pil"), # what are the inputs?
66
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
67
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
68
+ # Create examples list from "examples/" directory
69
+ examples=example_list,
70
+ title=title,
71
+ description=description,
72
+ article=article)
73
+
74
+ # Launch the demo!
75
+ demo.launch()
examples/1383396.jpg ADDED
examples/3757027.jpg ADDED
examples/441659.jpg ADDED
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_model(num_classes:int=3,
8
+ seed:int=42):
9
+ """Creates the feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the classifier head.
13
+ Defaults to 3.
14
+ seed (int, optional): random seed value. Defaults to 42.
15
+
16
+ Returns:
17
+ model (torch.nn.Module): EffNetB2 feature extractor model.
18
+ transforms (torchvision.transforms): EffNetB2 image transforms.
19
+ """
20
+ # Create EffNetB2 pretrained weights, transforms and model
21
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
22
+ transforms = weights.transforms()
23
+ model = torchvision.models.efficientnet_b2(weights=weights)
24
+
25
+ # Freeze all layers in base model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier = nn.Sequential(
32
+ nn.Dropout(p=0.3, inplace=True),
33
+ nn.Linear(in_features=1408, out_features=num_classes),
34
+ )
35
+
36
+ return model, transforms
pretrained_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2e6bf003d5bc481ed6499462f7158237bd74ad534bcca7cdefb7ad403345cdf6
3
+ size 31307450
requirements.txt ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ # via gradio
3
+ altair==5.1.2
4
+ # via gradio
5
+ annotated-types==0.6.0
6
+ # via pydantic
7
+ anyio==3.7.1
8
+ # via
9
+ # fastapi
10
+ # httpcore
11
+ # starlette
12
+ attrs==23.1.0
13
+ # via
14
+ # jsonschema
15
+ # referencing
16
+ certifi==2023.7.22
17
+ # via
18
+ # httpcore
19
+ # httpx
20
+ # requests
21
+ charset-normalizer==3.3.1
22
+ # via requests
23
+ click==8.1.7
24
+ # via uvicorn
25
+ colorama==0.4.6
26
+ # via
27
+ # click
28
+ # tqdm
29
+ contourpy==1.1.1
30
+ # via matplotlib
31
+ cycler==0.12.1
32
+ # via matplotlib
33
+ fastapi==0.104.0
34
+ # via gradio
35
+ ffmpy==0.3.1
36
+ # via gradio
37
+ filelock==3.12.4
38
+ # via
39
+ # huggingface-hub
40
+ # torch
41
+ fonttools==4.43.1
42
+ # via matplotlib
43
+ fsspec==2023.10.0
44
+ # via
45
+ # gradio-client
46
+ # huggingface-hub
47
+ # torch
48
+ gradio==3.50.2
49
+ # via -r requirements.in
50
+ gradio-client==0.6.1
51
+ # via gradio
52
+ h11==0.14.0
53
+ # via
54
+ # httpcore
55
+ # uvicorn
56
+ httpcore==0.18.0
57
+ # via httpx
58
+ httpx==0.25.0
59
+ # via
60
+ # gradio
61
+ # gradio-client
62
+ huggingface-hub==0.18.0
63
+ # via
64
+ # gradio
65
+ # gradio-client
66
+ idna==3.4
67
+ # via
68
+ # anyio
69
+ # httpx
70
+ # requests
71
+ importlib-resources==6.1.0
72
+ # via gradio
73
+ jinja2==3.1.2
74
+ # via
75
+ # altair
76
+ # gradio
77
+ # torch
78
+ jsonschema==4.19.1
79
+ # via altair
80
+ jsonschema-specifications==2023.7.1
81
+ # via jsonschema
82
+ kiwisolver==1.4.5
83
+ # via matplotlib
84
+ markupsafe==2.1.3
85
+ # via
86
+ # gradio
87
+ # jinja2
88
+ matplotlib==3.8.0
89
+ # via gradio
90
+ mpmath==1.3.0
91
+ # via sympy
92
+ networkx==3.2
93
+ # via torch
94
+ numpy==1.26.1
95
+ # via
96
+ # altair
97
+ # contourpy
98
+ # gradio
99
+ # matplotlib
100
+ # pandas
101
+ # torchvision
102
+ orjson==3.9.9
103
+ # via gradio
104
+ packaging==23.2
105
+ # via
106
+ # altair
107
+ # gradio
108
+ # gradio-client
109
+ # huggingface-hub
110
+ # matplotlib
111
+ pandas==2.1.1
112
+ # via
113
+ # altair
114
+ # gradio
115
+ pillow==10.1.0
116
+ # via
117
+ # gradio
118
+ # matplotlib
119
+ # torchvision
120
+ pydantic==2.4.2
121
+ # via
122
+ # fastapi
123
+ # gradio
124
+ # pydantic-settings
125
+ pydantic-core==2.10.1
126
+ # via pydantic
127
+ pydantic-settings==2.0.3
128
+ # via -r requirements.in
129
+ pydub==0.25.1
130
+ # via gradio
131
+ pyparsing==3.1.1
132
+ # via matplotlib
133
+ python-dateutil==2.8.2
134
+ # via
135
+ # matplotlib
136
+ # pandas
137
+ python-dotenv==1.0.0
138
+ # via
139
+ # -r requirements.in
140
+ # pydantic-settings
141
+ python-multipart==0.0.6
142
+ # via gradio
143
+ pytz==2023.3.post1
144
+ # via pandas
145
+ pyyaml==6.0.1
146
+ # via
147
+ # gradio
148
+ # huggingface-hub
149
+ referencing==0.30.2
150
+ # via
151
+ # jsonschema
152
+ # jsonschema-specifications
153
+ requests==2.31.0
154
+ # via
155
+ # gradio
156
+ # gradio-client
157
+ # huggingface-hub
158
+ # torchvision
159
+ rpds-py==0.10.6
160
+ # via
161
+ # jsonschema
162
+ # referencing
163
+ semantic-version==2.10.0
164
+ # via gradio
165
+ six==1.16.0
166
+ # via python-dateutil
167
+ sniffio==1.3.0
168
+ # via
169
+ # anyio
170
+ # httpcore
171
+ # httpx
172
+ starlette==0.27.0
173
+ # via fastapi
174
+ sympy==1.12
175
+ # via torch
176
+ toolz==0.12.0
177
+ # via altair
178
+ torch==2.1.0
179
+ # via
180
+ # -r requirements.in
181
+ # torchvision
182
+ torchvision==0.16.0
183
+ # via -r requirements.in
184
+ tqdm==4.66.1
185
+ # via huggingface-hub
186
+ typing-extensions==4.8.0
187
+ # via
188
+ # fastapi
189
+ # gradio
190
+ # gradio-client
191
+ # huggingface-hub
192
+ # pydantic
193
+ # pydantic-core
194
+ # torch
195
+ tzdata==2023.3
196
+ # via pandas
197
+ urllib3==2.0.7
198
+ # via requests
199
+ uvicorn==0.23.2
200
+ # via gradio
201
+ websockets==11.0.3
202
+ # via
203
+ # gradio
204
+ # gradio-client