File size: 1,789 Bytes
b60902e
 
 
 
 
 
d82d587
b60902e
 
 
f94173d
d82d587
b60902e
 
 
 
 
 
 
f94173d
 
 
b60902e
 
 
 
 
 
 
 
 
 
 
 
 
ac1d126
b60902e
 
ac1d126
b60902e
 
c446819
 
55c6bb4
c446819
b60902e
55c6bb4
 
d82d587
55c6bb4
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image
import gradio as gr

# ๋ชจ๋ธ ํด๋ž˜์Šค ์ •์˜ (ResNet18)
class MyModel(torch.nn.Module):
    def __init__(self, num_classes):
        super(MyModel, self).__init__()
        self.model = models.resnet18(weights=None)  # ResNet18 ๋ชจ๋ธ ์‚ฌ์šฉ
        self.model.fc = torch.nn.Linear(self.model.fc.in_features, num_classes)  # ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด ์ˆ˜์ •

    def forward(self, x):
        return self.model(x)

# ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
num_classes = 6  # ํด๋ž˜์Šค ์ˆ˜์— ๋งž๊ฒŒ ์„ค์ •
model = MyModel(num_classes)

# ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ ๋กœ๋“œ (CPU๋กœ ๋งคํ•‘)
model.load_state_dict(torch.load('resnet18_finetuned_2.pth', map_location=torch.device('cpu')), strict=False)
model.eval()

# ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ ์ •์˜
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# ํด๋ž˜์Šค ์ด๋ฆ„ ๋ฆฌ์ŠคํŠธ ์ •์˜
class_names = ['disposablecup', 'envmark', 'label', 'mug', 'nonlabel', 'reusablecup']

def predict(image):
    image = transform(image).unsqueeze(0)
    with torch.no_grad():
        outputs = model(image)
        print("Raw outputs:", outputs)  # ์›๋ณธ ์ถœ๋ ฅ๊ฐ’ ํ™•์ธ
        _, predicted = torch.max(outputs, 1)
    label = class_names[predicted.item()]
    if label in ['disposablecup','label'] : result = "Failed"
    else : result = "Successed (+1)"
    # ์˜ˆ์ธก ๊ฒฐ๊ณผ์™€ ์›๋ณธ ์ถœ๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜
    return result  # ์›๋ณธ ์ถœ๋ ฅ๊ฐ’์„ NumPy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • (๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ)
iface = gr.Interface(fn=predict, inputs=gr.Image(type='pil'), outputs=['label', 'numpy'], live=True)
iface.launch()