ValueError:“连接”层要求输入的形状与连接轴一致,但连接轴除外

时间:2019-01-20 20:27:29

标签: python tensorflow keras

我正在尝试使用Keras功能API实现一种特殊类型的神经网络,如下所示:

enter image description here

但是我在连接层上遇到了问题:

  

ValueError:“连接”层需要具有匹配形状的输入   除了concat轴。得到了输入形状:[[None,160,160,384),   (无,160、160、48)]

通知:根据我的研究,我认为这个问题不是重复的,我见过this questionthis post(谷歌翻译),但他们没有似乎没有用(相反,它们甚至使问题稍微“更糟”)。


以下是concat层之前的神经网络代码:

from keras.layers import Input, Dense, Conv2D, ZeroPadding2D, MaxPooling2D, BatchNormalization, concatenate
from keras.activations import relu
from keras.initializers import RandomUniform, Constant, TruncatedNormal

#  Network 1, Layer 1
screenshot = Input(shape=(1280, 1280, 0), dtype='float32', name='screenshot')
# padded1 = ZeroPadding2D(padding=5, data_format=None)(screenshot)
conv1 = Conv2D(filters=96, kernel_size=11, strides=(4, 4), activation=relu, padding='same')(screenshot)
# conv1 = Conv2D(filters=96, kernel_size=11, strides=(4, 4), activation=relu, padding='same')(padded1)
pooling1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1)
normalized1 = BatchNormalization()(pooling1)  # https://stats.stackexchange.com/questions/145768/importance-of-local-response-normalization-in-cnn

# Network 1, Layer 2

# padded2 = ZeroPadding2D(padding=2, data_format=None)(normalized1)
conv2 = Conv2D(filters=256, kernel_size=5, activation=relu, padding='same')(normalized1)
# conv2 = Conv2D(filters=256, kernel_size=5, activation=relu, padding='same')(padded2)
normalized2 = BatchNormalization()(conv2)
# padded3 = ZeroPadding2D(padding=1, data_format=None)(normalized2)
conv3 = Conv2D(filters=384, kernel_size=3, activation=relu, padding='same',
               kernel_initializer=TruncatedNormal(stddev=0.01),
               bias_initializer=Constant(value=0.1))(normalized2)
# conv3 = Conv2D(filters=384, kernel_size=3, activation=relu, padding='same',
#               kernel_initializer=RandomUniform(stddev=0.1),
#               bias_initializer=Constant(value=0.1))(padded3)

# Network 2, Layer 1

textmaps = Input(shape=(160, 160, 128), dtype='float32', name='textmaps')
txt_conv1 = Conv2D(filters=48, kernel_size=1, activation=relu, padding='same',
                   kernel_initializer=TruncatedNormal(stddev=0.01), bias_initializer=Constant(value=0.1))(textmaps)

# (Network 1 + Network 2), Layer 1

merged = concatenate([conv3, txt_conv1], axis=1)

这就是解释器如何计算变量conv3txt_conv1的方式:

>>> conv3
<tf.Tensor 'conv2d_3/Relu:0' shape=(?, 160, 160, 384) dtype=float32>
>>> txt_conv1
<tf.Tensor 'conv2d_4/Relu:0' shape=(?, 160, 160, 48) dtype=float32>

这是在将txt_conv1设置为conv3之后,解释器如何评估image_data_formatchannels_first变量:

>>> conv3
<tf.Tensor 'conv2d_3/Relu:0' shape=(?, 384, 160, 0) dtype=float32>
>>> txt_conv1
<tf.Tensor 'conv2d_4/Relu:0' shape=(?, 48, 160, 128) dtype=float32>

这两个层的形状在体系结构中都没有实际描述。


有什么办法解决这个问题?也许我没有编写适当的代码(我是Keras的新手)。

PS

我知道上面的代码不是有组织的,我只是在测试。

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该在连接层中将轴更改为-1,因为要连接的两个张量的形状仅在其最后一个维度上不同。然后,所得的张量将为super.configure()