kdallash commited on
Commit
9e2f3a9
·
verified ·
1 Parent(s): d9f4858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -54
app.py CHANGED
@@ -1,54 +1,54 @@
1
- ### 1 Imports and class names setup###
2
- import gradio as gr
3
- import os
4
- import torch
5
- from model import create_effnetb2_model
6
- from timeit import default_timer as timer
7
- from typing import List, Dict
8
-
9
- class_names = ["pizza", "steak", "sushi"]
10
-
11
- ### 2 model and transform preparation###
12
- effnetb2, transforms = create_effnetb2_model(num_classes=len(class_names))
13
- effnetb2_loaded.load_state_dict("11-model_deployment_effnetb2.pth",map_location="cpu")
14
-
15
-
16
- ### 3 we need a predict function###
17
- def predict(img) -> Tuple[Dict,float]:
18
- #start a timer
19
- start_time = timer()
20
-
21
- # transform the image
22
- transformed_image = effnet_transform(img).unsqueeze(0)
23
-
24
- # putting the model in eval mode and make the prediction
25
- effnetb2_loaded.eval()
26
- with torch.inference_mode():
27
- logit = effnetb2_loaded(transformed_image)
28
-
29
- probs = torch.softmax(logit, dim=1)
30
- # Create a prediction label and prediction probability dictionary
31
- pred_label_dict ={class_names[i] : probs[0][i].item() for i in range(len(class_names))}
32
-
33
- # calculate the pred time
34
- end_time = timer()
35
- inference_time = round(end_time - start_time, 4)
36
- # return the label dict and inference time
37
- return pred_label_dict, inference_time
38
- ###Grad###
39
- title = "FoodVision mini models 🍕,🥩,🍣"
40
- description = "An EfficientnetB2 feature extraction model is used to classifay images as pizza, steak, sushi"
41
-
42
- example_list =[["example/"+example] for example in os.listdir("example")]
43
- # create a gradio demo
44
- demo = gr.Interface(
45
- fn=predict,
46
- inputs=gr.Image(type="pil"),
47
- outputs=[gr.Label(num_top_classes = 3,label= "prediction"),
48
- gr.Number(label=" Prediction time in second")],
49
- examples=example_list[0],
50
- title=title,
51
- description=description
52
- )
53
- demo.launch(share= False)
54
-
 
1
+ ### 1 Imports and class names setup###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as timer
7
+ from typing import List, Dict
8
+
9
+ class_names = ["pizza", "steak", "sushi"]
10
+
11
+ ### 2 model and transform preparation###
12
+ effnetb2_loaded, transforms = create_effnetb2_model(num_classes=len(class_names))
13
+ effnetb2_loaded.load_state_dict("11-model_deployment_effnetb2.pth",map_location="cpu")
14
+
15
+
16
+ ### 3 we need a predict function###
17
+ def predict(img) -> Tuple[Dict,float]:
18
+ #start a timer
19
+ start_time = timer()
20
+
21
+ # transform the image
22
+ transformed_image = effnet_transform(img).unsqueeze(0)
23
+
24
+ # putting the model in eval mode and make the prediction
25
+ effnetb2_loaded.eval()
26
+ with torch.inference_mode():
27
+ logit = effnetb2_loaded(transformed_image)
28
+
29
+ probs = torch.softmax(logit, dim=1)
30
+ # Create a prediction label and prediction probability dictionary
31
+ pred_label_dict ={class_names[i] : probs[0][i].item() for i in range(len(class_names))}
32
+
33
+ # calculate the pred time
34
+ end_time = timer()
35
+ inference_time = round(end_time - start_time, 4)
36
+ # return the label dict and inference time
37
+ return pred_label_dict, inference_time
38
+ ###Grad###
39
+ title = "FoodVision mini models 🍕,🥩,🍣"
40
+ description = "An EfficientnetB2 feature extraction model is used to classifay images as pizza, steak, sushi"
41
+
42
+ example_list =[["example/"+example] for example in os.listdir("example")]
43
+ # create a gradio demo
44
+ demo = gr.Interface(
45
+ fn=predict,
46
+ inputs=gr.Image(type="pil"),
47
+ outputs=[gr.Label(num_top_classes = 3,label= "prediction"),
48
+ gr.Number(label=" Prediction time in second")],
49
+ examples=example_list[0],
50
+ title=title,
51
+ description=description
52
+ )
53
+ demo.launch(share= False)
54
+