Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision.transforms as transforms
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from model import AlexNet
|
| 6 |
+
|
| 7 |
+
CLASSES = ['airplane','automobile','bird','cat','deer',
|
| 8 |
+
'dog','frog','horse','ship','truck']
|
| 9 |
+
|
| 10 |
+
# 모델 로드
|
| 11 |
+
model = AlexNet()
|
| 12 |
+
model.load_state_dict(torch.load('alexnet_cifar10.pth', map_location='cpu'))
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
transform = transforms.Compose([
|
| 16 |
+
transforms.Resize((32, 32)),
|
| 17 |
+
transforms.ToTensor(),
|
| 18 |
+
transforms.Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))
|
| 19 |
+
])
|
| 20 |
+
|
| 21 |
+
def predict(image):
|
| 22 |
+
img = transform(image).unsqueeze(0)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
output = model(img)
|
| 25 |
+
probs = torch.softmax(output, dim=1)[0]
|
| 26 |
+
return {CLASSES[i]: float(probs[i]) for i in range(10)}
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=predict,
|
| 30 |
+
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs=gr.Label(num_top_classes=3),
|
| 32 |
+
title="AlexNet CIFAR-10 분류기",
|
| 33 |
+
description="이미지를 업로드하면 10개 클래스 중 하나로 분류해요."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|