Skip to content

Instantly share code, notes, and snippets.

@kairess
Created November 23, 2017 12:08
Show Gist options
  • Save kairess/7308cfeddac74e3042269d379897ac14 to your computer and use it in GitHub Desktop.
Save kairess/7308cfeddac74e3042269d379897ac14 to your computer and use it in GitHub Desktop.
MNIST mini batch optimization for Keras, early stopping and save model checkpoint applied.
from __future__ import print_function
import keras
import os
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.models import load_model
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
batch_size = 128
num_classes = 10
epochs = 30
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(lr=1e-02, beta_1=0.9, beta_2=0.999, epsilon=0.001),
metrics=['accuracy'])
# save checkpoint
filepath="weights.best.h5"
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# check 5 epochs
early_stop = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=1e-04, patience=5, mode='max')
callbacks_list = [checkpoint, early_stop]
def trainBatchGenerator():
while True:
for i in range(x_train.shape[0] // batch_size):
x_batch = x_train[i * batch_size:(i + 1) * batch_size]
y_batch = y_train[i * batch_size:(i + 1) * batch_size]
yield x_batch, y_batch
def valBatchGenerator():
while True:
for ii in range(x_test.shape[0] // batch_size):
x_val_batch = x_test[ii * batch_size:(ii + 1) * batch_size]
y_val_batch = y_test[ii * batch_size:(ii + 1) * batch_size]
yield x_val_batch, y_val_batch
model.fit_generator(
generator=trainBatchGenerator(),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs, verbose=1, shuffle=True,
validation_data=valBatchGenerator(),
validation_steps=x_test.shape[0] // batch_size,
callbacks=callbacks_list)
# model.save('my_model.h5')
# with open("my_model.json", "w") as json_file:
# json_file.write(model.to_json())
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment