|
import torch |
|
import torch.nn as nn |
|
from torchvision import models, transforms |
|
from PIL import Image |
|
import numpy as np |
|
import gradio as gr |
|
|
|
|
|
class PretrainedModel(nn.Module): |
|
def __init__(self, num_classes=19): |
|
super(PretrainedModel, self).__init__() |
|
|
|
weights = models.ResNet50_Weights.IMAGENET1K_V2 |
|
self.model = models.resnet50(weights=weights) |
|
|
|
|
|
for param in self.model.parameters(): |
|
param.requires_grad = False |
|
|
|
|
|
num_features = self.model.fc.in_features |
|
self.model.fc = nn.Linear(num_features, num_classes) |
|
|
|
def forward(self, x): |
|
return self.model(x) |
|
|
|
|
|
model = PretrainedModel(num_classes=19) |
|
|
|
|
|
state_dict = torch.load('model_v11.pt', map_location=torch.device('cpu')) |
|
model.load_state_dict(state_dict) |
|
model.eval() |
|
|
|
|
|
preprocess = transforms.Compose([ |
|
transforms.Resize((224, 224)), |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) |
|
]) |
|
|
|
def classify_image(img): |
|
|
|
img_tensor = preprocess(img).unsqueeze(0) |
|
with torch.no_grad(): |
|
output = model(img_tensor) |
|
probabilities = torch.softmax(output, dim=1) |
|
predicted_class_index = probabilities.argmax().item() |
|
predicted_probability = probabilities[0][predicted_class_index].item() |
|
|
|
return f"Class {predicted_class_index}, Confidence: {predicted_probability:.4f}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text" |
|
) |
|
|
|
iface.launch(share=True) |
|
|