无法在keras中串联两个输入行。

时间:2018-09-08 09:17:44

标签: python tensorflow keras resnet

我正在尝试以下代码:

import tensorflow as tf 

from keras.layers import Input, Dense
from keras.models import Model, Sequential
from keras.layers import Conv2D, Concatenate
from keras.utils.vis_utils import plot_model

if __name__ == '__main__':
    imgRows = imgCols = 28
    print ("ImgRow and imgCols " , imgRows, imgCols)
    inputLayer = Input(shape=( 1,28,28))

    conv1 = Conv2D(64,(3,3),strides=1, padding="same", activation='relu') (inputLayer)

    #Residual 1 
    skip = Conv2D(128, (1,1), strides=1, padding="same", activation='relu') (conv1)
    conv1 = Conv2D(128, (3,3), strides=3, padding="same", activation='relu') (skip)
    conv1 = Conv2D(128, (3,3), strides=3, padding="same", activation='relu') (conv1)
    r1= Concatenate([skip, conv1])


    #residual 2 
    conv1 = Conv2D(128, (3,3), strides=3, padding="same", activation='relu') (r1)
    conv1 = Conv2D(128, (3,3), strides=3, padding="same", activation='relu') (conv1)

    conv1= Concatenate([r1, conv1])

    # Residual 3 
    skip = Conv2D(256, (1,1), strides=1, padding="same", activation='relu') (conv1)
    conv1 = Conv2D(256, (3,3), strides=3, padding="same", activation='relu') (conv1)
    conv1 = Conv2D(256, (3,3), strides=3, padding="same", activation='relu') (conv1)
    conv1= Concatenate([skip, conv1])
    out =  Conv2D(1, (1,1), strides=1, padding="same", activation='sigmoid') (conv1)



    #model =  Sequential()
    #model.add (inputLayer)
    #model.add ( conv1)

    model = Model(input=inputLayer, output=conv1)

    model.compile(optimizer=Nadam(lr=1e-5), loss="mean_square_error")

    plot_model (model, to_file="./keestu_model.png", show_shapes=True)

我收到以下错误:

错误消息是:

ValueError: Layer conv2d_5 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 0x7fd543841590>]. 
       All inputs to the layer should be tensors.

问题?

错误消息对我来说很清楚,第5层希望将其输入作为张量对象而不是连接对象。但是我该如何解决?

1 个答案:

答案 0 :(得分:2)

这是因为Concatenate是具有两个API版本的图层类:

  • Concatenate()([tensor1, tensor2])创建一个串联的新实例,并将其应用于给定的张量。这是标准的功能API样式。
  • concatenate([tensor1, tensor2])将实现相同的功能,但会为您创建一个隐式实例。从documentation
      

    keras.layers.concatenate(inputs,axis = -1):连接层的功能接口。

为方便起见,所有merge layers都具有此双重接口。