参考
/check-point-deep-learning-models-keras/
/callbacks/
/en/latest/other/callbacks/
语法
(filepath,monitor='val_loss',verbose=0,save_best_only=False, save_weights_only=False, mode='auto', period=1)
参数说明:
- filename:字符串,保存模型的路径
- monitor:需要监视的值
- verbose:信息展示模式,0或1(checkpoint的保存信息,类似Epoch 00001: saving model to ...)
- save_best_only:当设置为True时,监测值有改进时才会保存当前的模型( the latest best model according to the quantity monitored will not be overwritten)
- mode:‘auto’,‘min’,‘max’之一,在save_best_only=True时决定性能最佳模型的评判准则,例如,当监测值为val_acc时,模式应为max,当监测值为val_loss时,模式应为min。在auto模式下,评价准则由被监测值的名字自动推断。
- save_weights_only:若设置为True,则只保存模型权重,否则将保存整个模型(包括模型结构,配置信息等)
- period:CheckPoint之间的间隔的epoch数
例子
代码
from __future__ import print_function
import keras
from import cifar10
from import ImageDataGenerator
from import Sequential
from import Dense, Dropout, Activation, Flatten
from import Conv2D, MaxPooling2D
from import ModelCheckpoint
import os
batch_size = 128
num_classes = 10
epochs = 30
num_predictions = 20
save_dir = ((), '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 = .to_categorical(y_train, num_classes)
y_test = .to_categorical(y_test, num_classes)
model = Sequential()
(Conv2D(32, (3, 3), padding='same',
input_shape=x_train.shape[1:]))
(Activation('relu'))
(Conv2D(32, (3, 3)))
(Activation('relu'))
(MaxPooling2D(pool_size=(2, 2)))
(Dropout(0.25))
(Conv2D(64, (3, 3), padding='same'))
(Activation('relu'))
(Conv2D(64, (3, 3)))
(Activation('relu'))
(MaxPooling2D(pool_size=(2, 2)))
(Dropout(0.25))
(Flatten())
(Dense(512))
(Activation('relu'))
(Dropout(0.5))
(Dense(num_classes))
(Activation('softmax'))
# initiate RMSprop optimizer
opt = (lr=0.0001, decay=1e-6)
# Let's train the model using RMSprop
(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((save_dir, filepath), monitor='val_acc',verbose=1,
save_best_only=True)
history = (x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True,
callbacks=[checkpoint])
print(())
# Save model and weights
if not (save_dir):
(save_dir)
model_path = (save_dir, model_name)
(model_path)
print('Saved trained model at %s ' % model_path)
# Score trained model.
scores = (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。