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

System information

  • TensorFlow installed from: pip
  • TensorFlow version: tf-nightly-2.0-preview-2.0.0.dev20190513
  • Python version: 3.6.7

Describe the current behavior

Attempting tf.keras.models.load_model on a Sequential model throws

ValueError: You are trying to load a weight file containing 2 layers into a model with 0 layers.

Might be caused by a layers/_layers mismatch as mentioned here. Not sure if this is the problem but _clone_sequential_model uses model._layers whereas save_model_to_hdf5 accesses model.layers.

Minimal example

import tensorflow as tf

model = tf.keras.Sequential(
    [tf.keras.Input(3), tf.keras.layers.Dense(3), tf.keras.layers.Dense(1)]
)
model.compile(loss="mse", optimizer="adam")
model.fit(tf.constant([[1, 2, 3], [4, 5, 6]]), tf.constant([1, 2]))
model.save("model.h5")
restored_model = tf.keras.models.load_model("model.h5")

About this issue

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

Most upvoted comments

@hartikainen Your example might not be, but the original Minimal example in the issue is related – if the Input is removed and input_shape set for the first Dense layer, the model does load.

So I believe the originally reported problem in this issue is a duplicate of #26809.

Your example is slightly different – your model does not specify input shape in any way, so it is constructed during the first call of the model. Such models however do not save the input shapes to h5 (and from h5 are similar to how a Sequential model is saved when the InputLayer is left out), so when you try to load the weights, the same code path triggers an error. If you

  • add input_shape=image_shape parameter to the Conv2D, the model can be loaded;
  • or if you load the model without loading weights, run it once, and only then load the weights, it can be loaded:
# Instead of the original
model2 = tf.keras.models.load_model(fd.name, compile=False)
# this works; the JSON is also stored in h5 file, but it cannot be easily accessed, so I take it directly from the original model
model2 = tf.keras.models.model_from_json(model.to_json())
model2(inputs)
model2.load_weights(fd.name)

This is fixed in latest TF 2.0 nightly build ‘2.0.0-dev20190718’ Thanks!

Tried running your snippet and was able to reproduce the error on mentioned TensorFlow version (2.0.0-dev20190513).

@jvishnuvardhan hey thanks a lot for the response and I found my error as I was having a problem with the version of TensorFlow and Keras.

@hartikainen Thanks for testing against TF 2.0 Can you please post a new issue for the same? Its better to track this separately. Thanks!

The case in which an Input or InputLayer is an explicit part of model specification (like in the Minimal example of this issue description) it is definitely a bug (because the Input layer with information about input sizes is not saved).

Your case where the model dimensions is defined by first usage is semantically a bit different – but personally I believe it is also a bug.