-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
keras_mnist.py
194 lines (163 loc) · 4.74 KB
/
keras_mnist.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
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import numpy as np
import argparse
import os
import glob
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential, model_from_json
from keras.layers import Dense
from keras.optimizers import RMSprop
from keras.callbacks import Callback
import tensorflow as tf
import mlflow
import mlflow.keras
from utils import load_data, one_hot_encode
print("Keras version:", keras.__version__)
print("Tensorflow version:", tf.__version__)
parser = argparse.ArgumentParser()
parser.add_argument(
"--data-folder",
type=str,
dest="data_folder",
default="data",
help="data folder mounting point",
)
parser.add_argument(
"--batch-size",
type=int,
dest="batch_size",
default=50,
help="mini batch size for training",
)
parser.add_argument(
"--first-layer-neurons",
type=int,
dest="n_hidden_1",
default=100,
help="# of neurons in the first layer",
)
parser.add_argument(
"--second-layer-neurons",
type=int,
dest="n_hidden_2",
default=100,
help="# of neurons in the second layer",
)
parser.add_argument(
"--learning-rate",
type=float,
dest="learning_rate",
default=0.001,
help="learning rate",
)
args = parser.parse_args()
# Start Logging
mlflow.start_run()
data_folder = args.data_folder
print("Data folder:", data_folder)
# load train and test set into numpy arrays
# note we scale the pixel intensity values to 0-1 (by dividing it with 255.0) so the model can converge faster.
X_train = load_data(
glob.glob(
os.path.join(data_folder, "**/train-images-idx3-ubyte.gz"), recursive=True
)[0],
False,
) / np.float32(255.0)
X_test = load_data(
glob.glob(
os.path.join(data_folder, "**/t10k-images-idx3-ubyte.gz"), recursive=True
)[0],
False,
) / np.float32(255.0)
y_train = load_data(
glob.glob(
os.path.join(data_folder, "**/train-labels-idx1-ubyte.gz"), recursive=True
)[0],
True,
).reshape(-1)
y_test = load_data(
glob.glob(
os.path.join(data_folder, "**/t10k-labels-idx1-ubyte.gz"), recursive=True
)[0],
True,
).reshape(-1)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep="\n")
training_set_size = X_train.shape[0]
n_inputs = 28 * 28
n_h1 = args.n_hidden_1
n_h2 = args.n_hidden_2
n_outputs = 10
n_epochs = 20
batch_size = args.batch_size
learning_rate = args.learning_rate
y_train = one_hot_encode(y_train, n_outputs)
y_test = one_hot_encode(y_test, n_outputs)
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep="\n")
# Build a simple MLP model
model = Sequential()
# first hidden layer
model.add(Dense(n_h1, activation="relu", input_shape=(n_inputs,)))
# second hidden layer
model.add(Dense(n_h2, activation="relu"))
# output layer
model.add(Dense(n_outputs, activation="softmax"))
model.summary()
model.compile(
loss="categorical_crossentropy",
optimizer=RMSprop(lr=learning_rate),
metrics=["accuracy"],
)
class LogRunMetrics(Callback):
# callback at the end of every epoch
def on_epoch_end(self, epoch, log):
# log a value repeated which creates a list
mlflow.log_metric("Loss", log["val_loss"])
mlflow.log_metric("Accuracy", log["val_accuracy"])
history = model.fit(
X_train,
y_train,
batch_size=batch_size,
epochs=n_epochs,
verbose=2,
validation_data=(X_test, y_test),
callbacks=[LogRunMetrics()],
)
score = model.evaluate(X_test, y_test, verbose=0)
# log a single value
mlflow.log_metric("Final test loss", score[0])
print("Test loss:", score[0])
mlflow.log_metric("Final test accuracy", score[1])
print("Test accuracy:", score[1])
fig = plt.figure(figsize=(6, 3))
plt.title("MNIST with Keras MLP ({} epochs)".format(n_epochs), fontsize=14)
plt.plot(history.history["val_accuracy"], "b-", label="Accuracy", lw=4, alpha=0.5)
plt.plot(history.history["val_loss"], "r--", label="Loss", lw=4, alpha=0.5)
plt.legend(fontsize=12)
plt.grid(True)
# log an image
mlflow.log_figure(fig, "Accuracy vs Loss.png")
##########################
# <save and register model>
##########################
# Registering the model to the workspace
print("Registering the model via MLFlow")
registered_model_name = "keras_dnn_mnist_model"
mlflow.keras.log_model(
keras_model=model,
registered_model_name=registered_model_name,
artifact_path=registered_model_name,
extra_pip_requirements=["protobuf~=3.20"],
)
# # Saving the model to a file
print("Saving the model via MLFlow")
mlflow.keras.save_model(
keras_model=model,
path=os.path.join(registered_model_name, "trained_model"),
extra_pip_requirements=["protobuf~=3.20"],
)
###########################
# </save and register model>
###########################
mlflow.end_run()