python - 当 channel 数增加时,ResNet 快捷连接的零填充

标签 python theano keras

我想在 Keras 中实现 ResNet 网络,并根据原始论文,在特征/ channel 尺寸不匹配时添加零条目的快捷连接:

When the dimensions increase (dotted line shortcuts in Fig. 3), we consider two options: (A) The shortcut still performs identity mapping, with extra zero entries padded for increasing dimensions ... http://arxiv.org/pdf/1512.03385v1.pdf

但是无法实现它,而且我似乎无法在网络或源代码上找到答案。我发现的所有实现都使用 1x1 卷积技巧在尺寸不匹配时进行快捷连接。

我想要实现的层基本上会将输入张量与全零张量的张量连接起来,以补偿维度不匹配。

这个想法是这样的,但我无法让它发挥作用:

def zero_pad(x, shape):
    return K.concatenate([x, K.zeros(shape)], axis=1)

有人知道如何实现这样的层吗?

非常感谢

最佳答案

问题已在 github 上得到解答: https://github.com/fchollet/keras/issues/2608

它会是这样的:

from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Lambda
from keras import backend as K


def zeropad(x):
    y = K.zeros_like(x)
    return K.concatenate([x, y], axis=1)


def zeropad_output_shape(input_shape):
    shape = list(input_shape)
    assert len(shape) == 4
    shape[1] *= 2
    return tuple(shape)


def shortcut(input_layer, nb_filters, output_shape, zeros_upsample=True):
    # TODO: Figure out why zeros_upsample doesn't work in Theano
    if zeros_upsample:
        x = MaxPooling2D(pool_size=(1,1),
                             strides=(2,2),
                             border_mode='same')(input_layer)
        x = Lambda(zeropad, output_shape=zeropad_output_shape)(x)
    else:
        # Options B, C in ResNet paper... 

关于python - 当 channel 数增加时,ResNet 快捷连接的零填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031106/

相关文章:

python - 列出和追加元素

python - 如何将数据 append 到存储在 HDFStore 文件中的面板

python - PyTorch:是否可以区分矩阵?

python - Theano: ifelse 类型错误

python - Theano 导入错误-windows 7

python - 适用于 Windows、Python 3、64 位的 Theano 安装

python - 属性错误 : 'Sequential' no attribute 'get_shape' when merging models

tensorflow - 使用 AWS Sagemaker 时,Keras Conv2D 层具有不同的输出

tensorflow - 使用稀疏_分类_交叉熵时如何定义自定义标签值?

c++ - 尝试了解编写 Python/C++ 混合程序的链接过程