主要是两个地方需要注意:第一个maxpool与stage1的衔接处,stage1与stage2的衔接处。也就是说上图中的双线叠加处需要注意。
bottlenck的代码:
class Bottleneck(nn.Module):
#Bottleneck是指下图中右侧图,其用于resnet50等深度较深的网络中,可减少参数,左图用于34等较浅的网络
expansion = 4
#此处的expansion是指在每个小残差块内,减小尺度增加维度过程,维度加深的倍数。如64*4=256
#inplanes是指此上个模块的输出通道数,planes是当前模块的输入通道数,是为了衔接时使用。
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(Bottleneck, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes * (base_width / 64.)) * groups
# Both self.conv2 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv1x1(inplanes, width)
self.bn1 = norm_layer(width)
self.conv2 = conv3x3(width, width, stride, groups, dilation)
self.bn2 = norm_layer(width)
self.conv3 = conv1x1(width, planes * self.expansion)
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride#将stride保存下来作为其判断改变特征图尺寸的依据
def forward(self, x):
identity = x
out = self.conv1(x)
out = self