|
|
| import gradio as gr |
| import torch |
| import torch.nn as nn |
| import torchvision.models as models |
| import torchvision.transforms as transforms |
| from PIL import Image |
| import json |
|
|
| |
| |
| with open("index_to_class_id.json") as f: |
| index_to_class_id = json.load(f) |
|
|
| with open("class_id_to_name.json") as f: |
| class_id_to_name = json.load(f) |
|
|
| |
| |
| model = models.resnet18(pretrained=False) |
| |
| |
|
|
| |
| num_ftrs = model.fc.in_features |
| model.fc = nn.Linear(num_ftrs, 200) |
|
|
| |
| |
| model_path = "resnet18_tiny_imagenet.pth" |
| model.load_state_dict(torch.load(model_path, map_location="cpu")) |
| model.eval() |
|
|
| |
| |
| val_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]) |
| ]) |
|
|
| |
| def predict(img_pil): |
| """ |
| Takes a PIL Image, transforms it, and returns a dictionary |
| of class probabilities. |
| """ |
| |
| img_tensor = val_transform(img_pil).unsqueeze(0) |
|
|
| |
| with torch.no_grad(): |
| outputs = model(img_tensor) |
| |
| |
| probabilities = torch.nn.functional.softmax(outputs, dim=1)[0] |
| |
| |
| confidences = {} |
| for i in range(len(probabilities)): |
| class_id = index_to_class_id[i] |
| class_name = class_id_to_name.get(class_id, "Unknown") |
| confidences[class_name] = float(probabilities[i]) |
| |
| return confidences |
|
|
| |
| |
| example_paths = ["val_0.JPEG", "val_1.JPEG", "val_10.JPEG"] |
|
|
| title = "Tiny ImageNet Classifier (ResNet18)" |
| description = ( |
| "A ResNet18 model trained on the Tiny ImageNet (200 classes) dataset. " |
| "Upload an image to see the model's top 5 predictions." |
| ) |
|
|
| gr_interface = gr.Interface( |
| fn=predict, |
| inputs=gr.Image(type="pil", label="Upload an Image"), |
| outputs=gr.Label(num_top_classes=5, label="Predictions"), |
| title=title, |
| description=description, |
| examples=example_paths, |
| allow_flagging="never" |
| ) |
|
|
| |
| if __name__ == "__main__": |
| gr_interface.launch() |
|
|