通道数增加时 ResNet 快捷连接的零填充
Zero-padding for ResNet shortcut connections when channel number increase
我想在 Keras 中实现 ResNet 网络,根据原始论文,当 features/channels 维度不匹配时,使用添加零条目的快捷方式连接:
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...
如果您仍在我的 GitHub 存储库中寻找它,我已经实现了它。请看https://github.com/nellopai/deepLearningModels。
我在网上找到的所有解决方案都没有真正起作用,并且与 ResNet 论文不一致。在回购协议中,您可以在 code/networks/resNet50 中找到更多详细信息。正确的实现方法是:
def pad_depth(x, desired_channels):
new_channels = desired_channels - x.shape.as_list()[-1]
output = tf.identity(x)
repetitions = new_channels/x.shape.as_list()[-1]
for _ in range(int(repetitions)):
zeroTensors = tf.zeros_like(x, name='pad_depth1')
output = tf.keras.backend.concatenate([output, zeroTensors])
return output
这对我有用,即使在惰性(非急切)评估模式下,不需要需要访问另一个张量正确的填充(例如 zeros_like
)。 D
是所需的通道数,nn
是我们要填充的张量。
def pad_depth(nn,D):
import tensorflow as tf
deltaD= D- nn.shape[-1]
paddings= [[0,0]]* len(nn.shape.as_list())
paddings[-1]= [0,deltaD]
nn= tf.pad(nn,paddings)
return nn
我想在 Keras 中实现 ResNet 网络,根据原始论文,当 features/channels 维度不匹配时,使用添加零条目的快捷方式连接:
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...
如果您仍在我的 GitHub 存储库中寻找它,我已经实现了它。请看https://github.com/nellopai/deepLearningModels。 我在网上找到的所有解决方案都没有真正起作用,并且与 ResNet 论文不一致。在回购协议中,您可以在 code/networks/resNet50 中找到更多详细信息。正确的实现方法是:
def pad_depth(x, desired_channels):
new_channels = desired_channels - x.shape.as_list()[-1]
output = tf.identity(x)
repetitions = new_channels/x.shape.as_list()[-1]
for _ in range(int(repetitions)):
zeroTensors = tf.zeros_like(x, name='pad_depth1')
output = tf.keras.backend.concatenate([output, zeroTensors])
return output
这对我有用,即使在惰性(非急切)评估模式下,不需要需要访问另一个张量正确的填充(例如 zeros_like
)。 D
是所需的通道数,nn
是我们要填充的张量。
def pad_depth(nn,D):
import tensorflow as tf
deltaD= D- nn.shape[-1]
paddings= [[0,0]]* len(nn.shape.as_list())
paddings[-1]= [0,deltaD]
nn= tf.pad(nn,paddings)
return nn