Spaces:
Runtime error
Runtime error
FoodVision Big initial
Browse filesFoodVision Big - Food 101 Dataset을 구분하는 모델
- 09_pretrained_effentb1_feature_extractor_food_101_20_percent.pth +3 -0
- app.py +64 -0
- class_names.txt +101 -0
- examples/pizza_1280.jpg +0 -0
- model.py +21 -0
- requirements.txt +3 -0
09_pretrained_effentb1_feature_extractor_food_101_20_percent.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c3241fc09d7152d840c43e94fed68cf355c52e609d4408dcb89ea8cb40f8251
|
| 3 |
+
size 27031802
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
###1. 라이브러리, 클래스 이름 불러오기
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from model import create_effnetb1_model
|
| 7 |
+
from timeit import default_timer as timer
|
| 8 |
+
from typing import Tuple, Dict
|
| 9 |
+
|
| 10 |
+
with open("class_names.txt", "r") as f:
|
| 11 |
+
class_names = [food_name.strip() for food_name in f.readlines()]
|
| 12 |
+
|
| 13 |
+
### 2. Model, transforms 준비
|
| 14 |
+
effnetb1, effnetb1_transforms = create_effnetb1_model(num_classes=101)
|
| 15 |
+
|
| 16 |
+
#load saved weights
|
| 17 |
+
effnetb1.load_state_dict(
|
| 18 |
+
torch.load(f="09_pretrained_effentb1_feature_extractor_food_101_20_percent.pth",
|
| 19 |
+
map_location=torch.device("cpu"))
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
### 3. Predict functions
|
| 23 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 24 |
+
#timer 시작
|
| 25 |
+
start_time = timer()
|
| 26 |
+
#image effnetb1 입력형태로 변환
|
| 27 |
+
img = effnetb1_transforms(img).unsqueeze(0) #batch dimension 0번째 차원에 더하기
|
| 28 |
+
#예측하기
|
| 29 |
+
effnetb1.eval()
|
| 30 |
+
with torch.inference_mode():
|
| 31 |
+
#image -> prediction logits -> prediction probability
|
| 32 |
+
pred_probs = torch.softmax(effnetb1(img), dim=1)
|
| 33 |
+
|
| 34 |
+
#prediction label, pred probability dictionary
|
| 35 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 36 |
+
#예측 시간 계산
|
| 37 |
+
end_time = timer()
|
| 38 |
+
pred_time = round(end_time-start_time, 4)
|
| 39 |
+
#pred dict, pred time 돌려주기
|
| 40 |
+
return pred_labels_and_probs, pred_time
|
| 41 |
+
|
| 42 |
+
### 4. Gradio app ###
|
| 43 |
+
|
| 44 |
+
title = "FoodVision Big"
|
| 45 |
+
description = "An EfficientNetB1 feature extractor for 101 classes of food "
|
| 46 |
+
article = "Created at 09. PyTorch Model Deployment."
|
| 47 |
+
|
| 48 |
+
#example list를 demo app 내부 경로로 수정
|
| 49 |
+
|
| 50 |
+
#예시 그림의 파일 경로 가져오기!
|
| 51 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 52 |
+
example_list
|
| 53 |
+
|
| 54 |
+
#Gradio demo
|
| 55 |
+
demo = gr.Interface(fn=predict,
|
| 56 |
+
inputs=gr.Image(type="pil"),
|
| 57 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
| 58 |
+
gr.Number(label="Prediction time (s)")],
|
| 59 |
+
examples= example_list,
|
| 60 |
+
title=title,
|
| 61 |
+
description=description,
|
| 62 |
+
article=article)
|
| 63 |
+
#데모 실행
|
| 64 |
+
demo.launch(debug=False) #디버그 방식 끄기
|
class_names.txt
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
apple_pie
|
| 2 |
+
baby_back_ribs
|
| 3 |
+
baklava
|
| 4 |
+
beef_carpaccio
|
| 5 |
+
beef_tartare
|
| 6 |
+
beet_salad
|
| 7 |
+
beignets
|
| 8 |
+
bibimbap
|
| 9 |
+
bread_pudding
|
| 10 |
+
breakfast_burrito
|
| 11 |
+
bruschetta
|
| 12 |
+
caesar_salad
|
| 13 |
+
cannoli
|
| 14 |
+
caprese_salad
|
| 15 |
+
carrot_cake
|
| 16 |
+
ceviche
|
| 17 |
+
cheese_plate
|
| 18 |
+
cheesecake
|
| 19 |
+
chicken_curry
|
| 20 |
+
chicken_quesadilla
|
| 21 |
+
chicken_wings
|
| 22 |
+
chocolate_cake
|
| 23 |
+
chocolate_mousse
|
| 24 |
+
churros
|
| 25 |
+
clam_chowder
|
| 26 |
+
club_sandwich
|
| 27 |
+
crab_cakes
|
| 28 |
+
creme_brulee
|
| 29 |
+
croque_madame
|
| 30 |
+
cup_cakes
|
| 31 |
+
deviled_eggs
|
| 32 |
+
donuts
|
| 33 |
+
dumplings
|
| 34 |
+
edamame
|
| 35 |
+
eggs_benedict
|
| 36 |
+
escargots
|
| 37 |
+
falafel
|
| 38 |
+
filet_mignon
|
| 39 |
+
fish_and_chips
|
| 40 |
+
foie_gras
|
| 41 |
+
french_fries
|
| 42 |
+
french_onion_soup
|
| 43 |
+
french_toast
|
| 44 |
+
fried_calamari
|
| 45 |
+
fried_rice
|
| 46 |
+
frozen_yogurt
|
| 47 |
+
garlic_bread
|
| 48 |
+
gnocchi
|
| 49 |
+
greek_salad
|
| 50 |
+
grilled_cheese_sandwich
|
| 51 |
+
grilled_salmon
|
| 52 |
+
guacamole
|
| 53 |
+
gyoza
|
| 54 |
+
hamburger
|
| 55 |
+
hot_and_sour_soup
|
| 56 |
+
hot_dog
|
| 57 |
+
huevos_rancheros
|
| 58 |
+
hummus
|
| 59 |
+
ice_cream
|
| 60 |
+
lasagna
|
| 61 |
+
lobster_bisque
|
| 62 |
+
lobster_roll_sandwich
|
| 63 |
+
macaroni_and_cheese
|
| 64 |
+
macarons
|
| 65 |
+
miso_soup
|
| 66 |
+
mussels
|
| 67 |
+
nachos
|
| 68 |
+
omelette
|
| 69 |
+
onion_rings
|
| 70 |
+
oysters
|
| 71 |
+
pad_thai
|
| 72 |
+
paella
|
| 73 |
+
pancakes
|
| 74 |
+
panna_cotta
|
| 75 |
+
peking_duck
|
| 76 |
+
pho
|
| 77 |
+
pizza
|
| 78 |
+
pork_chop
|
| 79 |
+
poutine
|
| 80 |
+
prime_rib
|
| 81 |
+
pulled_pork_sandwich
|
| 82 |
+
ramen
|
| 83 |
+
ravioli
|
| 84 |
+
red_velvet_cake
|
| 85 |
+
risotto
|
| 86 |
+
samosa
|
| 87 |
+
sashimi
|
| 88 |
+
scallops
|
| 89 |
+
seaweed_salad
|
| 90 |
+
shrimp_and_grits
|
| 91 |
+
spaghetti_bolognese
|
| 92 |
+
spaghetti_carbonara
|
| 93 |
+
spring_rolls
|
| 94 |
+
steak
|
| 95 |
+
strawberry_shortcake
|
| 96 |
+
sushi
|
| 97 |
+
tacos
|
| 98 |
+
takoyaki
|
| 99 |
+
tiramisu
|
| 100 |
+
tuna_tartare
|
| 101 |
+
waffles
|
examples/pizza_1280.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision
|
| 4 |
+
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
def create_effnetb1_model(num_classes:int=101):
|
| 8 |
+
# 1,2,3 EffNetB1의 미리 훈련된 weights, transforms, model 얻기
|
| 9 |
+
weights = torchvision.models.EfficientNet_B1_Weights.DEFAULT
|
| 10 |
+
transforms = weights.transforms()
|
| 11 |
+
model = torchvision.models.efficientnet_b1(weights=weights)
|
| 12 |
+
|
| 13 |
+
# 4. 기본 모델의 layer 고정
|
| 14 |
+
for param in model.parameters():
|
| 15 |
+
param.requires_grad = False
|
| 16 |
+
# 5. classifier head 바꾸기
|
| 17 |
+
model.classifier = nn.Sequential(
|
| 18 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 19 |
+
nn.Linear(in_features=1280, out_features=num_classes)
|
| 20 |
+
)
|
| 21 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.1.0
|
| 2 |
+
torchvision==0.16.0
|
| 3 |
+
gr==1.23.2
|