MiladAbdollahi commited on
Commit
0384ae1
1 Parent(s): 667f15d

Upload 6 files

Browse files
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_model
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+
10
+ # Loading saved weights
11
+ model, transforms = create_model(num_classes=13)
12
+ model_load_dict = torch.load('iran_cars_model_dict.pth', map_location=torch.device('cpu'))
13
+ model.load_state_dict(model_load_dict['state_dict'])
14
+ class_names = model_load_dict['class_names']
15
+
16
+
17
+ def predict(img):
18
+ """Transforms and performs a prediction on img
19
+ and returns prediction and time taken.
20
+ """
21
+ # starting the timer
22
+ start_time = timer()
23
+
24
+ # transforming the target image and adding a batch dimention
25
+ img = transforms(img).unsqueeze(0)
26
+
27
+ # putting model into evaluation mode
28
+ model.eval()
29
+
30
+ # turning on inference_mode in context manager
31
+ with torch.inference_mode():
32
+ pred_probs = torch.softmax(model(img), dim=1)
33
+
34
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
35
+
36
+ pred_time = round(timer() - start_time, 5)
37
+
38
+ return pred_labels_and_probs, pred_time
39
+
40
+
41
+
42
+ title = 'Iran Cars CoputerVision_V0 🚗'
43
+ description = 'an EfficientNetb0 CV model created by MiladAbdollahi'
44
+ article = 'github.com/Milad-Abdollahi'
45
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
46
+
47
+ demo = gr.Interface(fn=predict,
48
+ inputs=gr.Image(type='pil'),
49
+ outputs=[gr.Label(num_top_classes=13, label='Predictions'),
50
+ gr.Number(Label="Prediction time (s)")],
51
+ examples=example_list,
52
+ title=title,
53
+ description=description,
54
+ article=article
55
+ )
56
+
57
+ demo.launch(debug=False, share=True)
examples/Peugeot-207i (203).jpg ADDED
examples/Peykan (3).jpg ADDED
examples/Samand (87).jpg ADDED
model.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ device='cpu'
7
+ def create_model(num_classes: int=13):
8
+ weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
9
+ transforms = weights.transforms()
10
+ model = torchvision.models.efficientnet_b0(weights=weights).to(device)
11
+
12
+ for param in model.features.parameters():
13
+ param.requires_grad = False
14
+
15
+ model.classifier = torch.nn.Sequential(
16
+ torch.nn.Dropout(p=0.2, inplace=True),
17
+ torch.nn.Linear(in_features=1280, out_features=13, bias=True)
18
+ ).to(device)
19
+
20
+ return model, transforms
21
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.1.4