1. 简介
ResNet是深度残差网络(Deep Residual Network)的缩写,是目前非常流行的卷积神经网络模型之一。ResNet通过引入残差块(Residual Block)的概念,解决了在训练非常深的网络时的梯度消失和梯度爆炸问题,大幅度提升了网络的性能。
2. ResNet结构
ResNet的主要特点是引入了残差块(Residual Block)。一个残差块是由两个卷积层组成,其中第二个卷积层输出的特征图与输入特征图进行相加。这样的结构允许网络学习到输入特征相对于残差块输出的残差差异。
2.1 ResNet50结构
ResNet50包括了50个卷积层,其中包括16个Residual Block。每个Residual Block由三个卷积层组成,其结构如下所示:
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.conv3 = nn.Conv2d(out_channels, out_channels*4, kernel_size=1, stride=1, bias=False)
self.bn3 = nn.BatchNorm2d(out_channels*4)
self.relu = nn.ReLU(inplace=True)
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)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.stride != 1 or self.in_channels != self.out_channels*4:
identity = self.conv3(x)
identity = self.bn3(identity)
out += identity
out = self.relu(out)
return out
在ResNet50中,每个Residual Block输出的通道数为前一个Residual Block输出通道数的4倍,其中第一个Residual Block输出通道数与输入通道数一致。网络最后会通过一个全局平均池化层和全连接层输出预测结果。
2.2 ResNet101结构
ResNet101相比于ResNet50多了一个残差块,总共包括了101个卷积层。其它结构与ResNet50基本类似,只是最后一个残差块输出通道数为前一个残差块输出通道数的8倍。
2.3 ResNet152结构
ResNet152相比于ResNet101再多了一个残差块,总共包括了152个卷积层。其它结构与ResNet101基本相同,最后一个残差块输出通道数为前一个残差块输出通道数的16倍。
3. PyTorch实现示例
以下代码为PyTorch实现ResNet50、ResNet101和ResNet152的示例:
import torch
import torch.nn as nn
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000):
super(ResNet, self).__init__()
self.in_channels = 64
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=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
def make_layer(self, block, out_channels, blocks, stride=1):
layers = []
layers.append(block(self.in_channels, out_channels, stride))
self.in_channels = out_channels * block.expansion
for _ in range(1, blocks):
layers.append(block(self.in_channels, out_channels))
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)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def ResNet50(num_classes=1000):
return ResNet(ResidualBlock, [3, 4, 6, 3], num_classes)
def ResNet101(num_classes=1000):
return ResNet(ResidualBlock, [3, 4, 23, 3], num_classes)
def ResNet152(num_classes=1000):
return ResNet(ResidualBlock, [3, 8, 36, 3], num_classes)
4. 总结
本文介绍了ResNet50、ResNet101和ResNet152的结构以及通过PyTorch实现的示例代码。ResNet模型通过引入残差块的概念,解决了训练深度神经网络时的梯度消失和梯度爆炸问题,可以提高网络的性能。通过使用PyTorch实现的示例代码,我们可以方便地在自己的项目中使用这些预训练好的ResNet模型,进行图像分类等任务。