ResNet pytorch 源码解读
当下许多CV模型的backbone都采用resnet网络,而pytorch很方便的将resnet以对象的形式为广大使用者编写完成。但是想要真正参透resnet的结构,只会用还是不够的,因此在这篇文章里我会以经过我的查找和我个人的理解对源码进行解读。
简要
- 提供的resnet的类型。因为个人使用情况,主要解读常见的resnet和wide _resnet
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
'wide_resnet50_2', 'wide_resnet101_2']
- 因为resnet所有的卷积只包含3x3 1x1,因此先定义好卷积函数
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
这里面空洞卷积dilation暂时先不管。
- 先看一下resnet结构,除了最开始的一组卷积,剩下的所有卷积都是这种残差结构,因此在实现上首先定义了 BasicBlock(18 34) Bottleneck(50 101 152)
- 再看一下各个resnet结构的汇总图,可以看到18 34是很普通的结构,50 101 152用了bottleneck结构(是一种用来减少参数量的结构策略)
基础模块
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(BasicBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64: # base_width 是一个基通道数
raise ValueError('BasicBlock only supports groups=1 and base_width=64')
if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = norm_layer(planes)
self.downsample = downsample # 用来处理resnet结构相加 维度不同的情况
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)