ajitsi commited on
Commit
cd60398
·
1 Parent(s): f6b9bb7

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
+ *.file_extension filter=lfs diff=lfs merge=lfs -text
37
+ 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,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 names
11
+ class_names =["pizza", "steak", "sushi"]
12
+
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create EffNetB2 model
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
17
+ num_classes=3
18
+ )
19
+
20
+ # Load saved weights
21
+ effnetb2.load_state_dict(
22
+ torch.load(
23
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
24
+ map_location=torch.device("cpu")
25
+ )
26
+ )
27
+
28
+ ### 3. Predict function ###
29
+
30
+ # Create predict function
31
+ def predict(img) -> Tuple[Dict, float]:
32
+ # Start the timer
33
+ start_time = timer()
34
+
35
+ # Transform the target image and add a batch dimension
36
+ img = effnetb2_transforms(img).unsqueeze(0)
37
+
38
+ # Put model into evaluation mode and turn on inference mode
39
+ effnetb2.eval()
40
+ with torch.inference_mode():
41
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
42
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
43
+
44
+ # Create prediction label and prediction probability dictionary for each prediction class
45
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
46
+
47
+ # Calculate the prediction time
48
+ pred_time = round(timer() - start_time, 5)
49
+
50
+ # Return the prediction dictionary and prediction time
51
+ return pred_labels_and_probs, pred_time
52
+
53
+ ### 4. Gradio app ##
54
+
55
+ # Create title, description and article strings
56
+ title = "FoodVision Mini 🍕🥩🍣"
57
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
58
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
59
+
60
+ # Create examples list from "examples/" directory
61
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
62
+
63
+ # Create the Gradio demo
64
+ demo = gr.Interface(fn=predict,
65
+ inputs=gr.Image(type="pil"),
66
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
67
+ gr.Number(label="Prediction time (s)")],
68
+ examples=example_list,
69
+ title=title,
70
+ description=description,
71
+ article=article)
72
+
73
+ # Launch the demo!
74
+ demo.launch()
75
+
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes:int=3,
7
+ seed:int=42):
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 with random seed for reproducibility
29
+ torch.manual_seed(seed)
30
+ model.classifier = nn.Sequential(
31
+ nn.Dropout(p=0.3, inplace=True),
32
+ nn.Linear(in_features=1408, out_features=num_classes)
33
+ )
34
+
35
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.5.0
2
+ torchvision==0.20.0
3
+ gradio==5.4.0