|
本帖最后由 Shaw0xyz 于 2024-6-23 15:46 编辑
1. 引言
卷积神经网络(CNN)是深度学习中广泛应用的一种神经网络结构。在众多CNN模型中,残差网络(ResNet)因其出色的性能和深度网络训练中的稳定性而备受关注。本文将介绍ResNet的基本概念,并提供一个简要的代码实现。
2. ResNet简介
2.1 残差网络的背景
深度神经网络在层数增加时,训练过程中的梯度消失和梯度爆炸问题使得网络性能难以提升。为了缓解这一问题,ResNet引入了残差块,通过残差连接(shortcut connection)使得网络更深且更易于训练。
2.2 残差块结构
ResNet的核心是残差块。一个典型的残差块包括两个卷积层和一个恒等映射(identity mapping)。其基本结构如下:
输入 → 卷积层1 → 批归一化(Batch Normalization)→ ReLU → 卷积层2 → 批归一化 → 残差连接 → ReLU → 输出
残差连接跳过了两个卷积层,将输入直接加到输出上,这样网络学习的是输入和输出之间的残差,而不是直接学习输出。
2.3 ResNet的变种
ResNet有多个变种,包括ResNet-18、ResNet-34、ResNet-50、ResNet-101等。数字表示网络层数。ResNet-50及以上的变种采用了“瓶颈结构”来减少计算量,这种结构在每个残差块中增加了一个1x1卷积层。
3. ResNet代码实现
下面我们以PyTorch为例,简要实现一个ResNet-18模型。
3.1 导入必要的库
首先,导入PyTorch及相关模块:
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
复制代码
3.2 定义残差块
- class BasicBlock(nn.Module):
- def __init__(self, in_channels, out_channels, stride=1):
- super(BasicBlock, self).__init__()
- self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, 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.shortcut = nn.Sequential()
- if stride != 1 or in_channels != out_channels:
- self.shortcut = nn.Sequential(
- nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
- nn.BatchNorm2d(out_channels)
- )
- def forward(self, x):
- out = F.relu(self.bn1(self.conv1(x)))
- out = self.bn2(self.conv2(out))
- out += self.shortcut(x)
- out = F.relu(out)
- return out
复制代码
3.3 定义ResNet模型
- class ResNet(nn.Module):
- def __init__(self, block, num_blocks, num_classes=10):
- super(ResNet, self).__init__()
- self.in_channels = 64
- self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
- self.bn1 = nn.BatchNorm2d(64)
- self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
- self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
- self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
- self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
- self.linear = nn.Linear(512, num_classes)
- def _make_layer(self, block, out_channels, num_blocks, stride):
- strides = [stride] + [1]*(num_blocks-1)
- layers = []
- for stride in strides:
- layers.append(block(self.in_channels, out_channels, stride))
- self.in_channels = out_channels
- return nn.Sequential(*layers)
- def forward(self, x):
- out = F.relu(self.bn1(self.conv1(x)))
- out = self.layer1(out)
- out = self.layer2(out)
- out = self.layer3(out)
- out = self.layer4(out)
- out = F.avg_pool2d(out, 4)
- out = out.view(out.size(0), -1)
- out = self.linear(out)
- return out
- def ResNet18():
- return ResNet(BasicBlock, [2, 2, 2, 2])
复制代码
3.4 模型训练和测试
在定义好模型后,可以进行模型训练和测试。以下是训练和测试代码的简要示例:
- import torch.optim as optim
- from torch.utils.data import DataLoader
- from torchvision import datasets, transforms
- transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
- train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
- train_loader = DataLoader(train_dataset, batch_size=100, shuffle=True)
- test_dataset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
- test_loader = DataLoader(test_dataset, batch_size=100, shuffle=False)
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model = ResNet18().to(device)
- criterion = nn.CrossEntropyLoss()
- optimizer = optim.Adam(model.parameters(), lr=0.001)
- # Training loop
- for epoch in range(10):
- model.train()
- for images, labels in train_loader:
- images, labels = images.to(device), labels.to(device)
- optimizer.zero_grad()
- outputs = model(images)
- loss = criterion(outputs, labels)
- loss.backward()
- optimizer.step()
- print(f'Epoch {epoch+1}, Loss: {loss.item()}')
- # Testing loop
- model.eval()
- correct = 0
- total = 0
- with torch.no_grad():
- for images, labels in test_loader:
- images, labels = images.to(device), labels.to(device)
- outputs = model(images)
- _, predicted = torch.max(outputs.data, 1)
- total += labels.size(0)
- correct += (predicted == labels).sum().item()
- print(f'Accuracy: {100 * correct / total}%')
复制代码
4. 结论
本文介绍了ResNet的基本概念、残差块的结构以及如何在PyTorch中实现ResNet-18模型。通过这一实践,读者可以深入理解ResNet的设计思想和实现方法,并应用于实际的深度学习任务中。
/ 荔枝学姐de课后专栏 /
Hi!这里是荔枝学姐~
欢迎来到我的课后专栏
自然语言学渣 NLP摆烂姐
热衷于技术写作 IT边角料
AIGC & Coding & linux ...
~互撩~ TG: @Shaw_0xyz
|
|