使用检查点在Keras恢复具有自定义损失功能的训练



我正在使用keras(带有Tensorflow后端(训练一个模型,其损失函数由我定义(在代码中命名为custom_loss(,我在训练期间以最佳精度保存模型:

model = Sequential()
model.add(...)
adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.01)
model.compile(loss=custom_loss, optimizer=adam, metrics=['accuracy'])  # better
filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
# Fit the model
model.fit(x_train, y_train, validation_split=0.1, epochs=150, batch_size=64, callbacks=callbacks_list)

我想在一些时期后停止训练,并再次重新加载模型,从保存的点恢复训练:

adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.01)
#Reload model
model = load_model('weights.best.hdf5')
model.compile(loss=custom_loss, optimizer=adam, metrics=['accuracy']) 
# checkpoint
filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
#Continue training
model.fit(x_train, y_train, validation_split=0.1, epochs=150,
batch_size=64, callbacks=callbacks_list)

但是,在加载模型后,我得到了这个错误,它说custom_loss是未知的;问题出在哪里?custom_loss是在主类中定义的

File "C:Program FilesJetBrainsPyCharm 2019.2.3helperspydevpydevd.py", line 2073, in <module>
main()
File "C:Program FilesJetBrainsPyCharm 2019.2.3helperspydevpydevd.py", line 2067, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:Program FilesJetBrainsPyCharm 2019.2.3helperspydevpydevd.py", line 1418, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:Program FilesJetBrainsPyCharm 2019.2.3helperspydevpydevd.py", line 1425, in _exec 
pydev_imports.execfile(file, globals, locals)  # execute the script
File "C:Program FilesJetBrainsPyCharm 2019.2.3helperspydev_pydev_imps_pydev_execfile.py", 
line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "C:/Projects/ML/Test_Yolo.py", line 194, in <module>
model = load_model('weights.best.hdf5')
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite 
packageskerasenginesaving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginesaving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginesaving.py", line 369, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginetraining.py", line 119, in compile
self.loss, self.output_names)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginetraining_utils.py", line 822, in prepare_loss_functions
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginetraining_utils.py", line 822, in <listcomp>
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasenginetraining_utils.py", line 705, in get_loss_function
loss_fn = losses.get(loss)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskeraslosses.py", line 795, in get
return deserialize(identifier)
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite-packageskeraslosses.py", line 
776, in deserialize
printable_module_name='loss function')
File "C:UsersAppDataLocalcondacondaenvstensorflow_envlibsite- 
packageskerasutilsgeneric_utils.py", line 167, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:custom_loss

加载模型时,必须使用custom_objects参数指定自定义损失函数(请参阅文档(:

custom_objects:可选字典映射名称(字符串(到自定义类或函数。

试试这个:

model = load_model('weights.best.hdf5', custom_objects={'loss': custom_loss})

你也应该看看这个。

相关内容

  • 没有找到相关文章

最新更新