aggreen/MTB-CNN

Cannot load saved model

julibeg opened this issue · 1 comments

Hi again!

I created a conda environment with the file in md_cnn/model_training/environment_reqs_MDCNN.txt, but was not able to load the model saved in saved_models/MDCNN_saved_model.tar.gz (after unpacking the tar archive of course). This is the error I got:

Click to expand error message
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/git/MTB-CNN/saved_models/try-loading-mdnn-model.py in <module>
      3     "masked_multi_weighted_bce": tb_cnn.masked_multi_weighted_bce,
      4 }
----> 5 m = keras.models.load_model("MDCNN_saved_model", custom_objects=custom_objects)

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    188     if isinstance(filepath, six.string_types):
    189       loader_impl.parse_saved_model(filepath)
--> 190       return saved_model_load.load(filepath, compile)
    191 
    192   raise IOError(

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile)
    114   # TODO(kathywu): Add saving/loading of optimizer, compiled losses and metrics.
    115   # TODO(kathywu): Add code to load from objects that contain all endpoints
--> 116   model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
    117 
    118   # pylint: disable=protected-access

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py in load_internal(export_dir, tags, loader_cls)
    602       loader = loader_cls(object_graph_proto,
    603                           saved_model_proto,
--> 604                           export_dir)
    605       root = loader.get(0)
    606       if isinstance(loader, Loader):

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in __init__(self, *args, **kwargs)
    186     self._models_to_reconstruct = []
    187 
--> 188     super(KerasObjectLoader, self).__init__(*args, **kwargs)
    189 
    190     # Now that the node object has been fully loaded, and the checkpoint has

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir)
    121       self._concrete_functions[name] = _WrapperFunction(concrete_function)
    122 
--> 123     self._load_all()
    124     self._restore_checkpoint()
    125 

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_all(self)
    207     # loaded from config may create variables / other objects during
    208     # initialization. These are recorded in `_nodes_recreated_from_config`.
--> 209     self._layer_nodes = self._load_layers()
    210 
    211     # Load all other nodes and functions.

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_layers(self)
    307         continue
    308 
--> 309       layers[node_id] = self._load_layer(proto.user_object, node_id)
    310 
    311     for node_id, proto in metric_list:

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_layer(self, proto, node_id)
    333     # Detect whether this object can be revived from the config. If not, then
    334     # revive from the SavedModel instead.
--> 335     obj, setter = self._revive_from_config(proto.identifier, metadata, node_id)
    336     if obj is None:
    337       obj, setter = revive_custom_object(proto.identifier, metadata)

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _revive_from_config(self, identifier, metadata, node_id)
    351       obj = (
    352           self._revive_graph_network(metadata, node_id) or
--> 353           self._revive_layer_from_config(metadata, node_id))
    354 
    355     if obj is None:

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _revive_layer_from_config(self, metadata, node_id)
    406     try:
    407       obj = layers_module.deserialize(
--> 408           generic_utils.serialize_keras_class_and_config(class_name, config))
    409     except ValueError:
    410       return None

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
    107       module_objects=globs,
    108       custom_objects=custom_objects,
--> 109       printable_module_name='layer')

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    373                 list(custom_objects.items())))
    374       with CustomObjectScope(custom_objects):
--> 375         return cls.from_config(cls_config)
    376     else:
    377       # Then `cls` may be a function returning a class.

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config)
    653         A layer instance.
    654     """
--> 655     return cls(**config)
    656 
    657   def compute_output_shape(self, input_shape):

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    597         kernel_constraint=constraints.get(kernel_constraint),
    598         bias_constraint=constraints.get(bias_constraint),
--> 599         **kwargs)
    600 
    601 

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, **kwargs)
    123         name=name,
    124         activity_regularizer=regularizers.get(activity_regularizer),
--> 125         **kwargs)
    126     self.rank = rank
    127     if filters is not None and not isinstance(filters, int):

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    454     self._self_setattr_tracking = False  # pylint: disable=protected-access
    455     try:
--> 456       result = method(self, *args, **kwargs)
    457     finally:
    458       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
    292     }
    293     # Validate optional keyword arguments.
--> 294     generic_utils.validate_kwargs(kwargs, allowed_kwargs)
    295 
    296     # Mutable properties

~/miniconda3/envs/mdcnn/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
    790   for kwarg in kwargs:
    791     if kwarg not in allowed_kwargs:
--> 792       raise TypeError(error_message, kwarg)
    793 
    794 

TypeError: ('Keyword argument not understood:', 'groups')

I investigated a little and turns out that this is probably caused by a mismatch in TensorFlow or Keras versions with the environment that was used to save the model (see here for a related SO question). I had to create a new environment with Python 3.8, installing tensorflow=2.3.0, I was able to load the model.

Duplicate with issue #3