|
import gradio as gr |
|
import torch |
|
import torchvision.transforms as transforms |
|
import torch.nn.functional as F |
|
import torchvision |
|
|
|
|
|
|
|
|
|
def load_model(checkpoint_path, num_classes): |
|
|
|
try: |
|
use_mps = torch.backends.mps.is_available() |
|
except AttributeError: |
|
use_mps = False |
|
|
|
if torch.cuda.is_available(): |
|
device = "cuda" |
|
elif use_mps: |
|
device = "mps" |
|
else: |
|
device = "cpu" |
|
|
|
model = torchvision.models.resnet50(weights=None) |
|
in_features = model.fc.in_features |
|
model.fc = torch.nn.Linear(in_features, num_classes) |
|
model.load_state_dict(torch.load(checkpoint_path, map_location=device)) |
|
model.eval() |
|
return model |
|
|
|
|
|
|
|
def process_image(image, image_size): |
|
|
|
preprocessing = transforms.Compose([ |
|
transforms.Resize((image_size, image_size)), |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
|
]) |
|
image = preprocessing(image).unsqueeze(0) |
|
return image |
|
|
|
|
|
|
|
def predict(image): |
|
classes = {'0': 'cat', '1': 'dog'} |
|
image = process_image(image, 256) |
|
with torch.no_grad(): |
|
outputs = model(image) |
|
probabilities = F.softmax(outputs, dim=1).squeeze() |
|
|
|
class_probabilities = {classes[str(i)]: float(prob) for i, prob in enumerate(probabilities)} |
|
return class_probabilities |
|
|
|
|
|
|
|
checkpoint_path = 'checkpoint/latest_checkpoint.pth' |
|
num_classes = 2 |
|
model = load_model(checkpoint_path, num_classes) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Label(num_top_classes=num_classes), |
|
title="Cat vs Dog Classifier", |
|
examples=["test_images/test_cat.jpg", "test_images/test_dog.jpg"] |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |