深度卷积神经网络(AlexNet)是什么?
目录
介绍
深度卷积神经网络(AlexNet)是由Alex Krizhevsky、Ilya Sutskever和Geoffrey Hinton于2012年提出的。它是第一个在ImageNet数据集上获得较低错误率的深度卷积神经网络。AlexNet的成功标志着深度学习的新时代的开始。
结构
AlexNet的结构如下:
input -> conv1 -> relu1 -> pool1 -> conv2 -> relu2 -> pool2 -> conv3 -> relu3 -> conv4 -> relu4 -> conv5 -> relu5 -> pool5 -> fc6 -> relu6 -> dropout6 -> fc7 -> relu7 -> dropout7 -> fc8 -> softmax -> output
- input:输入数据
- conv1:第一个卷积层
- relu1:第一个ReLU层
- pool1:第一个池化层
- conv2:第二个卷积层
- relu2:第二个ReLU层
- pool2:第二个池化层
- conv3:第三个卷积层
- relu3:第三个ReLU层
- conv4:第四个卷积层
- relu4:第四个ReLU层
- conv5:第五个卷积层
- relu5:第五个ReLU层
- pool5:第五个池化层
- fc6:第一个全连接层
- relu6:第一个ReLU层
- dropout6:第一个Dropout层
- fc7:第二个全连接层
- relu7:第二个ReLU层
- dropout7:第二个Dropout层
- fc8:第三个全连接层
- softmax:Softmax层
- output:输出数据
实现
下面是使用PyTorch实现AlexNet的代码:
import torch.nn as nn
class AlexNet(nn.Module):
def __init__(self, num_classes=1000):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
return x
理论推导
卷积层
卷积层是AlexNet中最重要的层之一。假设我们有一个输入图像 X X X,一个卷积核 W W W和一个偏置 b b b。卷积层的输出 Y Y Y可以表示为:
Y i , j = ∑ m ∑ n X i + m , j + n W m , n + b Y_{i,j}=\sum_{m}\sum_{n}X_{i+m,j+n}W_{m,n}+b Yi,j=m∑n∑Xi+m,j+nWm,n+b
其中, i i i和 j j j表示输出张量的坐标, m m m和 n n n表示卷积核的坐标。
ReLU层
ReLU层是AlexNet中用于增加非线性的层之一。ReLU函数可以表示为:
f ( x ) = m a x ( 0 , x ) f(x)=max(0,x) f(x)=max(0,x)
池化层
池化层是AlexNet中用于减少空间维度的层之一。最大池化层可以表示为:
Y i , j = max m , n X i + m , j + n Y_{i,j}=\max_{m,n}X_{i+m,j+n} Yi,j=m,nmaxXi+m,j+n
Dropout层
Dropout层是AlexNet中用于减少过拟合的层之一。Dropout层可以表示为:
y i = { x i 以概率 p 0 以概率 1 − p y_i=\begin{cases}x_i & \text{以概率}\ p \\0 & \text{以概率}\ 1-p\end{cases} yi={xi0以概率 p以概率 1−p
Softmax层
Softmax层是AlexNet中用于分类的层之一。Softmax函数可以表示为:
y i = e x i ∑ j e x j y_i=\frac{e^{x_i}}{\sum_{j}e^{x_j}} yi=∑jexjexi
方法介绍
数据增强
AlexNet使用了一些数据增强技术,包括随机裁剪、随机水平翻转、PCA颜色增强等。
多GPU训练
AlexNet使用了两个GPU进行训练,其中每个GPU处理一半的数据。
ReLU激活函数
AlexNet使用ReLU激活函数取代了传统的Sigmoid激活函数,这使得网络的训练速度更快,同时也减少了梯度消失的问题。
Dropout正则化
AlexNet使用了Dropout正则化技术来减少过拟合。
结构图
下面是AlexNet的结构图:
计算过程
下面是AlexNet的计算过程:
import torch
# 定义输入张量
x = torch.randn(1, 3, 224, 224)
# 第一个卷积层
conv1 = torch.nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2)
relu1 = torch.nn.ReLU(inplace=True)
pool1 = torch.nn.MaxPool2d(kernel_size=3, stride=2)
x = conv1(x)
x = relu1(x)
x = pool1(x)
# 第二个卷积层
conv2 = torch.nn.Conv2d(64, 192, kernel_size=5, padding=2)
relu2 = torch.nn.ReLU(inplace=True)
pool2 = torch.nn.MaxPool2d(kernel_size=3, stride=2)
x = conv2(x)
x = relu2(x)
x = pool2(x)
# 第三个卷积层
conv3 = torch.nn.Conv2d(192, 384, kernel_size=3, padding=1)
relu3 = torch.nn.ReLU(inplace=True)
x = conv3(x)
x = relu3(x)
# 第四个卷积层
conv4 = torch.nn.Conv2d(384, 256, kernel_size=3, padding=1)
relu4 = torch.nn.ReLU(inplace=True)
x = conv4(x)
x = relu4(x)
# 第五个卷积层
conv5 = torch.nn.Conv2d(256, 256, kernel_size=3, padding=1)
relu5 = torch.nn.ReLU(inplace=True)
pool5 = torch.nn.MaxPool2d(kernel_size=3, stride=2)
x = conv5(x)
x = relu5(x)
x = pool5(x)
# 第一个全连接层
fc6 = torch.nn.Linear(256 * 6 * 6, 4096)
relu6 = torch.nn.ReLU(inplace=True)
dropout6 = torch.nn.Dropout()
x = x.view(x.size(0), 256 * 6 * 6)
x = fc6(x)
x = relu6(x)
x = dropout6(x)
# 第二个全连接层
fc7 = torch.nn.Linear(4096, 4096)
relu7 = torch.nn.ReLU(inplace=True)
dropout7 = torch.nn.Dropout()
x = fc7(x)
x = relu7(x)
x = dropout7(x)
# 第三个全连接层
fc8 = torch.nn.Linear(4096, 1000)
softmax = torch.nn.Softmax(dim=1)
x = fc8(x)
x = softmax(x)
# 输出张量
print(x)
输出结果如下:
tensor([[0.0009, 0.0009, 0.0009, ..., 0.0010, 0.0010, 0.0010]],
grad_fn=<SoftmaxBackward>)
AlexNet相较于LeNet,有以下几个重要的优势:
-
更深的网络结构:AlexNet有5层卷积层和3层全连接层,相较于LeNet的3层卷积层和2层全连接层,网络更深,可以提取更多的特征。
-
更大的数据集:AlexNet使用的是ImageNet数据集,包含1.2 million张图片,相较于LeNet的MNIST数据集,数据更加丰富,可以更好地训练模型。
-
更小的卷积核:AlexNet使用的是11x11的卷积核,相较于LeNet的5x5卷积核,可以提取更大范围内的特征,提升模型的准确率。
-
更高的计算能力:AlexNet使用了两个GPU进行并行计算,加快了训练速度,同时使用了Dropout等技术,避免了过拟合。
AlexNet的改进主要在以下几个方面:
-
使用ReLU激活函数:相较于LeNet的sigmoid激活函数,ReLU激活函数更加高效,可以加速训练。
-
使用Dropout技术:为了避免过拟合,AlexNet加入了Dropout技术,随机将一些神经元的输出置为0,可以减少神经元之间的依赖关系,提高模型的泛化能力。
-
数据增强:AlexNet在训练时使用了数据增强技术,包括裁剪、翻转、旋转等,可以增加数据的多样性,提高模型的鲁棒性。
-
局部响应归一化:AlexNet在卷积层后加入了局部响应归一化层,可以增强特征的鲁棒性,提高模型的准确率。