ValueError:输入不是符号张量

时间:2018-01-11 19:17:16

标签: python keras-layer

我是python的新手,正在开发一个图像处理项目,并构建了以下模型。我在模型下面发布了错误。

在我的研究中,我找到了一些关于这个错误的答案:

Merge 2 sequential models in Keras 上面的问题是Concatenate 2模型而不是Concatenate 2层

https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models 在keras示例中,Concatenate 2层但是1层是输入层。我想了解如何连接2个常规图层并使用连接图层作为图层序列中的图层。与初始模型类似的概念。

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

以上是我试图绘制我的最终目标:)

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

错误:

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 个答案:

答案 0 :(得分:2)

对@thepartofspeech答案(https://stackoverflow.com/a/51624786/8096768)的进一步解释。

摘自Concatenate层(https://keras.io/layers/merge/#concatenate)的keras文档


keras.layers.Concatenate(axis=-1)

连接输入列表的层。

它将一系列张量的张量作为输入作为输入,除了级联轴外,其余所有形状都相同,并返回单个张量,即所有输入的级联。


在这种情况下,应直接在Concatenate层上使用axis=-1选项调用该层,然后再输入tensor,例如

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

注意:我没有尝试使用超过2个输入张量的方法-但是在2个输入的情况下,它可以工作。

我希望这会有所帮助。

答案 1 :(得分:1)

我认为您需要Concatenate(axis=-1)([tensor_1, tensor_2])

答案 2 :(得分:0)

我无法重建你的问题。你的代码对我来说很好。 尝试更新keras,然后再次运行。 如果你使用anaconda,请执行:

conda update --all
conda -n root update conda