https://machinelearningmastery.com/check-point-deep-learning-models-keras/
https://keras.io/callbacks/
http://keras-cn.readthedocs.io/en/latest/other/callbacks/
keras.callbacks.ModelCheckpoint(filepath,monitor='val_loss',verbose=0,save_best_only=False, save_weights_only=False, mode='auto', period=1)
参数说明:
from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint
import os
batch_size = 128
num_classes = 10
epochs = 30
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'
# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
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, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
filepath="model_{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(os.path.join(save_dir, filepath), monitor='val_acc',verbose=1,
save_best_only=True)
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True,
callbacks=[checkpoint])
print(history.history.keys())
# Save model and weights
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)
# Score trained model.
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
Train on 50000 samples, validate on 10000 samples
Epoch 1/30
50000/50000 [==============================] - 5s 106us/step - loss: 1.9831 - acc: 0.2734 - val_loss: 1.7195 - val_acc: 0.3934
Epoch 00001: val_acc improved from -inf to 0.39340, saving model to /home/weiliu/PG/keras/saved_models/model_01-0.39.hdf5
Epoch 2/30
50000/50000 [==============================] - 4s 84us/step - loss: 1.6651 - acc: 0.3963 - val_loss: 1.5001 - val_acc: 0.4617
Epoch 00002: val_acc improved from 0.39340 to 0.46170, saving model to /home/weiliu/PG/keras/saved_models/model_02-0.46.hdf5
Epoch 3/30
50000/50000 [==============================] - 4s 88us/step - loss: 1.5249 - acc: 0.4482 - val_loss: 1.4158 - val_acc: 0.4923
Epoch 00003: val_acc improved from 0.46170 to 0.49230, saving model to /home/weiliu/PG/keras/saved_models/model_03-0.49.hdf5
Epoch 4/30
50000/50000 [==============================] - 4s 87us/step - loss: 1.4479 - acc: 0.4775 - val_loss: 1.3469 - val_acc: 0.5199
Epoch 00004: val_acc improved from 0.49230 to 0.51990, saving model to /home/weiliu/PG/keras/saved_models/model_04-0.52.hdf5
Epoch 5/30
50000/50000 [==============================] - 4s 89us/step - loss: 1.3838 - acc: 0.5048 - val_loss: 1.3058 - val_acc: 0.5332
Epoch 00005: val_acc improved from 0.51990 to 0.53320, saving model to /home/weiliu/PG/keras/saved_models/model_05-0.53.hdf5
Epoch 6/30
50000/50000 [==============================] - 4s 87us/step - loss: 1.3359 - acc: 0.5226 - val_loss: 1.3543 - val_acc: 0.5226
Epoch 00006: val_acc did not improve from 0.53320
Epoch 7/30
50000/50000 [==============================] - 4s 88us/step - loss: 1.2934 - acc: 0.5399 - val_loss: 1.1970 - val_acc: 0.5769
Epoch 00007: val_acc improved from 0.53320 to 0.57690, saving model to /home/weiliu/PG/keras/saved_models/model_07-0.58.hdf5
Epoch 8/30
50000/50000 [==============================] - 4s 87us/step - loss: 1.2522 - acc: 0.5536 - val_loss: 1.1682 - val_acc: 0.5865
由以上输出不难发现,有部分epoch未保存checkpoint,如第6个epoch。