简体   繁体   中英

can't load keras model with unknown custom lambda loss function

my model looks like

Single output multiple loss functions in Keras: https://stackoverflow.com/a/51705573/9079093

model = Model(inputs=[sketch_inp, color_inp], outputs=disc_outputs)

opt = Adam(lr=learning_rate, beta_1=.5)



model.compile(loss=lambda y_true, y_pred : tf.keras.losses.binary_crossentropy(y_true, y_pred) + \
                                                 pixelLevelLoss_weight * pixelLevelLoss(y_true, y_pred) + \
                                                 totalVariationLoss_weight * totalVariationLoss(y_true, y_pred) + \
                                                 featureLevelLoss_weight * featureLevelLoss(y_true, y_pred),\
                    optimizer=opt)

After saving the model, I want to load it and complete the training but I don't how to load it with this custom loss function

While loading your model, just use cutom_objects argument to pass the loss.

If the model you want to load includes custom layers or other custom classes or functions, you can pass them to the loading mechanism via the custom_objects argument:

from keras.models import load_model
# Assuming your model includes instance of an "AttentionLayer" class
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM