6.3.1 CNN 路线图:把图像变成特征图
CNN 学习局部视觉模式。它不是把图像直接摊平成一行,而是扫描小区域并生成特征图。
先看图像流


| 概念 | 第一层意思 |
|---|---|
| channel | 颜色或学到的特征维度 |
| kernel | 小的滑动滤波器 |
| feature map | 滤波器扫过图像后的输出 |
| pooling / stride | 缩小空间尺寸 |
| transfer learning | 复用预训练视觉骨干 |
跑一次卷积
创建 cnn_first_loop.py,安装 torch 后运行。
import torch
image = torch.randn(1, 3, 32, 32)
conv = torch.nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, padding=1)
features = conv(image)
print("input_shape:", tuple(image.shape))
print("feature_shape:", tuple(features.shape))
预期输出:
input_shape: (1, 3, 32, 32)
feature_shape: (1, 8, 32, 32)
把形状读成 [batch, channels, height, width]。卷积把 3 个输入通道变成了 8 个学习到的特征通道。
按这个顺序学
| 顺序 | 阅读 | 练什么 |
|---|---|---|
| 1 | 6.3.2 卷积基础 | kernel、stride、padding、channel |
| 2 | 6.3.3 CNN 结构 | 卷积块、池化、分类头 |
| 3 | 6.3.4 经典架构 | LeNet、AlexNet、VGG、ResNet 直觉 |
| 4 | 6.3.5 迁移学习 | 冻结骨干、微调 |
| 5 | 6.3.6 图像分类实战 | 数据集、训练、预测样例 |
通过标准
能解释输入图像形状和特征图形状之间发生了什么变化,并知道为什么小数据集常复用预训练 CNN 骨干,就算通过。