How to load the Keras model with custom layers from .h5 file correctly?

前端 未结 3 780
Happy的楠姐
Happy的楠姐 2021-01-11 19:49

I built a Keras model with a custom layers, and it was saved to a .h5 file by the callback ModelCheckPoint. When I tried to load this model after

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-11 20:09

    Actually I don't think you can load this model.

    The most likely issue is that you did not implement the get_config() method in your layer. This method returns a dictionary of configuration values that should be saved:

    def get_config(self):
        config = {'pool_size': self.pool_size,
                  'axis': self.axis}
        base_config = super(MyMeanPooling, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))
    

    You have to retrain the model after adding this method to your layer, as the previously saved model does not have the configuration for this layer saved into it. This is why you cannot load it, it requires retraining after making this change.

提交回复
热议问题