Gosula commited on
Commit
4825f44
·
1 Parent(s): 40a522a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -67
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from sklearn.datasets import fetch_openml
2
  from sklearn.model_selection import train_test_split
3
  import numpy as np
@@ -11,59 +12,34 @@ from PIL import Image
11
  import streamlit as st
12
  from streamlit_drawable_canvas import st_canvas
13
  import cv2
 
 
14
  import torch
15
  from skorch import NeuralNetClassifier
16
  from torch import nn
17
  import torch.nn.functional as F
18
  import matplotlib.pyplot as plt
 
 
 
 
 
 
 
19
  mnist = fetch_openml('mnist_784', as_frame=False, cache=False)
20
  X = mnist.data.astype('float32')
21
  y = mnist.target.astype('int64')
22
  X /= 255.0
23
 
24
-
25
  #device = 'cuda' if torch.cuda.is_available() else 'cpu'
26
- XCnn = X.reshape(-1, 1, 28, 28)
27
- XCnn_train, XCnn_test, y_train, y_test = train_test_split(XCnn, y, test_size=0.25, random_state=42)
28
 
29
- from PIL import Image
30
- import torchvision.transforms as transforms
31
- class Cnn(nn.Module):
32
- def __init__(self, dropout=0.5):
33
- super(Cnn, self).__init__()
34
- self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
35
- self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
36
- self.conv2_drop = nn.Dropout2d(p=dropout)
37
- self.fc1 = nn.Linear(1600, 100) # 1600 = number channels * width * height
38
- self.fc2 = nn.Linear(100, 10)
39
- self.fc1_drop = nn.Dropout(p=dropout)
40
-
41
- def forward(self, x):
42
- x = torch.relu(F.max_pool2d(self.conv1(x), 2))
43
- x = torch.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
44
-
45
- # flatten over channel, height and width = 1600
46
- x = x.view(-1, x.size(1) * x.size(2) * x.size(3))
47
-
48
- x = torch.relu(self.fc1_drop(self.fc1(x)))
49
- x = torch.softmax(self.fc2(x), dim=-1)
50
- return x
51
  torch.manual_seed(0)
52
 
53
 
54
- # # Create an instance of your model
55
- # model = NeuralNetClassifier(
56
- # Cnn,
57
- # max_epochs=10,
58
- # lr=0.002,
59
- # optimizer=torch.optim.Adam,
60
- # device=device,
61
- import streamlit as st
62
- import torch
63
- from PIL import Image
64
- import cv2
65
- import numpy as np
66
 
 
67
 
68
  #device = 'cuda' if torch.cuda.is_available() else 'cpu'
69
  XCnn = X.reshape(-1, 1, 28, 28)
@@ -71,37 +47,10 @@ XCnn_train, XCnn_test, y_train, y_test = train_test_split(XCnn, y, test_size=0.2
71
 
72
  from PIL import Image
73
  import torchvision.transforms as transforms
74
- class Cnn(nn.Module):
75
- def __init__(self, dropout=0.5):
76
- super(Cnn, self).__init__()
77
- self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
78
- self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
79
- self.conv2_drop = nn.Dropout2d(p=dropout)
80
- self.fc1 = nn.Linear(1600, 100) # 1600 = number channels * width * height
81
- self.fc2 = nn.Linear(100, 10)
82
- self.fc1_drop = nn.Dropout(p=dropout)
83
-
84
- def forward(self, x):
85
- x = torch.relu(F.max_pool2d(self.conv1(x), 2))
86
- x = torch.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
87
-
88
- # flatten over channel, height and width = 1600
89
- x = x.view(-1, x.size(1) * x.size(2) * x.size(3))
90
-
91
- x = torch.relu(self.fc1_drop(self.fc1(x)))
92
- x = torch.softmax(self.fc2(x), dim=-1)
93
- return x
94
  torch.manual_seed(0)
95
 
96
 
97
- # # Create an instance of your model
98
- # model = NeuralNetClassifier(
99
- # Cnn,
100
- # max_epochs=10,
101
- # lr=0.002,
102
- # optimizer=torch.optim.Adam,
103
- # device=device,
104
- # )
105
  model=Cnn()
106
 
107
  # Specify the path to the saved model weights
@@ -112,6 +61,7 @@ model.load_state_dict(torch.load(model_weights_path,map_location=torch.device('c
112
 
113
  # Set the model to evaluation mode for inference
114
  model.eval()
 
115
  # Create a NeuralNetClassifier using the loaded model
116
  cnn = NeuralNetClassifier(
117
  module=model,
@@ -120,7 +70,9 @@ cnn = NeuralNetClassifier(
120
  optimizer=torch.optim.Adam, # You can set the optimizer used during training
121
  device='cpu' # You can specify the device ('cpu' for CPU, 'cuda' for GPU, etc.)
122
  )
 
123
  cnn.fit(XCnn_train, y_train)
 
124
  stroke_width = st.sidebar.slider("Stroke width: ", 1, 35, 32)
125
  stroke_color = st.sidebar.color_picker("Stroke color hex: ")
126
  bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
@@ -154,9 +106,9 @@ if canvas_result.image_data is not None:
154
  image1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
155
  image1 = cv2.resize(image1,(28,28))
156
  st.image(image1)
157
-
158
-
159
  image1.resize(1,1,28,28)
160
  st.title(np.argmax(cnn.predict(image1)))
 
 
161
  if canvas_result.json_data is not None:
162
  st.dataframe(pd.json_normalize(canvas_result.json_data["objects"]))
 
1
+ # importing all the packages
2
  from sklearn.datasets import fetch_openml
3
  from sklearn.model_selection import train_test_split
4
  import numpy as np
 
12
  import streamlit as st
13
  from streamlit_drawable_canvas import st_canvas
14
  import cv2
15
+ from PIL import Image
16
+ import torchvision.transforms as transforms
17
  import torch
18
  from skorch import NeuralNetClassifier
19
  from torch import nn
20
  import torch.nn.functional as F
21
  import matplotlib.pyplot as plt
22
+ from model import Cnn
23
+
24
+
25
+
26
+
27
+
28
+ # Reading the data
29
  mnist = fetch_openml('mnist_784', as_frame=False, cache=False)
30
  X = mnist.data.astype('float32')
31
  y = mnist.target.astype('int64')
32
  X /= 255.0
33
 
 
34
  #device = 'cuda' if torch.cuda.is_available() else 'cpu'
35
+ XCnn = X.reshape(-1, 1, 28, 28) #reshape input
36
+ XCnn_train, XCnn_test, y_train, y_test = train_test_split(XCnn, y, test_size=0.25, random_state=42) #train test split
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  torch.manual_seed(0)
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ # reshape and train test split
43
 
44
  #device = 'cuda' if torch.cuda.is_available() else 'cpu'
45
  XCnn = X.reshape(-1, 1, 28, 28)
 
47
 
48
  from PIL import Image
49
  import torchvision.transforms as transforms
50
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  torch.manual_seed(0)
52
 
53
 
 
 
 
 
 
 
 
 
54
  model=Cnn()
55
 
56
  # Specify the path to the saved model weights
 
61
 
62
  # Set the model to evaluation mode for inference
63
  model.eval()
64
+
65
  # Create a NeuralNetClassifier using the loaded model
66
  cnn = NeuralNetClassifier(
67
  module=model,
 
70
  optimizer=torch.optim.Adam, # You can set the optimizer used during training
71
  device='cpu' # You can specify the device ('cpu' for CPU, 'cuda' for GPU, etc.)
72
  )
73
+
74
  cnn.fit(XCnn_train, y_train)
75
+
76
  stroke_width = st.sidebar.slider("Stroke width: ", 1, 35, 32)
77
  stroke_color = st.sidebar.color_picker("Stroke color hex: ")
78
  bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
 
106
  image1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
107
  image1 = cv2.resize(image1,(28,28))
108
  st.image(image1)
 
 
109
  image1.resize(1,1,28,28)
110
  st.title(np.argmax(cnn.predict(image1)))
111
+
112
+
113
  if canvas_result.json_data is not None:
114
  st.dataframe(pd.json_normalize(canvas_result.json_data["objects"]))