-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handwritten-Image-Detection-With-Keras.py
556 lines (350 loc) · 14.2 KB
/
Handwritten-Image-Detection-With-Keras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/env python
# coding: utf-8
# ## Handwritten Image Detection with Keras
#
# In this project we will work with image data: specifically the famous MNIST and Fashion MNIST data sets. MNIST data set contains 70,000 images of handwritten digits in grayscale (0=black, 255 = white). Fashion MNIST data set contains 70,000 images of clothing in grayscale (0=black, 255 = white). All the images are 28 pixels by 28 pixels for a total of 784 pixels. This is quite small by image standards. Also, the images are well centered and isolated. This makes this problem solvable with standard fully connected neural nets without too much pre-work. <br><br>
# We will use a Convolutional Neural Network and compare it with a linear neural network.
# In the first part of this notebook, we will walk you through loading in the data, building a network, and training it. Then it will be your turn to try different models and see if you can improve performance
# In[1]:
# Preliminaries
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.optimizers import RMSprop
from keras.datasets import fashion_mnist
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# Let's explore the dataset a little bit
# In[2]:
# Load the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# In[3]:
x_train.shape
# In[4]:
#Let's just look at a particular example to see what is inside
x_train[333] ## Just a 28 x 28 numpy array of ints from 0 to 255
# In[5]:
# What is the corresponding label in the training set?
y_train[333]
# In[6]:
# Let's see what this image actually looks like
plt.imshow(x_train[333], cmap='Greys_r')
# In[7]:
# this is the shape of the np.array x_train
# it is 3 dimensional.
print(x_train.shape, 'train samples')
print(x_test.shape, 'test samples')
# In[8]:
## For our purposes, these images are just a vector of 784 inputs, so let's convert
x_train = x_train.reshape(len(x_train), 28*28)
x_test = x_test.reshape(len(x_test), 28*28)
## Keras works with floats, so we must cast the numbers to floats
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
## Normalize the inputs so they are between 0 and 1
x_train /= 255
x_test /= 255
# In[9]:
# convert class vectors to binary class matrices
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_train[333] # now the digit k is represented by a 1 in the kth entry (0-indexed) of the length 10 vector
# In[10]:
# We will build a model with two hidden layers of size 512
# Fully connected inputs at each layer
# We will use dropout of .2 to help regularize
model_1 = Sequential()
model_1.add(Dense(64, activation='relu', input_shape=(784,)))
model_1.add(Dropout(0.2))
model_1.add(Dense(64, activation='relu'))
model_1.add(Dropout(0.2))
model_1.add(Dense(10, activation='softmax'))
# In[11]:
## Note that this model has a LOT of parameters
model_1.summary()
# In[12]:
# Let's compile the model
learning_rate = .001
model_1.compile(loss='categorical_crossentropy',
optimizer=RMSprop(lr=learning_rate),
metrics=['accuracy'])
# note that `categorical cross entropy` is the natural generalization
# of the loss function we had in binary classification case, to multi class case
# In[13]:
# And now let's fit.
batch_size = 128 # mini-batch with 128 examples
epochs = 30
history = model_1.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
# In[14]:
## We will use Keras evaluate function to evaluate performance on the test set
score = model_1.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# In[15]:
def plot_loss_accuracy(history):
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 2, 1)
ax.plot(history.history["loss"],'r-x', label="Train Loss")
ax.plot(history.history["val_loss"],'b-x', label="Validation Loss")
ax.legend()
ax.set_title('cross_entropy loss')
ax.grid(True)
ax = fig.add_subplot(1, 2, 2)
ax.plot(history.history["accuracy"],'r-x', label="Train Accuracy")
ax.plot(history.history["val_accuracy"],'b-x', label="Validation Accuracy")
ax.legend()
ax.set_title('accuracy')
ax.grid(True)
plot_loss_accuracy(history)
# This is reasonably good performance, but we can do even better! Next you will build an even bigger network and compare the performance.
# ### -----------------------------------------------------------------------------------------------------------------------------
# ### Keras Layers for CNNs
# - Previously we built Neural Networks using primarily the Dense, Activation and Dropout Layers.
#
# - Here we will describe how to use some of the CNN-specific layers provided by Keras
#
# ### (!!!) NOTE THAT WE ARE USING A DIFFERENT MODEL
#
# #### Conv2D
#
# ```python
# keras.layers.convolutional.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)
# ```
#
# A few parameters explained:
# - `filters`: the number of filter used per location. In other words, the depth of the output.
# - `kernel_size`: an (x,y) tuple giving the height and width of the kernel to be used
# - `strides`: and (x,y) tuple giving the stride in each dimension. Default is `(1,1)`
# - `input_shape`: required only for the first layer
#
# Note, the size of the output will be determined by the kernel_size, strides
#
# #### MaxPooling2D
# `keras.layers.pooling.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)`
#
# - `pool_size`: the (x,y) size of the grid to be pooled.
# - `strides`: Assumed to be the `pool_size` unless otherwise specified
#
# #### Flatten
# Turns its input into a one-dimensional vector (per instance). Usually used when transitioning between convolutional layers and fully connected layers.
#
# # Exercise
# ### Build your own CNN model
# Use the Keras "Sequential" functionality to build a convolutional neural network `model_2` with the following specifications:
# <br>
# <br>
# Model Architecture:<br>
# We will build the famous LeNet-5 architecutre and measure its performance.
# <br>
# Convolution -> Relu -> Max pooling -> Convolution -> Relu -> Max pooling -> FC1 -> Relu -> FC2 -> Output(SoftMax)
# <br>
#
# 1. Convolution1 kernel size: 5(H) x 5(W) x 6(filters), stride = 1, no padding
# 2. Max pooling1 kernel size: 2(H) x 2(W), stride = 2
# 3. Convolution2 kernel size: 5(H) x 5(W) x 16(filters), stride = 1, no padding
# 4. Max pooling2 kernel size: 2(H) x 2(W), stride = 2
# 5. Fully Connected1 size: 120
# 6. Fully Connected1 size: 84
# 7. How many parameters does your model have? How does it compare with the previous model?
# 8. Train this model for 20 epochs with RMSProp at a learning rate of .001 and a batch size of 128
# 9. Plot the loss and accuracy graph for training the new model
# 10. Evaluate the model on test data
# To use the LeNet model, we need to do some preprocessing of the data first.
# In[16]:
# Data is currently flattened i.e. m X 784, we need to reshape it back to 28 * 28. To do that we reshape the data.
x_train = np.reshape(x_train, [-1, 28, 28])
x_test = np.reshape(x_test, [-1, 28, 28])
x_train.shape, x_test.shape
# In[17]:
# LeNet requires input of 32 X 32. So, we will pad the train and test images with zeros to increase the size to 32 X 32.
x_train=np.pad(x_train, ((0,0), (2,2), (2, 2)), 'constant')
x_test=np.pad(x_test, ((0,0), (2,2), (2, 2)), 'constant')
x_train.shape, x_test.shape
# In[18]:
# Convolutional model requires input to be of 3 dimensions. We will add a channel dimension to it.
x_train = np.reshape(x_train, [-1, 32, 32, 1])
x_test = np.reshape(x_test, [-1, 32, 32, 1])
x_train.shape, x_test.shape
# Write your model below
# In[19]:
#(!!!) UNTESTED MODEL
#is padding invalid by default?
model2 = Sequential()
#CNN LAYER 1
#6 filters is the STARTING number
#'valid' padding is no padding
#Tuple for strides is the same number all throughout
#Strides have same dimensions as kernel
model2.add(Conv2D(6, kernel_size=(5,5), strides=(1,1),activation='relu', padding='valid'))
model2.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
#CNN LAYER 2
#16 filters is the STARTING number
#'valid' padding is no padding
model2.add(Conv2D(16, kernel_size=(5,5), strides=(1,1),activation='relu', padding='valid'))
model2.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
#Fully Connected
#NEED activation function at every layer
#Here, we will always use relu
model2.add(Flatten())
#Just replace Flatten with Dropout for bonus project
model2.add(Dense(120, activation='relu'))
model2.add(Dense(84, activation='relu'))
model2.add(Dense(10, activation='softmax'))
#Because 10 numbers
# In[20]:
#(!!!) UNTESTED COMPILATION
#20 epochs
#With RMSProp
#Learning rate of .001
#Batch size of 128
model2.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop(lr=0.001),
metrics=['accuracy'])
"""model2.fit(x_train, y_train,
batch_size=128,
epochs=20,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[history])"""
#-----------------------------------------------
#(!!!) WILL CHANGE BATCH SIZE AND EPOCH NUMBER
batch_size = 128 # mini-batch with 128 examples
epochs = 20
history2 = model2.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
#-----------------------------------------------
score = model2.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# In[21]:
## Note that this model has a LOT of parameters
#Numbers verified to be correct
model2.summary()
# In[22]:
plot_loss_accuracy(history2)
# ### -----------------------------------------------------------------------------------------------------------------------------
# # Fashion MNIST
# We will do the similar things for Fashion MNIST dataset. Fashion MNIST has 10 categories of clothing items:<br>
#
# | Label | Description |
# | --- | --- |
# | 0 | T-shirt/top |
# | 1 | Trouser |
# | 2 | Pullover |
# | 3 | Dress |
# | 4 | Coat |
# | 5 | Sandal |
# | 6 | Shirt |
# | 7 | Sneaker |
# | 8 | Bag |
# | 9 | Ankle boot |
# In[25]:
# Load the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
# In[26]:
x_train[0].shape
# In[27]:
#Let's just look at a particular example to see what is inside
x_train[333] ## Just a 28 x 28 numpy array of ints from 0 to 255
# In[28]:
# What is the corresponding label in the training set?
y_train[333]
# In[29]:
# Let's see what this image actually looks like
plt.imshow(x_train[333], cmap='Greys_r')
# In[30]:
# this is the shape of the np.array x_train
# it is 3 dimensional.
print(x_train.shape, 'train samples')
print(x_test.shape, 'test samples')
# In[31]:
## For our purposes, these images are just a vector of 784 inputs, so let's convert
x_train = x_train.reshape(len(x_train), 28*28)
x_test = x_test.reshape(len(x_test), 28*28)
## Keras works with floats, so we must cast the numbers to floats
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
## Normalize the inputs so they are between 0 and 1
x_train /= 255
x_test /= 255
# In[32]:
# convert class vectors to binary class matrices
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_train[333]
# ## Reperforming the earlier preprocessing methods
# In[33]:
# Data is currently flattened i.e. m X 784, we need to reshape it back to 28 * 28. To do that we reshape the data.
x_train = np.reshape(x_train, [-1, 28, 28])
x_test = np.reshape(x_test, [-1, 28, 28])
x_train.shape, x_test.shape
# In[34]:
# LeNet requires input of 32 X 32. So, we will pad the train and test images with zeros to increase the size to 32 X 32.
x_train=np.pad(x_train, ((0,0), (2,2), (2, 2)), 'constant')
x_test=np.pad(x_test, ((0,0), (2,2), (2, 2)), 'constant')
x_train.shape, x_test.shape
# In[35]:
# Convolutional model requires input to be of 3 dimensions. We will add a channel dimension to it.
x_train = np.reshape(x_train, [-1, 32, 32, 1])
x_test = np.reshape(x_test, [-1, 32, 32, 1])
x_train.shape, x_test.shape
# I then built a similar convolutional model with a differnet structure, learning rate or number of epochs, etc. that would result in a good model for the dataset
# In[37]:
# write your model here.
"""
--WHAT FACTORS I CAN CHANGE FOR A GOOD MODEL--
# of layers
# of filters (starting number)
#filter size (dimensions)
#size
70% would be GOOD"""
# In[38]:
model3 = Sequential()
model3.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
model3.add(MaxPooling2D(pool_size=(2, 2)))
model3.add(Dropout(0.2))
model3.add(Flatten())
model3.add(Dense(128, activation='relu'))
model3.add(Dense(10, activation='softmax'))
# In[39]:
model3.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'])
#-----------------------------------------------
#(!!!) DEBUGGING PURPOSES
#(!!!) CHANGED BATCH SIZE TO 512
#(!!!) CHANGED EPOCHS TO 2
#(!!!) WILL CHANGE BATCH SIZE AND EPOCH NUMBER
batch_size = 128 # mini-batch with 128 examples
epochs = 20
history3 = model3.fit(
x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
#-----------------------------------------------
score = model3.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# In[40]:
## Note that this model has a LOT of parameters
#Numbers verified to be correct
model3.summary()
# In[41]:
plot_loss_accuracy(history3)
# In[ ]: