mekhiya commited on
Commit
ef893a9
·
1 Parent(s): ef33056

first commit

Browse files
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a78dc0038266acfbb92e016f69eab2669b27013d998be16cbb7d46d063c7c60c
3
+ size 31307450
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
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
+ class_names = ["pizza", "steak", "sushi"]
11
+
12
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
13
+
14
+ effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent",
15
+ map_location=torch.device("cpu")))
16
+
17
+ def predict(img) -> Tuple[Dict, float]:
18
+ start_time = timer()
19
+
20
+ img = effnetb2_transforms(img).unsqueeze(0)
21
+
22
+ effnetb2.eval()
23
+ with torch.inference_mode():
24
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
25
+
26
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
27
+
28
+ pred_time = round(timer() - start_time, 5)
29
+
30
+ return pred_labels_and_probs, pred_time
31
+
32
+ title = "FoodVision Mini 🍕🥩🍣"
33
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
34
+ article = "Created by Nitin M."
35
+
36
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
37
+
38
+ demo = gr.Interface(fn=predict,
39
+ inputs=gr.Image(type="pil"),
40
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), gr.Number(label="Prediction time (s)")],
41
+ examples=examples_list,
42
+ title=title,
43
+ description=description,
44
+ article=article)
45
+
46
+ demo.launch()
examples/100135.jpg ADDED
examples/1008844.jpg ADDED
examples/1025041.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_effnetb2_model(num_classes:int=3,
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
+
36
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.1.0
2
+ torchvision==0.16.0
3
+ gradio==4.10.0