File size: 1,768 Bytes
ab11ff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fc81b2
 
02ac556
 
 
 
 
 
 
2fc81b2
 
 
 
 
ab11ff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pathlib import Path

import torch
import gradio as gr
from torch import nn


LABELS = Path('class_names.txt').read_text().splitlines()

model = nn.Sequential(
    nn.Conv2d(1, 32, 3, padding='same'),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(32, 64, 3, padding='same'),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(64, 128, 3, padding='same'),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Flatten(),
    nn.Linear(1152, 256),
    nn.ReLU(),
    nn.Linear(256, len(LABELS)),
)
state_dict = torch.load('pytorch_model.bin', map_location='cpu')
model.load_state_dict(state_dict, strict=False)
model.eval()

def predict(im):
    
    def list_shape(lst):
        if not isinstance(lst, list):
            return []
        if not lst:
            return [0]
        shapes = [list_shape(sublist) for sublist in lst]
        max_shape = max(shapes, key=len)
        return [len(lst)] + [max(dimensions[i] if i < len(dimensions) else 0 for dimensions in shapes) for i in range(len(max_shape))]

    
    print(list_shape(im))

    
    x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.

    with torch.no_grad():
        out = model(x)

    probabilities = torch.nn.functional.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', 
    theme="huggingface", 
    title="Sketch Recognition", 
    description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!", 
    article = "<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
    live=True)
interface.launch(debug=True)