PyTorch 实现 Alexnet图像分类
本文主要介绍了如何在昇腾上,使用pytorch对经典的Alexnet小模型在公开的CIFAR10数据集进行分类训练的实战讲解。内容包括Alexnet网络模型创新点介绍、Alexnet的网络架构剖析与网络模型代码实战分析等等
本实验的目录结构安排如下所示:
- Alexnet网络模型创新点介绍
- Alexnet的网络架构剖析
- 网络模型代码实战分析
Alexnet网络模型创新点介绍
- 使用ReLU作为CNN的激活函数,并验证其效果在较深的网络超过了Sigmoid,成功解决了Sigmoid在网络较深时的梯度弥散问题。虽然ReLU激活函数在很久之前就被提出了,但是直到AlexNet的出现才将其发扬光大。
- 训练时使用Dropout随机忽略一部分神经元,以避免模型过拟合。Dropout虽有单独的论文论述,但是AlexNet将其实用化,通过实践证实了它的效果。在AlexNet中主要是最后几个全连接层使用了Dropout。
- 在CNN中使用重叠的最大池化。此前CNN中普遍使用平均池化,AlexNet全部使用最大池化,避免平均池化的模糊化效果。并且AlexNet中提出让步长比池化核的尺寸小,这样池化层的输出之间会有重叠和覆盖,提升了特征的丰富性。
- 提出了LRN层,对局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力。
- 在训练过程中使用数据增强的方式扩充数据集,并增加泛化能力,该方法在后续多个分类模型中被复用。
Alexnet的网络架构剖析
AlexNet网络是由Geoffrey和他的学生Alex提出,并在2012年的ILSVRC竞赛中获得了第一名。整个网络共有8层结构,前5层为卷积层,后三层为全连接层,该网络是当时最深的卷积神经网络模型。
需要注意的是论文中给的输入是224*224,而本实验训练的是cifir数据集,其图片大小为32x32,因此在实际的训练过程中会将输入图片resize到32x32,所以这里图片的输入size应为32x32,图中给的size是按照224计算,因此在推算过程中需要将224改成32得到cifar数据集训练过程中的每一层实际输出大小。
Alexnet网络代码实现分析
网络总共分为两大组成部分:5个卷积层(池化)与3个全连接层。前两层卷积层与最后一层卷积层后接有池化层来对提取的特征图进行降维,后面连续接3个卷积层再对特征进行提取后再通过一个池化层对特征图进行降维。降维后通过'nn.Flatten'把一个数据拉成一维向量适配后面第二部分的全连接层。
后面三个全连接层用于分类,分别是将输出从'nn.Flatten'的输出减少至9216、4608,最终到10也就是本实验要求的cifar数据集10分类任务中每一类别的预测概率。AlexNetModel中'__init'函数中定义了网络模型需要的卷积层(conv1到conv5),中间穿插使用pool操作(max_pool1到max_pool5)对特征图进行降维。在'forward'函数中定义了整个网络前向传播过程,该前向传播过程与网络定义结构相对应,但是输入size对应维本文实验数据集中的32x32而不是224x224.
import torch
import torch.nn as nn
class AlexNetModel(nn.Module):
def __init__(self, **kwargs):
super(AlexNetModel, self).__init__(**kwargs)
# input:3x32x32, output:64*15*15
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=4, stride=2),
nn.ReLU(True)
)
# input:64*15*15, output: 64*14*14
self.max_pool1 = nn.MaxPool2d(kernel_size=2, stride=1)
# input: 64*14*14 output:256*14*14
self.conv2 = nn.Sequential(
nn.Conv2d(64, 256, kernel_size=3, padding=1),
nn.ReLU(True)
)
# input:256*14*14, output:256*7*7
self.max_pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# input:256*7*7, output:384*7*7
self.conv3 = nn.Sequential(
nn.Conv2d(256, 384, kernel_size=3, padding=1),
nn.ReLU(True)
)
# input:384*7*7, output:384*7*7
self.conv4 = nn.Sequential(
nn.Conv2d(384, 384, kernel_size=3, padding=1), # 384*7*7
nn.ReLU(True)
)
# input:384*7*7, output:256*7*7
self.conv5 = nn.Sequential(
nn.Conv2d(384, 256, kernel_size=3, padding=1), # 256*7*7
nn.ReLU(True)
)
# input:256*7*7, output:256*6*6
self.max_pool3 = nn.MaxPool2d(kernel_size=2, stride=1)
# input:256*6*6, output:9216
self.flaten1 = nn.Flatten()
# input:9216, output:4608
self.fc1 = nn.Sequential(
nn.Linear(9216, 4608),
nn.ReLU(True),
nn.Dropout(p=0.5)
)
# input:4608, output:4608
self.fc2 = nn.Sequential(
nn.Linear(4608, 4608),
nn.ReLU(True),
nn.Dropout(p=0.5)
)
# input:4608, output:10
self.fc3 = nn.Linear(4608, 10)
# 定义模型的前向计算,如何根据输入x计算返回所需要的模型输出
def forward(self, x):
x = self.conv1(x)
x = self.max_pool1(x)
x = self.conv2(x)
x = self.max_pool2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.max_pool3(x)
x = self.flaten1(x)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
将Alexnet网络模型打印出来可以看到,前面conv1~5是5个卷积层,卷积层后接入一个flaten层将高维的特征图进行降维以便于后续的三个全连接层进行连接。
print( AlexNetModel())
AlexNetModel(
(conv1): Sequential(
(0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2))
(1): ReLU(inplace=True)
)
(max_pool1): MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, ceil_mode=False)
(conv2): Sequential(
(0): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
)
(max_pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv3): Sequential(
(0): Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
)
(conv4): Sequential(
(0): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
)
(conv5): Sequential(
(0): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
)
(max_pool3): MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, ceil_mode=False)
(flaten1): Flatten(start_dim=1, end_dim=-1)
(fc1): Sequential(
(0): Linear(in_features=9216, out_features=4608, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
)
(fc2): Sequential(
(0): Linear(in_features=4608, out_features=4608, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
)
(fc3): Linear(in_features=4608, out_features=10, bias=True)
)
Alexnet网络用于cifir数据集分类实战
基于上述搭建好的网络模型,我们现在就可以正式来使用该模型开始训练cifar数据集。
导入昇腾npu相关库transfer_to_npu、该模块可以使能模型自动迁移至昇腾上。
import torch_npu
/home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/dynamo/__init__.py:18: UserWarning: Register eager implementation for the 'npu' backend of dynamo, as torch_npu was not compiled with torchair.
warnings.warn(
from torch_npu.contrib import transfer_to_npu
/home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:164: ImportWarning:
*************************************************************************************************************
The torch.Tensor.cuda and torch.nn.Module.cuda are replaced with torch.Tensor.npu and torch.nn.Module.npu now..
The torch.cuda.DoubleTensor is replaced with torch.npu.FloatTensor cause the double type is not supported now..
The backend in torch.distributed.init_process_group set to hccl now..
The torch.cuda.* and torch.cuda.amp.* are replaced with torch.npu.* and torch.npu.amp.* now..
The device parameters have been replaced with npu in the function below:
torch.logspace, torch.randint, torch.hann_window, torch.rand, torch.full_like, torch.ones_like, torch.rand_like, torch.randperm, torch.arange, torch.frombuffer, torch.normal, torch._empty_per_channel_affine_quantized, torch.empty_strided, torch.empty_like, torch.scalar_tensor, torch.tril_indices, torch.bartlett_window, torch.ones, torch.sparse_coo_tensor, torch.randn, torch.kaiser_window, torch.tensor, torch.triu_indices, torch.as_tensor, torch.zeros, torch.randint_like, torch.full, torch.eye, torch._sparse_csr_tensor_unsafe, torch.empty, torch._sparse_coo_tensor_unsafe, torch.blackman_window, torch.zeros_like, torch.range, torch.sparse_csr_tensor, torch.randn_like, torch.from_file, torch._cudnn_init_dropout_state, torch._empty_affine_quantized, torch.linspace, torch.hamming_window, torch.empty_quantized, torch._pin_memory, torch.Tensor.new_empty, torch.Tensor.new_empty_strided, torch.Tensor.new_full, torch.Tensor.new_ones, torch.Tensor.new_tensor, torch.Tensor.new_zeros, torch.Tensor.to, torch.nn.Module.to, torch.nn.Module.to_empty
*************************************************************************************************************
warnings.warn(msg, ImportWarning)
torchvision模块中集成了一些当今比较流行的数据集、模型架构和用于计算机视觉的常见图像转换功能,torchvision模块中含有本次实验所需要的CIFAR数据集,因此导入该模块用于数据集的下载。tqdm是用于训练过程中训练进度条,便于我们能够清晰的看到整个训练过程。
import torchvision
import torchvision.transforms as transforms
from tqdm import tqdm
数据集预处理功能定义: 对图像数据集进行不同程度的变化,包括裁剪、翻转等方式增加数据的多样性,防止过拟合现象的出现,以增强模型的泛化能力。
调用了torchvision中的transform库中的compose方法,使用裁剪(RandomCrop)、翻转(RandomHorizontalFlip)等组合成tensor形式后并对tensor进行正则化(Normalize)。
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
cifar数据集共有60000张彩色图像,这些图像是32*32,分为10个类,每类6000张图。有50000张用于训练,构成了5个训练批,每一批10000张图;另外10000用于测试,单独构成一批。测试批的数据里,取自10类中的每一类,每一类随机取1000张。抽剩下的就随机排列组成了训练批。注意一个训练批中的各类图像并不一定数量相同,总的来看训练批,每一类都有5000张图。
数据集加载: torchvision中集成了一些通用的开源数据集,其中也包含cifar,此处通过torchvision函数加载cifar数据集到工作目录上的指定路径,如果已经下载好了,会直接校验通过,不会二次进行下载。
trainset = torchvision.datasets.CIFAR10(
root='/home/pengyongrong/workspace/cifarDatasat/', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=128, shuffle=True)
testset = torchvision.datasets.CIFAR10(
root='/home/pengyongrong/workspace/cifarDatasat/', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(
testset, batch_size=100, shuffle=False)
Files already downloaded and verified
Files already downloaded and verified
训练模块: 根据传入的迭代次数'epoch'开始训练网络模型,这里需要在model开始前加入'net.train()',使用随机梯度下降算法是将梯度值初始化为0('zero_grad()'),计算梯度、通过梯度下降算法更新模型参数的值以及统计每次训练后的loss值(每隔100次打印一次)。
def train(epoch):
net.train()
train_loss = 0.0
epoch_loss = 0.0
for batch_idx, (inputs, targets) in enumerate(tqdm(trainloader, 0)):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
lr_scheduler.step()
train_loss += loss.item()
epoch_loss += loss.item()
if batch_idx % 100 == 99: # 每100次迭代打印一次损失
print(f'[Epoch {epoch + 1}, Iteration {batch_idx + 1}] loss: {train_loss / 100:.3f}')
train_loss = 0.0
return epoch_loss / len(trainloader)
测试模块: 每训练一轮将会对最新得到的训练模型效果进行测试,使用的是数据集准备时期划分得到的测试集,每类约为1000张。
def test():
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(tqdm(testloader)):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return 100 * correct / total
主功能调用模块: 该模块用于开启模型在指定数据集(cifar)上训练,其中定义了硬件设备为昇腾npu(device = 'npu'),定义了损失函数为交叉熵损失'CrossEntropyLoss()',梯度下降优化算法为SGD并同时指定了学习率等参数。
import torch.optim as optim
device = 'npu'
net = AlexNetModel()
net = net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=1.0, weight_decay=5e-4)
lr_scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer,0.1,steps_per_epoch=len(trainloader),
epochs=150,div_factor=25,final_div_factor=10000,pct_start=0.3)
训练与测试的次数为5次,这里用户可以根据需要自行选择设置更高或更低,每个epoch的测试准确率都会被打印出来,如果不需要将代码注释掉即可。
for epoch in range(5):
epoch_loss = train(epoch)
test_accuray = test()
print(f'\nTest accuracy for AlexNet at epoch {epoch + 1}: {test_accuray:.2f}%')
print(f'Epoch loss for AlexNet at epoch {epoch + 1}: {epoch_loss:.3f}')
26%|███████████████████▎ | 101/391 [00:10<00:30, 9.56it/s]
[Epoch 1, Iteration 100] loss: 1.577
51%|██████████████████████████████████████▌ | 201/391 [00:20<00:19, 9.67it/s]
[Epoch 1, Iteration 200] loss: 1.505
77%|█████████████████████████████████████████████████████████▋ | 301/391 [00:30<00:09, 9.60it/s]
[Epoch 1, Iteration 300] loss: 1.459
100%|███████████████████████████████████████████████████████████████████████████| 391/391 [00:39<00:00, 9.87it/s]
100%|███████████████████████████████████████████████████████████████████████████| 100/100 [00:04<00:00, 20.98it/s]
Test accuracy for AlexNet at epoch 1: 53.27%
Epoch loss for AlexNet at epoch 1: 1.482
26%|███████████████████▌ | 102/391 [00:10<00:28, 10.18it/s]
[Epoch 2, Iteration 100] loss: 1.381
51%|██████████████████████████████████████▌ | 201/391 [00:20<00:19, 9.89it/s]
[Epoch 2, Iteration 200] loss: 1.315
77%|█████████████████████████████████████████████████████████▋ | 301/391 [00:30<00:09, 9.92it/s]
[Epoch 2, Iteration 300] loss: 1.256
100%|███████████████████████████████████████████████████████████████████████████| 391/391 [00:39<00:00, 9.90it/s]
100%|███████████████████████████████████████████████████████████████████████████| 100/100 [00:04<00:00, 21.67it/s]
Test accuracy for AlexNet at epoch 2: 59.23%
Epoch loss for AlexNet at epoch 2: 1.296
26%|███████████████████▎ | 101/391 [00:09<00:28, 10.07it/s]
[Epoch 3, Iteration 100] loss: 1.165
51%|██████████████████████████████████████▌ | 201/391 [00:19<00:18, 10.08it/s]
[Epoch 3, Iteration 200] loss: 1.122
77%|█████████████████████████████████████████████████████████▋ | 301/391 [00:29<00:09, 9.98it/s]
[Epoch 3, Iteration 300] loss: 1.060
100%|███████████████████████████████████████████████████████████████████████████| 391/391 [00:38<00:00, 10.15it/s]
100%|███████████████████████████████████████████████████████████████████████████| 100/100 [00:04<00:00, 21.21it/s]
Test accuracy for AlexNet at epoch 3: 64.63%
Epoch loss for AlexNet at epoch 3: 1.097
26%|███████████████████▎ | 101/391 [00:09<00:27, 10.59it/s]
[Epoch 4, Iteration 100] loss: 1.008
51%|██████████████████████████████████████▌ | 201/391 [00:19<00:18, 10.40it/s]
[Epoch 4, Iteration 200] loss: 1.028
77%|█████████████████████████████████████████████████████████▋ | 301/391 [00:29<00:08, 10.21it/s]
[Epoch 4, Iteration 300] loss: 0.964
100%|███████████████████████████████████████████████████████████████████████████| 391/391 [00:37<00:00, 10.36it/s]
100%|███████████████████████████████████████████████████████████████████████████| 100/100 [00:04<00:00, 21.62it/s]
Test accuracy for AlexNet at epoch 4: 70.41%
Epoch loss for AlexNet at epoch 4: 0.984
26%|███████████████████▎ | 101/391 [00:10<00:29, 9.88it/s]
[Epoch 5, Iteration 100] loss: 0.900
52%|██████████████████████████████████████▋ | 202/391 [00:20<00:18, 10.28it/s]
[Epoch 5, Iteration 200] loss: 0.879
77%|█████████████████████████████████████████████████████████▉ | 302/391 [00:29<00:08, 10.51it/s]
[Epoch 5, Iteration 300] loss: 0.849
100%|███████████████████████████████████████████████████████████████████████████| 391/391 [00:38<00:00, 10.18it/s]
100%|███████████████████████████████████████████████████████████████████████████| 100/100 [00:04<00:00, 21.74it/s]
Test accuracy for AlexNet at epoch 5: 71.80%
Epoch loss for AlexNet at epoch 5: 0.877
由于Alexnet模型参数较少,因此只需要单卡就能跑起来,内存占用约为1GB。
Reference
[1] Krizhevsky A, Sutskever I, Hinton G E. Imagenet classification with deep convolutional neural networks[J]. Advances in neural information processing systems, 2012, 25.