benbatman commited on
Commit
cb90574
·
1 Parent(s): f109be0

first commit

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ 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:a87d9e104a39a3abdfae52a5351f17ec9c6cd2ed3e5ac3d632d8b4cff37b19ff
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Set up 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") # load to CPU
25
+ )
26
+ )
27
+
28
+ ### 3. Predict function ###
29
+
30
+ # Create predict function
31
+ def predict(img) -> Tuple[Dict, float]:
32
+ """
33
+ Transforms and peforms a prediction on img and retunrs predictions
34
+ """
35
+
36
+ # Start the timer
37
+ start_time = timer()
38
+
39
+ # Transform the target image and add a batch dimension
40
+ img = effnetb2_transforms(img).unsqueeze(0)
41
+
42
+ # put the model into evaluation mode and turn on inference mode
43
+ effnetb2.eval()
44
+ with torch.inference_mode():
45
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
46
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
47
+
48
+ # Create a prediction label and predictoin probability dictionary Gradio interface
49
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
50
+
51
+ # Calculate the prediction time
52
+ pred_time = round(timer() - start_time, 5)
53
+
54
+ # Return the prediction dictionary and prediction time
55
+ return pred_labels_and_probs, pred_time
56
+
57
+ ### 4. Gradio app ###
58
+
59
+ # Create title, description and article strings
60
+ title = "FoodVision Mini"
61
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizze, steak, or sushi"
62
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
63
+
64
+ # Create examples list from "examples/" directory
65
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
66
+
67
+ # Create the Gradio demo
68
+ demo = gr.Interface(fn=predict, # mapping function from input to output
69
+ inputs=gr.Image(type='pil'),
70
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
71
+ gr.Number(label="Prediction time (s)")],
72
+ examples=example_list,
73
+ title=title,
74
+ description=description,
75
+ article=article
76
+ )
77
+
78
+ # Launch the demo
79
+ demo.launch()
examples/100274.jpg ADDED
examples/1032754.jpg ADDED
examples/124279.jpg ADDED
model.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ """
10
+
11
+ """
12
+
13
+ # Create EffNetB2 pretrained weights, transforms and model
14
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
15
+ transforms = weights.transforms()
16
+ model = torchvision.models.efficientnet_b2(weights=weights)
17
+
18
+ # Freeze all layers in the base model
19
+ for param in model.parameters():
20
+ param.requires_grad = False
21
+
22
+ # Change classifier head with random seed for reproducibility
23
+ torch.manual_seed(seed)
24
+ model.classifier = nn.Sequential(
25
+ nn.Dropout(p=0.3, inplace=True),
26
+ nn.Linear(in_features=1408, out_features=num_classes)
27
+ )
28
+
29
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.14.0