Aedelon commited on
Commit
ecaa064
·
1 Parent(s): 098583d

Init commit

Browse files
09_pretrained_effnet2b_foodvision_mini.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ecc22a4e2d81f2d383fc1d5573cdfea8a0ce41e2fc08b1b762d2721efd83cfd1
3
+ size 31291787
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports --------------------------------
2
+ import os
3
+ import torch
4
+ import gradio as gr
5
+
6
+ from model import create_effnetb2_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Script ---------------------------------
11
+ # Setup class names
12
+ class_names = ['pizza', 'steak', 'sushi']
13
+
14
+ # Create model and load saved weights
15
+ effnetB2, effnetb2_transforms = create_effnetb2_model(
16
+ num_classes=3
17
+ )
18
+
19
+ effnetb2.load_state_dict(
20
+ torch.load(
21
+ f="09_pretrained_effnet2b_foodvision_mini.pth",
22
+ map_location=torch.device("cpu")
23
+ )
24
+ )
25
+
26
+ # Predict function
27
+ def predict(img) -> Tuple[Dict, float]:
28
+ # Start a timer
29
+ start_time = timer()
30
+ # Transform the input image for use with EffNetB2
31
+ img = effnetb2_transforms(img).unsqueeze(0)
32
+ # Put model into eval mode and make prediction
33
+ effnetb2.eval()
34
+
35
+ with torch.inference_mode():
36
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
37
+
38
+ # Create a prediction label and prediction probability dictionary
39
+ pred_labels_and_probs = {
40
+ class_names[i]: float(pred_probs[0][i]) \
41
+ for i in range(len(class_names))
42
+ }
43
+
44
+ # Calculate pred time
45
+ pred_time = round(timer() - start_time, 4)
46
+
47
+ # Return pred dict and pred time
48
+ return pred_labels_and_probs, pred_time
49
+
50
+ # Launch demo
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
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
56
+
57
+ demo = gr.Interface(
58
+ fn=predict,
59
+ inputs=gr.Image(type="pil"),
60
+ outputs=[
61
+ gr.Label(num_top_classes=3, label="Predictions"),
62
+ gr.Number(label="Prediction time (s)")
63
+ ],
64
+ examples=example_list,
65
+ title=title,
66
+ description=description,
67
+ article=article
68
+ )
69
+
70
+ demo.launch(
71
+ debug=False,
72
+ share=True
73
+ )
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Creates an EfficientNetB2 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the classifier head.
13
+ Defaults to 3.
14
+ seed (int, optional): random seed value. Defaults to 42.
15
+
16
+ Returns:
17
+ model (torch.nn.Module): EffNetB2 feature extractor model.
18
+ transforms (torchvision.transforms): EffNetB2 image transforms.
19
+ """
20
+ # Create EffNetB2 pretrained weights, transforms and model
21
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
22
+ transforms = weights.transforms()
23
+ model = torchvision.models.efficientnet_b2(weights=weights)
24
+
25
+ # Freeze all layers in base model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier = nn.Sequential(
32
+ nn.Dropout(p=0.3, inplace=True),
33
+ nn.Linear(in_features=1408, out_features=num_classes),
34
+ )
35
+
36
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.0.0
2
+ torchvision==0.15.0
3
+ gradio=3.27.0