2021-11-04 16:44:38 +08:00
|
|
|
|
# 特征图可视化指南
|
2021-11-25 15:40:40 +08:00
|
|
|
|
-----
|
|
|
|
|
## 目录
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
* [1. 概述](#1)
|
|
|
|
|
* [2. 准备工作](#2)
|
|
|
|
|
* [3. 修改模型](#3)
|
|
|
|
|
* [4. 结果](#4)
|
|
|
|
|
|
|
|
|
|
<a name='1'></a>
|
|
|
|
|
|
|
|
|
|
## 1. 概述
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
|
|
|
|
特征图是输入图片在卷积网络中的特征表达,对特征图的研究可以有利于我们对于模型的理解与设计,所以基于动态图我们使用本工具来可视化特征图。
|
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
<a name='2'></a>
|
|
|
|
|
|
|
|
|
|
## 2. 准备工作
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2022-09-16 16:48:16 +08:00
|
|
|
|
首先需要选定研究的模型,本文设定 ResNet50 作为研究模型,将模型组网代码[resnet.py](../../../../ppcls/arch/backbone/legendary_models/resnet.py)拷贝到[目录](../../../../ppcls/utils/feature_maps_visualization/)下,并下载[ResNet50 预训练模型](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_pretrained.pdparams),或使用以下命令下载。
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2021-11-04 17:45:44 +08:00
|
|
|
|
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_pretrained.pdparams
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-09-16 14:41:19 +08:00
|
|
|
|
其他模型网络结构代码及预训练模型请自行下载:[模型库](../../../ppcls/arch/backbone/),[预训练模型](../../models/ImageNet1k/model_list.md)。
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
<a name='3'></a>
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
## 3. 修改模型
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
找到我们所需要的特征图位置,设置 self.fm 将其 fetch 出来,本文以 resnet50 中的 stem 层之后的特征图为例。
|
|
|
|
|
|
|
|
|
|
在 ResNet50 的 forward 函数中指定要可视化的特征图
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
|
|
|
|
```python
|
2021-11-04 17:45:44 +08:00
|
|
|
|
def forward(self, x):
|
|
|
|
|
with paddle.static.amp.fp16_guard():
|
|
|
|
|
if self.data_format == "NHWC":
|
|
|
|
|
x = paddle.transpose(x, [0, 2, 3, 1])
|
|
|
|
|
x.stop_gradient = True
|
|
|
|
|
x = self.stem(x)
|
|
|
|
|
fm = x
|
|
|
|
|
x = self.max_pool(x)
|
|
|
|
|
x = self.blocks(x)
|
|
|
|
|
x = self.avg_pool(x)
|
|
|
|
|
x = self.flatten(x)
|
|
|
|
|
x = self.fc(x)
|
|
|
|
|
return x, fm
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```
|
2021-11-04 17:45:44 +08:00
|
|
|
|
|
2022-09-16 14:41:19 +08:00
|
|
|
|
然后修改代码[fm_vis.py](../../../../ppcls/utils/feature_maps_visualization/fm_vis.py),引入 `ResNet50`,实例化 `net` 对象:
|
2021-11-04 17:45:44 +08:00
|
|
|
|
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```python
|
2021-11-04 17:45:44 +08:00
|
|
|
|
from resnet import ResNet50
|
|
|
|
|
net = ResNet50()
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```
|
2021-11-04 17:45:44 +08:00
|
|
|
|
|
|
|
|
|
最后执行函数
|
|
|
|
|
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```bash
|
2021-11-04 19:41:10 +08:00
|
|
|
|
python tools/feature_maps_visualization/fm_vis.py \
|
|
|
|
|
-i the image you want to test \
|
|
|
|
|
-c channel_num -p pretrained model \
|
|
|
|
|
--show whether to show \
|
|
|
|
|
--interpolation interpolation method\
|
|
|
|
|
--save_path where to save \
|
|
|
|
|
--use_gpu whether to use gpu
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```
|
2021-11-04 17:45:44 +08:00
|
|
|
|
|
2021-11-04 16:44:38 +08:00
|
|
|
|
参数说明:
|
|
|
|
|
+ `-i`:待预测的图片文件路径,如 `./test.jpeg`
|
2021-11-04 17:45:44 +08:00
|
|
|
|
+ `-c`:特征图维度,如 `5`
|
2021-11-04 19:41:10 +08:00
|
|
|
|
+ `-p`:权重文件路径,如 `./ResNet50_pretrained`
|
2021-11-26 19:28:16 +08:00
|
|
|
|
+ `--interpolation`: 图像插值方式,默认值 1
|
2021-11-04 16:44:38 +08:00
|
|
|
|
+ `--save_path`:保存路径,如:`./tools/`
|
|
|
|
|
+ `--use_gpu`:是否使用 GPU 预测,默认值:True
|
|
|
|
|
|
2021-11-25 15:40:40 +08:00
|
|
|
|
<a name='4'></a>
|
|
|
|
|
|
|
|
|
|
## 4. 结果
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
|
|
|
|
* 输入图片:
|
|
|
|
|
|
2022-09-16 14:41:19 +08:00
|
|
|
|

|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
|
|
|
|
* 运行下面的特征图可视化脚本
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
python tools/feature_maps_visualization/fm_vis.py \
|
|
|
|
|
-i ./docs/images/feature_maps/feature_visualization_input.jpg \
|
|
|
|
|
-c 5 \
|
|
|
|
|
-p pretrained/ResNet50_pretrained/ \
|
|
|
|
|
--show=True \
|
|
|
|
|
--interpolation=1 \
|
|
|
|
|
--save_path="./output.png" \
|
2021-11-04 17:45:44 +08:00
|
|
|
|
--use_gpu=False
|
2021-11-04 16:44:38 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-26 19:28:16 +08:00
|
|
|
|
* 输出特征图保存为 `output.png`,如下所示。
|
2021-11-04 16:44:38 +08:00
|
|
|
|
|
2022-09-16 14:41:19 +08:00
|
|
|
|

|