Search code examples
pythonneural-networktheanokeras

Keras error with merge layer


I am trying to build a model to colorize images. I am using the LAB color space. The input to the model is the L channel and the model is to be trained to predict the A and B channels. I want to run the L channel through a few convolutions and then split it off into two other models which independently calculate the A and B channels. At the end I want to merge them back together to get the output.

model = Sequential()
model.add(InputLayer((1, H, W)))

model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
last = Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu')
model.add(last)

a_model = Sequential()
a_model.add(last)
a_model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
a_model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
a_model.add(Convolution2D(1, 3, 3, border_mode = 'same', activation = 'sigmoid'))

b_model = Sequential()
b_model.add(last)
b_model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
b_model.add(Convolution2D(64, 5, 5, border_mode = 'same', activation = 'relu'))
b_model.add(Convolution2D(1, 3, 3, border_mode = 'same', activation = 'sigmoid'))

model.add(Merge((a_model, b_model), mode = 'concat'))

I get the following error when I try to create the Merge layer.

Using Theano backend.
Using gpu device 0: GeForce GTX TITAN (CNMeM is disabled, cuDNN 5004)
Traceback (most recent call last):
  File "/home/chase/workspace/Colorizer/colorizer2.py", line 79, in <module>
    model.add(Merge((a_model, b_model), mode = 'concat'))
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 1118, in __init__
    self.add_inbound_node(layers, node_indices, tensor_indices)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 524, in add_inbound_node
    assert len(node_indices) == len(inbound_layers)
AssertionError

I want the outut of the model to be (2, H, W) where H and W are the image height and width.


Solution

  • The Sequential model doesn't allow you to create forks in the network. Use the functional API (new in Keras 1.0) instead. You can follow this tutorial.