Bala-A87 commited on
Commit
960e26b
1 Parent(s): 76d4469

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:29e76c4da4682eb81e3e08537864fc811bb0d8cd1ca2690b359f074bfe0c8da6
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os, torch
3
+ from model import create_effnetb2_model
4
+ from timeit import default_timer as timer
5
+ from typing import Tuple, Dict
6
+
7
+ class_names = ['pizza', 'steak', 'sushi']
8
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
9
+ effnetb2.load_state_dict(
10
+ torch.load(
11
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
12
+ map_location=torch.device('cpu')
13
+ )
14
+ )
15
+
16
+ def predict(img) -> Tuple[Dict, float]:
17
+ """
18
+ Transforms and performs a prediction on img and
19
+ returns prediction and time taken.
20
+ """
21
+ start_time = timer()
22
+ img = effnetb2_transforms(img).unsqueeze(0)
23
+ effnetb2.eval()
24
+ with torch.inference_mode():
25
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
26
+ pred_labels_and_probs = {
27
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
28
+ }
29
+ pred_time = round(timer() - start_time, 5)
30
+ return pred_labels_and_probs, pred_time
31
+
32
+ title = "FoodVision Mini 🍕🥩🍣"
33
+ description = 'An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi.'
34
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
35
+
36
+ example_list = [['examples/' + example] for example in os.listdir('examples')]
37
+
38
+ demo = gr.Interface(
39
+ fn=predict,
40
+ inputs=gr.Image(type='pil'),
41
+ outputs=[
42
+ gr.Label(num_top_classes=3, label='Predictions'),
43
+ gr.Number(label='Prediction time(s)')
44
+ ],
45
+ examples=example_list,
46
+ title=title,
47
+ description=description,
48
+ article=article
49
+ )
50
+
51
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch, torchvision
2
+ from torch import nn
3
+
4
+ def create_effnetb2_model(
5
+ num_classes: int = 3,
6
+ seed: int = 42
7
+ ):
8
+ """
9
+ Creates an EfficientNetB2 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the
13
+ classifier head. 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
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
21
+ transforms = weights.transforms()
22
+ model = torchvision.models.efficientnet_b2(weights=weights)
23
+
24
+ for param in model.parameters():
25
+ param.requires_grad = False
26
+
27
+ torch.manual_seed(seed)
28
+ model.classifier = nn.Sequential(
29
+ nn.Dropout(p=0.3, inplace=True),
30
+ nn.Linear(in_features=1408, out_features=num_classes)
31
+ )
32
+ 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