|
import torch |
|
import torch.nn as nn |
|
import torchvision.transforms as transforms |
|
from torchvision import models |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
class Params: |
|
def __init__(self): |
|
self.batch_size = 128 |
|
self.name = "resnet_50_sgd" |
|
self.workers = 4 |
|
self.lr = 0.1 |
|
self.momentum = 0.9 |
|
self.weight_decay = 1e-4 |
|
self.lr_step_size = 30 |
|
self.lr_gamma = 0.1 |
|
|
|
def __repr__(self): |
|
return str(self.__dict__) |
|
|
|
def __eq__(self, other): |
|
return self.__dict__ == other.__dict__ |
|
|
|
|
|
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() |
|
|