-
Notifications
You must be signed in to change notification settings - Fork 0
/
04._mnistCNN.py
126 lines (112 loc) · 2.57 KB
/
04._mnistCNN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Convoluted neural network (CNN) implementation
on solving the MNIST dataset classification.
"""
# to supress tensorflow-gpu debug information
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Dropout
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.datasets import mnist
# load mnist dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# compute the number of labels
num_labels = len(np.unique(y_train))
# convert to one-hot vector
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# input image dimensions
image_size = x_train.shape[1]
# resize and normalize
x_train = np.reshape(x_train, [-1, image_size, image_size, 1])
x_test = np.reshape(x_test, [-1, image_size, image_size, 1])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
# network parameters
# image is processed as is (square grayscale)
input_shape = (image_size, image_size, 1)
batch_size = 128
kernel_size = 3
pool_size = 2
filters = 64
dropout = 0.2
# model is a stack of CNN-ReLU-MaxPooling
model = Sequential()
model.add(
Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu',
input_shape=input_shape
)
)
model.add(
MaxPooling2D(
pool_size
)
)
model.add(
Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu'
)
)
model.add(
MaxPooling2D(
pool_size
)
)
model.add(
Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu'
)
)
model.add(
Flatten()
)
# dropout added as regularizer
model.add(
Dropout(
dropout
)
)
# output layer is 10-dim one-hot vector
model.add(
Dense(
num_labels
)
)
model.add(
Activation(
'softmax'
)
)
# shows the model summary and save it
model.summary()
plot_model(model, to_file='cnn-mnist.png', show_shapes=True)
# loss function for one-hot vector
# use of adam optimizer
# accuracy is good metric for classification tasks
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# train the network
model.fit(x_train, y_train, epochs=10, batch_size=batch_size)
_, acc = model.evaluate(
x_test,
y_test,
batch_size=batch_size,
verbose=0
)
# shows the accuracy
print("\nTest accuracy: %.2f%%" % (100.0 * acc))