stefantaga commited on
Commit
74c2e3b
Β·
1 Parent(s): 5746d82

first commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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
+ 07_effnetb2_data_20_percent_10_epochs.pth filter=lfs diff=lfs merge=lfs -text
07_effnetb2_data_20_percent_10_epochs.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:45faaa630bbf3990195fa6bdb606a47a58a1f3e10eab9c1e24950417441f40ed
3
+ size 31273033
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+ class_names = ["pizza", "steak", "sushi"]
10
+
11
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
12
+ num_classes=3, # len(class_names) would also work
13
+ )
14
+
15
+ effnetb2.load_state_dict(
16
+ torch.load(
17
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
18
+ map_location=torch.device("cpu"), # load to CPU
19
+ )
20
+ )
21
+
22
+ def predict(img) -> Tuple[Dict, float]:
23
+ # Start the timer
24
+ start_time = timer()
25
+
26
+ # Transform the target image and add a batch dimension
27
+ img = effnetb2_transforms(img).unsqueeze(0)
28
+
29
+ # Put model into evaluation mode and turn on inference mode
30
+ effnetb2.eval()
31
+ with torch.inference_mode():
32
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
33
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
34
+
35
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required 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 prediction time
39
+ pred_time = round(timer() - start_time, 5)
40
+
41
+ # Return the prediction dictionary and prediction time
42
+ return pred_labels_and_probs, pred_time
43
+
44
+ ### 4. Gradio app ###
45
+
46
+ # Create title, description and article strings
47
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
48
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
49
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
50
+
51
+ # Create examples list from "examples/" directory
52
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
53
+
54
+ # Create the Gradio demo
55
+ demo = gr.Interface(fn=predict, # mapping function from input to output
56
+ inputs=gr.Image(type="pil"), # what are the inputs?
57
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
58
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
59
+ # Create examples list from "examples/" directory
60
+ examples=example_list,
61
+ title=title,
62
+ description=description,
63
+ article=article)
64
+
65
+ # Launch the demo!
66
+ demo.launch()
examples/175783.jpg ADDED
examples/195160.jpg ADDED
examples/296375.jpg ADDED
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb2_model(num_classes:int=3,
8
+ seed:int=42):
9
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
10
+ transforms = weights.transforms()
11
+ model = torchvision.models.efficientnet_b2(weights=weights)
12
+
13
+ for param in model.parameters():
14
+ param.requires_grad = False
15
+
16
+ torch.manual_seed(seed)
17
+ model.classifier = nn.Sequential(
18
+ nn.Dropout(p=0.3, inplace=True),
19
+ nn.Linear(in_features=1408, out_features=num_classes),
20
+ )
21
+
22
+ 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