|
import gradio as gr |
|
import torch |
|
from PIL import Image |
|
from torchvision import transforms |
|
import torchvision.models as models |
|
import torch.nn as nn |
|
|
|
|
|
class_names = ['911', 'cayenne', 'cayman', 'macan', 'panamera', 'taycan'] |
|
|
|
|
|
model_ft = models.resnet34(weights=models.ResNet34_Weights.DEFAULT) |
|
for param in model_ft.parameters(): |
|
param.requires_grad = False |
|
for param in model_ft.layer4.parameters(): |
|
param.requires_grad = True |
|
|
|
num_ftrs = model_ft.fc.in_features |
|
model_ft.fc = nn.Linear(num_ftrs, len(class_names)) |
|
model_ft = model_ft.to('cuda' if torch.cuda.is_available() else 'cpu') |
|
model_ft.load_state_dict(torch.load('model_ft.pth')) |
|
model_ft.eval() |
|
|
|
|
|
preprocess = transforms.Compose([ |
|
transforms.Resize(256), |
|
transforms.CenterCrop(224), |
|
transforms.ToTensor(), |
|
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) |
|
]) |
|
|
|
|
|
def predict(image): |
|
image = preprocess(image).unsqueeze(0).to(model_ft.device) |
|
with torch.no_grad(): |
|
outputs = model_ft(image) |
|
_, predicted = torch.max(outputs, 1) |
|
return class_names[predicted.item()] |
|
|
|
|
|
iface = gr.Interface(fn=predict, |
|
inputs=gr.inputs.Image(type="pil"), |
|
outputs="text") |
|
|
|
iface.launch() |