CNN TF Keras
CNN TF Keras
August 1, 2021
1 Digit Recognition
1.1 Convolutional Neural Network implementation using TensorFlow - Keras
• Venkata A. Bharat Pothavajhala
[1]: '2.4.0'
1
[6]: from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten,␣
,→Dense
2
[7]: model = Sequential()
inputShape = (28,28,1)
# The input layer accepts an image and applies a convolution that uses 32 6x6␣
,→filters and a rectified linear unit activation function
model.add(Conv2D(32,␣
,→kernel_size=(6,6),strides=(1,1),activation='relu',input_shape = inputShape))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(32, kernel_size=(6,6),strides=(1,1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
# model.add(Conv2D(32, kernel_size=(3,3),strides=(1,1),activation='relu'))
# model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(10,activation='softmax'))
[8]: # With the layers defined, we can now compile the model for categorical␣
,→(multi-class) classification
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print(model.summary())
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 23, 23, 32) 1184
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 11, 11, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 6, 6, 32) 36896
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 3, 3, 32) 0
_________________________________________________________________
dropout (Dropout) (None, 3, 3, 32) 0
_________________________________________________________________
flatten (Flatten) (None, 288) 0
_________________________________________________________________
dense (Dense) (None, 10) 2890
=================================================================
Total params: 40,970
Trainable params: 40,970
Non-trainable params: 0
_________________________________________________________________
None
3
[9]: train_X.shape
train_X = train_X.reshape(-1,28,28,1)
train_X.shape
[11]: # Train the model over 5 epochs using 30-image batches and using the validation␣
,→holdout dataset for validation
num_epochs = 5
batch_size = 30
history = model.fit(train_X,y,batch_size=batch_size,epochs = num_epochs)
Epoch 1/5
2000/2000 [==============================] - 43s 21ms/step - loss: 1.7607 -
accuracy: 0.8177
Epoch 2/5
2000/2000 [==============================] - 43s 21ms/step - loss: 0.1073 -
accuracy: 0.9689
Epoch 3/5
2000/2000 [==============================] - 42s 21ms/step - loss: 0.0813 -
accuracy: 0.9756
Epoch 4/5
2000/2000 [==============================] - 43s 21ms/step - loss: 0.0771 -
accuracy: 0.9770
Epoch 5/5
2000/2000 [==============================] - 43s 21ms/step - loss: 0.0658 -
accuracy: 0.9813
[12]: model.save('nn_tfTrained.mdl')
4
files.download('/content/nn_tfTrained.mdl')
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
[ ]: test_X[1].shape[1]
[ ]: 28
[ ]: classnames = list(range(10))
classnames
[ ]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5
plt.axis('off')
plt.imshow(img)
[ ]: