Spaces:
Sleeping
Sleeping
Israel Azoulay
commited on
Commit
·
043007c
1
Parent(s):
b5f700c
initial commit
Browse files- .gitattributes +2 -0
- app.py +75 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +39 -0
- pretrained_effnetb2_feature_extractor_20_percent.pth +3 -0
- requirements.txt +4 -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 |
+
pretrained_effnetb2_feature_extractor_20_percent.pth filter=lfs diff=lfs merge=lfs -text
|
37 |
+
.pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from model import create_effnetb2_model
|
5 |
+
from timeit import default_timer as timer
|
6 |
+
from typing import Tuple, Dict
|
7 |
+
|
8 |
+
|
9 |
+
# Define the class names
|
10 |
+
class_names = ["pizza", "steak", "sushi"]
|
11 |
+
|
12 |
+
# Create the pretrained EffNetB2 model
|
13 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
14 |
+
num_classes=3,
|
15 |
+
)
|
16 |
+
|
17 |
+
# Load to the CPU the EffNetB2 model's saved weights
|
18 |
+
effnetb2.load_state_dict(
|
19 |
+
torch.load(
|
20 |
+
f="pretrained_effnetb2_feature_extractor_20_percent.pth",
|
21 |
+
map_location=torch.device("cpu"),
|
22 |
+
)
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
+
def predict(img) -> Tuple[Dict, float]:
|
27 |
+
"""This function transforms and performs a prediction on an image, and returns
|
28 |
+
the prediction and time taken.
|
29 |
+
|
30 |
+
Returns: the prediction dictionary and prediction time.
|
31 |
+
"""
|
32 |
+
# Begin the prediction's timer
|
33 |
+
start_time = timer()
|
34 |
+
|
35 |
+
# Transform the target image with the pretrained EffNetB2 model's transforms, and add a batch dimension
|
36 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
37 |
+
|
38 |
+
# Set the model to evaluation model
|
39 |
+
effnetb2.eval()
|
40 |
+
|
41 |
+
# Activate the inference mode
|
42 |
+
with torch.inference_mode():
|
43 |
+
# Pass the transformed image through the model, and transform the prediction logits (the model's outputs) into prediction probabilities
|
44 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
45 |
+
|
46 |
+
# Create a prediction label and prediction probability dictionary for each prediction class
|
47 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
48 |
+
|
49 |
+
# Calculating the prediction's time
|
50 |
+
pred_time = round(timer() - start_time, 5)
|
51 |
+
|
52 |
+
return pred_labels_and_probs, pred_time
|
53 |
+
|
54 |
+
|
55 |
+
# Define the title
|
56 |
+
title = "DishVision - Multi Class Food Image Classifier"
|
57 |
+
# Define the description
|
58 |
+
description = "An EfficientNetB2-based transfer learning model for feature extraction in computer vision, designed to classify images into three distinct food categories."
|
59 |
+
# Define the article
|
60 |
+
article = "Computer Vision Project"
|
61 |
+
|
62 |
+
# Create the "examples" list from "examples/" directory
|
63 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
64 |
+
|
65 |
+
# Create the Gradio deom interface
|
66 |
+
demo = gr.Interface(fn=predict,
|
67 |
+
inputs=gr.Image(type="pil"),
|
68 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
69 |
+
gr.Number(label="Prediction time (s)")],
|
70 |
+
examples=example_list,
|
71 |
+
title=title,
|
72 |
+
description=description,
|
73 |
+
article=article)
|
74 |
+
|
75 |
+
demo.launch(share=True)
|
examples/2582289.jpg
ADDED
![]() |
examples/3622237.jpg
ADDED
![]() |
examples/592799.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torch import nn
|
4 |
+
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes:int=3,
|
7 |
+
seed:int=42):
|
8 |
+
"""This function creates an EfficientNetB2 feature extractor model and transforms.
|
9 |
+
|
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 |
+
|
15 |
+
Returns:
|
16 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
17 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
18 |
+
"""
|
19 |
+
# Get the pretrained 'efficientnet_b2' model's weights
|
20 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
21 |
+
# Get the pretrained 'efficientnet_b2' model's transforms
|
22 |
+
transforms = weights.transforms()
|
23 |
+
# Set up the pretrained 'efficientnet_b2' model
|
24 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
25 |
+
|
26 |
+
# Freeze the base layers in the 'efficientnet_b2' model (for Feature Extraction)
|
27 |
+
for param in model.parameters():
|
28 |
+
param.requires_grad = False
|
29 |
+
|
30 |
+
# Set the torch manual seed
|
31 |
+
torch.manual_seed(seed)
|
32 |
+
|
33 |
+
# Update the classifier head to suit to our problem
|
34 |
+
model.classifier = nn.Sequential(
|
35 |
+
nn.Dropout(p=0.3, inplace=True),
|
36 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
37 |
+
)
|
38 |
+
|
39 |
+
return model, transforms
|
pretrained_effnetb2_feature_extractor_20_percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a14a8d707956ac7a4ced4ee884df9c92850795ea18ac3b5d41c3c34dbc614253
|
3 |
+
size 31298682
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.0.1
|
2 |
+
torchvision==0.15.2
|
3 |
+
gradio==4.38.1
|
4 |
+
numpy<2.0.0
|