File size: 1,087 Bytes
c3151ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3405fea
c3151ab
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
import torchvision.transforms as transforms
from PIL import Image
import gradio as gr
from model import AlexNet

CLASSES = ['airplane','automobile','bird','cat','deer',
           'dog','frog','horse','ship','truck']

# ๋ชจ๋ธ ๋กœ๋“œ
model = AlexNet()
model.load_state_dict(torch.load('alexnet_cifar10.pth', map_location='cpu'))
model.eval()

transform = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
    transforms.Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))
])

def predict(image):
    img = transform(image).unsqueeze(0)
    with torch.no_grad():
        output = model(img)
        probs = torch.softmax(output, dim=1)[0]
    return {CLASSES[i]: float(probs[i]) for i in range(10)}

demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="AlexNet CIFAR-10 ๋ถ„๋ฅ˜๊ธฐ",
    description="๋น„ํ–‰๊ธฐ, ์ž๋™์ฐจ, ์ƒˆ, ๊ณ ์–‘์ด, ์‚ฌ์Šด, ๊ฐœ, ๊ฐœ๊ตฌ๋ฆฌ, ๋ง, ๋ฐฐ, ํŠธ๋Ÿญ. ์ด 10๊ฐ€์ง€ ์‚ฌ์ง„์„ ๋ถ„๋ฅ˜ํ•ด์ฃผ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ง„์„ ๋„ฃ์–ด์ฃผ์„ธ์š”"
)

demo.launch()