Spaces:
Sleeping
Sleeping
File size: 1,027 Bytes
b88e16c 7270c85 b88e16c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import torch
import timm
import gradio as gr
from PIL import Image
import torchvision.transforms as transforms
# --------- Model ---------
model = timm.create_model(
"efficientnetv2_s",
pretrained=False,
num_classes=4
)
model.load_state_dict(torch.load("efficient.pth", map_location="cpu"))
model.eval()
# --------- Transform ---------
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.Lambda(lambda x: x.convert("RGB")),
transforms.ToTensor()
])
classes = [
"Class 0",
"Class 1",
"Class 2",
"Class 3"
]
# --------- Prediction ---------
def predict(img):
img = transform(img).unsqueeze(0)
with torch.no_grad():
outputs = model(img)
pred = outputs.argmax(1).item()
return classes[pred]
# --------- UI ---------
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="text",
title="OCTMNIST Medical Image Classifier",
description="EfficientNetV2-S trained on OCTMNIST dataset"
)
demo.launch() |