Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
d6b920b4
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
接近 2 年 前同步成功
通知
116
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d6b920b4
编写于
11月 04, 2021
作者:
G
gaotingquan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: adapt to release 2.3
上级
ea5b7254
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
458 addition
and
221 deletion
+458
-221
docs/zh_CN/others/feature_visiualization.md
docs/zh_CN/others/feature_visiualization.md
+30
-28
ppcls/utils/feature_maps_visualization/download_resnet50_pretrained.sh
...eature_maps_visualization/download_resnet50_pretrained.sh
+0
-2
ppcls/utils/feature_maps_visualization/fm_vis.py
ppcls/utils/feature_maps_visualization/fm_vis.py
+3
-8
ppcls/utils/feature_maps_visualization/resnet.py
ppcls/utils/feature_maps_visualization/resnet.py
+425
-183
未找到文件。
docs/zh_CN/others/feature_visiualization.md
浏览文件 @
d6b920b4
...
...
@@ -6,43 +6,45 @@
## 二、准备工作
首先需要选定研究的模型,本文设定ResNet50作为研究模型,将
resnet.py从
[
模型库
](
../../../ppcls/arch/architecture/
)
拷贝到当前目录下,并下载预训练模型
[
预训练模型
](
../../zh_CN/models/models_intro
)
, 复制resnet50的模型链接,使用下列命令下载并解压预训练模型
。
首先需要选定研究的模型,本文设定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
)
,或使用以下命令下载
。
```
bash
wget The Link
for
Pretrained Model
tar
-xf
Downloaded Pretrained Model
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_pretrained.pdparams
```
以resnet50为例:
```
bash
wget https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar
tar
-xf
ResNet50_pretrained.tar
```
其他模型网络结构代码及预训练模型请自行下载:
[
模型库
](
../../../ppcls/arch/backbone/
)
,
[
预训练模型
](
../models/models_intro.md
)
。
## 三、修改模型
找到我们所需要的特征图位置,设置self.fm将其fetch出来,本文以resnet50中的stem层之后的特征图为例。
在
fm_vis.py中修改模型的名字。
在
ResNet50的forward函数中指定要可视化的特征图
在ResNet50的__init__函数中定义self.fm
```
python
self
.
fm
=
None
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
```
在ResNet50的forward函数中指定特征图
然后修改代码
[
fm_vis.py
](
../../../ppcls/utils/feature_maps_visualization/fm_vis.py
)
,引入
`ResNet50`
,实例化
`net`
对象:
```
python
def
forward
(
self
,
inputs
):
y
=
self
.
conv
(
inputs
)
self
.
fm
=
y
y
=
self
.
pool2d_max
(
y
)
for
bottleneck_block
in
self
.
bottleneck_block_list
:
y
=
bottleneck_block
(
y
)
y
=
self
.
avg_pool
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_output
])
y
=
self
.
out
(
y
)
return
y
,
self
.
fm
from
resnet
import
ResNet50
net
=
ResNet50
()
```
执行函数
最后执行函数
```
bash
python tools/feature_maps_visualization/fm_vis.py
-i
the image you want to
test
\
-c
channel_num
-p
pretrained model
\
...
...
@@ -51,9 +53,10 @@ python tools/feature_maps_visualization/fm_vis.py -i the image you want to test
--save_path
where to save
\
--use_gpu
whether to use gpu
```
参数说明:
+
`-i`
:待预测的图片文件路径,如
`./test.jpeg`
+
`-c`
:特征图维度,如
`
./resnet50_vd/model
`
+
`-c`
:特征图维度,如
`
5
`
+
`-p`
:权重文件路径,如
`./ResNet50_pretrained/`
+
`--interpolation`
: 图像插值方式, 默认值 1
+
`--save_path`
:保存路径,如:
`./tools/`
...
...
@@ -63,7 +66,7 @@ python tools/feature_maps_visualization/fm_vis.py -i the image you want to test
*
输入图片:


*
运行下面的特征图可视化脚本
...
...
@@ -75,10 +78,9 @@ python tools/feature_maps_visualization/fm_vis.py \
--show=True \
--interpolation=1 \
--save_path="./output.png" \
--use_gpu=False \
--load_static_weights=True
--use_gpu=False
```
*
输出特征图保存为
`output.png`
,如下所示。


ppcls/utils/feature_maps_visualization/download_resnet50_pretrained.sh
已删除
100644 → 0
浏览文件 @
ea5b7254
wget https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar
tar
-xf
ResNet50_pretrained.tar
\ No newline at end of file
ppcls/utils/feature_maps_visualization/fm_vis.py
浏览文件 @
d6b920b4
...
...
@@ -19,7 +19,7 @@ import os
import
sys
__dir__
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..'
)))
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..
/..
'
)))
import
paddle
from
paddle.distributed
import
ParallelEnv
...
...
@@ -33,18 +33,13 @@ def parse_args():
return
v
.
lower
()
in
(
"true"
,
"t"
,
"1"
)
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"-i"
,
"--image_file"
,
type
=
str
)
parser
.
add_argument
(
"-i"
,
"--image_file"
,
required
=
True
,
type
=
str
)
parser
.
add_argument
(
"-c"
,
"--channel_num"
,
type
=
int
)
parser
.
add_argument
(
"-p"
,
"--pretrained_model"
,
type
=
str
)
parser
.
add_argument
(
"--show"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--interpolation"
,
type
=
int
,
default
=
1
)
parser
.
add_argument
(
"--save_path"
,
type
=
str
,
default
=
None
)
parser
.
add_argument
(
"--use_gpu"
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
"--load_static_weights"
,
type
=
str2bool
,
default
=
False
,
help
=
'Whether to load the pretrained weights saved in static mode'
)
return
parser
.
parse_args
()
...
...
@@ -79,7 +74,7 @@ def main():
place
=
paddle
.
set_device
(
place
)
net
=
ResNet50
()
load_dygraph_pretrain
(
net
,
args
.
pretrained_model
,
args
.
load_static_weights
)
load_dygraph_pretrain
(
net
,
args
.
pretrained_model
)
img
=
cv2
.
imread
(
args
.
image_file
,
cv2
.
IMREAD_COLOR
)
data
=
preprocess
(
img
,
operators
)
...
...
ppcls/utils/feature_maps_visualization/resnet.py
浏览文件 @
d6b920b4
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录