tensorflow: tf.keras.models.load_model fails on Sequential model

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.04
  • TensorFlow installed from (source or binary): Binary
  • TensorFlow version (use command below): tf-nightly-gpu-2.0-preview==2.0.0-dev20190718
  • Python version: 3.7

Describe the current behavior Attempt to run tf.keras.models.load_model on a tf.keras.Sequential with lazily generated inputs produces an error.

Describe the expected behavior No error is thrown when running the script below. model2 is a clone of model.

Code to reproduce the issue

import tempfile

import tensorflow as tf


def main():
    batch_size = 3

    image_shape = (32, 32, 3)
    inputs = tf.random.uniform((batch_size, *image_shape))

    model = tf.keras.Sequential((
        tf.keras.layers.Conv2D(
            filters=16,
            kernel_size=3,
            strides=2,
            padding='SAME',
            activation='linear'),
    ))

    _ = model(inputs)

    with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
        tf.keras.models.save_model(model, fd.name, overwrite=True)
        model2 = tf.keras.models.load_model(fd.name, compile=False)

    print(model2.summary())


if __name__ == '__main__':
    main()

Other info / logs Running the script above produces the following error:

Traceback (most recent call last):
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/kristian/github/hartikainen/softlearning-2/tests/test_sequential_serialize.py", line 31, in <module>
    main()
  File "/home/kristian/github/hartikainen/softlearning-2/tests/test_sequential_serialize.py", line 25, in main
    model2 = tf.keras.models.load_model(fd.name, compile=False)
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 138, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 165, in load_model_from_hdf5
    load_weights_from_hdf5_group(f['model_weights'], model.layers)
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 671, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 1 layers into a model with 0 layers.
Exception ignored in: <function _TensorCacheDeleter.__del__ at 0x7f9fabdde158>
Traceback (most recent call last):
  File "/home/kristian/conda/envs/softlearning-2/lib/python3.7/site-packages/tensorflow_core/python/eager/context.py", line 316, in __del__
TypeError: argument of type 'NoneType' is not iterable

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Neat, this looks good in the colab, so I’ll close it. Thanks a lot @jvishnuvardhan!

Hey @jvishnuvardhan. I don’t think your comment resolves the issue. I’m aware that explicitly specifying the input_shape makes the example work, but my issue was specific to the case where we feed in an input to the Sequential model only after it’s initialization, and thus the input shape is inferred later on.

If you decide that this is a TensorFlow feature and not a bug, then I’m fine with closing the issue. Still, personally I think it’s a bug and tensorflow should support my example’s way of model saving/loading since it has all the information needed to do that.

Unfortunately, I’m not able to reopen the issue, so in case you think this is still a bug, could you do it?

Also, @tensorflow-bot’s links don’t work (note that I haven’t answered to the link in this issue yet): image