ahamedddd commited on
Commit
f5ba80b
·
1 Parent(s): e476460

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:37b329ab24c80214f862782cf003468562f4f3eef4b96ebe1296bb096e5e2c36
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### 1. Imports and class names setup ###
3
+ import gradio as gr
4
+ import os
5
+ import torch
6
+ import torchvision
7
+
8
+ from model import create_effnetb2_model
9
+ from timeit import default_timer as timer
10
+ from typing import Tuple, Dict
11
+
12
+ # Setup class names
13
+ class_names = ["pizza", "steak", "sushi"]
14
+
15
+ ### 2. Model and transforms preparation ###
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
17
+ num_classes = 3
18
+ )
19
+
20
+ # load save 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 the model to the CPU
25
+ )
26
+ )
27
+
28
+ ### 3. Predict function ###
29
+
30
+ def predict(img) -> Tuple[Dict, float]:
31
+ # Start a timer
32
+ start_time = timer()
33
+
34
+ # Transform the input image for use with EffNetB2
35
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index
36
+
37
+ # Put the model into eval mode, make prediction
38
+ effnetb2.eval()
39
+ with torch.inference_mode():
40
+ # Pass transformed image through the model and turn the prediction logits into probabilities
41
+ pred_probs = torch.softmax(effnetb2(img), dim = 1)
42
+
43
+ # Create a prediction label and prediction probability dictionary
44
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
45
+
46
+ # Calculate pred time
47
+ end_time = timer()
48
+ pred_time = round(end_time - start_time, 4)
49
+
50
+ # Return pred dict and pred time
51
+ return pred_labels_and_probs, pred_time
52
+
53
+ ### 4. Gradio app ###
54
+
55
+ # Create title, description and article
56
+ title = "Foodvision Mini 🍕🥩🍣"
57
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model to classify images as pizza, steak or sushi."
58
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/#74-building-a-gradio-interface)."
59
+
60
+ # Create an example list
61
+ example_list = [["examples/"+example] for example in os.listdir("examples")]
62
+
63
+ # Create the Gradio demo
64
+ demo = gr.Interface(
65
+ fn = predict, # maps inputs to outputs
66
+ inputs = gr.Image(type="pil"),
67
+ outputs = [
68
+ gr.Label(num_top_classes=3, label="Predictions"),
69
+ gr.Number(label="Prediction time (s)")
70
+ ],
71
+ examples = example_list,
72
+ title = title,
73
+ description = description,
74
+ article = article
75
+ )
76
+
77
+ # launch the demo!
78
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+ def create_effnetb2_model(num_classes:int=3, # default output classes = 3 (pizza, steak, sushi)
6
+ seed:int=42):
7
+ # 1, 2, 3 Create EffNetB2 pretrained weights, transforms and model
8
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
9
+ transforms = weights.transforms()
10
+ model = torchvision.models.efficientnet_b2(weights=weights)
11
+
12
+ # 4. Freeze all layers in the base model
13
+ for param in model.parameters():
14
+ param.requires_grad = False
15
+
16
+ # 5. Change classifier head with random seed for reproducibility
17
+ torch.manual_seed(seed)
18
+ model.classifier = nn.Sequential(
19
+ nn.Dropout(p=0.3, inplace=True),
20
+ nn.Linear(in_features=1408, out_features=num_classes)
21
+ )
22
+
23
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch == 1.12.0
2
+ torchvision == 0.13.0
3
+ gradio == 3.1.4