|
import gradio as gr |
|
import torch |
|
from torch import nn |
|
from torchvision import models, transforms |
|
from PIL import Image |
|
|
|
|
|
model_id = "KabeerAmjad/food_classification_model" |
|
model = models.resnet50(pretrained=False) |
|
model.fc = nn.Linear(model.fc.in_features, 11) |
|
model.load_state_dict(torch.load(model_id)) |
|
model.eval() |
|
|
|
|
|
transform = 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 = transform(img).unsqueeze(0) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(img) |
|
probs = torch.softmax(outputs, dim=-1) |
|
|
|
|
|
top_label = model.config.id2label[probs.argmax().item()] |
|
return top_label |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Food Image Classification", |
|
description="Upload an image to classify if it’s an apple pie, etc." |
|
) |
|
|
|
|
|
iface.launch() |
|
|