nampham1106 commited on
Commit
1d31025
·
1 Parent(s): a49cdc3

deploy huggingface cloud

Browse files
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from pathlib import Path
6
+
7
+ from model import create_effnetb3_model
8
+ from timeit import default_timer as timer
9
+ from typing import Tuple, Dict
10
+
11
+ class_names = ['Banh beo', 'Banh bot loc', 'Banh can', 'Banh canh', 'Banh chung','Banh cuon', 'Banh duc', 'Banh gio','Banh khot',
12
+ 'Banh mi','Banh pia', 'Banh tet', 'Banh trang nuong', 'Banh xeo', 'Bun bo Hue', 'Bun dau mam tom','Bun mam', 'Bun rieu', 'Bun thit nuong',
13
+ 'Ca kho to', 'Canh chua', 'Cao lau', 'Chao long', 'Com tam', 'Goi cuon', 'Hu tieu', 'Mi quang', 'Nem chua', 'Pho', 'Xoi xeo']
14
+
15
+ effnetb3, effnetb3_transforms = create_effnetb3_model(num_classes=30)
16
+
17
+ effnetb3.load_state_dict(
18
+ torch.load(
19
+ f= "./models/pretrained_effnetb3_vietnamese_food.pth",
20
+ map_location=torch.device("cpu")
21
+ )
22
+ )
23
+
24
+ def predict(img) -> Tuple[Dict, float]:
25
+ start_time = timer()
26
+ img = effnetb3_transforms(img).unsqueeze(0)
27
+
28
+ effnetb3.eval()
29
+ with torch.inference_mode():
30
+ pred_probs = torch.softmax(effnetb3(img), dim = 1)
31
+
32
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
33
+
34
+ pred_time = round(timer() - start_time, 4)
35
+
36
+ return pred_labels_and_probs, pred_time
37
+
38
+ title = "Vietnamese food vision"
39
+ description = "An EfficientNetB3 feature extractor computer vision model"
40
+
41
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
42
+
43
+ demo = gr.Interface(fn=predict,
44
+ inputs=gr.Image(type="pil"),
45
+ outputs=[gr.Label(num_top_classes=3, label="Prediction"),
46
+ gr.Number(label="Prediction time (s)")],
47
+ examples=example_list,
48
+ title=title,
49
+ description=description)
50
+
51
+ demo.launch(share=True)
examples/breadbread.jpeg ADDED
examples/bundaumamtombundaumamtom.jpeg ADDED
examples/pho.jpeg ADDED
model.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+ from torchvision.models._api import WeightsEnum
6
+ from torch.hub import load_state_dict_from_url
7
+
8
+ def get_state_dict(self, *args, **kwargs):
9
+ kwargs.pop("check_hash")
10
+ return load_state_dict_from_url(self.url, *args, **kwargs)
11
+
12
+ WeightsEnum.get_state_dict = get_state_dict
13
+
14
+ def create_effnetb3_model(num_classes:int=30,
15
+ seed:int=42):
16
+ weights = torchvision.models.EfficientNet_B3_Weights.DEFAULT
17
+ transforms = weights.transforms()
18
+ model = torchvision.models.efficientnet_b3(weights=weights)
19
+
20
+ for param in model.parameters():
21
+ param.requires_grad = False
22
+
23
+ torch.manual_seed(seed)
24
+ model.classifier = nn.Sequential(
25
+ nn.Dropout(p=0.3, inplace=True),
26
+ nn.Linear(in_features=1536, out_features=128),
27
+ nn.ReLU(),
28
+ nn.Linear(in_features=128,
29
+ out_features=num_classes),
30
+ )
31
+ return model, transforms
models/pretrained_effnetb3_vietnamese_food.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:31392fa55dc44551073a938d2941d6baf99ae1ce612168b1e73be4ec84ab61f4
3
+ size 44159481
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.1.0+cpu
2
+ torchvision==0.16.0+cpu
3
+ gradio==4.7.1