现在重新稍微系统的介绍一下ResNet网络结构。 ResNet结构首先通过一个卷积层然后有一个池化层,然后通过一系列的残差结构,最后再通过一个平均池化下采样操作,以及一个全连接层的得到了一个输出。ResNet网络可以达到很深的层数的原因就是不断的堆叠残差结构而来的。
当层数过多的时候,假设每一层的误差梯度都是一个小于1的数值,当进行方向传播的过程中,每向前传播一层,都要乘以一个小于1的误差梯度,当网络越来越深时,所成的小于1的系数也就越来越多,此时梯度便越趋近于0,这样梯度便会越来越小。这便会造成梯度消失的现象。
当解决了梯度消失或者梯度爆炸的问题之后,其实网络的效果可能还是不尽如意,还可能有退化问题。为此,ResNet提出了残差结构来解决这个退化问题。 也正是因为有这个残差的结构,所以才可以搭建这么深的网络。
作图是针对ResNet-18/34层浅层网络的结构,右图是ResNet-50/101/152层深层网络的结构,其中注意:主分支与shortcut 的输出特征矩阵shape。
需要注意,有些残差结构的ShortCut是实线,而有的是虚线,这两者是不同的。对于左图来说,ShortCut是实线,这表明输入与输出的shape是一样的,所以可以直接的进行相加。而对于右图来说,其输入的shape与输出的shape是不一样的,这时候需要调整步长stribe与kernel size来使得两条路(主分支与捷径分支)所处理好的shape是一模一样的。
右侧虚线残差结构的主分支上、第一个1x1卷积层的步距是2,第二个3x3卷积层的步距是1.
对于第一个大模块的第一个残差结构,其第二个3x3的卷积中,步长是1的,而其他的三个大模块的步长均为2.
在每一个大模块的维度变换中,主要是第一个残差结构使得shape减半,而模块中其他的残差结构都是没有改变shape的。也真因为没有改变shape,所以这些残差结构才可以直接的通过实线进行相加。
Batch Normalization是google团队在2015年论文《Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift》提出的。通过该方法能够加速网络的收敛并提升准确率。
import torch
import torch.nn as nn
# 分类数目
num_class = 5
# 各层数目
resnet18_params = [2, 2, 2, 2]
resnet34_params = [3, 4, 6, 3]
resnet50_params = [3, 4, 6, 3]
resnet101_params = [3, 4, 23, 3]
resnet152_params = [3, 8, 36, 3]
# 定义Conv1层
def Conv1(in_planes, places, stride=2):
return nn.Sequential(
nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
# 浅层的残差结构
class BasicBlock(nn.Module):
def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 1):
super(BasicBlock,self).__init__()
self.expansion = expansion
self.downsampling = downsampling
# torch.Size([1, 64, 56, 56]), stride = 1
# torch.Size([1, 128, 28, 28]), stride = 2
# torch.Size([1, 256, 14, 14]), stride = 2
# torch.Size([1, 512, 7, 7]), stride = 2
self.basicblock = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(places * self.expansion),
)
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 512, 7, 7])
# 每个大模块的第一个残差结构需要改变步长
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(places*self.expansion)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# 实线分支
residual = x
out = self.basicblock(x)
# 虚线分支
if self.downsampling:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# 深层的残差结构
class Bottleneck(nn.Module):
# 注意:默认 downsampling=False
def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):
super(Bottleneck,self).__init__()
self.expansion = expansion
self.downsampling = downsampling
self.bottleneck = nn.Sequential(
# torch.Size([1, 64, 56, 56]),stride=1
# torch.Size([1, 128, 56, 56]),stride=1
# torch.Size([1, 256, 28, 28]), stride=1
# torch.Size([1, 512, 14, 14]), stride=1
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
# torch.Size([1, 64, 56, 56]),stride=1
# torch.Size([1, 128, 28, 28]), stride=2
# torch.Size([1, 256, 14, 14]), stride=2
# torch.Size([1, 512, 7, 7]), stride=2
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
# torch.Size([1, 256, 56, 56]),stride=1
# torch.Size([1, 512, 28, 28]), stride=1
# torch.Size([1, 1024, 14, 14]), stride=1
# torch.Size([1, 2048, 7, 7]), stride=1
nn.Conv2d(in_channels=places, out_channels=places * self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(places * self.expansion),
)
# torch.Size([1, 256, 56, 56])
# torch.Size([1, 512, 28, 28])
# torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7])
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(places*self.expansion)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# 实线分支
residual = x
out = self.bottleneck(x)
# 虚线分支
if self.downsampling:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self,blocks, blockkinds, num_classes=num_class):
super(ResNet,self).__init__()
self.blockkinds = blockkinds
self.conv1 = Conv1(in_planes = 3, places= 64)
# 对应浅层网络结构
if self.blockkinds == BasicBlock:
self.expansion = 1
# 64 -> 64
self.layer1 = self.make_layer(in_places=64, places=64, block=blocks[0], stride=1)
# 64 -> 128
self.layer2 = self.make_layer(in_places=64, places=128, block=blocks[1], stride=2)
# 128 -> 256
self.layer3 = self.make_layer(in_places=128, places=256, block=blocks[2], stride=2)
# 256 -> 512
self.layer4 = self.make_layer(in_places=256, places=512, block=blocks[3], stride=2)
self.fc = nn.Linear(512, num_classes)
# 对应深层网络结构
if self.blockkinds == Bottleneck:
self.expansion = 4
# 64 -> 64
self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
# 256 -> 128
self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
# 512 -> 256
self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
# 1024 -> 512
self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)
self.fc = nn.Linear(2048, num_classes)
self.avgpool = nn.AvgPool2d(7, stride=1)
# 初始化网络结构
for m in self.modules():
if isinstance(m, nn.Conv2d):
# 采用了何凯明的初始化方法
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def make_layer(self, in_places, places, block, stride):
layers = []
# torch.Size([1, 64, 56, 56]) -> torch.Size([1, 256, 56, 56]), stride=1 故w,h不变
# torch.Size([1, 256, 56, 56]) -> torch.Size([1, 512, 28, 28]), stride=2 故w,h变
# torch.Size([1, 512, 28, 28]) -> torch.Size([1, 1024, 14, 14]),stride=2 故w,h变
# torch.Size([1, 1024, 14, 14]) -> torch.Size([1, 2048, 7, 7]), stride=2 故w,h变
# 此步需要通过虚线分支,downsampling=True
layers.append(self.blockkinds(in_places, places, stride, downsampling =True))
# torch.Size([1, 256, 56, 56]) -> torch.Size([1, 256, 56, 56])
# torch.Size([1, 512, 28, 28]) -> torch.Size([1, 512, 28, 28])
# torch.Size([1, 1024, 14, 14]) -> torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7]) -> torch.Size([1, 2048, 7, 7])
# print("places*self.expansion:", places*self.expansion)
# print("block:", block)
# 此步需要通过实线分支,downsampling=False, 每个大模块的第一个残差结构需要改变步长
for i in range(1, block):
layers.append(self.blockkinds(places*self.expansion, places))
return nn.Sequential(*layers)
def forward(self, x):
# conv1层
x = self.conv1(x) # torch.Size([1, 64, 56, 56])
# conv2_x层
x = self.layer1(x) # torch.Size([1, 256, 56, 56])
# conv3_x层
x = self.layer2(x) # torch.Size([1, 512, 28, 28])
# conv4_x层
x = self.layer3(x) # torch.Size([1, 1024, 14, 14])
# conv5_x层
x = self.layer4(x) # torch.Size([1, 2048, 7, 7])
x = self.avgpool(x) # torch.Size([1, 2048, 1, 1]) / torch.Size([1, 512])
x = x.view(x.size(0), -1) # torch.Size([1, 2048]) / torch.Size([1, 512])
x = self.fc(x) # torch.Size([1, 5])
return x
def ResNet18():
return ResNet(resnet18_params, BasicBlock)
def ResNet34():
return ResNet(resnet34_params, BasicBlock)
def ResNet50():
return ResNet(resnet50_params, Bottleneck)
def ResNet101():
return ResNet(resnet101_params, Bottleneck)
def ResNet152():
return ResNet(resnet152_params, Bottleneck)
if __name__=='__main__':
# model = torchvision.models.resnet50()
# 模型测试
# model = ResNet18()
# model = ResNet34()
# model = ResNet50()
# model = ResNet101()
model = ResNet152()
# print(model)
input = torch.randn(1, 3, 224, 224)
out = model(input)
print(out.shape)