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

前端 未结 3 782
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:18

    From the answer of "LiamHe commented on Sep 27, 2017" on the following issue: https://github.com/keras-team/keras/issues/4871.

    I met the same problem today : ** TypeError: init() missing 1 required positional arguments**. Here is how I solve the problem : (Keras 2.0.2)

    1. Give the positional arguments of the layer with some default values
    2. Override get_config function to the layer with some thing like
    def get_config(self):
        config = super().get_config()
        config['pool_size'] = # say self._pool_size  if you store the argument in __init__
        return config
    
    1. Add layer class to custom_objects when you are loading model.

提交回复
热议问题