digantamisra98 / Mish

Official Repository for "Mish: A Self Regularized Non-Monotonic Neural Activation Function" [BMVC 2020]
https://www.bmvc2020-conference.com/assets/papers/0928.pdf
MIT License
1.29k stars 129 forks source link

"Unexpected keyword argument" error when loading model with TF-keras version of Mish #10

Closed Lauler closed 5 years ago

Lauler commented 5 years ago

Hi. I trained a model with the TF-Keras version of MIsh and saved it. However, I cannot figure out how to load the Mish custom layer object when attempting to load the model.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-99cc25741e5d> in <module>
    208     learning_rate=1e-4, base_lr=1e-4, max_lr=2e-4,
    209     img_size=300, train_img_folder="data/train_supercropboy/", valid_img_folder="data/train_supercropboy/",
--> 210     df_valid=None, pretrain=False, freeze_weights=False, freeze_weights_epochs=5)

<ipython-input-36-99cc25741e5d> in regression_cv(df, nfolds, epochs, batch_size, learning_rate, base_lr, max_lr, train_img_folder, valid_img_folder, img_size, df_valid, start_fold, pretrain, freeze_weights, freeze_weights_epochs)
    137         # Create model
    138 
--> 139         model = create_model(img_size=img_size, pretrain=pretrain, freeze_weights=freeze_weights)
    140 
    141         if freeze_weights:

<ipython-input-36-99cc25741e5d> in create_model(img_size, pretrain, freeze_weights)
     22         model = load_model("effnet_pretrain.h5",
     23                           custom_objects = {"root_mse": root_mse,
---> 24                                             "Mish": Mish})
     25 
     26     return(model)

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\save.py in load_model(filepath, custom_objects, compile)
    144       h5py is not None and (
    145           isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 146     return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    147 
    148   if isinstance(filepath, six.string_types):

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    210     model_config = json.loads(model_config.decode('utf-8'))
    211     model = model_config_lib.model_from_config(model_config,
--> 212                                                custom_objects=custom_objects)
    213 
    214     # set weights

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\saving\model_config.py in model_from_config(config, custom_objects)
     53                     '`Sequential.from_config(config)`?')
     54   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
---> 55   return deserialize(config, custom_objects=custom_objects)
     56 
     57 

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\layers\serialization.py in deserialize(config, custom_objects)
     87       module_objects=globs,
     88       custom_objects=custom_objects,
---> 89       printable_module_name='layer')

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    190             custom_objects=dict(
    191                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 192                 list(custom_objects.items())))
    193       with CustomObjectScope(custom_objects):
    194         return cls.from_config(cls_config)

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py in from_config(cls, config, custom_objects)
   1119     # First, we create all layers and enqueue nodes to be processed
   1120     for layer_data in config['layers']:
-> 1121       process_layer(layer_data)
   1122     # Then we process nodes in order of layer depth.
   1123     # Nodes that cannot yet be processed (if the inbound node

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\network.py in process_layer(layer_data)
   1103       from tensorflow.python.keras.layers import deserialize as deserialize_layer  # pylint: disable=g-import-not-at-top
   1104 
-> 1105       layer = deserialize_layer(layer_data, custom_objects=custom_objects)
   1106       created_layers[layer_name] = layer
   1107 

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\layers\serialization.py in deserialize(config, custom_objects)
     87       module_objects=globs,
     88       custom_objects=custom_objects,
---> 89       printable_module_name='layer')

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    192                 list(custom_objects.items())))
    193       with CustomObjectScope(custom_objects):
--> 194         return cls.from_config(cls_config)
    195     else:
    196       # Then `cls` may be a function returning a class.

~\Anaconda3\envs\r-tensorflow\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in from_config(cls, config)
    444         A layer instance.
    445     """
--> 446     return cls(**config)
    447 
    448   def compute_output_shape(self, input_shape):

TypeError: __init__() got an unexpected keyword argument 'name'

Any advice on how to successfully load a saved model?

digantamisra98 commented 5 years ago

@Lauler Just fixed the code. Can you please retry now. Make sure you follow the convention of: X = Activation('Mish', name="conv1_act")(X_input) Hopefully, it will work. Let me know if you face any issues.