昇腾910-PyTorch 实现 Alexnet图像分类

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分类任务中每一类的预测概率。

```python

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网络用于cifir数据集分类实战

基于上述搭建好的网络模型,我们现在就可以正式来使用该模型开始训练cifar数据集。导入昇腾npu相关库transfer_to_npu、该模块可以使能模型自动迁移至昇腾上。

```python

import torch_npu

from torch_npu.contrib import transfer_to_npu

```

torchvision模块中集成了一些当今比较流行的数据集、模型架构和用于计算机视觉的常见图像转换功能,torchvision模块中含有本次实验所需要的CIFAR数据集,因此导入该模块用于数据集的下载。tqdm是用于训练过程中训练进度条,便于我们能够清晰的看到整个训练过程。

```python

import torchvision

import torchvision.transforms as transforms

from tqdm import tqdm

```

数据集预处理功能定义: 对图像数据集进行不同程度的变化,包括裁剪、翻转等方式增加数据的多样性,防止过拟合现象的出现,以增强模型的泛化能力。

调用了torchvision中的transform库中的compose方法,使用裁剪(RandomCrop)、翻转(RandomHorizontalFlip)等组合成tensor形式后并对tensor进行正则化(Normalize)。

```python

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数据集到工作目录上的指定路径,如果已经下载好了,会直接校验通过,不会二次进行下载。

```python

trainset = torchvision.datasets.CIFAR10(root='/home/ma-user/work/resnet50Experiments/dataset/cifar-10-batches-py/', train=True, download=True, transform=transform_train)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True)

testset = torchvision.datasets.CIFAR10(root='/home/ma-user/work/resnet50Experiments/dataset/cifar-10-batches-py/', train=False, download=True, transform=transform_test)

testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False)

```

训练模块: 根据传入的迭代次数'epoch'开始训练网络模型,这里需要在model开始前加入'net.train()',使用随机梯度下降算法是将梯度值初始化为0('zero_grad()'),计算梯度、通过梯度下降算法更新模型参数的值以及统计每次训练后的loss值(每隔100次打印一次)。

```python

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张。

```python

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并同时指定了学习率等参数。

```python

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)

```

训练与测试的次数为60次,这里用户可以根据需要自行选择设置更高或更低,每个epoch的测试准确率都会被打印出来,如果不需要将代码注释掉即可。

```python

for epoch in range(60): 

    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}') 

```

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.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容