目录

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

以上就是python编程pytorch深度卷积神经网络alexnet详解的详细内容,更多关于pytorch卷积神经网络的资料请关注www.887551.com其它相关文章!