[ 图像分类 ] 经典网络模型1——AlexNet 详解与复现

本文详细介绍了AlexNet的网络结构和特点,它是2012年ImageNet比赛的冠军,开启了深度卷积神经网络的新篇章。网络包括5个卷积层和3个全连接层,使用ReLU激活函数、LRN、Overlapping Pooling等创新技术。文章还提供了使用PyTorch实现AlexNet的代码,并通过torchinfo库展示了模型的参数数量和计算复杂度。
摘要由CSDN通过智能技术生成

🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


[ 图像分类 ] 经典网络模型1——AlexNet 详解与复现

🚀 AlexNet

AlexNet 是2012年 Alex Krizhevsky 等人提出的一种的深度学习结构,并以其名命名;

拥有6000万个参数和65万个神经元,由5个卷积层组成,后面跟着3个全连接层和1000的softmax;

在2012年第一届 ImageNet 挑战赛(ILSVRC) classification 任务中获得 冠军, 开辟了深度卷积神经网络的新纪元;


🔗 论文地址:ImageNet Classification with Deep Convolutional Neural Networks


🚀 AlexNet 详解

🎨 AlexNet 网络结构

AlexNet 一共包含 8个学习层 —— 5个卷积层 和 3个全连接层 ;

单一映射:卷积 第 2 层、第 4 层 和 第 5 层 的核只连接到前一层的核映射上 ;

多映射:卷积 第 3 层 的核连接到 第 2 层 的所有核映射上 ;

全连接:全连接层中的神经元与 前一层中的所有神经元相连接 ;

非线性激活:每个卷积层 和 全连接层的输出应用 ReLU非线性 ;

网络结构图

AlexNex


🎨 AlexNet 网络特点

它在多方面使用了创新性的结构 ;

(1)提出了 非饱和神经元 ReLU 减小 梯度下降 的训练时间;
(2)提用了 多GPU并行卷积操作 实现模型训练 加速
(3)提用了 LRN(Local Response Normalization) 实现局部响应 归一化
(4)提出了 Overlapping Pooling 使用 stride=2,kernal_size=3 使池化重叠,优于之前的 stride=2,kernal_size=2 ;
(5)引入了 dropout 正则化方法减少 全连接层中的 过拟合
(6)此外,还采用 数据增强 的方法扩充数据集用以减小 过拟合 线性;

论文中 数据增强 采用的方式 :1、图像的平移和水平旋转;2、改变图像RGB通道的强度;


🚀 AlexNet 复现

# Here is the code :

import torch
import torch.nn as nn
from torchinfo import summary


class AlexNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(48, 128, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(128, 192, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.classifier = nn.Sequential(
            nn.Linear(128 * 6 * 6, 2048),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(2048, 2048),
            nn.ReLU(inplace=True),
            nn.Linear(2048, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = torch.flatten(x, 1)
        out = self.classifier(x)
        return out


def test():
    net = AlexNet()
    y = net(torch.randn(1, 3, 224, 224))
    print(y.size())
    summary(net, (1, 3, 224, 224))


if __name__ == '__main__':
    test()

输出结果:

torch.Size([1, 1000])
==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
AlexNet                                  --                        --
├─Sequential: 1-1                        [1, 128, 6, 6]            --
│    └─Conv2d: 2-1                       [1, 48, 55, 55]           17,472
│    └─ReLU: 2-2                         [1, 48, 55, 55]           --
│    └─MaxPool2d: 2-3                    [1, 48, 27, 27]           --
│    └─Conv2d: 2-4                       [1, 128, 27, 27]          153,728
│    └─ReLU: 2-5                         [1, 128, 27, 27]          --
│    └─MaxPool2d: 2-6                    [1, 128, 13, 13]          --
│    └─Conv2d: 2-7                       [1, 192, 13, 13]          221,376
│    └─ReLU: 2-8                         [1, 192, 13, 13]          --
│    └─Conv2d: 2-9                       [1, 192, 13, 13]          331,968
│    └─ReLU: 2-10                        [1, 192, 13, 13]          --
│    └─Conv2d: 2-11                      [1, 128, 13, 13]          221,312
│    └─ReLU: 2-12                        [1, 128, 13, 13]          --
│    └─MaxPool2d: 2-13                   [1, 128, 6, 6]            --
├─Sequential: 1-2                        [1, 1000]                 --
│    └─Linear: 2-14                      [1, 2048]                 9,439,232
│    └─ReLU: 2-15                        [1, 2048]                 --
│    └─Dropout: 2-16                     [1, 2048]                 --
│    └─Linear: 2-17                      [1, 2048]                 4,196,352
│    └─ReLU: 2-18                        [1, 2048]                 --
│    └─Linear: 2-19                      [1, 1000]                 2,049,000
==========================================================================================
Total params: 16,630,440
Trainable params: 16,630,440
Non-trainable params: 0
Total mult-adds (M): 311.52
==========================================================================================
Input size (MB): 0.60
Forward/backward pass size (MB): 2.64
Params size (MB): 66.52
Estimated Total Size (MB): 69.77
==========================================================================================


  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
AlexNet是由Alex Krizhevsky等人在2012年提出的一个深度卷积神经网络(Convolutional Neural Network,CNN),它在ImageNet图像识别挑战赛中取得了显著突破,首次击败了传统的计算机视觉方法。这标志着深度学习在计算机视觉领域的重大进展。 在TensorFlow框架中,AlexNet可以被用来作为预训练模型,用于迁移学习任务,即在一个大型数据集(如ImageNet)上训练好的模型,在新的、具有相似任务的小规模数据集上进行微调。TensorFlow库提供了方便的接口,如`tf.keras.applications.AlexNet()`,可以直接加载预训练的AlexNet模型,并允许用户进行前向传播或对某些层进行修改和训练。 以下是使用AlexNet的基本步骤: 1. **导入模型**: ```python from tensorflow.keras.applications import AlexNet model = AlexNet(weights='imagenet') # 加载预训练权重 ``` 2. **冻结层**(如果不需要训练整个网络): ```python for layer in model.layers[:-10]: # 冻结除最后几层之外的层 layer.trainable = False ``` 3. **添加新层**(如果需要自定义输出): ```python new_output_layer = tf.keras.layers.Dense(units=...)(model.output) model = tf.keras.Model(inputs=model.input, outputs=new_output_layer) ``` 4. **编译和训练**(替换为新数据集): ```python model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=..., validation_data=(x_val, y_val)) ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值