Spaces:
Sleeping
Sleeping
Download app.py from MaryOrg/EfficientNet: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/MaryOrg/EfficientNet/resolve/main/app.py
- Command line
-
hf download hf://spaces/MaryOrg/EfficientNet/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MaryOrg/EfficientNet/resolve/main/app.py
1.03 kB
| 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() |