Reputation: 601
When i try to save my model as hdf5
path = 'path.h5'
model.save(path)
then load the model again
my_reloaded_model = tf.keras.models.load_model(path)
I get the following error
ValueError: Unknown layer: KerasLayer
Any help ? I'm using
tensorflow version: 2.2.0
keras version: 2.3.0-tf
Upvotes: 18
Views: 13892
Reputation: 109
You may also face this problem if you have made a custom model and not included get_config
method in the custom layers and models.
class CustomLayer(keras.layers.Layer):
def __init__(self, sublayer, **kwargs):
super().__init__(**kwargs)
self.sublayer = layer
def call(self, x):
return self.sublayer(x)
def get_config(self):
base_config = super().get_config()
config = {
"sublayer": keras.saving.serialize_keras_object(self.sublayer),
}
return {**base_config, **config}
@classmethod
def from_config(cls, config):
sublayer_config = config.pop("sublayer")
sublayer = keras.saving.deserialize_keras_object(sublayer_config)
return cls(sublayer, **config)
Upvotes: 0
Reputation: 601
I just found a solution that worked for me
my_reloaded_model = tf.keras.models.load_model(
(path),
custom_objects={'KerasLayer':hub.KerasLayer}
)
Upvotes: 42