当前位置:网站首页>卷积神经网络模型之——AlexNet网络结构与代码实现
卷积神经网络模型之——AlexNet网络结构与代码实现
2022-07-25 12:43:00 【1 + 1=王】
文章目录
AlexNet简介
AlexNet原文地址:https://proceedings.neurips.cc/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.pdf
AlexNet诞生于2012年,由2012年ImageNet竞赛冠军获得者Hinton和他的学生Alex Krizhevsky设计的。
AlexNet的贡献点:
首次使用GPU加速网络训练
使用ReLU激活函数,代替不是传统的Sigmoid和Tanh,解决了Sigmoid的梯度消失问题,使收敛更快。

训练时使用Dropout随机忽略一部分神经元,以减少模型过拟合。

使用了LRN局部响应归一化提高准确率。
在CNN中使用重叠的最大池化,提升了特征的丰富性。
AlexNet网络的原始输入图像大小为【3,224,224】,由5个卷积层、3个池化层和3个全连接层构成,并且在每一个卷积层和全连接层之后都进行一次ReLU激活。其中的3个池化层分别跟在第1、第2和第5个卷积层的激活之后。网络结构图如下:
AlexNet网络结构解析
卷积层1(Conv + ReLU + MaxPool)

Conv1使用卷积核大小为11,步距为4,padding为2。
输入大小:【3,224,224】
输出大小:【48,55,55】
N = (W-F+2P)/ S + 1 = (224-11+2*2)/4+1=55。
卷积之后跟着一个ReLU激活,激活之后接着一个最大池化上采样,池化核大小为3,步距为2,池化后输出大小为【48,27,27】。
PyTorch表述本层为:
# input[3, 224, 224]
nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2), # output[48, 55, 55]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[48, 27, 27]
卷积层2(Conv + ReLU + MaxPool)

Conv2使用卷积核大小为5,步距为1,padding为2。
输入大小:【48,27,27】
输出大小:【128,27,27】
N = (W-F+2P)/ S + 1 = (27-5+2*2)/1+1=27。
卷积之后跟着一个ReLU激活,激活之后接着一个最大池化上采样,池化核大小为3,步距为2,池化后输出大小为【128,13,13】。
PyTorch表述本层为:
# input[48,27,27]
nn.Conv2d(48, 128, kernel_size=5, padding=2), # output[128, 27, 27]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 13, 13]
卷积层3(Conv + ReLU )

Conv3使用卷积核大小为3,步距为1,padding为1。
输入大小:【128, 13, 13】
输出大小:【192,13,13】
N = (W-F+2P)/ S + 1 = (13-3+1*2)/1+1=13。
卷积之后跟着一个ReLU激活,没有池化。
PyTorch表述本层为:
# input[128, 13, 13]
nn.Conv2d(128, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
卷积层4(Conv + ReLU )

Conv4使用卷积核大小为3,步距为1,padding为1。
输入大小:【192, 13, 13】
输出大小:【192,13,13】
N = (W-F+2P)/ S + 1 = (13-3+1*2)/1+1=13。
卷积之后跟着一个ReLU激活,没有池化。
PyTorch表述本层为:
# input[192, 13, 13]
nn.Conv2d(192, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
卷积层5(Conv + ReLU + MaxPool)

Conv5使用卷积核大小为3,步距为1,padding为1。
输入大小:【192, 13, 13】
输出大小:【128,13,13】
N = (W-F+2P)/ S + 1 = (13-3+1*2)/1+1=13。
卷积之后跟着一个ReLU激活,激活之后接着一个最大池化上采样,池化核大小为3,步距为2,池化后输出大小为【128,6,6】。
PyTorch表述本层为:
# input[192, 13, 13]
nn.Conv2d(192, 128, kernel_size=3, padding=1), # output[128, 13, 13]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 6, 6]
第5个卷积层之后,就是三个全连接层。
FC1

全连接FC1之前先进行一次Dropout。
FC1使用4096个神经元,对128个大小为66的特征图,进行一个全连接。
输入大小:【12866】
输出大小:【2048】
FC1之后进行一次ReLU激活。
PyTorch表述本层为:
nn.Dropout(p=0.5),
nn.Linear(128 * 6 * 6, 2048),
nn.ReLU(inplace=True),
FC2

全连接FC2之前先进行一次Dropout。
FC1使用2048个神经元,对2048特征图,进行一个全连接。
输入大小:【2048】
输出大小:【2048】
FC1之后进行一次ReLU激活。
PyTorch表述本层为:
nn.Dropout(p=0.5),
nn.Linear(2048, 2048),
nn.ReLU(inplace=True),
FC3

FC3是AlexNet的输出层,输出大小为1000,对应1000个类别。
PyTorch表述本层为:
nn.Linear(2048, 1000),
使用PyTorch搭建AlexNet网络结构
前面在网络结构解析的时候,都已经给出了每一层的代码表述。
init
这里我们使用nn.Sequential将5个卷积层放在一起,定义为features(义为提取特征);将3个全连接层放在一起,定义为classifier(义为分类)。
def __init__(self):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2), # output[48, 55, 55]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[48, 27, 27]
nn.Conv2d(48, 128, kernel_size=5, padding=2), # output[128, 27, 27]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 13, 13]
nn.Conv2d(128, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 128, kernel_size=3, padding=1), # output[128, 13, 13]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 6, 6]
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(128 * 6 * 6, 2048),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(2048, 2048),
nn.ReLU(inplace=True),
nn.Linear(2048, num_classes),
)
forward
然后定义正向传播过程
def forward(self, x):
x = self.features(x) # 5个卷积层
x = torch.flatten(x, start_dim=1) # 将3维展平成一维,进行全连接
x = self.classifier(x) # 3个全连接层
return x
完整代码
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2), # output[48, 55, 55]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[48, 27, 27]
nn.Conv2d(48, 128, kernel_size=5, padding=2), # output[128, 27, 27]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 13, 13]
nn.Conv2d(128, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 192, kernel_size=3, padding=1), # output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 128, kernel_size=3, padding=1), # output[128, 13, 13]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2), # output[128, 6, 6]
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(128 * 6 * 6, 2048),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(2048, 2048),
nn.ReLU(inplace=True),
nn.Linear(2048, 1000),
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
model = AlexNet();
print(model)
打印结构:
边栏推荐
- ORAN专题系列-21:主要的玩家(设备商)以及他们各自的态度、擅长领域
- 零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
- Deep learning MEMC framing paper list
- AtCoder Beginner Contest 261 F // 树状数组
- Use of hystrix
- [300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
- I register the absolutely deleted data in the source sqlserver, send it to maxcomputer, and write the absolute data when cleaning the data
- 卷积神经网络模型之——VGG-16网络结构与代码实现
- I want to ask whether DMS has the function of regularly backing up a database?
- Software testing interview question: Please list the testing methods of several items?
猜你喜欢

A turbulent life

LeetCode 1184. 公交站间的距离

2022.07.24 (lc_6126_design food scoring system)

深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】

How to understand metrics in keras
![[shutter -- layout] stacked layout (stack and positioned)](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] stacked layout (stack and positioned)

Substance Designer 2021软件安装包下载及安装教程

The larger the convolution kernel, the stronger the performance? An interpretation of replknet model

工业互联网的内涵及其应用

Kyligence was selected into Gartner 2022 data management technology maturity curve report
随机推荐
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
Plus SBOM: assembly line BOM pbom
ECCV2022 | TransGrasp类级别抓取姿态迁移
吕蒙正《破窑赋》
Docker学习 - Redis集群-3主3从-扩容-缩容搭建
Shell常用脚本:获取网卡IP地址
go : gin 自定义日志输出格式
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
What is ci/cd?
CONDA common commands: install, update, create, activate, close, view, uninstall, delete, clean, rename, change source, problem
LeetCode 0133. 克隆图
Eccv2022 | transclassp class level grab posture migration
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Mysql 远程连接权限错误1045问题
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary problem solution (g, J, K, l)
程序员奶爸自制AI喂奶检测仪,预判宝宝饿点,不让哭声影响老婆睡眠
【运维、实施精品】月薪10k+的技术岗位面试技巧
MySQL implements inserting data from one table into another table
Word style and multi-level list setting skills (II)
【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.