Update app.py
Browse files
app.py
CHANGED
@@ -1,172 +1,19 @@
|
|
1 |
-
## Importing The Dependencies
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import seaborn as sns
|
6 |
-
import cv2
|
7 |
-
import cv2_imshow
|
8 |
-
import PIL
|
9 |
-
import tensorflow as tf
|
10 |
-
tf.random.set_seed(3)
|
11 |
-
from tensorflow import keras
|
12 |
-
from keras.datasets import mnist
|
13 |
-
from sklearn.metrics import confusion_matrix
|
14 |
-
|
15 |
-
|
16 |
-
#Loading MINST Data from Keras.datasets
|
17 |
-
|
18 |
-
(x_train,y_train),(x_test,y_test) = mnist.load_data()
|
19 |
-
type(x_train)
|
20 |
-
# Shape of Numpy Arrays
|
21 |
-
|
22 |
-
print(x_train.shape,y_train.shape,x_test.shape,y_test.shape)
|
23 |
-
# Training Data = 60000
|
24 |
-
# Testing Data = 10000
|
25 |
-
# Image Diemention = 28x28
|
26 |
-
# Grayscale Image = 1 Channel
|
27 |
-
#Printing 10th images
|
28 |
-
|
29 |
-
print(x_train[10])
|
30 |
-
print(x_train[10].shape)
|
31 |
-
#Displaying The Imgae
|
32 |
-
plt.imshow(x_train[25])
|
33 |
-
|
34 |
-
#Displaying Labels
|
35 |
-
print(y_train[25])
|
36 |
-
|
37 |
-
## Image Labels
|
38 |
-
print(y_train.shape,y_test.shape)
|
39 |
-
#uinque Values in Y_train
|
40 |
-
print(np.unique(y_train))
|
41 |
-
|
42 |
-
#uinque Values in Y_test
|
43 |
-
print(np.unique(y_test))
|
44 |
-
|
45 |
-
# We can use these labels as such or we can also apply OneHOtencoding
|
46 |
-
|
47 |
-
# All the images have same diemention in this data set ,if not ,we have to resize all the images to a common dimention
|
48 |
-
#Scalling the values
|
49 |
-
|
50 |
-
x_train = x_train/255
|
51 |
-
x_test = x_test/255
|
52 |
-
#Printing 10th images
|
53 |
-
|
54 |
-
print(x_train[10])
|
55 |
-
# Building The Neural Network
|
56 |
-
# Setting up the layers of Neural Network
|
57 |
-
|
58 |
-
model = keras.Sequential([
|
59 |
-
keras.layers.Flatten(input_shape=(28,28)),
|
60 |
-
keras.layers.Dense(50,activation='relu'),
|
61 |
-
keras.layers.Dense(50,activation='relu'),
|
62 |
-
keras.layers.Dense(10,activation='sigmoid')
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
])
|
67 |
-
#Compiling the neural network
|
68 |
-
|
69 |
-
model.compile(optimizer='adam',loss = 'sparse_categorical_crossentropy',metrics=['accuracy'])
|
70 |
-
# Training the Neural Network
|
71 |
-
|
72 |
-
model.fit(x_train,y_train,epochs=10,)
|
73 |
-
|
74 |
-
# Training Data Acurracy is : 98.83%
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
# ***Accuracy on Test Data***
|
80 |
-
|
81 |
-
loss,accuracy = model.evaluate(x_test,y_test)
|
82 |
-
print(accuracy)
|
83 |
-
## **Test Data Acurracy is : 96.99%**
|
84 |
-
|
85 |
-
print(x_test.shape)
|
86 |
-
#First test point in x_test
|
87 |
-
|
88 |
-
plt.imshow(x_test[0])
|
89 |
-
plt.show()
|
90 |
-
print(y_test[0])
|
91 |
-
Y_pred = model.predict(x_test)
|
92 |
-
print(Y_pred.shape)
|
93 |
-
print(Y_pred[0])
|
94 |
-
|
95 |
-
# model.predict gives prediction of probability of each class for that data point
|
96 |
-
|
97 |
-
# Converting the prediction probability to class label
|
98 |
-
|
99 |
-
Label_for_first_image = np.argmax(Y_pred[0])
|
100 |
-
print(Label_for_first_image)
|
101 |
-
# Converting the prediction probability to class label for all test data
|
102 |
-
|
103 |
-
Y_pred_label = [np.argmax(i) for i in Y_pred]
|
104 |
-
print(Y_pred_label)
|
105 |
-
|
106 |
-
|
107 |
-
# y_test - is my true Labels
|
108 |
-
# Y_pred labels - my prdicted labels
|
109 |
-
|
110 |
-
## confusion Matrix
|
111 |
-
conf_max = confusion_matrix(y_test,Y_pred_label)
|
112 |
-
print(conf_max)
|
113 |
-
plt.figure(figsize=(15,7))
|
114 |
-
sns.heatmap(conf_max,annot=True,fmt='d',cmap='Blues')
|
115 |
-
|
116 |
-
|
117 |
-
## Building a Predictive System
|
118 |
-
input_image_path = '/content/download.png'
|
119 |
-
|
120 |
-
input_image = cv2.imread(input_image_path)
|
121 |
-
|
122 |
-
type(input_image)
|
123 |
-
print(input_image)
|
124 |
-
cv2_imshow(input_image)
|
125 |
-
input_image.shape
|
126 |
-
Grayscale = cv2.cvtColor(input_image,cv2.COLOR_RGB2GRAY)
|
127 |
-
Grayscale.shape
|
128 |
-
input_image_resize = cv2.resize(Grayscale,(28,28))
|
129 |
-
input_image_resize.shape
|
130 |
-
cv2_imshow(input_image_resize)
|
131 |
-
input_image_resize = input_image_resize/255
|
132 |
-
input_reshaped = np.reshape(input_image_resize,[1,28,28])
|
133 |
-
input_prediction = model.predict(input_reshaped)
|
134 |
-
print(input_prediction)
|
135 |
-
input_pred_label = np.argmax(input_prediction)
|
136 |
-
print(input_pred_label)
|
137 |
-
# Predictive System
|
138 |
-
input_image_path = input("Path of the image to be predicted :")
|
139 |
-
|
140 |
-
input_image = cv2.imread(input_image_path)
|
141 |
-
|
142 |
-
cv2_imshow(input_image)
|
143 |
-
|
144 |
-
Grayscale = cv2.cvtColor(input_image,cv2.COLOR_RGB2GRAY)
|
145 |
-
|
146 |
-
input_image_resize = cv2.resize(Grayscale,(28,28))
|
147 |
-
|
148 |
-
input_image_resize = input_image_resize/255
|
149 |
-
|
150 |
-
input_reshaped = np.reshape(input_image_resize,[1,28,28])
|
151 |
-
|
152 |
-
input_prediction = model.predict(input_reshaped)
|
153 |
-
|
154 |
-
input_pred_label = np.argmax(input_prediction)
|
155 |
-
|
156 |
-
print("the Handwritten digit recognized as : ",input_pred_label)
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
import gradio as gr
|
|
|
|
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from tensorflow import keras as k
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
loaded_CNN = k.models.load_model('Digit_classification_model.h5')
|
6 |
+
|
7 |
+
def predict(img):
|
8 |
+
img_array = np.array(img)
|
9 |
+
img_array = img_array.reshape(1, 28, 28)
|
10 |
+
img_array = img_array/255
|
11 |
+
pred = loaded_CNN.predict(img_array)
|
12 |
+
print(pred)
|
13 |
+
return np.argmax(pred)
|
14 |
+
|
15 |
+
iface = gr.Interface(predict, inputs = 'sketchpad',
|
16 |
+
outputs = 'text',
|
17 |
+
allow_flagging = 'never',
|
18 |
+
description = 'Draw a Digit Below... (Draw in the centre for best results)')
|
19 |
+
iface.launch(share = True, width = 500, height = 500)
|