使用检查点在 Keras 中使用自定义损失函数恢复训练
Resuming Training with customized loss function in Keras using checkpoint
我正在使用 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 在 main class:
中定义
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 2073, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 2067, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 1418, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 1425, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_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:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site
packages\keras\engine\saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\saving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\saving.py", line 369, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training.py", line 119, in compile
self.loss, self.output_names)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 822, in prepare_loss_functions
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 822, in <listcomp>
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 705, in get_loss_function
loss_fn = losses.get(loss)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\losses.py", line 795, in get
return deserialize(identifier)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\losses.py", line
776, in deserialize
printable_module_name='loss function')
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\utils\generic_utils.py", line 167, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:custom_loss
加载模型时,必须使用 custom_objects
参数指定自定义损失函数(参见 docs):
custom_objects
: Optional dictionary mapping names (strings) to custom
classes or functions to be considered during deserialization.
试试这个:
model = load_model('weights.best.hdf5', custom_objects={'loss': custom_loss})
你也应该看看 。
我正在使用 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 在 main class:
中定义 File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 2073, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 2067, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 1418, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevd.py", line 1425, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_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:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site
packages\keras\engine\saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\saving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\saving.py", line 369, in _deserialize_model
sample_weight_mode=sample_weight_mode)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training.py", line 119, in compile
self.loss, self.output_names)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 822, in prepare_loss_functions
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 822, in <listcomp>
loss_functions = [get_loss_function(loss) for _ in output_names]
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\engine\training_utils.py", line 705, in get_loss_function
loss_fn = losses.get(loss)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\losses.py", line 795, in get
return deserialize(identifier)
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-packages\keras\losses.py", line
776, in deserialize
printable_module_name='loss function')
File "C:\Users\AppData\Local\conda\conda\envs\tensorflow_env\lib\site-
packages\keras\utils\generic_utils.py", line 167, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:custom_loss
加载模型时,必须使用 custom_objects
参数指定自定义损失函数(参见 docs):
custom_objects
: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.
试试这个:
model = load_model('weights.best.hdf5', custom_objects={'loss': custom_loss})
你也应该看看