0

I used a custom layer for my Keras model, namely the DepthwiseConv3D layer. I trained the model and saved it using model.save("model.h5")

from DepthwiseConv3D import DepthwiseConv3D

model = load_model('model.h5',
          custom_objects={'DepthwiseConv3D': DepthwiseConv3D})

But I am getting "TypeError: unorderable types: NoneType() > int()", raised by DepthWiseConv3D at:

if (self.groups > self.input_dim):
       raise ValueError('The number of groups cannot exceed the number of channels')

The layers config is:

 def get_config(self):
        config = super(DepthwiseConv3D, self).get_config()
        config.pop('filters')
        config.pop('kernel_initializer')
        config.pop('kernel_regularizer')
        config.pop('kernel_constraint')
        config['depth_multiplier'] = self.depth_multiplier
        config['depthwise_initializer'] = initializers.serialize(self.depthwise_initializer)
        config['depthwise_regularizer'] = regularizers.serialize(self.depthwise_regularizer)
        config['depthwise_constraint'] = constraints.serialize(self.depthwise_constraint)
        return config

I instantiated my layer as

x = DepthwiseConv3D(kernel_size=(7,7,7),
                depth_multiplier=1,groups=9, 
                padding ="same", use_bias=False,
                input_shape=(50, 37, 25, 9))(x)
x = DepthwiseConv3D(depth_multiplier= 32, groups=8, kernel_size=(7,7,7), 
                strides=(2,2,2), activation='relu', padding = "same")(x)
x = DepthwiseConv3D(depth_multiplier= 64, groups=8, kernel_size=(7,7,7), 
                strides=(2,2,2), activation='relu', padding = "same")(x)

How can I load my model?

PascalIv
  • 595
  • 7
  • 21
  • It is possible that the get_config is not correct, how are you instantiating the layer initially? – Dr. Snoopy Mar 26 '20 at 15:32
  • @MatiasValdenegro added my code of the instantiation, all other layers are standard Keras layers. – PascalIv Mar 26 '20 at 16:30
  • Yeah I think the get_config is not saving all necessary variables, you should complain to the author of this code. – Dr. Snoopy Mar 26 '20 at 21:25
  • ok, thanks. I trained this model for two weeks... Can you think of any hack to recover the model? The weights must be saved somehow? – PascalIv Mar 27 '20 at 08:25
  • You can just load the weights with model.load_weights, once you have an instance of the model. – Dr. Snoopy Mar 27 '20 at 09:30
  • Thanks, I did not know it is that easy. I was misguided by this answer: https://stackoverflow.com/questions/57506707/how-to-load-the-keras-model-with-custom-layers-from-h5-file-correctly "You have to retrain the model" scared me. Maybe you want to turn this into a short answer, I'd accept. – PascalIv Mar 27 '20 at 09:48

1 Answers1

1

The get_config method in the custom layer you are using is not correctly implemented, it does not save all the parameters that it needs, so it errors when loading back the model.

If you can instance the model using the same original code, you can load the weights from the same file using model.load_weights. This is just a workaround to the problem, and it should work. A proper solution would be to implement a correct version of get_config, and that would require re-training the model.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140