Home > Software design >  Errors when saving a Keras ML model
Errors when saving a Keras ML model

Time:10-10

I am getting an error saving a ML model. I searched here on SO and it looked like the advice was to change the parameters of the function to be '=None'. But when I tried that, I got an error that None types are not iterable. Any ideas?

# Save the model
model.save('./alexnet_model.hdf5')
# Load the model
alexnet_model = tf.keras.models.load_model('./alexnet_model.hdf5', custom_objects={'AlexNet': AlexNet})

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-504e90d49459> in <module>()
      6 model.save('./alexnet_model.hdf5')
      7 # Load the model
----> 8 alexnet_model = tf.keras.models.load_model('./alexnet_model.hdf5', custom_objects={'AlexNet': AlexNet})
      9 #alexnet_model = tf.keras.models.load_model('./alexnet_model.hdf5')

5 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py in from_config(cls, config, custom_objects)
    428       build_input_shape = None
    429       layer_configs = config
--> 430     model = cls(name=name)
    431     for layer_config in layer_configs:
    432       layer = layer_module.deserialize(layer_config,

TypeError: __init__() missing 2 required positional arguments: 'input_shape' and 'num_classes'

Here is the first few lines of the model architecture:

# Define the AlexNet model
class AlexNet(Sequential):
   def __init__(self, input_shape, num_classes, **kwargs):
    super().__init__()

CodePudding user response:

Did you define get_config method in your model to return your init params? Can you look at their tutorial again to compare your model configuration?

https://www.tensorflow.org/guide/keras/save_and_serialize

CodePudding user response:

As @firattamur mentioned, you need to add a get_config method which can return the parameters of the constructor while deserializing the model. See this section in Save and Load Keras models. It mentions that,

The architecture of subclassed models and layers are defined in the methods __init__ and call. They are considered Python bytecode, which cannot be serialized into a JSON-compatible config -- you could try serializing the bytecode (e.g. via pickle), but it's completely unsafe and means your model cannot be loaded on a different system.

In order to save/load a model with custom-defined layers, or a subclassed model, you should overwrite the get_config and optionally from_config methods. Additionally, you should use register the custom object so that Keras is aware of it.

Add the get_config method to your class AlexNet,

class AlexNet(Sequential):

   def __init__(self, input_shape, num_classes, **kwargs):
    super().__init__()

   def call( self , inputs ):
    ...

   def get_config( self ):
      # Modify these according to your requirements
      return { 'input_shape' : ( 224 , 224 , 1 ) , 
               'num_classes' : 3 }
  • Related