|
import gradio as gr |
|
import torch |
|
from torchvision import models |
|
from PIL import Image |
|
from torch import nn |
|
|
|
|
|
num_classes = 3 |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
class_names = ["pizza", "steak", "sushi"] |
|
class_dict = {"pizza": 0, "steak": 1, "sushi": 2} |
|
|
|
|
|
def classify_image(image_path): |
|
|
|
model = models.mobilenet_v3_large( |
|
weights=models.MobileNet_V3_Large_Weights.IMAGENET1K_V1 |
|
) |
|
model.classifier[-1] = nn.Linear(model.classifier[-1].in_features, num_classes) |
|
model.load_state_dict( |
|
torch.load( |
|
"MobileNetV3-Food-Classification.pth", |
|
weights_only=True, |
|
map_location=device, |
|
) |
|
) |
|
model.to(device) |
|
model.eval() |
|
|
|
|
|
weights = models.MobileNet_V3_Large_Weights.IMAGENET1K_V1 |
|
preprocess = weights.transforms() |
|
|
|
|
|
image = Image.open(image_path) |
|
input_tensor = preprocess(image) |
|
|
|
|
|
input_batch = input_tensor.unsqueeze(0) |
|
|
|
|
|
input_batch = input_batch.to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
output = model(input_batch) |
|
|
|
|
|
probabilities = torch.nn.functional.softmax(output[0], dim=0) |
|
|
|
|
|
top_prob, top_catid = torch.topk(probabilities, 1) |
|
top_category = class_names[top_catid.item()] |
|
top_probability = top_prob.item() |
|
|
|
|
|
return f"Prediction: {top_category.title()} ({top_probability:.1%} confident)" |
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image( |
|
type="filepath", label="Upload a food image (pizza, steak, or sushi)" |
|
), |
|
outputs=gr.Text(label="Classification Result"), |
|
title="Food Classifier", |
|
description="This model classifies images of pizza, steak, and sushi.", |
|
) |
|
demo.launch() |
|
|