Spaces:
Runtime error
Runtime error
Commit
·
efc2ac4
1
Parent(s):
cdb8251
initial commit
Browse files- .gitattributes +1 -0
- __pycache__/model.cpython-38.pyc +0 -0
- app.py +77 -0
- examples/1032754.jpg +0 -0
- examples/1082384.jpg +0 -0
- examples/1346344.jpg +0 -0
- foodvision_mini/.DS_Store +0 -0
- foodvision_mini/__pycache__/model.cpython-38.pyc +0 -0
- foodvision_mini/app.py +77 -0
- foodvision_mini/examples/1032754.jpg +0 -0
- foodvision_mini/examples/1082384.jpg +0 -0
- foodvision_mini/examples/1346344.jpg +0 -0
- foodvision_mini/model.py +29 -0
- foodvision_mini/pretrained_effnetb2_feature_extractor.pth +3 -0
- foodvision_mini/requirements.txt +6 -0
- model.py +29 -0
- pretrained_effnetb2_feature_extractor.pth +3 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
pretrained_effnetb2_feature_extractor.pth filter=lfs diff=lfs merge=lfs -text
|
__pycache__/model.cpython-38.pyc
ADDED
|
Binary file (749 Bytes). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from model import create_effnet_b2
|
| 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 |
+
#model and transforms preparation
|
| 14 |
+
effnetb2, effnetb2_transforms = create_effnet_b2(
|
| 15 |
+
num_classes = 3)
|
| 16 |
+
|
| 17 |
+
#load saved weights
|
| 18 |
+
effnetb2.load_state_dict(
|
| 19 |
+
torch.load(f = 'pretrained_effnetb2_feature_extractor.pth',
|
| 20 |
+
map_location = torch.device('cpu')) #hardcoding to load state dict onto the cpu
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
#Predict function
|
| 24 |
+
|
| 25 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 26 |
+
|
| 27 |
+
#Start a timer
|
| 28 |
+
start_time = timer()
|
| 29 |
+
|
| 30 |
+
#transform the input image for use with effnetb2
|
| 31 |
+
transformed_image = effnetb2_transforms(img).unsqueeze(0)
|
| 32 |
+
|
| 33 |
+
#put model into deval mode, make preiction
|
| 34 |
+
effnetb2.eval()
|
| 35 |
+
with torch.inference_mode():
|
| 36 |
+
pred_logits = effnetb2(transformed_image)
|
| 37 |
+
pred_probs = torch.softmax(pred_logits, dim = 1)
|
| 38 |
+
|
| 39 |
+
# create a prediction label and pred prob dictionary
|
| 40 |
+
pred_labels_and_probs = {effnet_class_names[i]: float(pred_probs[0][i])
|
| 41 |
+
for i in range(len(effnet_class_names))}
|
| 42 |
+
|
| 43 |
+
#calculate pred time
|
| 44 |
+
end_time = timer()
|
| 45 |
+
pred_time = end_time - start_time
|
| 46 |
+
|
| 47 |
+
#return pred dict and pred time
|
| 48 |
+
print(pred_probs[0])
|
| 49 |
+
return pred_labels_and_probs, pred_time
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Gradio app
|
| 53 |
+
|
| 54 |
+
import gradio as gr
|
| 55 |
+
|
| 56 |
+
#Create title, description and article
|
| 57 |
+
title = 'FoodVision Mini'
|
| 58 |
+
description = 'An EfficientNetB2 feature extractor to classify food as pizza, steak, and sushi'
|
| 59 |
+
|
| 60 |
+
#Create example list
|
| 61 |
+
example_list = [['examples/' + example] for example in os.listdir('examples')]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo = gr.Interface(fn = predict,
|
| 65 |
+
inputs = gr.Image(type='pil'),
|
| 66 |
+
outputs = [gr.Label(num_top_classes = 3, label = 'Predictions'),
|
| 67 |
+
gr.Number(label = 'Prediction time (s)')],
|
| 68 |
+
|
| 69 |
+
examples = example_list,
|
| 70 |
+
title = title,
|
| 71 |
+
description = description)
|
| 72 |
+
|
| 73 |
+
demo.launch(debug = False,
|
| 74 |
+
share = True)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
examples/1032754.jpg
ADDED
|
examples/1082384.jpg
ADDED
|
examples/1346344.jpg
ADDED
|
foodvision_mini/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
foodvision_mini/__pycache__/model.cpython-38.pyc
ADDED
|
Binary file (749 Bytes). View file
|
|
|
foodvision_mini/app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from model import create_effnet_b2
|
| 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 |
+
#model and transforms preparation
|
| 14 |
+
effnetb2, effnetb2_transforms = create_effnet_b2(
|
| 15 |
+
num_classes = 3)
|
| 16 |
+
|
| 17 |
+
#load saved weights
|
| 18 |
+
effnetb2.load_state_dict(
|
| 19 |
+
torch.load(f = 'pretrained_effnetb2_feature_extractor.pth',
|
| 20 |
+
map_location = torch.device('cpu')) #hardcoding to load state dict onto the cpu
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
#Predict function
|
| 24 |
+
|
| 25 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 26 |
+
|
| 27 |
+
#Start a timer
|
| 28 |
+
start_time = timer()
|
| 29 |
+
|
| 30 |
+
#transform the input image for use with effnetb2
|
| 31 |
+
transformed_image = effnetb2_transforms(img).unsqueeze(0)
|
| 32 |
+
|
| 33 |
+
#put model into deval mode, make preiction
|
| 34 |
+
effnetb2.eval()
|
| 35 |
+
with torch.inference_mode():
|
| 36 |
+
pred_logits = effnetb2(transformed_image)
|
| 37 |
+
pred_probs = torch.softmax(pred_logits, dim = 1)
|
| 38 |
+
|
| 39 |
+
# create a prediction label and pred prob dictionary
|
| 40 |
+
pred_labels_and_probs = {effnet_class_names[i]: float(pred_probs[0][i])
|
| 41 |
+
for i in range(len(effnet_class_names))}
|
| 42 |
+
|
| 43 |
+
#calculate pred time
|
| 44 |
+
end_time = timer()
|
| 45 |
+
pred_time = end_time - start_time
|
| 46 |
+
|
| 47 |
+
#return pred dict and pred time
|
| 48 |
+
print(pred_probs[0])
|
| 49 |
+
return pred_labels_and_probs, pred_time
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Gradio app
|
| 53 |
+
|
| 54 |
+
import gradio as gr
|
| 55 |
+
|
| 56 |
+
#Create title, description and article
|
| 57 |
+
title = 'FoodVision Mini'
|
| 58 |
+
description = 'An EfficientNetB2 feature extractor to classify food as pizza, steak, and sushi'
|
| 59 |
+
|
| 60 |
+
#Create example list
|
| 61 |
+
example_list = [['examples/' + example] for example in os.listdir('examples')]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
demo = gr.Interface(fn = predict,
|
| 65 |
+
inputs = gr.Image(type='pil'),
|
| 66 |
+
outputs = [gr.Label(num_top_classes = 3, label = 'Predictions'),
|
| 67 |
+
gr.Number(label = 'Prediction time (s)')],
|
| 68 |
+
|
| 69 |
+
examples = example_list,
|
| 70 |
+
title = title,
|
| 71 |
+
description = description)
|
| 72 |
+
|
| 73 |
+
demo.launch(debug = False,
|
| 74 |
+
share = True)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
foodvision_mini/examples/1032754.jpg
ADDED
|
foodvision_mini/examples/1082384.jpg
ADDED
|
foodvision_mini/examples/1346344.jpg
ADDED
|
foodvision_mini/model.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision
|
| 4 |
+
from torch import nn
|
| 5 |
+
|
| 6 |
+
#Function that creates an effnetb2
|
| 7 |
+
|
| 8 |
+
def create_effnet_b2(num_classes: int = 3,
|
| 9 |
+
seed: int = 42):
|
| 10 |
+
|
| 11 |
+
#Get weights
|
| 12 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 13 |
+
transforms = weights.transforms()
|
| 14 |
+
model = torchvision.models.efficientnet_b2(weights = weights)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
#Freeze parameters in features layer
|
| 18 |
+
for param in model.parameters():
|
| 19 |
+
param.requires_grad = False
|
| 20 |
+
|
| 21 |
+
#Change classification layer
|
| 22 |
+
torch.manual_seed(seed)
|
| 23 |
+
model.classifier = nn.Sequential(
|
| 24 |
+
nn.Dropout(p = .3),
|
| 25 |
+
nn.Linear(in_features = 1408,
|
| 26 |
+
out_features = num_classes))
|
| 27 |
+
|
| 28 |
+
return model, transforms
|
| 29 |
+
|
foodvision_mini/pretrained_effnetb2_feature_extractor.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d4ab6ca54bf0bb2d03b2ef7a1487be135c47da838aba93d2bfd0df9c41d1632f
|
| 3 |
+
size 31282061
|
foodvision_mini/requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch == 1.12.0
|
| 2 |
+
torchvision == .13.0
|
| 3 |
+
gradio ==3.1.4
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
model.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision
|
| 4 |
+
from torch import nn
|
| 5 |
+
|
| 6 |
+
#Function that creates an effnetb2
|
| 7 |
+
|
| 8 |
+
def create_effnet_b2(num_classes: int = 3,
|
| 9 |
+
seed: int = 42):
|
| 10 |
+
|
| 11 |
+
#Get weights
|
| 12 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 13 |
+
transforms = weights.transforms()
|
| 14 |
+
model = torchvision.models.efficientnet_b2(weights = weights)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
#Freeze parameters in features layer
|
| 18 |
+
for param in model.parameters():
|
| 19 |
+
param.requires_grad = False
|
| 20 |
+
|
| 21 |
+
#Change classification layer
|
| 22 |
+
torch.manual_seed(seed)
|
| 23 |
+
model.classifier = nn.Sequential(
|
| 24 |
+
nn.Dropout(p = .3),
|
| 25 |
+
nn.Linear(in_features = 1408,
|
| 26 |
+
out_features = num_classes))
|
| 27 |
+
|
| 28 |
+
return model, transforms
|
| 29 |
+
|
pretrained_effnetb2_feature_extractor.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d4ab6ca54bf0bb2d03b2ef7a1487be135c47da838aba93d2bfd0df9c41d1632f
|
| 3 |
+
size 31282061
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch == 1.12.0
|
| 2 |
+
torchvision == .13.0
|
| 3 |
+
gradio ==3.1.4
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|