1. more scientific model training and model save
filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
# fit model
model.fit(x, y, epochs=20, verbose=2, callbacks=[checkpoint], validation_data=(x, y))
After save_best_only opens, as follows:
ETA: 3s - loss: 0.5820Epoch 00017: val_loss did not improve
If val_loss will improve the preservation, no increase will not be saved.
ModelCheckpoint
keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
The callback function to save the model after each epochfilepath
filepath
It may be formatted string, which will be a placeholder epoch
value and passed on_epoch_end
the logs
keywords filled
For example, filepath
if it is weights.{epoch:02d-{val_loss:.2f}}.hdf5
, it will correspond to a plurality of files and validation sets epoch generation loss.
parameter
-
filename: string that holds the model of Path
-
monitor: the value to be monitored
-
verbose: information presentation mode, 0 or 1
-
save_best_only: When set
True
, it will save only the best performance on the verification set model -
mode: 'auto', 'min ', one of 'max', in
save_best_only=True
determining the best model performance evaluation criterion, e.g., when the monitored valueval_acc
, the mode should bemax
, when the detection valueval_loss
, the pattern should bemin
. Inauto
mode, the evaluation criteria are monitored automatically by inference value name. -
save_weights_only: If set to True, only save the model weights, or save the entire model (including the model structure, configuration information, etc.)
-
period: Number epoch interval between CheckPoint.
2. How to use tensorboard in the keras
RUN = RUN + 1 if 'RUN' in locals() else 1 # locals() 函数会以字典类型返回当前位置的全部局部变量。
LOG_DIR = model_save_path + '/training_logs/run{}'.format(RUN)
LOG_FILE_PATH = LOG_DIR + '/checkpoint-{epoch:02d}-{val_loss:.4f}.hdf5' # 模型Log文件以及.h5模型文件存放地址
tensorboard = TensorBoard(log_dir=LOG_DIR, write_images=True)
checkpoint = ModelCheckpoint(filepath=LOG_FILE_PATH, monitor='val_loss', verbose=1, save_best_only=True)
early_stopping = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
history = model.fit_generator(generator=gen.generate(True), steps_per_epoch=int(gen.train_batches / 4),
validation_data=gen.generate(False), validation_steps=int(gen.val_batches / 4),
epochs=EPOCHS, verbose=1, callbacks=[tensorboard, checkpoint, early_stopping])
All play a role in the callback function:
-
EarlyStopping patience: When Early
(. 1) STOP is activated (e.g., loss found on a training epoch is not decreased compared), then after a patience stop training epoch.
(2) mode: 'auto' , 'min', one of 'max', in min mode, if the detection value stops decreasing aborting training. In max mode, when the detected value is no longer increased training is stopped. -
Model checkpoint ModelCheckpoint
(. 1) save_best_only: When set to True, the verification will only save the best performance on the current model
(2) mode: 'auto' , 'min', 'max' one, in save_best_only = True determining the best model performance evaluation criterion, e.g., when monitoring is val_acc, mode should be max, when the detection value val_loss, mode should be min. In auto mode, inferring evaluation criteria to be monitored automatically by the value of the name.
(3) save_weights_only: If set to True, only save the model weights, or to save the entire model (including the model structure, configuration information, etc.)
(. 4) period: Number epoch interval between CheckPoint - Visualization tensorboard write_images: whether the model weights in the form of visual images