Mardion
Mardion
Mardion
July 7, 2024
[3]: # The MNIST data is split between 60,000 28 x 28 pixel training images and␣
↪10,000 28 x 28 pixel images
for i in range(9):
plt.subplot(3,3,i+1)
num = random.randint(0, len(X_train))
plt.imshow(X_train[num], cmap='gray', interpolation='none')
plt.title("Class {}".format(y_train[num]))
plt.tight_layout()
1
[5]: # just a little function for pretty printing a matrix
def matprint(mat, fmt="g"):
col_maxes = [max([len(("{:"+fmt+"}").format(x)) for x in col]) for col in␣
↪mat.T]
for x in mat:
for i, y in enumerate(x):
print(("{:"+str(col_maxes[i])+fmt+"}").format(y), end=" ")
print("")
# now print!
matprint(X_train[num])
2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 114 12 89 207 253
176 253 253 84 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 29 85 101 225 253 152 197 252 252
253 252 252 84 0 0 0 0 0 0
0 0 0 0 19 166 197 197 198 215 252 252 252 196 161 43 149 195
253 252 252 84 0 0 0 0 0 0
0 0 0 0 178 252 252 252 253 252 252 138 84 0 47 0 0 0
253 252 252 84 0 0 0 0 0 0
0 0 0 0 146 252 252 252 253 252 141 37 0 0 0 0 0 0
253 252 220 37 0 0 0 0 0 0
0 0 0 0 0 57 240 140 141 94 0 0 0 0 0 0 13 191
255 253 196 0 0 0 0 0 0 0
0 0 0 0 0 6 24 0 0 0 0 0 0 0 0 0 154 252
253 252 148 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 187 252
253 186 12 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 79 252 252
253 151 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 85 252 252 252
112 12 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 147 253 253 253
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 225 252 242 89
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 57 231 252 192 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 16 253 252 214 28 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 140 253 252 118 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 92 253 255 215 31 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 19 196 252 253 167 0 0 0
0 0 0 0 0 0 0 0 0 0
3
0 0 0 0 0 0 0 0 0 0 57 252 252 253 167 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 57 252 252 253 74 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 25 205 252 190 12 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
X_test = X_test.astype('float32')
X_test /= 255
[8]: # The Sequential model is a linear stack of layers and is very common.
model = Sequential()
[9]: # The first hidden layer is a set of 512 nodes (artificial neurons).
# Each node will receive an element from each input vector and apply some␣
↪weight and bias to it.
4
# It checks the new value of the node, and decides whether that artifical␣
↪neuron has fired.
# The Rectified Linear Unit (ReLU) converts all negative inputs to nodes in the␣
↪next layer to be zero.
model.add(Activation('relu'))
[11]: # Dropout zeroes a selection of random outputs (i.e., disables their activation)
# Dropout helps protect the model from memorizing or "overfitting" the training␣
↪data.
model.add(Dropout(0.2))
[12]: # The second hidden layer appears identical to our first layer.
# However, instead of each of the 512-node receiving 784-inputs from the input␣
↪image data,
# they receive 512 inputs from the output of the first 512-node layer.
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
# The final layer of a FCN should be equal to the number of desired classes (10␣
↪in this case).
model.add(Dense(10))
model.add(Activation('softmax'))
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 512) 401920
5
dropout (Dropout) (None, 512) 0
=================================================================
Total params: 669706 (2.55 MB)
Trainable params: 669706 (2.55 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/5
469/469 [==============================] - 12s 23ms/step - loss: 0.2507 -
accuracy: 0.9239
Epoch 2/5
469/469 [==============================] - 14s 31ms/step - loss: 0.1006 -
accuracy: 0.9692
Epoch 3/5
469/469 [==============================] - 12s 26ms/step - loss: 0.0721 -
accuracy: 0.9776
Epoch 4/5
469/469 [==============================] - 11s 24ms/step - loss: 0.0566 -
accuracy: 0.9818
Epoch 5/5
469/469 [==============================] - 9s 20ms/step - loss: 0.0466 -
accuracy: 0.9843
6
313/313 [==============================] - 1s 4ms/step - loss: 0.0714 -
accuracy: 0.9790
Test score: 0.07143609970808029
Test accuracy: 0.9789999723434448
[20]: plt.figure()
for i, correct in enumerate(correct_indices[:9]):
plt.subplot(3,3,i+1)
plt.imshow(X_test[correct].reshape(28,28), cmap='gray',␣
↪interpolation='none')
plt.tight_layout()
plt.figure()
for i, incorrect in enumerate(incorrect_indices[:9]):
plt.subplot(3,3,i+1)
plt.imshow(X_test[incorrect].reshape(28,28), cmap='gray',␣
↪interpolation='none')
plt.tight_layout()
7
8
[41]: # import some additional tools
9
# Except we do not flatten each image into a 784-length vector because we want␣
↪to perform convolutions first
X_test = X_test.astype('float32')
X_test /= 255
# Convolution Layer 1
model.add(Conv2D(32, (3, 3), input_shape=(28,28,1))) # 32 different 3x3 kernels␣
↪-- so 32 feature maps
# Convolution Layer 2
model.add(Conv2D(32, (3, 3))) # 32 different 3x3 kernels␣
↪-- so 32 feature maps
model.add(Activation('relu')) # activation
convLayer02 = MaxPooling2D(pool_size=(2,2)) # Pool the max values over␣
↪a 2x2 kernel
10
model.add(convLayer02)
# Convolution Layer 3
model.add(Conv2D(64,(3, 3))) # 64 different 3x3 kernels␣
↪-- so 64 feature maps
# Convolution Layer 4
model.add(Conv2D(64, (3, 3))) # 64 different 3x3 kernels␣
↪-- so 64 feature maps
model.add(Activation('relu')) # activation
convLayer04 = MaxPooling2D(pool_size=(2,2)) # Pool the max values over␣
↪a 2x2 kernel
model.add(convLayer04)
model.add(Flatten()) # Flatten final 4x4x64␣
↪output matrix into a 1024-length vector
[26]: model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320
11
batch_normalization_1 (Bat (None, 24, 24, 32) 128
chNormalization)
=================================================================
Total params: 597738 (2.28 MB)
Trainable params: 596330 (2.27 MB)
Non-trainable params: 1408 (5.50 KB)
_________________________________________________________________
12
[27]: # we'll use the same optimizer
model.compile(loss='categorical_crossentropy', optimizer='adam',␣
↪metrics=['accuracy'])
[28]: # data augmentation prevents overfitting by slightly changing the data randomly
# Keras has a great built-in feature to do automatic augmentation
height_shift_range=0.08, zoom_range=0.08)
test_gen = ImageDataGenerator()
# because we are actually LOADING the data into the network in batches before␣
↪processing each batch
# Before the data was all loaded into memory, but then processed in batches.
[30]: # We can now train our model which is fed data by our batch loader
# Steps per epoch should always be total size of the set divided by the batch␣
↪size
validation_data=test_generator, validation_steps=10000//128)
Epoch 1/5
<ipython-input-30-758777735d04>:6: UserWarning: `Model.fit_generator` is
deprecated and will be removed in a future version. Please use `Model.fit`,
which supports generators.
model.fit_generator(train_generator, steps_per_epoch=60000//128, epochs=5,
verbose=1,
468/468 [==============================] - 201s 425ms/step - loss: 0.1306 -
accuracy: 0.9590 - val_loss: 0.2779 - val_accuracy: 0.9135
Epoch 2/5
468/468 [==============================] - 211s 450ms/step - loss: 0.0506 -
accuracy: 0.9847 - val_loss: 0.0316 - val_accuracy: 0.9895
13
Epoch 3/5
468/468 [==============================] - 206s 441ms/step - loss: 0.0393 -
accuracy: 0.9877 - val_loss: 0.0306 - val_accuracy: 0.9904
Epoch 4/5
468/468 [==============================] - 207s 442ms/step - loss: 0.0336 -
accuracy: 0.9897 - val_loss: 0.0228 - val_accuracy: 0.9923
Epoch 5/5
468/468 [==============================] - 195s 417ms/step - loss: 0.0293 -
accuracy: 0.9906 - val_loss: 0.0364 - val_accuracy: 0.9889
def visualize(layer):
inputs = [K.learning_phase()] + model.inputs
def convout1_f(X):
# The [0] is to disable the training phase flag
return _convout1_f([0] + [X])
convolutions = convout1_f(img)
convolutions = np.squeeze(convolutions)
m = convolutions.shape[2]
n = int(np.ceil(np.sqrt(m)))
14
# Visualization of each filter of the layer
fig = plt.figure(figsize=(15,12))
for i in range(m):
ax = fig.add_subplot(n,n,i+1)
ax.imshow(convolutions[:,:,i], cmap='gray')
[47]: plt.figure()
plt.imshow(X_test[3].reshape(28,28), cmap='gray', interpolation='none')
15
1/1 [==============================] - 0s 41ms/step
[ ]:
16