深度学习网络——alexnet
论文链接:http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf
代码链接:https://pytorch.org/docs/stable/_modules/torchvision/models/alexnet.html
参考链接:
ImageNet Classification with Deep Convolutional Neural Networks 译
卷积神经网络之AlexNet
初探Alexnet网络结构
# -*- coding:UTF-8 -*-
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
__all__ = ['AlexNet', 'alexnet']
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
}
class AlexNet(nn.Module):
def __init__(self, num_classes=1000):
# super 加载父类中的__init__()函数
super(AlexNet, self).__init__()
self.features = nn.Sequential(
# image_size: n * n
# kernel_size: k * k
# padding: p
# stride: s
# out_size = (n + 2 * p - k) / S + 1
# 输入为 3x227x277 的图片
# out = (227 + 2 * 2 - 11) / 4 + 1 = 56
# 3x227x277 ==> 64x56x56
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
# out = (56 + 0 - 3) / 2 + 1 = 27
# 64x56x56 ==> 64x27x27
nn.MaxPool2d(kernel_size=3, stride=2),
# out = (27 + 2 * 2 - 5) / s + 1 = 27
# 64x27x27 ==> 192x27x27
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
# out = (27 + 0 - 3) / 2 + 1 = 13
# 192x27x27 ==> 192x13x13
nn.MaxPool2d(kernel_size=3, stride=2),
# out = (13 + 2 * 1 - 3) / 1 + 1 = 13
# 192x13x13 ==> 384x13x13
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# out = (13 + 2 * 1 - 3) / 1 + 1 = 13
# 384x13x13 ==> 256x13x13
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# out = (13 + 2 * 1 - 3) / 1 + 1 = 13
# 256x13x13 ==> 256x13x13
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
# out = (13 + 0 - 3) / 2 + 1 = 6
# 256x13x13 ==> 256x6x6
nn.MaxPool2d(kernel_size=3, stride=2),
)
# https://www.zhihu.com/question/282046628
# 在实际的项目当中,我们往往预先只知道的是输入数据和输出数据的大小,而不知道核与步长的大小。
# 如果使用上面的方法创建汇聚层,我们每次都需要手动计算核的大小和步长的值。
# 而自适应(Adaptive)能让我们从这样的计算当中解脱出来,只要我们给定输入数据和输出数据的大小,自适应算法能够自动帮助我们计算核的大小和每次移动的步长。
# 相当于我们对核说,我已经给你输入和输出的数据了,你自己适应去吧。
# 你要长多大,你每次要走多远,都由你自己决定,总之最后你的输出符合我的要求就行了。
self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
# 分类器
self.classifier = nn.Sequential(
# 失活层
nn.Dropout(),
# 线性层
nn.Linear(256 * 6 * 6, 4096),
# relu 激活层
nn.ReLU(inplace=True),
# 失活层
nn.Dropout(),
# 线性层
nn.Linear(4096, 4096),
# relu 激活层
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
def alexnet(pretrained=False, **kwargs):
r"""AlexNet model architecture from the
`"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = AlexNet(**kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['alexnet']))
return model
if __name__ == '__main__':
net = alexnet()
print(net)