这篇论文在2012年发表,文章中的模型AlexNet,参加的竞赛是ImageNet LSVRC-2010,该ImageNet数据集有1.2 million幅高分辨率图像,总共有1000个类别。测试集分为top-1和top-5,并且分别拿到了37.5%和17%的error rates,CNN重新引起人们的关注。下面就AlexNet做一下简要的分析。也是记录一下今天一天的学习。
1、AlexNet的特点:
1:AlexNet一共有八层,五个卷积层和三个全连接层。由于是对ImageNet数据集进行分类,所以最后一层的输出会接上softmax,一共1000个输出(ImageNet一共有1000个类别),softmax会产生1000类标签。
2:在AlexNet中,使用RuLu函数来增加模型的非线性能力。
3:使用了Dropout训练期间选择性的暂时忽略一些神经元,来减小模型的过拟合。(Dropout可以减小模型的过拟合)
4:局部响应归一化层(LRN):提高精度,LRN现在好像用得不多了,一般使用BN。
5:双GPU并行运行,提高训练速度。
下图就是AlexNet的网络构架,下面就其进行简要分析:
输入图像:在论文输入图像的大小是224224,但是在网上有人应该是227227.其实我认为两个都有其道理,假如说是224224的话,那么在第一层的卷积核中将padding设置为2即可,假如是227227的话,padding设置为0;
我用pytorch中的torchvision.model调用了AlexNet,然后将其网络架构输出,如下:
2、AlexNet具体实现过程
3、AlexNet实现代码
pytorch代码实现
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 96,11,4,0) # 定义conv1函数的是图像卷积函数:输入为图像(3个频道,即彩色图),输出为6张特征图, 卷积核为5x5正方形
self.pool = nn.MaxPool2d(3, 2)
self.conv2 = nn.Conv2d(96, 256, 5, 1, 2)
self.Conv3 = nn.Conv2d(256, 384, 3, 1, 1)
self.Conv4 = nn.Conv2d(384,384, 3, 1, 1)
self.Conv5 = nn.Conv2d(384,256, 3, 1, 1)
self.drop = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 4096)
self.fc2 = nn.Linear(4096, 4096)
self.fc3 = nn.Linear(4096, 100)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = self.pool(F.relu(self.conv5(x)))
x = x.view(-1, self.num_flat_features(x))
x = self.drop(x)
x = self.drop(F.relu(self.fc1(x)))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
打印的结果如下:
或其他的方式如下: