yussaaa commited on
Commit
7dfd71e
·
1 Parent(s): 86ee929

Initial 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:12aae0c0d7228af6e83e2c49c47367109baa0e9a7ebdb67afa8cc3fc187a0997
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### 1. Imports and class names setup ###
3
+ import gradio as gr
4
+ import os
5
+ import torch
6
+
7
+ from model import create_effnetb2_model
8
+ from timeit import default_timer as timer
9
+ from typing import Tuple, Dict
10
+
11
+ # Setup class names
12
+ class_names = ["pizza", "steak", "sushi"]
13
+
14
+ # 2. Model generation and weight
15
+ effnetb2, effnetb2_transforms = create_EffNetB2_model(num_classes=len(class_names))
16
+
17
+ model.load_state_dict(
18
+ torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
19
+ map_location=torch.device("cpu"), # load to CPU))
20
+
21
+
22
+ # 3. Prefict function
23
+
24
+ def predict(img) -> Tuple[Dict, float]:
25
+ """Transforms and performs a prediction on img and returns prediction and time taken.
26
+ """
27
+ # Start the timer
28
+ start_time = timer()
29
+
30
+ # Transform the target image and add a batch dimension
31
+ img = effnetb2_transforms(img).unsqueeze(0)
32
+
33
+ # Put model into evaluation mode and turn on inference mode
34
+ effnetb2.eval()
35
+ with torch.inference_mode():
36
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
37
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
38
+
39
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
40
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
41
+
42
+ # Calculate the prediction time
43
+ pred_time = round(timer() - start_time, 5)
44
+
45
+ # Return the prediction dictionary and prediction time
46
+ return pred_labels_and_probs, pred_time
47
+
48
+ # 4. Gradio app
49
+
50
+ # Create title, description and article strings
51
+ title = "FoodVision Mini 🍕🥩🍣"
52
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
53
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
54
+
55
+ # Create the Gradio demo
56
+ demo = gr.Interface(fn=predict, # mapping function from input to output
57
+ inputs=gr.Image(type="pil"), # what are the inputs?
58
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
59
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
60
+ examples=example_list,
61
+ title=title,
62
+ description=description,
63
+ article=article)
64
+
65
+ # Launch the demo!
66
+ demo.launch(debug=False, # print errors locally?
67
+ share=True) # generate a publically shareable URL?
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ from torch import nn
5
+
6
+ def create_EffNetB2_model(num_classes:int=3,
7
+ seed:int = 42):
8
+ """
9
+
10
+ """
11
+
12
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
13
+ transform = weights.transforms()
14
+
15
+ model = torchvision.models.efficientnet_b2(weights=weights)
16
+
17
+ for parm in model.parameters():
18
+ parm.requires_grad = False
19
+
20
+ set_seeds(seed)
21
+
22
+ model.classifier = nn.Sequential(nn.Dropout(p=0.2, inplace=True),
23
+ nn.Linear(in_features=1408,
24
+ out_features=num_classes,
25
+ bias=True
26
+ ))
27
+
28
+ return model, transform
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.1.4