Spaces:
Sleeping
Sleeping
jyfgyjsgf
Browse files- .gitignore +7 -0
- app.py +38 -3
- model.py +18 -0
- models/model.pth +3 -0
- requirements.txt +1 -0
.gitignore
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.ipynb_checkpoints
|
2 |
+
.vscode
|
3 |
+
data
|
4 |
+
data/training
|
5 |
+
data/testing
|
6 |
+
__pycache__
|
7 |
+
nb.ipynb
|
app.py
CHANGED
@@ -1,7 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
import torch
|
4 |
+
from torch.nn import Softmax
|
5 |
+
from torchvision.io import read_image
|
6 |
+
from torchvision.transforms.functional import rgb_to_grayscale
|
7 |
|
8 |
+
from model import NeuralNet
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
model = NeuralNet().to(device)
|
13 |
+
model.load_state_dict(torch.load("models/model.pth"))
|
14 |
+
|
15 |
+
|
16 |
+
labels = [
|
17 |
+
"zero",
|
18 |
+
"one",
|
19 |
+
"two",
|
20 |
+
"three",
|
21 |
+
"four",
|
22 |
+
"five",
|
23 |
+
"six",
|
24 |
+
"seven",
|
25 |
+
"eight",
|
26 |
+
"nine"
|
27 |
+
]
|
28 |
+
|
29 |
+
def predict(image):
|
30 |
+
img = read_image(image).to(device)
|
31 |
+
img = rgb_to_grayscale(img) / 255
|
32 |
+
pred = model(img)[0]
|
33 |
+
prob = Softmax(dim=0)(pred).tolist()
|
34 |
+
return {l:p for l, p in zip(labels, prob)}
|
35 |
+
|
36 |
+
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=predict,
|
39 |
+
inputs=gr.Image(label="Upload", type="filepath", shape=(28, 28)),
|
40 |
+
outputs=gr.Label(num_top_classes=10),
|
41 |
+
)
|
42 |
iface.launch()
|
model.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
|
3 |
+
class NeuralNet(nn.Module):
|
4 |
+
def __init__(self):
|
5 |
+
super().__init__()
|
6 |
+
self.flatten = nn.Flatten()
|
7 |
+
self.layers = nn.Sequential(
|
8 |
+
nn.Linear(28*28, 512),
|
9 |
+
nn.ReLU(),
|
10 |
+
nn.Linear(512, 512),
|
11 |
+
nn.ReLU(),
|
12 |
+
nn.Linear(512, 10)
|
13 |
+
)
|
14 |
+
|
15 |
+
def forward(self, x):
|
16 |
+
x = self.flatten(x)
|
17 |
+
x = self.layers(x)
|
18 |
+
return x
|
models/model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f1d2226400fd63af4ccf83ed5c9f937cbe87ab5775bd42199f5270dc8da14acb
|
3 |
+
size 2680775
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
torch
|