|
import torch |
|
import torchvision.transforms as transforms |
|
from torchvision import models |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
device = torch.device('cpu') |
|
|
|
|
|
model = models.resnet50(pretrained=False) |
|
model.load_state_dict(torch.load("model.pth", map_location=device)) |
|
model.to(device) |
|
|
|
model.eval() |
|
|
|
|
|
transform = transforms.Compose([ |
|
transforms.Resize(256), |
|
transforms.CenterCrop(224), |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
|
]) |
|
|
|
|
|
LABELS = ["class_1", "class_2", "class_3", "class_4", "class_5", |
|
"class_6", "class_7", "class_8", "class_9", "class_10"] |
|
|
|
|
|
def predict(image): |
|
image = Image.open(image).convert("RGB") |
|
image = transform(image).unsqueeze(0) |
|
|
|
|
|
image = image.to(device) |
|
|
|
with torch.no_grad(): |
|
outputs = model(image) |
|
|
|
_, predicted = torch.max(outputs, 1) |
|
return LABELS[predicted.item()] |
|
|
|
|
|
interface = gr.Interface(fn=predict, inputs=gr.inputs.Image(type="pil"), outputs="text") |
|
|
|
|
|
interface.launch() |
|
|