I have a neural network with SeqSelfAttention (https://pypi.org/project/keras-self-attention/). Also I implemented a custom metric for F1. Then I saved the model without problems, but when the model is loaded using keras.models.load_model(model_path, custom_objects=SeqSelfAttention.get_custom_objects())

the next error is presented: ValueError: Unknown metric function: f1

If I coded with the F1 metric as: saved_model = load_model(Modelfilename, custom_objects={"f1": f1})

the error say:

ValueError: Unknown layer: SeqSelfAttention

Are there a way of combine the two parameters in "custom_objects"?


1 Answer 1


You need to use the custom_objects with dependencies term, check this code, I will explain line by line below:

iou_metric = train.iou_metric

dependencies = {'iou_metric': iou_metric}

# load the saved model
saved_model = load_model('saved_model.h5', custom_objects=dependencies)

I used a variable named iou_metric and assigned my defined function from train file and then used that variable in the dependencies and at last while loading the model the custom_objects are used with dependencies.

Note: dependencies is just a common practice, you can use any name instead of dependencies.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.