Spaces:
Runtime error
Runtime error
first commit
Browse files- 09_pretrained_effnetb2_fine_tuned_food101_full_20_percent.pth +3 -0
- app.py +84 -0
- class_names.txt +101 -0
- examples/gyoza.jpg +0 -0
- examples/pizza.jpg +0 -0
- examples/poutine.jpg +0 -0
- model.py +34 -0
- requirements.txt +5 -0
09_pretrained_effnetb2_fine_tuned_food101_full_20_percent.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4abcd9124ec1b95399c2809669d00b0b9e9d25e0a11847b656fdab2b531fa8ee
|
| 3 |
+
size 31825353
|
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
with open("class_names.txt", "r") as f: # reading them in from class_names.txt
|
| 12 |
+
class_names = [food_name.strip() for food_name in f.readlines()]
|
| 13 |
+
|
| 14 |
+
### 2. Model and transforms preparation ###
|
| 15 |
+
|
| 16 |
+
# Create model
|
| 17 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 18 |
+
num_classes=101, # could also use len(class_names)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Load saved weights
|
| 22 |
+
effnetb2.load_state_dict(
|
| 23 |
+
torch.load(
|
| 24 |
+
f="09_pretrained_effnetb2_fine_tuned_food101_full_20_percent.pth",
|
| 25 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
### 3. Predict function ###
|
| 30 |
+
|
| 31 |
+
# Create predict function
|
| 32 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 33 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 34 |
+
"""
|
| 35 |
+
# Start the timer
|
| 36 |
+
start_time = timer()
|
| 37 |
+
|
| 38 |
+
# Transform the target image and add a batch dimension
|
| 39 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 40 |
+
|
| 41 |
+
# Put model into evaluation mode and turn on inference mode
|
| 42 |
+
effnetb2.eval()
|
| 43 |
+
with torch.inference_mode():
|
| 44 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 45 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
| 46 |
+
|
| 47 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 48 |
+
pred_labels_and_probs = {
|
| 49 |
+
class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Calculate the prediction time
|
| 53 |
+
pred_time = round(timer() - start_time, 5)
|
| 54 |
+
|
| 55 |
+
# Return the prediction dictionary and prediction time
|
| 56 |
+
return pred_labels_and_probs, pred_time
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
### 4. Gradio app ###
|
| 60 |
+
|
| 61 |
+
# Create title, description and article strings
|
| 62 |
+
title = "FoodVision Big 🍔👁"
|
| 63 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food into [101 different classes](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
|
| 64 |
+
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
| 65 |
+
|
| 66 |
+
# Create examples list from "examples/" directory
|
| 67 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 68 |
+
|
| 69 |
+
# Create Gradio interface
|
| 70 |
+
demo = gr.Interface(
|
| 71 |
+
fn=predict,
|
| 72 |
+
inputs=gr.Image(type="pil"),
|
| 73 |
+
outputs=[
|
| 74 |
+
gr.Label(num_top_classes=3, label="Predictions"),
|
| 75 |
+
gr.Number(label="Prediction time (s)"),
|
| 76 |
+
],
|
| 77 |
+
examples=example_list,
|
| 78 |
+
title=title,
|
| 79 |
+
description=description,
|
| 80 |
+
article=article,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Launch the app!
|
| 84 |
+
demo.launch(share=True)
|
class_names.txt
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apple pie
|
| 2 |
+
Baby back ribs
|
| 3 |
+
Baklava
|
| 4 |
+
Beef carpaccio
|
| 5 |
+
Beef tartare
|
| 6 |
+
Beet salad
|
| 7 |
+
Beignets
|
| 8 |
+
Bibimbap
|
| 9 |
+
Bread pudding
|
| 10 |
+
Breakfast burrito
|
| 11 |
+
Bruschetta
|
| 12 |
+
Caesar salad
|
| 13 |
+
Cannoli
|
| 14 |
+
Caprese salad
|
| 15 |
+
Carrot cake
|
| 16 |
+
Ceviche
|
| 17 |
+
Cheese plate
|
| 18 |
+
Cheesecake
|
| 19 |
+
Chicken curry
|
| 20 |
+
Chicken quesadilla
|
| 21 |
+
Chicken wings
|
| 22 |
+
Chocolate cake
|
| 23 |
+
Chocolate mousse
|
| 24 |
+
Churros
|
| 25 |
+
Clam chowder
|
| 26 |
+
Club sandwich
|
| 27 |
+
Crab cakes
|
| 28 |
+
Creme brulee
|
| 29 |
+
Croque madame
|
| 30 |
+
Cup cakes
|
| 31 |
+
Deviled eggs
|
| 32 |
+
Donuts
|
| 33 |
+
Dumplings
|
| 34 |
+
Edamame
|
| 35 |
+
Eggs benedict
|
| 36 |
+
Escargots
|
| 37 |
+
Falafel
|
| 38 |
+
Filet mignon
|
| 39 |
+
Fish and chips
|
| 40 |
+
Foie gras
|
| 41 |
+
French fries
|
| 42 |
+
French onion soup
|
| 43 |
+
French toast
|
| 44 |
+
Fried calamari
|
| 45 |
+
Fried rice
|
| 46 |
+
Frozen yogurt
|
| 47 |
+
Garlic bread
|
| 48 |
+
Gnocchi
|
| 49 |
+
Greek salad
|
| 50 |
+
Grilled cheese sandwich
|
| 51 |
+
Grilled salmon
|
| 52 |
+
Guacamole
|
| 53 |
+
Gyoza
|
| 54 |
+
Hamburger
|
| 55 |
+
Hot and sour soup
|
| 56 |
+
Hot dog
|
| 57 |
+
Huevos rancheros
|
| 58 |
+
Hummus
|
| 59 |
+
Ice cream
|
| 60 |
+
Lasagna
|
| 61 |
+
Lobster bisque
|
| 62 |
+
Lobster roll sandwich
|
| 63 |
+
Macaroni and cheese
|
| 64 |
+
Macarons
|
| 65 |
+
Miso soup
|
| 66 |
+
Mussels
|
| 67 |
+
Nachos
|
| 68 |
+
Omelette
|
| 69 |
+
Onion rings
|
| 70 |
+
Oysters
|
| 71 |
+
Pad thai
|
| 72 |
+
Paella
|
| 73 |
+
Pancakes
|
| 74 |
+
Panna cotta
|
| 75 |
+
Peking duck
|
| 76 |
+
Pho
|
| 77 |
+
Pizza
|
| 78 |
+
Pork chop
|
| 79 |
+
Poutine
|
| 80 |
+
Prime rib
|
| 81 |
+
Pulled pork sandwich
|
| 82 |
+
Ramen
|
| 83 |
+
Ravioli
|
| 84 |
+
Red velvet cake
|
| 85 |
+
Risotto
|
| 86 |
+
Samosa
|
| 87 |
+
Sashimi
|
| 88 |
+
Scallops
|
| 89 |
+
Seaweed salad
|
| 90 |
+
Shrimp and grits
|
| 91 |
+
Spaghetti bolognese
|
| 92 |
+
Spaghetti carbonara
|
| 93 |
+
Spring rolls
|
| 94 |
+
Steak
|
| 95 |
+
Strawberry shortcake
|
| 96 |
+
Sushi
|
| 97 |
+
Tacos
|
| 98 |
+
Takoyaki
|
| 99 |
+
Tiramisu
|
| 100 |
+
Tuna tartare
|
| 101 |
+
Waffles
|
examples/gyoza.jpg
ADDED
|
examples/pizza.jpg
ADDED
|
examples/poutine.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
|
| 4 |
+
from torch import nn
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def create_effnetb2_model(num_classes:int=3,
|
| 8 |
+
seed:int=42):
|
| 9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
| 10 |
+
Args:
|
| 11 |
+
num_classes (int, optional): number of classes in the classifier head.
|
| 12 |
+
Defaults to 3.
|
| 13 |
+
seed (int, optional): random seed value. Defaults to 42.
|
| 14 |
+
Returns:
|
| 15 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
| 16 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
| 17 |
+
"""
|
| 18 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
| 19 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 20 |
+
transforms = weights.transforms()
|
| 21 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
| 22 |
+
|
| 23 |
+
# Freeze all layers in base model
|
| 24 |
+
for param in model.parameters():
|
| 25 |
+
param.requires_grad = False
|
| 26 |
+
|
| 27 |
+
# Change classifier head with random seed for reproducibility
|
| 28 |
+
torch.manual_seed(seed)
|
| 29 |
+
model.classifier = nn.Sequential(
|
| 30 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 31 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==1.4.3
|
| 2 |
+
protobuf==3.19.4
|
| 3 |
+
tensorflow==2.9.1
|
| 4 |
+
numpy==1.23.1
|
| 5 |
+
gradio==3.4.0
|