Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to deserialize Sequential model from config #37

Open
shkarupa-alex opened this issue Aug 18, 2023 · 2 comments
Open

Unable to deserialize Sequential model from config #37

shkarupa-alex opened this issue Aug 18, 2023 · 2 comments
Assignees
Labels

Comments

@shkarupa-alex
Copy link
Contributor

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): no
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS 13.4.1
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): v2.13.0-rc2-7-g1cb1a030a62 2.13.0
  • Python version: 3.11
  • GPU model and memory: no gpu
  • Exact command to reproduce: see below
  • Do you want to contribute a PR? (yes/no): no

Describe the problem.

Unable to deserialize Sequential model from config.

Describe the current behavior.

Got exception during deserialization.

Describe the expected behavior.

As far as Sequential model is a subtype of regular Model it should be sirializable and deserializable

Standalone code to reproduce the issue.

import tensorflow as tf
from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Conv2D(32, 3, padding='same', name='conv'),
    layers.BatchNormalization(name='bn'),   
])
_ = model(tf.zeros([1, 16, 16, 3]))  # aka "build"

model2 = models.Model.from_config(model.get_config())

Source code / logs.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/.pyenv/versions/3.11.2/lib/python3.11/site-packages/keras/src/engine/training.py:3244, in Model.from_config(cls, config, custom_objects)
   3243 try:
-> 3244     model = cls(**config)
   3245 except TypeError as e:

File ~/.pyenv/versions/3.11.2/lib/python3.11/site-packages/tensorflow/python/trackable/base.py:204, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
    203 try:
--> 204   result = method(self, *args, **kwargs)
    205 finally:

File ~/.pyenv/versions/3.11.2/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:

File ~/.pyenv/versions/3.11.2/lib/python3.11/site-packages/keras/src/utils/generic_utils.py:514, in validate_kwargs(kwargs, allowed_kwargs, error_message)
    513 if kwarg not in allowed_kwargs:
--> 514     raise TypeError(error_message, kwarg)

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

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[3], line 13
      9 _ = model(tf.zeros([1, 16, 16, 3]))
     11 # print([w.name for w in model.weights])
---> 13 model2 = models.Model.from_config(model.get_config())
     14 # [w.name for w in model2.weights]

File ~/.pyenv/versions/3.11.2/lib/python3.11/site-packages/keras/src/engine/training.py:3246, in Model.from_config(cls, config, custom_objects)
   3244         model = cls(**config)
   3245     except TypeError as e:
-> 3246         raise TypeError(
   3247             "Unable to revive model from config. When overriding "
   3248             "the `get_config()` method, make sure that the "
   3249             "returned config contains all items used as arguments "
   3250             f"in the  constructor to {cls}, "
   3251             "which is the default behavior. "
   3252             "You can override this default behavior by defining a "
   3253             "`from_config(cls, config)` class method to specify "
   3254             "how to create an "
   3255             f"instance of {cls.__name__} from its config.\n\n"
   3256             f"Received config={config}\n\n"
   3257             f"Error encountered during deserialization: {e}"
   3258         )
   3259 return model

TypeError: Unable to revive model from config. When overriding the `get_config()` method, make sure that the returned config contains all items used as arguments in the  constructor to <class 'keras.src.engine.training.Model'>, which is the default behavior. You can override this default behavior by defining a `from_config(cls, config)` class method to specify how to create an instance of Model from its config.

Received config={'name': 'sequential_1', 'layers': [{'module': 'keras.layers', 'class_name': 'InputLayer', 'config': {'batch_input_shape': (1, 16, 16, 3), 'dtype': 'float32', 'sparse': False, 'ragged': False, 'name': 'conv_input'}, 'registered_name': None}, {'module': 'keras.layers', 'class_name': 'Conv2D', 'config': {'name': 'conv', 'trainable': True, 'dtype': 'float32', 'filters': 32, 'kernel_size': (3, 3), 'strides': (1, 1), 'padding': 'same', 'data_format': 'channels_last', 'dilation_rate': (1, 1), 'groups': 1, 'activation': 'linear', 'use_bias': True, 'kernel_initializer': {'module': 'keras.initializers', 'class_name': 'GlorotUniform', 'config': {'seed': None}, 'registered_name': None}, 'bias_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': (1, 16, 16, 3)}}, {'module': 'keras.layers', 'class_name': 'BatchNormalization', 'config': {'name': 'bn', 'trainable': True, 'dtype': 'float32', 'axis': [3], 'momentum': 0.99, 'epsilon': 0.001, 'center': True, 'scale': True, 'beta_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'gamma_initializer': {'module': 'keras.initializers', 'class_name': 'Ones', 'config': {}, 'registered_name': None}, 'moving_mean_initializer': {'module': 'keras.initializers', 'class_name': 'Zeros', 'config': {}, 'registered_name': None}, 'moving_variance_initializer': {'module': 'keras.initializers', 'class_name': 'Ones', 'config': {}, 'registered_name': None}, 'beta_regularizer': None, 'gamma_regularizer': None, 'beta_constraint': None, 'gamma_constraint': None}, 'registered_name': None, 'build_config': {'input_shape': (1, 16, 16, 32)}}]}

Error encountered during deserialization: ('Keyword argument not understood:', 'layers')
@tilakrayal
Copy link
Collaborator

@shkarupa-alex,
Could you please confirm whether you are facing the similar issue which was suggested in another issue.
#414
If it is the same, the issue is assigned to developer and the team is working on the same. Thank you!

@shkarupa-alex
Copy link
Contributor Author

@tilakrayal , i'm not sure if these issues related.
There is no any subclassing in my case and code example.

@sachinprasadhs sachinprasadhs transferred this issue from keras-team/keras Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants