ffcm commited on
Commit
35cfa5c
1 Parent(s): 1bae304

Initial commit

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+
2
+ .venv/
3
+ __pycache__/
.vscode/settings.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "[python]": {
3
+ "editor.defaultFormatter": "ms-python.autopep8"
4
+ },
5
+ "python.formatting.provider": "none"
6
+ }
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: Cnn Pytorch Mnist
3
- emoji: 🏢
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
@@ -10,4 +10,4 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: CNN MNIST Classifier
3
+ emoji: 🔢
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
 
10
  license: mit
11
  ---
12
 
13
+ The model used is a Convolutional Neural Network made with PyTorch.
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import torch
4
+ import numpy as np
5
+
6
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
7
+
8
+ with open('cnn_model_9961_sm.bin', 'rb') as f:
9
+ nn = pickle.load(f)
10
+
11
+ nn.to(device)
12
+
13
+
14
+ def predict(input):
15
+ if input is None:
16
+ return 'None'
17
+
18
+ x = np.array([[input]])
19
+ x = torch.tensor(x).to(device)
20
+ p = nn(x)
21
+ p = p[0].cpu().detach().numpy()
22
+
23
+ return dict(enumerate(p.tolist()))
24
+
25
+
26
+ demo = gr.Interface(
27
+ fn=predict,
28
+ inputs=[
29
+ gr.Sketchpad(
30
+ shape=(28, 28),
31
+ brush_radius=1.2,
32
+ )
33
+ ],
34
+ outputs=[
35
+ gr.Label(
36
+ num_top_classes=3,
37
+ scale=2,
38
+ )
39
+ ],
40
+ live=True,
41
+ allow_flagging='never',
42
+ ).launch()
cnn_model_9907.bin ADDED
Binary file (140 kB). View file
 
cnn_model_9961_sm.bin ADDED
Binary file (140 kB). View file
 
cnn_model_9973.bin ADDED
Binary file (140 kB). View file
 
networktorch.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from torch import nn
2
+
3
+
4
+ class NeuralNetworkTorch(nn.Module):
5
+ def __init__(self):
6
+ super().__init__()
7
+
8
+ self.stack = nn.Sequential(
9
+ nn.Linear(784, 64),
10
+ nn.Sigmoid(),
11
+
12
+ nn.Linear(64, 10),
13
+ nn.Sigmoid()
14
+ )
15
+
16
+ def forward(self, x):
17
+ return self.stack(x)
18
+
19
+
20
+ class ConvNeuralNetworkTorch(nn.Module):
21
+ def __init__(self):
22
+ super().__init__()
23
+
24
+ self.conv = nn.Sequential(
25
+ nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1),
26
+ nn.ReLU(),
27
+
28
+ nn.MaxPool2d(kernel_size=2, stride=2),
29
+
30
+ nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1),
31
+ nn.ReLU(),
32
+
33
+ # nn.MaxPool2d(kernel_size=2, stride=2),
34
+ )
35
+
36
+ self.fc = nn.Sequential(
37
+ nn.Linear(16 * 14 * 14, 10),
38
+ nn.Sigmoid(),
39
+ )
40
+
41
+ def forward(self, x):
42
+ # we do some reshaping here simply to avoid making changes to the caller
43
+ # so it continues to work with the fully conected network above
44
+ x = x.reshape(-1, 1, 28, 28) / 255
45
+
46
+ conv_output = self.conv(x)
47
+ flat = conv_output.reshape(len(x), -1)
48
+ final_output = self.fc(flat)
49
+
50
+ return final_output
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy==1.25.2
2
+ torch==2.0.1
3
+ torchvision==0.15.2
4
+ gradio==3.47.1