Ammar971 commited on
Commit
37ea13a
1 Parent(s): 9aa7565

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -32,3 +32,81 @@ print('X_train shape', X_train.shape)
32
  print('y_train shape', y_train.shape)
33
  print('X_test shape', X_test.shape)
34
  print('y_test shape', y_test.shape)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  print('y_train shape', y_train.shape)
33
  print('X_test shape', X_test.shape)
34
  print('y_test shape', y_test.shape)
35
+
36
+ X_train /= 255
37
+ X_test /= 255
38
+
39
+ X_train = X_train.values.reshape(-1,28,28,1)
40
+ X_test = X_test.values.reshape(-1,28,28,1)
41
+
42
+ X_train = X_train.astype('float32')
43
+ X_test = X_test.astype('float32')
44
+
45
+
46
+ print("Training matrix shape", X_train.shape)
47
+ print("Testing matrix shape", X_test.shape)
48
+
49
+ nb_classes = 10
50
+
51
+ Y_train = to_categorical(y_train, nb_classes)
52
+ Y_test = to_categorical(y_test, nb_classes)
53
+
54
+ print(nb_classes)
55
+ print(Y_train.shape)
56
+ print(Y_test.shape)
57
+
58
+ import sklearn
59
+ from sklearn.model_selection import train_test_split
60
+
61
+ X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=4)
62
+
63
+ print(X_train.shape)
64
+ print(Y_train.shape)
65
+ print(X_val.shape)
66
+ print(Y_val.shape)
67
+
68
+ #Creating CNN model
69
+
70
+ model = Sequential()
71
+
72
+ model.add(layers.Conv2D(filters = 80, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28,28,1)))
73
+
74
+ model.add(layers.MaxPool2D(pool_size=(2,2)))
75
+
76
+ model.add(layers.Dropout(0.25))
77
+
78
+ model.add(layers.Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', activation ='relu'))
79
+
80
+ model.add(layers.MaxPool2D(pool_size=(2,2)))
81
+ model.add(layers.Dropout(0.25))
82
+
83
+
84
+ model.add(layers.Flatten())
85
+
86
+ model.add(layers.Dense(128, activation = "relu"))
87
+
88
+ model.add(layers.Dropout(0.25))
89
+
90
+ model.add(layers.Dense(10, activation = "softmax"))
91
+
92
+ model.summary()
93
+
94
+ from tensorflow.keras.optimizers import SGD
95
+ optimizer = SGD(learning_rate=0.001, momentum=0.30)
96
+
97
+ model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
98
+
99
+ history = model.fit( X_train,Y_train, batch_size=64, epochs = 30, validation_data = (X_val, Y_val), verbose = 1)
100
+
101
+ def predict_image(img):
102
+ img_3d = img.reshape(-1, 28,28)
103
+ img_scaled = img_3d/255
104
+ prediction = model.predict(img_scaled)
105
+ pred = np.argmax(prediction)
106
+
107
+ return pred.item()
108
+
109
+
110
+ iface = gr.Interface(predict_image, inputs='sketchpad', outputs='label', title='Digit Recognition Model By Debamrita Paul', description='Draw a single digit(0 to 9)')
111
+
112
+ iface.launch(share=True)