Saugat2002 commited on
Commit
9baeefb
Β·
1 Parent(s): e29c9bd
.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:eff07ee6a9faf1b1cbaf25837bd5990025f46ac083ea629919de57c82a86c157
3
+ size 31314554
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
13
+ num_classes=len(class_names),
14
+ )
15
+
16
+ effnetb2.load_state_dict(
17
+ torch.load(
18
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
19
+ map_location=torch.device("cpu"), # load to CPU
20
+ )
21
+ )
22
+
23
+ def predict(img) -> Tuple[Dict, float]:
24
+ """Transforms and performs a prediction on img and returns prediction and time taken.
25
+ """
26
+ # Start the timer
27
+ start_time = timer()
28
+
29
+ # Transform the target image and add a batch dimension
30
+ img = effnetb2_transforms(img).unsqueeze(0)
31
+
32
+ # Put model into evaluation mode and turn on inference mode
33
+ effnetb2.eval()
34
+ with torch.inference_mode():
35
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
36
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
37
+
38
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
39
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
40
+
41
+ # Calculate the prediction time
42
+ pred_time = round(timer() - start_time, 5)
43
+
44
+ # Return the prediction dictionary and prediction time
45
+ return pred_labels_and_probs, pred_time
46
+
47
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
48
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
49
+ article = "Created using PyTorch"
50
+
51
+ # Create examples list from "examples/" directory
52
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
53
+
54
+ # Create the Gradio demo
55
+ demo = gr.Interface(fn=predict, # mapping function from input to output
56
+ inputs=gr.Image(type="pil"), # what are the inputs?
57
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
58
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
59
+ # Create examples list from "examples/" directory
60
+ examples=example_list,
61
+ title=title,
62
+ description=description,
63
+ article=article)
64
+
65
+ # Launch the demo!
66
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes:int = 3, seed:int = 42):
7
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
8
+ transforms = weights.transforms()
9
+
10
+ model = torchvision.models.efficientnet_b2(weights=weights).to(device)
11
+
12
+ for param in model.parameters():
13
+ param.requires_grad = False
14
+
15
+ torch.manual_seed(seed)
16
+ model.classifier = nn.Sequential(
17
+ nn.Dropout(p=0.3, inplace=True),
18
+ nn.Linear(in_features=1408, out_features=num_classes, bias=True)
19
+ )
20
+
21
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.5.1
2
+ torchvision==0.20.1
3
+ gradio==5.11.0