【问题标题】:Residual Network : Operands could not be broadcast together with shapes (128, 128, 16) (126, 126, 16)残差网络:操作数无法与形状一起广播 (128, 128, 16) (126, 126, 16)
【发布时间】:2020-07-08 21:57:17
【问题描述】:

我正在尝试根据 paper 在 Keras 中编写 ResNet-12 代码。 但是我在第8层有一个错误,在我下面的代码中,problelem在函数Layer_Type3中。

我看不出问题出在哪里,有人可以帮忙吗?提前致谢。

错误是:

ValueError:操作数无法与形状一起广播(128, 128, 16) (126, 126, 16)

def Layer_Type1(n_output):
    # n_output: number of feature maps in the block
    # upscale: should we use the 1x1 conv2d mapping for shortcut or not

    # keras functional api: return the function of type
    # Tensor -> Tensor
    def f(x):

        # convolution
        h = Conv2D(kernel_size=3, filters=n_output,  strides=1, padding='SAME',kernel_regularizer=regularizers.l2(0.01))(x)

        # second pre-activation
        h = BatchNormalization()(h)
        h = Activation(relu)(h)

        return h

    return f


def Layer_Type2(n_output):
    # n_output: number of feature maps in the block
    # upscale: should we use the 1x1 conv2d mapping for shortcut or not

    # keras functional api: return the function of type
    # Tensor -> Tensor
    def f(x):


        # first convolution
        h = Layer_Type1(n_output)(x)

        # second convolution
        h = Conv2D(kernel_size=3, filters=n_output , strides=1, padding='SAME',kernel_regularizer=regularizers.l2(0.01))(h)

        # second pre-activation
        h = BatchNormalization()(h)


        # F_l(x) = f(x) + H_l(x):
        return add([x, h])

    return f


def Layer_Type3(n_output):

    def f(x):

        # first convolution
        h = Layer_Type1(n_output)(x)

        # second convolution
        h = Conv2D(kernel_size=3 ,filters=n_output, strides=1,kernel_regularizer=regularizers.l2(0.01))(h)

        # second pre-activation
        h = BatchNormalization()(h)
        h = AveragePooling2D(pool_size=(3,3), strides=2)(h)

        # short cut
        d = Conv2D(kernel_size=1, filters=n_output, strides=2)(x)
        d =BatchNormalization()(d)

        return add([d, h])

    return f


def Layer_Type4(n_output):

    def f(x):

        # first convolution
        h = Layer_Type1(n_output)(x)

        # second convolution
        h = Conv2D(kernel_size=3, filters=n_output, strides=1, kernel_regularizer=regularizers.l2(0.01))(h)

        # second pre-activation
        h = BatchNormalization()(h)
        h = GlobalAveragePooling2D()(h)




        return h

    return f


input_tensor = Input((256,256,1))
## 2 Layers of type1 :
x= Layer_Type1(64)(input_tensor)
x= Layer_Type1(16)(x)

# 5 layers pf type 2:
for i in range(5) :
  x = Layer_Type2(16)(x)

## 4 layers of type 3
x= Layer_Type3(16)(x) #1  # ########## Error here 
x=Layer_Type3(64)(x) #2
x=Layer_Type3(128)(x) #3
x=Layer_Type3(256)(x) # 4

# 1 layer of type 4 :
x=Layer_Type4(512)(x)


x = Dropout(0.2)(x)
# last softmax layer
x = Dense(units=2, kernel_regularizer=regularizers.l2(0.01))(x)
x = Activation(softmax)(x)

model = Model(inputs=input_tensor, outputs=x)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

【问题讨论】:

  • 您需要为图层类型 3 和 4 添加“相同”的填充。

标签: python machine-learning deep-learning broadcast deep-residual-networks


【解决方案1】:

错误消息来自Numpy 库。也许在对 numpy 数组进行操作时会出现,但它们的形状不兼容。从您的代码中,我猜当您尝试添加两个卷积图(特征图)(一个是(126,126,126),另一个是(128,128,1))时会发生这种情况。尝试仔细检查具有填充、池大小和步幅的图层。

【讨论】:

  • 我为平均池添加了 paddig,它非常完美。谢谢
猜你喜欢
  • 2019-09-01
  • 2020-05-22
  • 2021-09-29
  • 2020-05-23
  • 2015-05-18
  • 2017-09-09
  • 2020-09-06
  • 2012-10-31
  • 2013-04-07
相关资源
最近更新 更多