kazeemkz commited on
Commit
8506669
·
1 Parent(s): 46ec729

initial commit

Browse files
09_preptrained_effnetb2_20_percent (2).pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e8e5bbde000377e5b344db2fb6a136c33820becbda8796ab59a7afd3796bb63
3
+ size 31288122
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ #from demos.foodvision_mini.model import create_effnetb2_model
6
+ from model import create_effnetb2_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ #setup classnames
11
+
12
+ class_names = ['pizza', 'steak', 'sushi']
13
+
14
+ # model and trandorms preparations
15
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
16
+
17
+ #load save weights
18
+
19
+ effnetb2.load_state_dict(
20
+ torch.load(f = "09_preptrained_effnetb2_20_percent (2).pth",
21
+ map_location = torch.device('cpu'))
22
+
23
+ )
24
+
25
+ # make predictions
26
+
27
+ def predict(img) -> Tuple[Dict,float] :
28
+ start_time = timer()
29
+ # this returns the prediction, and then, time
30
+ #start a timers
31
+ # transform the input image for use with effnetb2
32
+ #put model into eval mode
33
+ # create a prediction label and prediction probability dictionary
34
+ img = effnetb2_transforms(img).unsqueeze(0)
35
+ effnetb2.eval()
36
+
37
+ with torch.inference_mode():
38
+ pred_probs = torch.softmax(effnetb2(img), dim = 1)
39
+
40
+ pred_labels_and_probs = {class_names[i]:float(pred_probs[0][i]) for i in range(len(class_names))}
41
+
42
+ end_time = timer()
43
+ pred_time = round(end_time - start_time, 4)
44
+
45
+ return pred_labels_and_probs, pred_time
46
+
47
+ import os
48
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
49
+
50
+ title = "FoodVision Mini"
51
+ Description = "An EfficientNetB2 feature computer vision model to classify images as pizza, steak or sushi"
52
+ article = "Cretated at......"
53
+
54
+ demo = gr.Interface(fn=predict,inputs=gr.Image(type='pil'),
55
+ outputs =[gr.Label(num_top_classes=3, label = "Predictions"),
56
+ gr.Number(label="Prediction time (s)")],
57
+ examples= example_list,
58
+ title = title,
59
+ description=Description,
60
+ article=article)
61
+
62
+ demo.launch(debug=False,share = True) # print errors locally, generate a publically shareable URL
examples/pizza.jpg ADDED
examples/pizza_1.jpg ADDED
examples/steak.jpg ADDED
model.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes:int=3, seed:int=42):
7
+
8
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
9
+
10
+ transforms = weights.transforms()
11
+
12
+ #setup pretrained model instance
13
+
14
+ model = torchvision.models.efficientnet_b2(weights=weights)
15
+
16
+ # free base layer in the model
17
+
18
+ for param in model.parameters():
19
+ param.requires_grad = False
20
+
21
+ torch.manual_seed(seed)
22
+ model.classifier = nn.Sequential(
23
+ nn.Dropout(p = 0.3, inplace= True),
24
+ nn.Linear(in_features = 1408,out_features =num_classes, bias = True)
25
+
26
+ )
27
+
28
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch ==2.2.0
2
+ torchvision ==0.17.0
3
+ gradio==4.26.0