Sadanand Modak commited on
Commit
adf757a
·
1 Parent(s): f163b42
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model import create_effnetb2_model
3
+ import os
4
+ import torch
5
+ from torch import nn
6
+ from typing import List, Dict, Tuple
7
+ from timeit import default_timer as timer
8
+
9
+ class_names = ['pizza', 'steak', 'sushi']
10
+ model, transforms = create_effnetb2_model(num_classes=len(class_names))
11
+
12
+ ckpt = torch.load('effnet_ckpt.tar', map_location='cpu')
13
+ model.load_state_dict(ckpt['model_state_dict'])
14
+ model.to('cpu')
15
+
16
+
17
+ def predict(img) -> Tuple[Dict, float]:
18
+ start = timer()
19
+ img = transforms(img)
20
+ img = img.unsqueeze(0)
21
+ img = img.to('cpu')
22
+ model.to('cpu')
23
+ model.eval()
24
+ with torch.inference_mode():
25
+ pred_logits = model(img)
26
+ pred_probs = nn.Softmax(dim=1)(pred_logits).squeeze(0)
27
+ pred_probs_dict = {class_names[i]: pred_probs[i].item() for i in range(len(class_names))}
28
+ end = timer()
29
+ return pred_probs_dict, round(end - start, 4)
30
+
31
+
32
+ examples_dir = 'examples'
33
+ examples = [[os.path.join(examples_dir, f)] for f in os.listdir(examples_dir)]
34
+
35
+ import gradio as gr
36
+ title = "Pizza, Steak, Sushi Classifier 🍕🥩🍣"
37
+ description = "This efficientnetb2 model classifies images of pizza, steak, and sushi."
38
+ article = "Created for practice using [Gradio](https://www.gradio.app/)"
39
+ demo = gr.Interface(fn=predict,
40
+ inputs=gr.Image(type="pil", label="Image of Pizza, Steak, or Sushi"),
41
+ outputs=[gr.Label(label="Predictions", num_top_classes=len(class_names)),
42
+ gr.Number(label="Prediction Time (s)")],
43
+ examples=examples,
44
+ title=title,
45
+ description=description,
46
+ article=article)
47
+ demo.launch(debug=False,
48
+ share=True)
effnet_ckpt.tar ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d650522724426fc86d8c3f630cc399b26ce5df5c35679be1b21e0aa94493498c
3
+ size 31275834
examples/3729167.jpg ADDED
examples/3757027.jpg ADDED
examples/57230.jpg ADDED
model.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+ from torchvision.models import efficientnet_b2, EfficientNet_B2_Weights
3
+
4
+
5
+ def create_effnetb2_model(num_classes=3):
6
+ weights_effnetb2 = EfficientNet_B2_Weights.DEFAULT
7
+ transforms_effnetb2 = weights_effnetb2.transforms()
8
+ model_effnetb2 = efficientnet_b2(weights=weights_effnetb2)
9
+ for param in model_effnetb2.parameters():
10
+ param.requires_grad = False
11
+ model_effnetb2.classifier[1] = nn.Linear(in_features=1408, out_features=num_classes)
12
+ return model_effnetb2, transforms_effnetb2
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch>=2.2.0
2
+ torchvision>=0.17.0
3
+ gradio>=4.26.0