shree commited on
Commit
6a4a7f2
β€’
1 Parent(s): 8e927b6

first commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
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:1cb101f9ff531f307470f23ce7b8a94c4dd949bb181985f8102ffaebf52506ae
3
+ size 31314554
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 name
11
+ class_names = ["pizza", "steak", "sushi"]
12
+
13
+ #creat a model and model_transformation
14
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))
15
+
16
+ #load save weights
17
+ effnetb2.load_state_dict(
18
+ torch.load(
19
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
20
+ map_location=torch.device("cpu"),
21
+ )
22
+ )
23
+
24
+ #prediction fn
25
+ def predict(img) -> Tuple[Dict, float]:
26
+ start_time = timer()
27
+
28
+ img = effnetb2_transforms(img).unsqueeze(0)
29
+
30
+ effnetb2.eval()
31
+ with torch.inference_mode():
32
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
33
+
34
+ #gradio recieve result in dict
35
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
36
+
37
+ pred_time = round(timer() - start_time, 5)
38
+
39
+ return pred_labels_and_probs, pred_time
40
+
41
+ # gradio app
42
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
43
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
44
+
45
+ #example list
46
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
47
+
48
+ demo = gr.Interface(fn=predict,
49
+ inputs=gr.Image(type="pil"), # what are the inputs?
50
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
51
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
52
+ # Create examples list from "examples/" directory
53
+ examples=example_list,
54
+ title=title,
55
+ description=description)
56
+
57
+ demo.lunch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.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
+ """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
+ # Create EffNetB2 pretrained weights, transforms and model
20
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
21
+ transforms = weights.transforms()
22
+ model = torchvision.models.efficientnet_b2(weights=weights)
23
+
24
+ # Freeze all layers in base model
25
+ for param in model.parameters():
26
+ param.requires_grad = False
27
+
28
+ # Change classifier head
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,3 @@
 
 
 
 
1
+ torch==2.3.0
2
+ torchvision==0.18.0
3
+ gradio==4.37.2