/
A0_MNIST_CNN_Keras_TypeA.py
151 lines (118 loc) · 3.7 KB
/
A0_MNIST_CNN_Keras_TypeA.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
'''
Data Engineering
'''
'''
D1. Import Libraries for Data Engineering
'''
import tensorflow as tf
import matplotlib.pyplot as plt
'''
D2. Load MNIST data / Only for Toy Project
'''
# print(tf.__version__)
## MNIST Dataset #########################################################
mnist = tf.keras.datasets.mnist
# class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
##########################################################################
## Fashion MNIST Dataset #################################################
# mnist = tf.keras.datasets.fashion_mnist
# class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
##########################################################################
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
# Change data type as float. If it is int type, it might cause error
'''
D3. Data Preprocessing
'''
# Normalizing
X_train, X_test = X_train / 255.0, X_test / 255.0
print(Y_train[0:10])
print(X_train.shape)
'''
D4. EDA(? / Exploratory data analysis)
'''
# plot first few images
for i in range(9):
# define subplot
plt.subplot(330 + 1 + i)
# plot raw pixel data
plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))
# if you want to invert color, you can use 'gray_r'. this can be used only for MNIST, Fashion MNIST not cifar10
# pyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray_r'))
# show the figure
plt.show()
'''
Model Engineering
'''
'''
M1. Import Libraries for Model Engineering
'''
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras import Model, Sequential
'''
M2. Set Hyperparameters
'''
hidden_size = 256
output_dim = 10 # output layer dimensionality = num_classes
EPOCHS = 30
'''
M3. Build NN model
'''
# in the case of Keras or TF2, type shall be [image_size, image_size, 1]
model = Sequential([
Conv2D(filters=64, kernel_size=3, activation=tf.nn.relu, padding='SAME',input_shape=(28, 28, 1)),
MaxPool2D(padding='SAME'),
Conv2D(filters=128, kernel_size=3, activation=tf.nn.relu, padding='SAME'),
MaxPool2D(padding='SAME'),
Conv2D(filters=256, kernel_size=3, activation=tf.nn.relu, padding='SAME'),
MaxPool2D(padding='SAME'),
Flatten(),
Dense(hidden_size, activation='relu'),
Dropout(0.2),
Dense(output_dim, activation='softmax')
])
model.summary()
'''
M4. Optimizer
'''
# Optimizer can be included at model.compile
'''
M5. Model Compilation - model.compile
'''
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
'''
M6. Train and Validation - `model.fit`
'''
model.fit(X_train, Y_train, epochs=EPOCHS, verbose=1)
'''
M7. Assess model performance
'''
loss, acc = model.evaluate(X_test, Y_test, verbose=0)
print('test_loss: {:.3f}, test_acc: {:.3f}'.format(
loss,
acc
))
'''
M7. [Opt] Assess model performance
'''
model.evaluate(X_test, Y_test, verbose=2)
'''
Keras Display Options
verbose default is 1
verbose=0 (silent)
verbose=1 (progress bar)
Train on 186219 samples, validate on 20691 samples
Epoch 1/2
186219/186219 [==============================] - 85s 455us/step - loss: 0.5815 - acc:
0.7728 - val_loss: 0.4917 - val_acc: 0.8029
Train on 186219 samples, validate on 20691 samples
Epoch 2/2
186219/186219 [==============================] - 84s 451us/step - loss: 0.4921 - acc:
0.8071 - val_loss: 0.4617 - val_acc: 0.8168
verbose=2 (one line per epoch)
Train on 186219 samples, validate on 20691 samples
Epoch 1/1
- 88s - loss: 0.5746 - acc: 0.7753 - val_loss: 0.4816 - val_acc: 0.8075
Train on 186219 samples, validate on 20691 samples
Epoch 1/1
- 88s - loss: 0.4880 - acc: 0.8076 - val_loss: 0.5199 - val_acc: 0.8046
'''