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

前端 未结 3 801
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 19:55

    If you don't have enough time to retrain the model in the solution way of Matias Valdenegro. You can set the default value of pool_size in class MyMeanPooling like the following code. Note that the value of pool_size should be consistent with the value while training the model. Then you can load the model.

    class MyMeanPooling(Layer):
        def __init__(self, pool_size, axis=1, **kwargs):
            self.supports_masking = True
            self.pool_size = 2  # The value should be consistent with the value while training the model
            self.axis = axis
            self.y_shape = None
            self.y_mask = None
            super(MyMeanPooling, self).__init__(**kwargs)
    

    ref: https://www.jianshu.com/p/e97112c34e43

提交回复
热议问题