ritish369 commited on
Commit
d6f4b07
·
1 Parent(s): 2626568

first commit

Browse files
.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
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
37
+ examples/*.jpg 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:1a0f7b7d5632c47bb99cb053f638038073b83d02f8ba460641caddb0096437be
3
+ size 31314554
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 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
+ ## Setup class names
11
+ class_names = ["pizza", "steak", "sushi"]
12
+
13
+ # Step 2
14
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))
15
+ effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
16
+ map_location=torch.device("cpu"), weights_only = True))
17
+
18
+ # Step 3
19
+ def predict(img) -> Tuple[Dict, float]:
20
+ """Transforms and performs a prediction on img and returns prediction and time taken.
21
+ """
22
+ # Timer start
23
+ start_time = timer()
24
+
25
+ # Transform the image and add a batch dimension
26
+ img = effnetb2_transforms(img).unsqueeze(0)
27
+
28
+ # Get model into eval() mode and turn on inference mode
29
+ effnetb2.eval()
30
+ with torch.inference_mode():
31
+ # Pass transformed image through the model and turn pred logits to pred probs
32
+ pred_logits = effnetb2(img)
33
+ pred_probs = torch.softmax(pred_logits, dim = 1)
34
+
35
+ # Create pred label and pred prob dict for each pred class (this is the reqd format for Gradio's output parameter)
36
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
37
+
38
+ # Calculate the pred time
39
+ pred_time = round(timer() - start_time, 5)
40
+
41
+ # return pred dict and pred time
42
+ return pred_labels_and_probs, pred_time
43
+
44
+ # Step 4
45
+ ## Create title, description and article strings
46
+ title = "FoodVision Mini 🍕🥩🍣"
47
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
48
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
49
+
50
+ ## Create examples list from "examples/" directory
51
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
52
+
53
+ ## Create the Gradio demo
54
+ demo = gr.Interface(fn=predict, inputs=gr.Image(type="pil"),
55
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
56
+ gr.Number(label="Prediction time (s)")],
57
+ examples=example_list,
58
+ title=title,
59
+ description=description,
60
+ article=article)
61
+
62
+ ## Launch the demo
63
+ demo.launch()
examples/148765.jpg ADDED

Git LFS Details

  • SHA256: 26fecb3866f17e86a69eca6a26dcf5fbbdf9cfa70cc1998038114a3fdc17d384
  • Pointer size: 130 Bytes
  • Size of remote file: 67.1 kB
examples/3494950.jpg ADDED

Git LFS Details

  • SHA256: 4f70f1a6a7af3cee51e9983a4331c16cd0504ec8e934b98e398933039f62d732
  • Pointer size: 131 Bytes
  • Size of remote file: 116 kB
examples/831681.jpg ADDED

Git LFS Details

  • SHA256: 1eb5db169c08203eb3314f4ca0b28ee3ed42330c59ac363b0a74150f37db3006
  • Pointer size: 130 Bytes
  • Size of remote file: 59.5 kB
model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
5
+ # Functionalize the EffNetB2 feature extractor model creation
6
+ def create_effnetb2_model(num_classes: int=3, seed: int=42):
7
+ """Creates an EfficientNetB2 feature extractor model and its transforms.
8
+ Returns the model and transforms.
9
+ """
10
+ # 1, 2, 3 Steps here
11
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
12
+ transforms = weights.transforms()
13
+ model = torchvision.models.efficientnet_b2(weights=weights)
14
+
15
+ # Step 4
16
+ for param in model.parameters():
17
+ param.requires_grad = False
18
+
19
+ # Step 5
20
+ model.classifier = nn.Sequential(
21
+ nn.Dropout(p=0.3, inplace=True),
22
+ nn.Linear(in_features=1408, out_features=num_classes)
23
+ )
24
+
25
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.5.0
2
+ torchvision==0.20.0
3
+ gradio==5.44.1