卷积神经网络模型之——AlexNet网络结构与代码实现

卷积神经网络模型之——AlexNet网络结构与代码实现AlexNet 原文地址 https proceedings neurips cc paper 2012 file cd3b9d6b76c8 Paper pdfAlexNet 诞生于 2012 年 由 2012 年 ImageNet 竞赛冠军获得者 Hinton 和他的学生 AlexKrizhevs 设计的 AlexNet 的贡献点 首次使用 GPU 加速网络训练使用 ReLU 激活函数 代替不是传统的 Sigmoid 和 Tanh 解决了 Sigmoid 的梯度消失问题 使收敛更快 训练时使用

AlexNet简介

AlexNet的贡献点:

  • 首次使用GPU加速网络训练
  • 使用ReLU激活函数,代替不是传统的Sigmoid和Tanh,解决了Sigmoid的梯度消失问题,使收敛更快。
    在这里插入图片描述
  • 训练时使用Dropout随机忽略一部分神经元,以减少模型过拟合。
    在这里插入图片描述
  • 使用了LRN局部响应归一化提高准确率。
  • 在CNN中使用重叠的最大池化,提升了特征的丰富性。

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)

在这里插入图片描述

卷积之后跟着一个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 )

在这里插入图片描述

卷积之后跟着一个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)

在这里插入图片描述

卷积之后跟着一个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之后进行一次ReLU激活。

PyTorch表述本层为:

nn.Dropout(p=0.5), nn.Linear(128 * 6 * 6, 2048), nn.ReLU(inplace=True), 

FC2

在这里插入图片描述

全连接FC2之前先进行一次Dropout。

FC1之后进行一次ReLU激活。

PyTorch表述本层为:

nn.Dropout(p=0.5), nn.Linear(2048, 2048), nn.ReLU(inplace=True), 

FC3

在这里插入图片描述

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) 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230334.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java常用的几种属性拷贝工具类使用总结

    怕什么真理无穷,进一步有近一步的欢喜文章目录开头聊几句Java属性拷贝工具类使用总结字段和属性使用说明**org.springframework.beans.BeanUtils#copyProperties**org.apache.commons.beanutils.PropertyUtils#_copyProperties_org.apache.commons.beanutils.BeanUtils#_copyProperties原理探索Spring#BeanUtilsapache.commons#.

    2022年3月1日
    68
  • mysql查看查询慢的语句_sql慢查询如何优化

    mysql查看查询慢的语句_sql慢查询如何优化Mysql慢查询设置分析MySQL语句查询性能的方法除了使用EXPLAIN输出执行计划,还可以让MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”。=========================================================方法一:这个方法我正在用,呵呵,比较喜欢这种即时性的。Mysql5.0以上的版本可以支持将执行…

    2022年10月14日
    3
  • STM32单片机介绍_基于单片机的智能循迹小车

    STM32单片机介绍_基于单片机的智能循迹小车      其实前几周就已经将小车调好并且也发了视频,但是每天忙于复习,也没有时间来对小车的流程设计、硬件设计、程序编写进行一个总结,正好周五可以休息一下,就分三个模块对这个智能小车项目进行一个总结。      这个小项目完全是我一个人搞得,因为组内的小伙伴们都还没学32,他们使用arduino搭的小车,但是毕竟实现的功能还是很简单的,也只是做了一周不到的时间。      首先是小车的…

    2022年10月18日
    5
  • Image Thresholding

    Image Thresholding摘自https://docs.opencv.org/4.2.0/d7/d4d/tutorial_py_thresholding.htmlSimpleThresholdingThefunctioncv.thresholdisusedtoapplythethresholding.Thefirstargumentisthesourceimage,whichsh…

    2022年4月30日
    72
  • pycharm2021.4.3激活破解方法

    pycharm2021.4.3激活破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    181
  • EasyBoot用户名注册码

    EasyBoot用户名注册码用户名:中华人民共和国注册码:2898-5448-5603-BB2D

    2022年7月4日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号