pythontensorflowkerasresuming-training

Resuming Training with customized loss function in Keras using checkpoint


I am training a model using keras (with Tensorflow backend) which its loss function is defined by me (named custom_loss in code) and I save the model with best accuracy during training:

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)

I want to stop training after some epochs and reload the model again to resume training from that saved point:

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)

But, after loading the model, I get this error which says the custom_loss is unknown; what is the problem? the custom_loss is defined in 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

Solution

  • When you load the model, you have to specify the custom loss function using custom_objects parameter (see docs):

    custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization.

    Try this out:

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

    You should take a look at this as well.