上篇博客主要是阅读论文《Deep Residual Learning for Image Recognition》时,按照自己简单理解做的笔记,本文主要根据自己的理解,先对论文中的部分代码从总体结构上进行简单分析,很多具体细节暂时还未完全明白。官方代码链接如下:https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py
1、model_urls
model_urls = { #这里保存的地址里是不同网络对应的预训练权重
'resnet18': 'https://download.pytorch.org/models/resnet18-f37072fd.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-b627a593.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-0676ba61.pth',
}
2、基本卷积结构
def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation) #使用nn网络中的2维卷积封装成resnet中常用到的3x3卷积结构
def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d: #封装成1x1卷积结构
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
3、残差网络结构
残差结构由BasicBlock类和Bottleneck类构成,两个类都继承自nn.Module类,并且都重写了类中的init()方法和forward()方法,这两个类实现的都是残差网络部分,区别在于其中包含的卷积层数不同,因此也适用于不同类型的resnet网络。
BasicBlock主要用来构建resnet18和resnet34网络,里面只包含两个卷积层,使用的都是前面已经封装好的conv3x3的卷积结构,具体实现如下:
class BasicBlock(nn.Module):
expansion: int = 1
def __init__(
self,
inplanes: int, #输入通道数
planes: int, #输出通道数
stride: int = 1, #步长
downsample: Optional[nn.Module] = None,
groups: int = 1,
base_width: int = 64,
dilation: int = 1,
norm_layer: Optional[Callable[..., nn.Module]] = None
) -> None:
super(BasicBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
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) #第一层使用3x3卷积
self.bn1 = norm_layer(planes) #归一化
self.relu = nn.ReLU(inplace=True) #激活函数
self.conv2 = conv3x3(planes, planes) #第二层使用的也是3x3卷积
self.bn2 = norm_layer(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x: Tensor) -> Tensor: #前向传播函数
identity = x #保存输入值
out = self.conv1(x) #第一层卷积
out = self.bn1(out) #BN层
out = self.relu(out) #relu激活函数
out = self.conv2(out) #第二层卷积
out = self.bn2(out) bn
if self.downsample is not None: #当尺寸不匹配时,需要使用下采样方法调整维度使其一致
identity = self.downsample(x)
out += identity #这里与传来的输入值直接相加作为结果输出,即是残差网络的核心。
out = self.relu(out) #再使用一层激活函数
return out
Bottleneck主要用在resnet50及以上的网络结构,它和BasicBlock不同,内部是一个三层残差网块,使用的也是前面封装好的conv1x1,conv3x3,conv1x1的卷积结构,这三层卷积分别用来压缩维度、卷积处理和恢复维度。
class Bottleneck(nn.Module):
expansion: int = 4
def __init__(
self,
inplanes: int,
planes: int,
stride: int = 1,
downsample: Optional[nn.Module] = None,
groups: int = 1,
base_width: int = 64,
dilation: int = 1,
norm_layer: Optional[Callable[..., nn.Module]] = None
) -> 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) #1x1卷积网络
self.bn1 = norm_layer(width) #归一化
self.conv2 = conv3x3(width, width, stride, groups, dilation) #3x3网络
self.bn2 = norm_layer(width)
self.conv3 = conv1x1(width, planes * self.expansion) #1x1网络
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x: Tensor) -> Tensor: #前向传播函数
identity = x #保存输入值
out = self.conv1(x) #1x1卷积层
out = self.bn1(out)
out = self.relu(out) #激活函数
out = self.conv2(out) #3x3卷积层
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out) #1x1卷积层
out = self.bn3(out)
if self.downsample is not None:
identity = self.downsample(x) #维度或通道数不匹配时,调整后再做后续运算
out += identity #加上输入值再作为输出结果
out = self.relu(out) #激活函数
return out
4、resnet类
resnet类也继承pytorch中的基类nn.Module,重写了方法init和forward,在init方法中定义好网络的各个层次,在forward方法中定义前向传播过程。
class ResNet(nn.Module):
def __init__(
self,
block: Type[Union[BasicBlock, Bottleneck]], #表明使用的是哪种类型的残差块,是BasicBlock或者Bottleneck
layers: List[int], #每个残差块的个数,比如resnet34, layers=[3,4,6,3],表示第一层有3个残差块,第二层有4个残差块,第三层有6个残差块,第四层有3个残差块。
) -> None:
super(ResNet, self).__init__()
......
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
bias=False) #第一层卷积网络
self.bn1 = norm_layer(self.inplanes) #BN层
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]) #第一个参数表示采用的block类型(basicblock或者bottleneck),按照通道维度数目(第二个参数)的不同分为四大类不同的层次结构(layer1-layer4),每一层内都有不同的block数目,具体数量由layers[0]-layers[3](第三个参数)表示。
self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) #平均池化下采样
self.fc = nn.Linear(512 * block.expansion, num_classes)
......
......
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential( #当维度不匹配时,调用它实现维度匹配
conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer)) #每一类self.layer的第一层需要downsample,以保证运算的通道数相匹配,所以单独列出来
self.inplanes = planes * block.expansion
for _ in range(1, blocks): #接下来将blocks中剩下的残差结构保存在layers列表中
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)
def _forward_impl(self, x: Tensor) -> Tensor: #前向传播函数
# See note [TorchScript super()]
x = self.conv1(x) #7x7的卷积核,步长为2
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)
x = self.avgpool(x) #平均池化
x = torch.flatten(x, 1)
x = self.fc(x) #全连接层
return x
def forward(self, x: Tensor) -> Tensor:
return self._forward_impl(x)
5、调用resnet类
def _resnet(
arch: str,
block: Type[Union[BasicBlock, Bottleneck]],
layers: List[int],
pretrained: bool,
progress: bool,
**kwargs: Any
) -> ResNet:
model = ResNet(block, layers, **kwargs)
if pretrained:
state_dict = load_state_dict_from_url(model_urls[arch],
progress=progress)
model.load_state_dict(state_dict)
return model
_resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,**kwargs)
_resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress,**kwargs)
_resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,**kwargs)
_resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress,**kwargs)
_resnet('resnet152', Bottleneck, [3, 8, 36, 3], pretrained, progress,**kwargs)
调用resnet类时,第一个参数对应不同层数的网络结构,第二个参数对应残差网络的类型,可以看出,resnet18和resnet34网络使用的是BasicBlock,当网络层数加深时,就需要使用Bottleneck,方便网络模型的存储和计算。第三个参数里的四个元素分别表示不同构建块中的块数,其中resnet34和resnet50的数目完全一样,但二者使用残差网络的类型不同。
参考链接:
https://www.jianshu.com/p/085f4c8256f1
https://www.cnblogs.com/wzyuan/p/9880342.html
https://blog.csdn.net/xz1308579340/article/details/86527971