Reputation: 1282
I'm training a model using TensorFlow and Keras. I would like to save the model and then load it. but I'm getting some errors.
I compile the model this way:
from tensorflow.keras.models import Model
import tensorflow as tf
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['accuracy', top3, top5])
and after training I'm loading the model this way:
model.save('model')
So I get a folder "model" containing:
---model
---assets
---variables
---keras_metadata.pb
---saved_model.pb
Finally, I try to load the model using:
import tensorflow as tf
new_model = tf.keras.models.load_model('model')
new_model.summary()
But I'm getting this error:
ValueError: Unable to restore custom object of type _tf_keras_metric
currently. Please make sure that the layer implements `get_config`and
`from_config` when saving. In addition, please use the `custom_objects`
arg when calling `load_model()`.
Upvotes: 0
Views: 648
Reputation: 5174
When your model utilizes custom objects, such as your custom metrics, you have to specify them with the custom_objects
argument of load_model
:
new_model = tf.keras.models.load_model('model', custom_objects={'top3': top3, 'top5': top5})
Note that the definitions of your custom metrics must be available in the same module/environment you are loading the model.
Upvotes: 1