现代卷积神经网络
| 模型 | 核心思想 | 解决问题 | 关键词 |
|---|---|---|---|
| AlexNet | 深层CNN + GPU | CNN重新崛起 | ReLU、Dropout |
| VGG | 重复小卷积 | 结构统一 | 3×3卷积堆叠 |
| NiN | 用1×1卷积代替FC | 降参数 | MLPConv |
| GoogLeNet | 多分支并行 | 多尺度特征 | Inception |
| ResNet | 残差连接 | 解决深层退化 | skip connection |
| DenseNet | 全连接特征复用 | 最大信息流 | dense connection |
AlexNet
CNN 是一个“逐层抽象”的特征学习系统
- 低层:边缘 / 颜色 / 纹理
- 中层:局部结构,比如:鼻子,眼睛,轮廓
- 高层:语义特征, 比如: 人 狗 飞机
深度学习之所以在2012年后爆发,是因为ImageNet提供了前所未有的大规模标注数据,使得高参数量的深度模型能够真正发挥优势,从而全面超越传统依赖小数据和人工特征的方法。
李飞飞通过构建 ImageNet,把“数据规模”带入计算机视觉,使深度学习模型第一次在真实世界任务中大幅超越传统方法,从而引爆了整个AI时代。
AlexNet和LeNet 对比
| 维度 | LeNet | AlexNet |
|---|---|---|
| 年代 | 1998 | 2012 |
| 任务 | 手写数字 | ImageNet(1000类) |
| 输入 | 28×28 灰度 | 224×224 RGB |
| 深度 | 浅(5层左右) | 深(8层) |
| 参数量 | ~6万 | ~6000万 |
| 激活函数 | Sigmoid | ReLU |
| 池化 | AvgPool | MaxPool |
| 数据规模 | 小数据集 | 大规模数据 |
| 训练设备 | CPU | GPU |
| 影响力 | 开创CNN | 引爆深度学习 |
LeNet 证明了卷积神经网络“可行”,而 AlexNet 证明了它在大数据和GPU支持下“可以成为最强方法”,从而开启了深度学习时代。
AlexNet模型文字图
Input: 224×224×3 (RGB Image)
↓
Conv1: 96 filters, 11×11, stride=4, padding=0
→ ReLU
→ MaxPool: 3×3, stride=2
↓
Conv2: 256 filters, 5×5, stride=1, padding=2
→ ReLU
→ MaxPool: 3×3, stride=2
↓
Conv3: 384 filters, 3×3, stride=1, padding=1
→ ReLU
↓
Conv4: 384 filters, 3×3, stride=1, padding=1
→ ReLU
↓
Conv5: 256 filters, 3×3, stride=1, padding=1
→ ReLU
→ MaxPool: 3×3, stride=2
↓
Flatten
↓
FC6: 4096
→ ReLU
→ Dropout
↓
FC7: 4096
→ ReLU
→ Dropout
↓
FC8: 1000 (类别数)
↓
Softmax
训练模型
import torch
from torch import nn
from d2l import torch as d2l
import time
# 使用 nn.Sequential 搭建 AlexNet 风格网络(针对 Fashion-MNIST 的 10 分类任务)
net = nn.Sequential(
# 这里使用一个11*11的更大窗口来捕捉对象。
# 同时,步幅为4,以减少输出的高度和宽度。
# 另外,输出通道的数目远大于LeNet
nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2),
# 减小卷积窗口,使用填充为2来使得输入与输出的高和宽一致,且增大输出通道数
nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2),
# 使用三个连续的卷积层和较小的卷积窗口。
# 除了最后的卷积层,输出通道的数量进一步增加。
# 在前两个卷积层之后,汇聚层不用于减少输入的高度和宽度
nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),
nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Flatten(),
# 这里,全连接层的输出数量是LeNet中的好几倍。使用dropout层来减轻过拟合
nn.Linear(6400, 4096), nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(4096, 4096), nn.ReLU(),
nn.Dropout(p=0.5),
# 最后是输出层。由于这里使用Fashion-MNIST,所以用类别数为10,而非论文中的1000
nn.Linear(4096, 10))
# 构造一个形状为 (batch_size, channel, height, width) = (1, 1, 224, 224) 的随机输入
# 通过逐层前向传播,打印每一层输出形状,便于理解特征图尺寸如何变化
X = torch.randn(1, 1, 224, 224)
for layer in net:
X=layer(X)
print(layer.__class__.__name__,'output shape:\t',X.shape)
# 读取 Fashion-MNIST 数据集,并将图像统一缩放到 224x224(与 AlexNet 输入尺寸匹配)
batch_size = 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
# 设置训练超参数:
# - lr:学习率
# - num_epochs:训练轮数
lr, num_epochs = 0.01, 10
start_time = time.time()
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
end_time = time.time()
print(f"训练总时长: {end_time - start_time:.2f} 秒")
| 层 | 操作 | 输出尺寸 | 作用 |
|---|---|---|---|
| 输入 | - | 1×224×224 | 灰度图 |
| Conv1 | 96×11×11, s=4 | 96×54×54 | 大感受野 |
| Pool1 | 3×3, s=2 | 96×26×26 | 降采样 |
| Conv2 | 256×5×5 | 256×26×26 | 提取复杂特征 |
| Pool2 | 3×3, s=2 | 256×12×12 | 再降采样 |
| Conv3 | 384×3×3 | 384×12×12 | 深层特征 |
| Conv4 | 384×3×3 | 384×12×12 | 特征组合 |
| Conv5 | 256×3×3 | 256×12×12 | 高级特征 |
| Pool3 | 3×3, s=2 | 256×5×5 | 压缩 |
| Flatten | - | 6400 | 展平 |
| FC1 | 4096 | 4096 | 高维表示 |
| FC2 | 4096 | 4096 | 深层表达 |
| FC3 | 10 | 10 | 分类 |
运行结果
Conv2d output shape: torch.Size([1, 96, 54, 54])
ReLU output shape: torch.Size([1, 96, 54, 54])
MaxPool2d output shape: torch.Size([1, 96, 26, 26])
Conv2d output shape: torch.Size([1, 256, 26, 26])
ReLU output shape: torch.Size([1, 256, 26, 26])
MaxPool2d output shape: torch.Size([1, 256, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 384, 12, 12])
ReLU output shape: torch.Size([1, 384, 12, 12])
Conv2d output shape: torch.Size([1, 256, 12, 12])
ReLU output shape: torch.Size([1, 256, 12, 12])
MaxPool2d output shape: torch.Size([1, 256, 5, 5])
Flatten output shape: torch.Size([1, 6400])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 4096])
ReLU output shape: torch.Size([1, 4096])
Dropout output shape: torch.Size([1, 4096])
Linear output shape: torch.Size([1, 10])
training on cuda:0
loss 0.328, train acc 0.881, test acc 0.881
2568.7 examples/sec on cuda:0
RTX3060 训练总时长: 290.84 秒
0
次点赞