吉井時秋 commited on
Commit
f6f537e
β€’
1 Parent(s): c526500

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
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 and transforms preparation
15
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
16
+ num_classes=3)
17
+
18
+ # Load saved weights
19
+ effnetb2.load_state_dict(
20
+ torch.load(
21
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
22
+ map_location=torch.device("cpu") # load the model to the CPU
23
+ )
24
+ )
25
+
26
+ ### 3. Predict function ###
27
+ def predict(img) -> Tuple[Dict, float]:
28
+ # Start a timer
29
+ start_time = timer()
30
+
31
+ # Transform the input image for use with EffNetB2
32
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index
33
+
34
+ # Put model into eval mode, make prediction
35
+ effnetb2.eval()
36
+ with torch.inference_mode():
37
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
38
+
39
+ # Create a prediction label and prediction probability dictionary
40
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
41
+
42
+ # Calculate pred time
43
+ end_time = timer()
44
+ pred_time = round(end_time-start_time, 4)
45
+
46
+ # Return pred dict and pred time
47
+ return pred_labels_and_probs, pred_time
48
+
49
+ ### 4. Gradio app ###
50
+ # Create a title, description, article
51
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
52
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b1.html#torchvision.models.efficientnet_b1)"
53
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
54
+
55
+ # Create example list
56
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
57
+
58
+ # Create the Gradio demo
59
+ demo = gr.Interface(fn=predict, # maps inputs to outputs
60
+ inputs=gr.Image(type="pil"),
61
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"),
62
+ gr.Number(label="Prediction time (s)")],
63
+ examples=example_list,
64
+ title=title,
65
+ description=description,
66
+ article=article)
67
+
68
+ # Launch the demo!
69
+ demo.launch()
examples/2378406.jpg ADDED
examples/3873655.jpg ADDED
examples/771336.jpg ADDED
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
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, bias=True)
21
+ )
22
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ torch==1.12.0
3
+ torchvision==0.13.0
4
+ gradio==3.1.4