Spaces:
Sleeping
Sleeping
import torch | |
from torch import nn | |
import torchvision.transforms as transforms | |
import torch.nn.functional as F | |
from pathlib import Path | |
import gradio as gr | |
from PIL import Image | |
import numpy as np | |
LABELS = Path('classes.txt').read_text().splitlines() | |
num_classes = len(LABELS) | |
model = nn.Sequential( | |
nn.Conv2d(1, 64, 3, padding='same'), | |
nn.ReLU(), | |
nn.MaxPool2d(2), | |
nn.Conv2d(64, 128, 3, padding='same'), | |
nn.ReLU(), | |
nn.MaxPool2d(2), | |
nn.Conv2d(128, 256, 3, padding='same'), | |
nn.ReLU(), | |
nn.MaxPool2d(2), | |
nn.Flatten(), | |
nn.Linear(2304, 512), | |
nn.ReLU(), | |
nn.Linear(512, num_classes), | |
) | |
state_dict = torch.load('model.pth', map_location='cpu') | |
model.load_state_dict(state_dict, strict=False) | |
model.eval() | |
def predict(im): | |
x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255. | |
with torch.no_grad(): | |
out = model(x) | |
probabilities = F.softmax(out[0], dim=0) | |
values, indices = torch.topk(probabilities, 5) | |
return {LABELS[i]: v.item() for i, v in zip(indices, values)} | |
interface = gr.Interface(predict, inputs='sketchpad', outputs='label', live=True) | |
interface.launch(debug=True) |