pytorch深度卷积神经网络AlexNet Python编程pytorch深度卷积神经网络AlexNet详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

pytorch深度卷积神经网络AlexNet Python编程pytorch深度卷积神经网络AlexNet详解

Supre_yuan   2021-10-11 我要评论
想了解Python编程pytorch深度卷积神经网络AlexNet详解的相关内容吗,Supre_yuan在本文为您仔细讲解pytorch深度卷积神经网络AlexNet的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python编程,pytorch深度卷积神经网络AlexNet,下面大家一起来学习吧。

2012年,AlexNet横空出世。它首次证明了学习到的特征可以超越手工设计的特征。它一举打破了计算机视觉研究的现状。AlexNet使用了8层卷积神经网络,并以很大的优势赢得了2012年的ImageNet图像识别挑战赛。

下图展示了从LeNet(左)到AlexNet(right)的架构。

在这里插入图片描述

AlexNet和LeNet的设计理念非常相似,但也有如下区别:

  • AlexNet比相对较小的LeNet5要深得多。
  • AlexNet使用ReLU而不是sigmoid作为其激活函数。

 容量控制和预处理

AlexNet通过dropout控制全连接层的模型复杂度,而LeNet只使用了权重衰减。为了进一步扩充数据,AlexNet在训练时增加了大量的图像增强数据,如翻转、裁剪和变色。这使得模型更加健壮,更大的样本量有效地减少了过拟合。

import torch
from torch import nn
from d2l import torch as d2l

net = nn.Sequential(
	# 这里,我们使用一个11*11的更大窗口来捕捉对象
	# 同时,步幅为4,以减少输出的高度和宽度
	# 另外,输出通道的数目远大于LeNet
	nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),
	nn.MaxPool2d(kernel_size=3, stride=2)
	# 减少卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数
	nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),
	nn.MaxPool2d(kernel_size=3, stride=2)
	# 使用三个连续的卷积层和较小的卷积窗口
	# 除了最后的卷积层,输出通道的数量进一步增加
	# 在前两个卷积层之后,汇聚层不用于减少输入的高度和宽度
	nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),
	nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
	nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
	nn.MaxPool2d(kernel_size=3, stride=2),
	nn.Flatten(),
	# 这里,全连接层的输出数量是LeNet中的好几倍。使用dropout层来减轻过度拟合
	nn.Linear(6400, 4096), nn.ReLU(),
	nn.Dropout(p=0.5),
	nn.Linear(4096, 4096), nn.ReLU(),
	nn.Dropout(p=0.5),
	# 最后是输出层。由于这里使用Fashion-MNIST,所以用类别数位10
	nn.Linear(4096, 10)
)

我们构造一个高度和宽度都为224的单通道数据,来观察每一层输出的形状。它与上面离得最近的图中的AlexNet架构相匹配。

X = torch.randn(1, 1, 224, 224)
for layer in net:
	X = layer(X)
	print(layer.__class__.__name__,'Output shape:\t', X.shape)
Conv2d Output shape: torch.Size([1, 96, 54, 54])
ReLU Output shape: torch.Size([1, 96, 54, 54])
MaxPool2d Output shape: torch.Size([1, 96, 26, 26])
Conv2d Output shape: torch.Size([1, 256, 26, 26])
ReLU Output shape: torch.Size([1, 256, 26, 26])
MaxPool2d Output shape: torch.Size([1, 256, 12, 12])
Conv2d Output shape: torch.Size([1, 384, 12, 12])
ReLU Output shape: torch.Size([1, 384, 12, 12])
Conv2d Output shape: torch.Size([1, 384, 12, 12])
ReLU Output shape: torch.Size([1, 384, 12, 12])
Conv2d Output shape: torch.Size([1, 256, 12, 12])
ReLU Output shape: torch.Size([1, 256, 12, 12])
MaxPool2d Output shape: torch.Size([1, 256, 5, 5])
Flatten Output shape: torch.Size([1, 6400])
Linear Output shape: torch.Size([1, 4096])
ReLU Output shape: torch.Size([1, 4096])
Dropout Output shape: torch.Size([1, 4096])
Linear Output shape: torch.Size([1, 4096])
ReLU Output shape: torch.Size([1, 4096])
Dropout Output shape: torch.Size([1, 4096])
Linear Output shape: torch.Size([1, 10])

读取数据集

在这里将AlexNet直接应用于Fashion-MNIST的识别,但这里有一个问题,那就是Fashion-MNIST图像的分辨率( 28 × 28 28\times28 28×28像素)低于ImageNet图像。为了解决这个问题,我们将它们增加到 224 × 224 224\times224 224×224(通常来讲这不是一个明智的做法,但我们在这里这样做是为了有效使用AlexNet结构)。我们使用d2l.load_data_fashion_mnist函数中的resize参数执行此调整。

batch_size = 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)

现在,我们可以开始训练AlexNet了,与LeNet相比,这里的主要变化是使用更小的学习速率训练,这是因为网络更深更广、图像分辨率更高,训练卷积伸进网络就更昂贵。

lr, num_epochs = 0.01, 10
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
loss 0.330, train acc 0.879, test acc 0.877
4163.0 examples/sec on cuda:0

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们