Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +34 -0
- requirements.txt +4 -0
- vis_trans_cat_dog.pth +3 -0
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import timm
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
model = timm.create_model('vit_base_patch16_224', pretrained=False, num_classes=2)
|
| 8 |
+
model.load_state_dict(torch.load("vis_trans_cat_dog.pth", map_location='cpu'))
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
transform = transforms.Compose([
|
| 12 |
+
transforms.Resize((224, 224)),
|
| 13 |
+
transforms.ToTensor(),
|
| 14 |
+
transforms.Normalize([0.5], [0.5])
|
| 15 |
+
])
|
| 16 |
+
|
| 17 |
+
def predict(image):
|
| 18 |
+
image = Image.open(image).convert('RGB')
|
| 19 |
+
image = transform(image).unsqueeze(0)
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(image)
|
| 22 |
+
_, predicted = torch.max(outputs, 1)
|
| 23 |
+
return 'Cat' if predicted.item() == 0 else 'Dog'
|
| 24 |
+
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=predict,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=gr.Label(),
|
| 29 |
+
title="ViT Cat vs Dog Classifier 🐱🐶",
|
| 30 |
+
description="Upload an image of a cat or dog and get a prediction from a Vision Transformer model."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
timm
|
| 4 |
+
gradio
|
vis_trans_cat_dog.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:712ce36578d4c202598b2e65083634ad767c2020c76395c5cae820794a75b436
|
| 3 |
+
size 343260442
|