【问题标题】:Type error for merge() function in Keras for 2d convolutional layerKeras 中用于 2d 卷积层的 merge() 函数的类型错误
【发布时间】:2019-04-26 17:59:24
【问题描述】:

我正在尝试重新创建 Inception 模型版本 4。但我想在我的图像数据集标准形状 (224,224,3) 上对其进行训练,所以我没有接受任何预训练的权重。 但是我遇到了这样的错误。

x = merge([x1, x2], mode='concat', concat_axis=channel_axis)
TypeError: 'module' object is not callable

代码如下:

def inception_stem(input):
    if K.image_dim_ordering() == "th":
        channel_axis = 1
    else:
        channel_axis = -1

    # Input Shape is 299 x 299 x 3 (th) or 3 x 299 x 299 (th)
    x = conv_block(input, 32, 3, 3, subsample=(2, 2), border_mode='valid')
    x = conv_block(x, 32, 3, 3, border_mode='valid')
    x = conv_block(x, 64, 3, 3)

    x1 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(x)
    x2 = conv_block(x, 96, 3, 3, subsample=(2, 2), border_mode='valid')
    x = tf.concat([x1,x2],axis=channel_axis)
    #x = merge([x1, x2], mode='concat', concat_axis=channel_axis) #here is the error occuring try find out the reason behind it

    x1 = conv_block(x, 64, 1, 1)
    x1 = conv_block(x1, 96, 3, 3, border_mode='valid')

    x2 = conv_block(x, 64, 1, 1)
    x2 = conv_block(x2, 64, 1, 7)
    x2 = conv_block(x2, 64, 7, 1)
    x2 = conv_block(x2, 96, 3, 3, border_mode='valid')

    x = merge([x1, x2], mode='concat', concat_axis=channel_axis)

    x1 = conv_block(x, 192, 3, 3, subsample=(2, 2), border_mode='valid')
    x2 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(x)

    x = merge([x1, x2], mode='concat', concat_axis=channel_axis)
    return x

我正在使用python 3.6keras 2.2.2tensorflow-gpu 1.9.0

我在 GitHub 上关注了这个问题,但答案并不明确和准确。 谁能找到解决办法。

【问题讨论】:

    标签: python-3.x tensorflow image-processing keras conv-neural-network


    【解决方案1】:

    使用连接层,应该对你有帮助

    from tensorflow.python.keras.layers import concatenate
    x = concatenate([x1, x2], axis=channel_axis)
    return x
    

    【讨论】:

    • 我试过这个,但在使用连接后我得到了这样的错误。方法。'得到输入形状:%s' % (input_shape)) ValueError:Concatenate 层需要具有匹配形状的输入,但 concat 轴除外。得到输入形状:[(None, 74, 74, 64), (None, 75, 75, 96)]
    • 是的,你需要强制在所有轴上都有相同维度的张量,但对于 concat 轴。在您的情况下,它应该是 [(None, 74, 74, 64), (None, 74, 74, 96)] 或 [(None, 75, 75, 64), (None, 75, 75, 96)] .我建议你检查之前图层的形状
    • 在传统的 inception 模型 v4 中,它们没有使用填充,所以当我连接两层时,它将具有不同的尺寸。所以我使用相同的填充来克服这个问题。有没有办法我可以在不使用填充的情况下连接具有两种不同形状的图层。
    • 您可以添加填充以使它们具有相同的形状,但连接层永远不会在连接轴以外的轴上使用不同的形状
    • 感谢您的见解。我会将此视为答案并关闭此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多