【Resnet】Resnet代码详解(图+代码)
具体理论就不说了。直接看⽹络结构以及其代码就好。深度学习超级新兵⼀个,记录下⾃⼰的学习过程,有错误欢迎⼤佬指出。万分感谢
1.先上⽹络整体结构。
如下图 分别放出来resnet18 resnet134 resnet50 resnet101 resnet152.⽂字注释应该写明⽩了⼤致的⽹络结构。
图1
2.对图1中的[ ]进⼀步说明
图2
图2左侧对应的代码中的BasicBlock, 具体实现⼀会贴代码。图2的右侧对应代码中的Bottleneck。
3. resnet⽹络的核⼼:Shortcut Connection
图3
图3可见两种连接⽅式:
⾃⼰层(cvnv2_x之类)内各个卷积层的连接⽅式,⽤实线。对应代码中的
out += identity #构成F(x) + x这个公式
cvnv2_x与conv3_x等层之间的卷积连接⽅式,⽤虚线。⽤虚线连接后可见 两组数据的channels是不同的 这样没法加啊。那么就
对 identity的通道数做⼀次修改。代码中⽤的downsample来实现。对应代码中的。downsample具体实现可具体查代码
if lf.downsample is not None:
identity = lf.downsample(x)
out += identity #构成F(x) + x这个公式
4.有了这⼏步的解释。⼀个完整的resnet的⽹络结构以及代码结构就清晰了。说⽩了就是很深的卷积层加上了个Shortcut Connection连接⽅式。这样让⽹络表达能⼒更好。具体为啥好去看原理吧。
5.贴代码:代码直接在github上下载的,可以⾃⼰去下载看,官⽅代码。代码中⼏个重要位置都注释了。
#encoding=utf-8
import torch
as nn
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
'wide_resnet50_2', 'wide_resnet101_2']
model_urls = {
'resnet18': '/models/resnet18-5c106cde.pth',
'resnet34': '/models/resnet34-333f7ec4.pth',
'resnet50': '/models/resnet50-19c8e357.pth',
'resnet101': '/models/resnet101-5d3b4d8f.pth',
'resnet152': '/models/resnet152-b121ed2d.pth',
'resnext50_32x4d': '/models/resnext50_32x4d-7cdf4587.pth',
'resnext101_32x8d': '/models/resnext101_32x8d-8ba56ff5.pth',
'wide_resnet50_2': '/models/wide_resnet50_2-95faca4d.pth',
'wide_resnet101_2': '/models/wide_resnet101_2-32ee1156.pth',
}
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=Fal, 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=Fal)
class BasicBlock(nn.Module):#⼀个BasicBlock对应于图1中的【】【】后⾯乘⼏就是说这种BasicBlock调⽤⼏次 expansion = 1
def __init__(lf, inplanes, planes, stride=1, downsample=None, groups=1,
ba_width=64, dilation=1, norm_layer=None):
super(BasicBlock, lf).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or ba_width != 64:
rai ValueError('BasicBlock only supports groups=1 and ba_width=64')
if dilation > 1:
rai NotImplementedError("Dilation > 1 not supported in BasicBlock")
# v1 and lf.downsample layers downsample the input when stride != 1
lf.bn1 = norm_layer(planes)
lf.bn2 = norm_layer(planes)
lf.downsample = downsample
lf.stride = stride
def forward(lf, x):
identity = x #x 给⾃⼰先备份⼀份
out = lf.conv1(x)#对x做卷积
out = lf.bn1(out)#对x归⼀化
out = lf.relu(out)#对x⽤激活函数
out = lf.conv2(out)#对x做卷积
out = lf.bn2(out)#归⼀化
if lf.downsample is not None:
identity = lf.downsample(x)
out += identity #这个是对应图2中的弯曲的箭头连接
out = lf.relu(out)
#上⾯这些对应图1中的【】中括号⾥⾯的操作
return out
class Bottleneck(nn.Module):
# Bottleneck in torchvision places the stride for downsampling at 3x3 v2)
# while original implementation places the stride at the first 1x1 v1)
# according to "Deep residual learning for image recognition"/abs/1512.03385.
# This variant is also known as ResNet V1.5 and improves accuracy according to
# /catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.
expansion = 4
def __init__(lf, inplanes, planes, stride=1, downsample=None, groups=1,
ba_width=64, dilation=1, norm_layer=None):
super(Bottleneck, lf).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes * (ba_width / 64.)) * groups
# v2 and lf.downsample layers downsample the input when stride != 1
lf.bn1 = norm_layer(width)
lf.bn2 = norm_layer(width)
lf.bn3 = norm_layer(planes * lf.expansion)
lf.downsample = downsample
lf.stride = stride
def forward(lf, x):
identity = x
out = lf.conv1(x)
out = lf.bn1(out)
out = lf.relu(out)
out = lf.conv2(out)
out = lf.bn2(out)
out = lf.relu(out)
out = lf.conv3(out)
out = lf.bn3(out)
if lf.downsample is not None:
identity = lf.downsample(x)
out += identity
out = lf.relu(out)
return out
class ResNet(nn.Module):
def __init__(lf, block, layers, num_class=6, zero_init_residual=Fal,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(ResNet, lf).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
lf._norm_layer = norm_layer
lf.inplanes = 64
lf.dilation = 1
if replace_stride_with_dilation is None:
# each element in the tuple indicates if we should replace
# the 2x2 stride with a dilated convolution instead
replace_stride_with_dilation = [Fal, Fal, Fal]
if len(replace_stride_with_dilation) != 3:
rai ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation)) lf.groups = groups
lf.ba_width = width_per_group
lf.bn1 = norm_layer(lf.inplanes)
lf.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
lf.layer1 = lf._make_layer(block, 64, layers[0])#对应着con2_x
lf.layer2 = lf._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])#对应着con3_x
lf.layer3 = lf._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])#对应着con4_x
lf.layer4 = lf._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])#对应着con5_x
lf.avgpool = nn.AdaptiveAvgPool2d((1, 1))
lf.fc = nn.Linear(512 * pansion, num_class)
for m dules():
for m dules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
stant_(m.weight, 1)
stant_(m.bias, 0)
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to /abs/1706.02677
if zero_init_residual:
for m dules():
if isinstance(m, Bottleneck):
stant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
stant_(m.bn2.weight, 0)
#blocks对应着表1中的 [2, 2, 2, 2]之中的2 这个数。它由resnet类型决定 block 对应于Bottleneck还是BasicBlock残差块 def _make_layer(lf, block, planes, blocks, stride=1, dilate=Fal):
norm_layer = lf._norm_layer
downsample = None
previous_dilation = lf.dilation
if dilate:
lf.dilation *= stride
stride = 1
if stride != 1 or lf.inplanes != planes * pansion:
downsample = nn.Sequential(
conv1x1(lf.inplanes, planes * pansion, stride),
norm_layer(planes * pansion),
)
layers = []
layers.append(block(lf.inplanes, planes, stride, downsample, lf.groups,
lf.ba_width, previous_dilation, norm_layer))
lf.inplanes = planes * pansion
for _ in range(1, blocks):
layers.append(block(lf.inplanes, planes, ups,
ba_width=lf.ba_width, dilation=lf.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)
def _forward_impl(lf, x):
# See note [TorchScript super()]
x = lf.conv1(x)#第⼀次做卷积对应图1中conv1 x shape [1 64 112 112]
x = lf.bn1(x)# 归⼀化处理了 x shape [1 64 112 112]
x = lf.relu(x)# 激活函数
x = lf.maxpool(x)#对应图1中的 conv2_x的3x3 maxpool x shape [1 64 56 56]
x = lf.layer1(x)
x = lf.layer2(x)
x = lf.layer3(x)
x = lf.layer4(x)
x = lf.avgpool(x)
x = torch.flatten(x, 1)
x = lf.fc(x)
return x
def forward(lf, x):
return lf._forward_impl(x)#resnet处理的主函数。x是输⼊的图像,待处理数据
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)