赶着放假,实验室人少了,不过还是得抓紧学习啊,毕竟对象找不到,那工作就是第二件大事啦
ResNet的重要性应该是不言而喻:随着网络深度的增加,网络开始出现退化现象,即深层网络的性能还不及浅层网络(注意:这既不是梯度消失/爆炸,也不是过拟合),鉴于此,文章设计了一种使用shortcut / skip connection 的残差结构使网络达到很深的层次,同时提升了性能。
复习就到此了,接下来一起探讨源码解析吧!
由于最近在看Triplet Loss代码,所以代码都是Triplet里的,不过都一样啊!
首先,咱们要知道,resnet中的卷积核不是1x1就是3x3,所以咱们得首先定义这两个卷积核的操作吧:
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
这就完事了!不过咱们要理清一个事,定义在class之外的def是fuction,相当于把这家伙给外包了;定义在class里面的def叫method.
关键点来了,你们的BasicBlock & BolleNeck是不是都是这样写的?
BasicBlock
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
看到代码,咱们来逐行解析。expansion=1——有两个意思,首先expansion是区分BasicBlock和BottleBlock的关键;其次expansion=1意味着输出维度与输入维度是一致的;def _ _init_ _()——该行代码中inplanes表示输入通道(input channel),pleans表示输出通道(output channel);self.conv1 = 3x3——这一步中,stride=2;self.conv2 = conv3x3——这一步中,stride默认等于1;
为什么要这样做呢?根据公式: 可知,卷积后的特征图的尺寸跟步长有很大关系,所以这么做无非是对图像进行维度变化。(padding默认为1)
self.downsample = downsample——默认情况downsample=None,表示不做下采样,但当输入维度x和output的通道数不一样,则需要下采样来匹配维度,这样才能相加啊。
BottleNeck
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
看到expansion=4了没?这印证了上诉expansion的第一点,此外,expansion=4还表示输出维度是输入维度的4倍(没忘吧?)
附上BasicBlock & BottleNeck 网络结构:
小结一下:
1.BasicBlock的expansion为1,即输入和输出的通道数是一致的。而Bottleneck的expansion为4,即输出通道数是输入通道数的4倍
2.不管是BasicBlock还是Bottleneck,最后都会做一个判断是否需要给x做downsample,因为必须让输入维度与输出维度一致,才能相加。
在解析ResNet结构之前,我们先要对ResNet有个基本的了解。源码中的ResNet类可以根据输入参数的不同,变成resnet18,34,50,101等。而我们最常用的是ResNet-50,其BottleNeck结构个数为3,4,6,3(论文链接:https://arxiv.org/abs/1512.03385)
首先,Pytorch是怎么调用ResNet的呢?
class Model(nn.Module):
def __init__(self, last_conv_stride=2):
super(Model, self).__init__()
self.base = resnet50(pretrained=True, last_conv_stride=last_conv_stride)
可以看到,resnet50(pretrained=True)可以看出,调用的是resnet50网络。进入到ResNet-50,
def resnet50(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(remove_fc(model_zoo.load_url(model_urls['resnet50'])))
return model
可以看到,ResNet-50调用了ResNet本体的参数,即这行代码:model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
第一个参数BottleNeck没什么好说的,调用我们的“瓶颈结构”;第二个参数【3,4,6,3】,是BottleNeck结构个数,怎么理解呢,看resnet源代码:
class ResNet(nn.Module):
def __init__(self, block, layers, last_conv_stride=2):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(
block, 512, layers[3], stride=last_conv_stride)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1): )
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x
怎么理解这段代码呢?咱们要学会从后往前看。从ResNet的forward代码来看,它是先经过conv1(),bn1,relu和maxpool()。如果你对ResNet网络结构清楚,可以知道,无论是resnet18,resnet34,resnet50还是resnet101等等的resnet一开始都必须经过这几层。这是静态的。而后面的layer1,layer2,layer3,layer4是动态的以区别是resnet18,resnet34,resnet50还是resnet101等等
接下来,我们开始逐行解析代码,为了方便,我们直接在代码上注释(请忽略我的chinglish)
class ResNet(nn.Module):
def __init__(self, block, layers, last_conv_stride=2): # block:i.e.BasicBlock or Bottleneck; # layers:corresponds to [3,4,6,3] in resnet50
self.inplanes = 64 # inplanes :'inplanes' represents the number of input channel
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0]) # 64:make_layer(planes=64); # layers[0]:3 ge Bottlenecks
self.layer2 = self._make_layer(block, 128, layers[1], stride=2) # 128:make_layer(planes=128); # layers[1]:4 ge Bottlenecks
self.layer3 = self._make_layer(block, 256, layers[2], stride=2) # 256:make_layer(planes=256); # layers[2]:6 ge Bottlenecks
self.layer4 = self._make_layer(
block, 512, layers[3], stride=last_conv_stride) # 512:make_layer(planes=512); # layers[3]:3 ge Bottlenecks
for m in self.modules(): # parameters initialization
if isinstance(m, nn.Conv2d): # isinstance():judge the type of paramenter 'm'
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels # number of points in convolution kernel
m.weight.data.normal_(0, math.sqrt(2. / n)) # means:0 std:sqrt(2./n)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1): # Note: the 'planes' here is not the number of output channels,but the number of referance channel(ji zhun tong dao)
downsample = None # block:means choice Bottleneck or BasicBlock # blocks:means [3,4,6,3]
if stride != 1 or self.inplanes != planes * block.expansion: # planes * expansion = the number of output channels
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
) # downsample:connects the different dimension between input and output dimension in Bottleneck
layers = []
layers.append(block(self.inplanes, planes, stride, downsample)) # stride=2,it will reduce the feature map size by a factor of two
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes)) # in this step,the stride=1 by default,so that no reduction the feature map size
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x
_make_layer方法中比较重要的两行代码是:
1、layers.append(block(self.inplanes, planes, stride, downsample)),该部分是将每个blocks的第一个residual结构保存在layers列表中。
2、 for i in range(1, blocks): layers.append(block(self.inplanes, planes)),该部分是将每个blocks的剩下residual 结构保存在layers列表中,这样就完成了一个blocks的构造。
参考博客:https://blog.csdn.net/u014453898/article/details/97115891https://blog.csdn.net/u014453898/article/details/97115891https://blog.csdn.net/u014453898/article/details/97115891