Repository navigation
Expand file tree
/
Copy pathtrain_save_model.py
More file actions
242 lines (187 loc) · 7.92 KB
/
Copy pathtrain_save_model.py
File metadata and controls
242 lines (187 loc) · 7.92 KB
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
import tensorflow as tf
import numpy as np
import itertools
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import datetime
import os
import io
# config
EPOCHS = 5
MODEL_DIR = "models"
MODEL_VERSION = 1
############################################################################################
#################################### PREPROCESSING #########################################
############################################################################################
# prepare data
## import Fashion MNIST Dataset using Keras
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
## data normalization -> Between 0 and 1
X_train = X_train / 255.0
X_test = X_test / 255.0
## reshape data to be = (no_of_images, 28, 28, 1) instead of (no_of_images, 28,28)
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
## define images classes
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# ## inspect data
# W_grid = 4
# L_grid = 4
# fig, axes = plt.subplots(L_grid, W_grid, figsize = (15, 15))
# axes = axes.ravel()
# n_training = len(X_train)
# for i in np.arange(0, L_grid * W_grid):
# index = np.random.randint(0, n_training)
# axes[i].imshow(X_train[index].reshape(28,28))
# axes[i].set_title(y_train[index])
# axes[i].axis('off')
# plt.subplots_adjust(hspace = 0.4)
# plt.show()
############################################################################################
##################################### BUILD MODEL ##########################################
############################################################################################
# model building
# ## first attempt
# classifier = tf.keras.Sequential([
# tf.keras.layers.Conv2D(input_shape=(28,28,1), filters=8, kernel_size=3,
# strides=2, activation='relu', name='Conv1'),
# tf.keras.layers.Flatten(),
# tf.keras.layers.Dense(10, name='Dense')
# ])
# ## third attempt
# classifier = tf.keras.Sequential([
# tf.keras.layers.Conv2D(6, (5,5), activation = 'relu', input_shape = (28,28,1)),
# tf.keras.layers.AveragePooling2D(),
# tf.keras.layers.Conv2D(16, (5,5), activation = 'relu'),
# tf.keras.layers.AveragePooling2D(),
# tf.keras.layers.Flatten(),
# tf.keras.layers.Dense(120, activation = 'relu'),
# tf.keras.layers.Dense(84, activation = 'relu'),
# tf.keras.layers.Dense(10, activation = 'softmax')
# ])
## second attempt (best!)
### added mirrored strategy
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
classifier = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (28,28,1)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation = 'relu'),
tf.keras.layers.Dense(10, activation = 'softmax')
])
classifier.summary()
classifier.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
############################################################################################
##################################### TENSORBOARD ##########################################
############################################################################################
##################################### PRINT IMAGES #########################################
# configuring tensorboard
## set log data location
log_dir="tensorboard/logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
## create a file writer for the log directory
file_writer = tf.summary.create_file_writer(log_dir)
def plot_to_image(figure):
# Converts the matplotlib plot specified by 'figure' to a PNG image and
# returns it. The supplied figure is closed and inaccessible after this call.
# Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
# Closing the figure prevents it from being displayed directly inside
# the notebook.
plt.close(figure)
buf.seek(0)
# Convert PNG buffer to TF image
image = tf.image.decode_png(buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
return image
def image_grid():
# Return a 5x5 grid of the MNIST images as a matplotlib figure.
# Create a figure to contain the plot.
figure = plt.figure(figsize=(10,10))
for i in range(25):
# Start next subplot.
plt.subplot(5, 5, i + 1, title=class_names[y_train[i]])
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(X_train[i], cmap=plt.cm.binary)
return figure
# Prepare the plot
figure = image_grid()
# Convert to image and log
with file_writer.as_default():
tf.summary.image("Training data", plot_to_image(figure), step=0)
## create Tensorboard callback
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
####################################### PRINT CM ###########################################
def plot_confusion_matrix(cm, class_names):
"""
Returns a matplotlib figure containing the plotted confusion matrix.
Args:
cm (array, shape = [n, n]): a confusion matrix of integer classes
class_names (array, shape = [n]): String names of the integer classes
"""
figure = plt.figure(figsize=(8, 8))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title("Confusion Matrix")
plt.colorbar()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks, class_names, rotation=45)
plt.yticks(tick_marks, class_names)
# Compute the labels from the normalized confusion matrix.
labels = np.around(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis], decimals=2)
# Use white text if squares are dark; otherwise black.
threshold = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
color = "white" if cm[i, j] > threshold else "black"
plt.text(j, i, labels[i, j], horizontalalignment="center", color=color)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
return figure
file_writer_cm = tf.summary.create_file_writer(log_dir + '/cm')
def log_confusion_matrix(epoch, logs):
# Use the model to predict the values from the validation dataset.
test_pred_raw = classifier.predict(X_test)
test_pred = np.argmax(test_pred_raw, axis=1)
# Calculate the confusion matrix.
cm = confusion_matrix(y_test, test_pred)
# Log the confusion matrix as an image summary.
figure = plot_confusion_matrix(cm, class_names=class_names)
cm_image = plot_to_image(figure)
# Log the confusion matrix as an image summary.
with file_writer_cm.as_default():
tf.summary.image("Confusion Matrix", cm_image, step=epoch)
# Define the per-epoch callback.
cm_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=log_confusion_matrix)
############################################################################################
#################################### MODEL TRAINING ########################################
############################################################################################
# model training
classifier.fit(X_train, y_train, epochs=EPOCHS,
callbacks=[tensorboard_callback, cm_callback],
validation_data=(X_test, y_test))
test_loss, test_acc = classifier.evaluate(X_test, y_test)
print('\nINFO :: Test accuracy: {}'.format(test_acc))
# save the model
## join the temp model directory with chosen version number
export_path = os.path.join(MODEL_DIR, str(MODEL_VERSION))
## save the model using `save_model`
tf.keras.models.save_model(
classifier,
export_path,
overwrite=True,
include_optimizer=True,
save_format=None,
signatures=None,
options=None
)
print('\nINFO :: Model Saved!')
# execute tensorboard
os.system("tensorboard --logdir tensorboard/logs")