Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,52 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from torch import nn
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
LABELS = Path('class_names.txt').read_text().splitlines()
|
| 9 |
-
|
| 10 |
-
model = nn.Sequential(
|
| 11 |
-
nn.Conv2d(1, 32, 3, padding='same'),
|
| 12 |
-
nn.ReLU(),
|
| 13 |
-
nn.MaxPool2d(2),
|
| 14 |
-
nn.Conv2d(32, 64, 3, padding='same'),
|
| 15 |
-
nn.ReLU(),
|
| 16 |
-
nn.MaxPool2d(2),
|
| 17 |
-
nn.Conv2d(64, 128, 3, padding='same'),
|
| 18 |
-
nn.ReLU(),
|
| 19 |
-
nn.MaxPool2d(2),
|
| 20 |
-
nn.Flatten(),
|
| 21 |
-
nn.Linear(1152, 256),
|
| 22 |
-
nn.ReLU(),
|
| 23 |
-
nn.Linear(256, len(LABELS)),
|
| 24 |
-
)
|
| 25 |
-
state_dict = torch.load('pytorch_model.bin', map_location='cpu')
|
| 26 |
-
model.load_state_dict(state_dict, strict=False)
|
| 27 |
-
model.eval()
|
| 28 |
-
|
| 29 |
-
def predict(im):
|
| 30 |
-
x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.
|
| 31 |
-
|
| 32 |
-
with torch.no_grad():
|
| 33 |
-
out = model(x)
|
| 34 |
-
|
| 35 |
-
probabilities = torch.nn.functional.softmax(out[0], dim=0)
|
| 36 |
-
|
| 37 |
-
values, indices = torch.topk(probabilities, 5)
|
| 38 |
-
|
| 39 |
-
return {LABELS[i]: v.item() for i, v in zip(indices, values)}
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
LABELS = Path('class_names.txt').read_text().splitlines()
|
| 9 |
+
|
| 10 |
+
model = nn.Sequential(
|
| 11 |
+
nn.Conv2d(1, 32, 3, padding='same'),
|
| 12 |
+
nn.ReLU(),
|
| 13 |
+
nn.MaxPool2d(2),
|
| 14 |
+
nn.Conv2d(32, 64, 3, padding='same'),
|
| 15 |
+
nn.ReLU(),
|
| 16 |
+
nn.MaxPool2d(2),
|
| 17 |
+
nn.Conv2d(64, 128, 3, padding='same'),
|
| 18 |
+
nn.ReLU(),
|
| 19 |
+
nn.MaxPool2d(2),
|
| 20 |
+
nn.Flatten(),
|
| 21 |
+
nn.Linear(1152, 256),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.Linear(256, len(LABELS)),
|
| 24 |
+
)
|
| 25 |
+
state_dict = torch.load('pytorch_model.bin', map_location='cpu')
|
| 26 |
+
model.load_state_dict(state_dict, strict=False)
|
| 27 |
+
model.eval()
|
| 28 |
+
|
| 29 |
+
def predict(im):
|
| 30 |
+
x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
out = model(x)
|
| 34 |
+
|
| 35 |
+
probabilities = torch.nn.functional.softmax(out[0], dim=0)
|
| 36 |
+
|
| 37 |
+
values, indices = torch.topk(probabilities, 5)
|
| 38 |
+
|
| 39 |
+
return {LABELS[i]: v.item() for i, v in zip(indices, values)}
|
| 40 |
+
|
| 41 |
+
sketchpad = gr.Image(label="Sketchpad", source="canvas"),
|
| 42 |
+
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
predict,
|
| 45 |
+
inputs=sketchpad,
|
| 46 |
+
outputs='label',
|
| 47 |
+
theme="huggingface",
|
| 48 |
+
title="Sketch Recognition",
|
| 49 |
+
description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!",
|
| 50 |
+
article = "<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
|
| 51 |
+
live=True)
|
| 52 |
+
interface.launch(debug=True)
|