4
votes

I am new to python and am working on an image processing project and built the below model. I get the error posted below the model.

In my research, I found some answers regarding this error:

Merge 2 sequential models in Keras the issue above is of Concatenate 2 models not Concatenate 2 layers

https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models In the keras example, Concatenate 2 layers but 1 layer is an input layer. I would like to understand how to Concatenate 2 regular layers and use the concatenated layer as a layer in a sequence of layers. similar concept to the Inception Model.

   input
  /  |  \
a1   b1  c1
|    |    | 
a2   b2  c2
\   |   / 
concatenate
 /  |   \
d1   e1  f1
|    |    | 
d2   e2  f2
 \   |   /
  output

above is my attempt to draw what my final goal :)

def fet_Model():

bnd_input = Input(shape=input_shape)

k31 = Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same')(bnd_input)
k31 = Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same')(k31)
k31 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(k31)

in_ly1_cv_1n1 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(k31)
in_ly1_cv_3n3 = Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same')(in_ly1_cv_1n1)
in_ly1_cv_5n5 = Conv2D(64, kernel_size=(5, 5), activation='relu', padding='same')(in_ly1_cv_1n1)

in_ly1_mx_pl = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), padding='same')(k31)
in_ly1_mxcv_1n1 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(in_ly1_mx_pl)

filt_concat1 = Concatenate([in_ly1_mxcv_1n1, in_ly1_cv_5n5, in_ly1_cv_3n3, k31])

# when running the below line I receive the error
in_ly2_cv_1n1 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(filt_concat1)
in_ly2_cv_3n3 = Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same')(in_ly2_cv_1n1)
in_ly2_cv_5n5 = Conv2D(64, kernel_size=(5, 5), activation='relu', padding='same')(in_ly2_cv_1n1)

in_ly2_mx_pl = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(filt_concat1)
in_ly2_mxcv_1n1 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(in_ly2_mx_pl)

filt_concat2 = Concatenate([in_ly2_mxcv_1n1, in_ly2_cv_5n5, in_ly2_cv_3n3, k31])

lst_ly_avg_pl = AveragePooling2D(pool_size=(3, 3), strides=(2, 2))(filt_concat2)

lst_ly_cnv2 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(lst_ly_avg_pl)

lst_ly_den = Dense(1, activation='sigmoid')(lst_ly_cnv2)

model = Model(inputs=bnd_input, outputs=lst_ly_den)

optimizer = Adam(lr= 0.002, beta_1=0.99, beta_2=0.999, epsilon=1e-08, decay=0.01)

model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

return model

Error:

Traceback (most recent call last):
   K.is_keras_tensor(x)
   raise ValueError('Unexpectedly found an instance of type `' + 
str(type(x)) + '`. '
ValueError: Unexpectedly found an instance of type `<class 'keras.layers.merge.Concatenate'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-194-ebff784b774c>", line 1, in <module>
   in_ly2_cv_1n1 = Conv2D(32, kernel_size=(1, 1), activation='relu', padding='same')(filt_concat1)
   self.assert_input_compatibility(inputs)
   str(inputs) + '. All inputs to the layer '
ValueError: Layer conv2d_41 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.merge.Concatenate'>. Full input: [<keras.layers.merge.Concatenate object at 0x1cee082b0>]. All inputs to the layer should be tensors.
3

3 Answers

3
votes

I think you need Concatenate(axis=-1)([tensor_1, tensor_2]).

2
votes

Further explanation to @thepartofspeech answers (https://stackoverflow.com/a/51624786/8096768).

From keras' documentation on the Concatenate layer (https://keras.io/layers/merge/#concatenate)


keras.layers.Concatenate(axis=-1)

Layer that concatenates a list of inputs.

It takes as input a list of tensors, all of the same shape except for the concatenation axis, and returns a single tensor, the concatenation of all inputs.


The layer should, in this case, be called with the axis=-1 option directly on the Concatenate layer, followed by the input tensors, e.g.

filt_concat1 = Concatenate(axis=-1)([in_ly1_mxcv_1n1, in_ly1_cv_5n5, in_ly1_cv_3n3, k31])

Note: I have not tried this with more than 2 input tensors - but in the case of 2 inputs, it works.

I hope this helps.

0
votes

I can not reconstruct your problem. Your code works fine for me. try to update keras, and run again. If you use anaconda, do:

conda update --all
conda -n root update conda