Repository: keras-team/keras

Issue:
load_model: ValueError('Missing layer: ' + inbound_layer_name)
I accidentally managed to create a model that can be saved but not loaded.

Here is a minimal example to reproduce the problem:

```python
from keras.models import Model, load_model
from keras.layers import Input, Dense, concatenate

input_shape = (16, 9, 3)
input_layer = Input(shape=input_shape)

dense_A = Dense(3, name='dense_A')
dense_B = Dense(3, name='dense_B')
dense_C = Dense(3, name='dense_C')

x1 = dense_B(dense_A(input_layer))
x2 = dense_A(dense_C(input_layer))
x = concatenate([x1, x2])

model = Model(inputs=input_layer, outputs=x)

model.save('model.h5')
model = load_model('model.h5')
```

Output:

```
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    model = load_model('model.h5')
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 233, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 307, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 54, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2450, in from_config
    process_layer(layer_data)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2437, in process_layer
    raise ValueError('Missing layer: ' + inbound_layer_name)
ValueError: Missing layer: dense_C
```

The model itself does not make a lot of sence of course, but as far as I understand, everything that can be serialized should also be deserializable. Is this a bug?

Keras version: 2.0.6
Python version: 3.5.2
Tensorflow version: 1.2.1


Discussion:
I am facing the same issue, model not getting loaded after getting saved

Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -2440,11 +2440,11 @@ def process_node(layer, node_data):
                     raise ValueError('Improperly formatted model config.')
                 if inbound_layer_name not in created_layers:
                     add_unprocessed_node(layer, node_data)
-                    continue
+                    return
                 inbound_layer = created_layers[inbound_layer_name]
                 if len(inbound_layer.inbound_nodes) <= inbound_node_index:
                     add_unprocessed_node(layer, node_data)
-                    continue
+                    return
                 inbound_node = inbound_layer.inbound_nodes[inbound_node_index]
                 input_tensors.append(inbound_node.output_tensors[inbound_tensor_index])
             # Call layer on its inputs, thus creating the node


Test patch:
diff --git a/tests/keras/engine/test_topology.py b/tests/keras/engine/test_topology.py
--- a/tests/keras/engine/test_topology.py
+++ b/tests/keras/engine/test_topology.py
@@ -710,6 +710,34 @@ def test_layer_sharing_at_heterogenous_depth():
     np.testing.assert_allclose(output_val, output_val_2, atol=1e-6)
 
 
+@keras_test
+def test_layer_sharing_at_heterogenous_depth_with_concat():
+    input_shape = (16, 9, 3)
+    input_layer = Input(shape=input_shape)
+
+    A = Dense(3, name='dense_A')
+    B = Dense(3, name='dense_B')
+    C = Dense(3, name='dense_C')
+
+    x1 = B(A(input_layer))
+    x2 = A(C(input_layer))
+    output = layers.concatenate([x1, x2])
+
+    M = Model(inputs=input_layer, outputs=output)
+
+    x_val = np.random.random((10, 16, 9, 3))
+    output_val = M.predict(x_val)
+
+    config = M.get_config()
+    weights = M.get_weights()
+
+    M2 = Model.from_config(config)
+    M2.set_weights(weights)
+
+    output_val_2 = M2.predict(x_val)
+    np.testing.assert_allclose(output_val, output_val_2, atol=1e-6)
+
+
 @keras_test
 def test_multi_output_mask():
     """Fixes #7589"""