Spaces:
Runtime error
Runtime error
Maternetworkin87
commited on
Commit
β’
cadb3ef
1
Parent(s):
e232701
Upload 7 files
Browse files- 100sports.pth +3 -0
- app.py +176 -0
- examples/1.jpg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpg +0 -0
- model.py +39 -0
- requirements.txt +3 -0
100sports.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:286c72f304b5bb5615f8d0592d12fb511fe2ef6791c3df2cc22b7dad429f0f92
|
3 |
+
size 43955834
|
app.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
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 |
+
from typing import Tuple, Dict
|
9 |
+
|
10 |
+
# Setup class names
|
11 |
+
class_names = ['air hockey',
|
12 |
+
'ampute football',
|
13 |
+
'archery',
|
14 |
+
'arm wrestling',
|
15 |
+
'axe throwing',
|
16 |
+
'balance beam',
|
17 |
+
'barell racing',
|
18 |
+
'baseball',
|
19 |
+
'basketball',
|
20 |
+
'baton twirling',
|
21 |
+
'bike polo',
|
22 |
+
'billiards',
|
23 |
+
'bmx',
|
24 |
+
'bobsled',
|
25 |
+
'bowling',
|
26 |
+
'boxing',
|
27 |
+
'bull riding',
|
28 |
+
'bungee jumping',
|
29 |
+
'canoe slamon',
|
30 |
+
'cheerleading',
|
31 |
+
'chuckwagon racing',
|
32 |
+
'cricket',
|
33 |
+
'croquet',
|
34 |
+
'curling',
|
35 |
+
'disc golf',
|
36 |
+
'fencing',
|
37 |
+
'field hockey',
|
38 |
+
'figure skating men',
|
39 |
+
'figure skating pairs',
|
40 |
+
'figure skating women',
|
41 |
+
'fly fishing',
|
42 |
+
'football',
|
43 |
+
'formula 1 racing',
|
44 |
+
'frisbee',
|
45 |
+
'gaga',
|
46 |
+
'giant slalom',
|
47 |
+
'golf',
|
48 |
+
'hammer throw',
|
49 |
+
'hang gliding',
|
50 |
+
'harness racing',
|
51 |
+
'high jump',
|
52 |
+
'hockey',
|
53 |
+
'horse jumping',
|
54 |
+
'horse racing',
|
55 |
+
'horseshoe pitching',
|
56 |
+
'hurdles',
|
57 |
+
'hydroplane racing',
|
58 |
+
'ice climbing',
|
59 |
+
'ice yachting',
|
60 |
+
'jai alai',
|
61 |
+
'javelin',
|
62 |
+
'jousting',
|
63 |
+
'judo',
|
64 |
+
'lacrosse',
|
65 |
+
'log rolling',
|
66 |
+
'luge',
|
67 |
+
'motorcycle racing',
|
68 |
+
'mushing',
|
69 |
+
'nascar racing',
|
70 |
+
'olympic wrestling',
|
71 |
+
'parallel bar',
|
72 |
+
'pole climbing',
|
73 |
+
'pole dancing',
|
74 |
+
'pole vault',
|
75 |
+
'polo',
|
76 |
+
'pommel horse',
|
77 |
+
'rings',
|
78 |
+
'rock climbing',
|
79 |
+
'roller derby',
|
80 |
+
'rollerblade racing',
|
81 |
+
'rowing',
|
82 |
+
'rugby',
|
83 |
+
'sailboat racing',
|
84 |
+
'shot put',
|
85 |
+
'shuffleboard',
|
86 |
+
'sidecar racing',
|
87 |
+
'ski jumping',
|
88 |
+
'sky surfing',
|
89 |
+
'skydiving',
|
90 |
+
'snow boarding',
|
91 |
+
'snowmobile racing',
|
92 |
+
'speed skating',
|
93 |
+
'steer wrestling',
|
94 |
+
'sumo wrestling',
|
95 |
+
'surfing',
|
96 |
+
'swimming',
|
97 |
+
'table tennis',
|
98 |
+
'tennis',
|
99 |
+
'track bicycle',
|
100 |
+
'trapeze',
|
101 |
+
'tug of war',
|
102 |
+
'ultimate',
|
103 |
+
'uneven bars',
|
104 |
+
'volleyball',
|
105 |
+
'water cycling',
|
106 |
+
'water polo',
|
107 |
+
'weightlifting',
|
108 |
+
'wheelchair basketball',
|
109 |
+
'wheelchair racing',
|
110 |
+
'wingsuit flying']
|
111 |
+
|
112 |
+
### 2. Model and transforms preparation ###
|
113 |
+
|
114 |
+
# Create EffNetB2 model
|
115 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
116 |
+
num_classes=3, # len(class_names) would also work
|
117 |
+
)
|
118 |
+
|
119 |
+
# Load saved weights
|
120 |
+
effnetb2.load_state_dict(
|
121 |
+
torch.load(
|
122 |
+
f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
|
123 |
+
map_location=torch.device("cpu"), # load to CPU
|
124 |
+
)
|
125 |
+
)
|
126 |
+
|
127 |
+
### 3. Predict function ###
|
128 |
+
|
129 |
+
# Create predict function
|
130 |
+
def predict(img) -> Tuple[Dict, float]:
|
131 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
132 |
+
"""
|
133 |
+
# Start the timer
|
134 |
+
start_time = timer()
|
135 |
+
|
136 |
+
# Transform the target image and add a batch dimension
|
137 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
138 |
+
|
139 |
+
# Put model into evaluation mode and turn on inference mode
|
140 |
+
effnetb2.eval()
|
141 |
+
with torch.inference_mode():
|
142 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
143 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
144 |
+
|
145 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
146 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
147 |
+
|
148 |
+
# Calculate the prediction time
|
149 |
+
pred_time = round(timer() - start_time, 5)
|
150 |
+
|
151 |
+
# Return the prediction dictionary and prediction time
|
152 |
+
return pred_labels_and_probs, pred_time
|
153 |
+
|
154 |
+
### 4. Gradio app ###
|
155 |
+
|
156 |
+
# Create title, description and article strings
|
157 |
+
title = "FoodVision Mini ππ₯©π£"
|
158 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
159 |
+
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
160 |
+
|
161 |
+
# Create examples list from "examples/" directory
|
162 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
163 |
+
|
164 |
+
# Create the Gradio demo
|
165 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
166 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
167 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
168 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
169 |
+
# Create examples list from "examples/" directory
|
170 |
+
examples=example_list,
|
171 |
+
title=title,
|
172 |
+
description=description,
|
173 |
+
article=article)
|
174 |
+
|
175 |
+
# Launch the demo!
|
176 |
+
demo.launch()
|
examples/1.jpg
ADDED
examples/2.jpg
ADDED
examples/3.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
|
7 |
+
def create_effnetb2_model(num_classes:int=100,
|
8 |
+
seed:int=42):
|
9 |
+
"""Creates an EfficientNetB2 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 |
+
model = torch.load('100sports.pth')
|
36 |
+
|
37 |
+
)
|
38 |
+
|
39 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|