Issue 1:
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")

Issue 2:
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 Debian Stable

Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A

TensorFlow installed from (source or binary): binary

TensorFlow version (use command below): CPU, both TF-2.0.0a0 and tf-nightly-2.0-preview-2.0.0.dev20190315

Python version: 3.5.3

Bazel version (if compiling from source): N/A

GCC/Compiler version (if compiling from source): N/A

CUDA/cuDNN version: N/A

GPU model and memory: N/A

Describe the current behavior

When a Sequential Keras model contains InputLayer and it is saved, it cannot be loaded and fails with a message

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

Describe the expected behavior

The model can be loaded correctly.

Code to reproduce the issue

import numpy as np
import tensorflow as tf

inputs = np.arange(10)
outputs = 2 * inputs

model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[1]),
tf.keras.layers.Dense(1),
])
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.MeanSquaredError(),
metrics=[tf.keras.metrics.MeanSquaredError()]
)
model.fit(inputs, outputs)
model.save("model.h5")

loaded_model = tf.keras.models.load_model("model.h5")

Other info / logs

The problem is in the fact that Sequential.layers does not return the InputLayer. To quote comments from the method:

def layers(self):
# Historically, `sequential.layers` only returns layers that were added
# via `add`, and omits the auto-generated `InputLayer` that comes at the
# bottom of the stack.
# `Trackable` manages the `_layers` attributes and does filtering
# over it.

The problem is that if InputLayer was added manually with a specified input_shape, then it is an error not to serialize it -- because then the following layers do not know what the input shape is.

Workarounds:

if instead of InputLayer you add input_shape to the Dense layer, it works. However, input dtype cannot be specified in this way and when you pass tf.int32 on input, using an InputLayer is required

in Functional API the tf.keras.layers.Input is serialized in the model (and, funnily, as a InputLayer).

Solutions:

when serializing a Sequential model, all layers need to be serialized (and no filtering of InputLayer performed).