Я тренирую модель, используя керасы (с бэкэндом 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)
I хочу прекратить обучение после нескольких эпох и перезагрузить модель снова, чтобы возобновить обучение с этой сохраненной точки:
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 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