Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision.transforms as transforms
|
| 3 |
+
from torchvision import models
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# ์ ์ฒด ๋ชจ๋ธ ๋ก๋ (์ด๋ฏธ ์ ์๋ MyModel ํด๋์ค๋ ํ์ํ์ง ์์)
|
| 7 |
+
model = torch.load('resnet18_finetuned_full.pth') # ์ ์ฒด ๋ชจ๋ธ์ ๋ถ๋ฌ์ด
|
| 8 |
+
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ ํ
|
| 9 |
+
|
| 10 |
+
# ๋ฐ์ดํฐ ๋ณํ ์ ์
|
| 11 |
+
transform = transforms.Compose([
|
| 12 |
+
transforms.Resize((224, 224)),
|
| 13 |
+
transforms.ToTensor(),
|
| 14 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 15 |
+
])
|
| 16 |
+
|
| 17 |
+
# ํด๋์ค ์ด๋ฆ ๋ฆฌ์คํธ ์ ์
|
| 18 |
+
class_names = ['disposablecup', 'envmark', 'label', 'mug', 'nonlabel', 'reusablecup']
|
| 19 |
+
|
| 20 |
+
def predict(image):
|
| 21 |
+
image = transform(image).unsqueeze(0)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(image)
|
| 24 |
+
print("Raw outputs:", outputs) # ์๋ณธ ์ถ๋ ฅ๊ฐ ํ์ธ
|
| 25 |
+
_, predicted = torch.max(outputs, 1)
|
| 26 |
+
label = class_names[predicted.item()]
|
| 27 |
+
print("Predicted label:", label)
|
| 28 |
+
|
| 29 |
+
# ์์ธก ๊ฒฐ๊ณผ์ ์๋ณธ ์ถ๋ ฅ๊ฐ์ ๋ชจ๋ ๋ฐํ
|
| 30 |
+
return label, outputs.numpy() # ์๋ณธ ์ถ๋ ฅ๊ฐ์ NumPy ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐํ
|
| 31 |
+
|
| 32 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์ (๋ ๊ฐ์ ์ถ๋ ฅ)
|
| 33 |
+
iface = gr.Interface(fn=predict, inputs=gr.Image(type='pil'), outputs=['label', 'numpy'], live=True)
|
| 34 |
+
iface.launch()
|