Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleX
提交
ab2942c1
P
PaddleX
项目概览
PaddlePaddle
/
PaddleX
通知
138
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
43
列表
看板
标记
里程碑
合并请求
5
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleX
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
43
Issue
43
列表
看板
标记
里程碑
合并请求
5
合并请求
5
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ab2942c1
编写于
5月 19, 2020
作者:
S
sunyanfang01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify the interpret
上级
25698c49
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
47 addition
and
18 deletion
+47
-18
paddlex/cv/models/classifier.py
paddlex/cv/models/classifier.py
+1
-14
paddlex/interpret/__init__.py
paddlex/interpret/__init__.py
+1
-1
paddlex/interpret/__pycache__/__init__.cpython-37.pyc
paddlex/interpret/__pycache__/__init__.cpython-37.pyc
+0
-0
paddlex/interpret/__pycache__/interpretation_predict.cpython-37.pyc
...terpret/__pycache__/interpretation_predict.cpython-37.pyc
+0
-0
paddlex/interpret/__pycache__/visualize.cpython-37.pyc
paddlex/interpret/__pycache__/visualize.cpython-37.pyc
+0
-0
paddlex/interpret/as_data_reader/__pycache__/data_path_utils.cpython-37.pyc
...as_data_reader/__pycache__/data_path_utils.cpython-37.pyc
+0
-0
paddlex/interpret/as_data_reader/__pycache__/readers.cpython-37.pyc
...terpret/as_data_reader/__pycache__/readers.cpython-37.pyc
+0
-0
paddlex/interpret/as_data_reader/data_path_utils.py
paddlex/interpret/as_data_reader/data_path_utils.py
+0
-0
paddlex/interpret/as_data_reader/readers.py
paddlex/interpret/as_data_reader/readers.py
+0
-0
paddlex/interpret/core/__pycache__/_session_preparation.cpython-37.pyc
...pret/core/__pycache__/_session_preparation.cpython-37.pyc
+0
-0
paddlex/interpret/core/__pycache__/interpretation.cpython-37.pyc
.../interpret/core/__pycache__/interpretation.cpython-37.pyc
+0
-0
paddlex/interpret/core/__pycache__/interpretation_algorithms.cpython-37.pyc
...core/__pycache__/interpretation_algorithms.cpython-37.pyc
+0
-0
paddlex/interpret/core/__pycache__/lime_base.cpython-37.pyc
paddlex/interpret/core/__pycache__/lime_base.cpython-37.pyc
+0
-0
paddlex/interpret/core/__pycache__/normlime_base.cpython-37.pyc
...x/interpret/core/__pycache__/normlime_base.cpython-37.pyc
+0
-0
paddlex/interpret/core/_session_preparation.py
paddlex/interpret/core/_session_preparation.py
+0
-0
paddlex/interpret/core/interpretation.py
paddlex/interpret/core/interpretation.py
+0
-0
paddlex/interpret/core/interpretation_algorithms.py
paddlex/interpret/core/interpretation_algorithms.py
+0
-0
paddlex/interpret/core/lime_base.py
paddlex/interpret/core/lime_base.py
+0
-0
paddlex/interpret/core/normlime_base.py
paddlex/interpret/core/normlime_base.py
+0
-0
paddlex/interpret/interpretation_predict.py
paddlex/interpret/interpretation_predict.py
+29
-0
paddlex/interpret/visualize.py
paddlex/interpret/visualize.py
+16
-3
未找到文件。
paddlex/cv/models/classifier.py
浏览文件 @
ab2942c1
...
@@ -274,20 +274,7 @@ class BaseClassifier(BaseAPI):
...
@@ -274,20 +274,7 @@ class BaseClassifier(BaseAPI):
'score'
:
result
[
0
][
0
][
l
]
'score'
:
result
[
0
][
0
][
l
]
}
for
l
in
pred_label
]
}
for
l
in
pred_label
]
return
res
return
res
def
interpretation_predict
(
self
,
images
):
self
.
arrange_transforms
(
transforms
=
self
.
test_transforms
,
mode
=
'test'
)
new_imgs
=
[]
for
i
in
range
(
images
.
shape
[
0
]):
img
=
images
[
i
]
new_imgs
.
append
(
self
.
test_transforms
(
img
)[
0
])
new_imgs
=
np
.
array
(
new_imgs
)
result
=
self
.
exe
.
run
(
self
.
test_prog
,
feed
=
{
'image'
:
new_imgs
},
fetch_list
=
list
(
self
.
explanation_feats
.
values
()))
return
result
class
ResNet18
(
BaseClassifier
):
class
ResNet18
(
BaseClassifier
):
def
__init__
(
self
,
num_classes
=
1000
):
def
__init__
(
self
,
num_classes
=
1000
):
...
...
paddlex/interpret.py
→
paddlex/interpret
/__init__
.py
浏览文件 @
ab2942c1
...
@@ -13,6 +13,6 @@
...
@@ -13,6 +13,6 @@
# limitations under the License.
# limitations under the License.
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
.
cv.models.interpret
import
visualize
from
.
import
visualize
visualize
=
visualize
.
visualize
visualize
=
visualize
.
visualize
\ No newline at end of file
paddlex/interpret/__pycache__/__init__.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/__pycache__/interpretation_predict.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/__pycache__/visualize.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/as_data_reader/__pycache__/data_path_utils.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/as_data_reader/__pycache__/readers.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/
cv/models/
interpret/as_data_reader/data_path_utils.py
→
paddlex/interpret/as_data_reader/data_path_utils.py
浏览文件 @
ab2942c1
文件已移动
paddlex/
cv/models/
interpret/as_data_reader/readers.py
→
paddlex/interpret/as_data_reader/readers.py
浏览文件 @
ab2942c1
文件已移动
paddlex/interpret/core/__pycache__/_session_preparation.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/core/__pycache__/interpretation.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/core/__pycache__/interpretation_algorithms.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/core/__pycache__/lime_base.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/interpret/core/__pycache__/normlime_base.cpython-37.pyc
0 → 100644
浏览文件 @
ab2942c1
文件已添加
paddlex/
cv/models/
interpret/core/_session_preparation.py
→
paddlex/interpret/core/_session_preparation.py
浏览文件 @
ab2942c1
文件已移动
paddlex/
cv/models/
interpret/core/interpretation.py
→
paddlex/interpret/core/interpretation.py
浏览文件 @
ab2942c1
文件已移动
paddlex/
cv/models/
interpret/core/interpretation_algorithms.py
→
paddlex/interpret/core/interpretation_algorithms.py
浏览文件 @
ab2942c1
文件已移动
paddlex/
cv/models/
interpret/core/lime_base.py
→
paddlex/interpret/core/lime_base.py
浏览文件 @
ab2942c1
文件已移动
paddlex/
cv/models/
interpret/core/normlime_base.py
→
paddlex/interpret/core/normlime_base.py
浏览文件 @
ab2942c1
文件已移动
paddlex/interpret/interpretation_predict.py
0 → 100644
浏览文件 @
ab2942c1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
def
interpretation_predict
(
model
,
images
):
model
.
arrange_transforms
(
transforms
=
model
.
test_transforms
,
mode
=
'test'
)
new_imgs
=
[]
for
i
in
range
(
images
.
shape
[
0
]):
img
=
images
[
i
]
new_imgs
.
append
(
model
.
test_transforms
(
img
)[
0
])
new_imgs
=
np
.
array
(
new_imgs
)
result
=
model
.
exe
.
run
(
model
.
test_prog
,
feed
=
{
'image'
:
new_imgs
},
fetch_list
=
list
(
model
.
explanation_feats
.
values
()))
return
result
\ No newline at end of file
paddlex/
cv/models/
interpret/visualize.py
→
paddlex/interpret/visualize.py
浏览文件 @
ab2942c1
...
@@ -17,6 +17,7 @@ import cv2
...
@@ -17,6 +17,7 @@ import cv2
import
copy
import
copy
import
os.path
as
osp
import
os.path
as
osp
import
numpy
as
np
import
numpy
as
np
from
.interpretation_predict
import
interpretation_predict
from
.core.interpretation
import
Interpretation
from
.core.interpretation
import
Interpretation
from
.core.normlime_base
import
precompute_normlime_weights
from
.core.normlime_base
import
precompute_normlime_weights
...
@@ -28,6 +29,18 @@ def visualize(img_file,
...
@@ -28,6 +29,18 @@ def visualize(img_file,
num_samples
=
3000
,
num_samples
=
3000
,
batch_size
=
50
,
batch_size
=
50
,
save_dir
=
'./'
):
save_dir
=
'./'
):
"""可解释性可视化。
Args:
img_file (str): 预测图像路径。
model (paddlex.cv.models): paddlex中的模型。
dataset (paddlex.datasets): 数据集读取器,默认为None。
algo (str): 可解释性方式,当前可选'lime'和'normlime'。
num_samples (int): 随机采样数量,默认为3000。
batch_size (int): 预测数据batch大小,默认为50。
save_dir (str): 可解释性可视化结果(保存为png格式文件)和中间文件存储路径。
"""
assert
model
.
model_type
==
'classifier'
,
\
'Now the interpretation visualize only be supported in classifier!'
if
model
.
status
!=
'Normal'
:
if
model
.
status
!=
'Normal'
:
raise
Exception
(
'The interpretation only can deal with the Normal model'
)
raise
Exception
(
'The interpretation only can deal with the Normal model'
)
model
.
arrange_transforms
(
model
.
arrange_transforms
(
...
@@ -59,7 +72,7 @@ def get_lime_interpreter(img, model, dataset, num_samples=3000, batch_size=50):
...
@@ -59,7 +72,7 @@ def get_lime_interpreter(img, model, dataset, num_samples=3000, batch_size=50):
image
[
i
]
=
cv2
.
cvtColor
(
image
[
i
],
cv2
.
COLOR_RGB2BGR
)
image
[
i
]
=
cv2
.
cvtColor
(
image
[
i
],
cv2
.
COLOR_RGB2BGR
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
out
=
model
.
interpretation_predict
(
image
)
out
=
interpretation_predict
(
model
,
image
)
model
.
test_transforms
.
transforms
=
tmp_transforms
model
.
test_transforms
.
transforms
=
tmp_transforms
return
out
[
0
]
return
out
[
0
]
labels_name
=
None
labels_name
=
None
...
@@ -78,7 +91,7 @@ def get_normlime_interpreter(img, model, dataset, num_samples=3000, batch_size=5
...
@@ -78,7 +91,7 @@ def get_normlime_interpreter(img, model, dataset, num_samples=3000, batch_size=5
image
=
image
.
astype
(
'float32'
)
image
=
image
.
astype
(
'float32'
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
out
=
model
.
interpretation_predict
(
image
)
out
=
interpretation_predict
(
model
,
image
)
model
.
test_transforms
.
transforms
=
tmp_transforms
model
.
test_transforms
.
transforms
=
tmp_transforms
return
out
[
0
]
return
out
[
0
]
def
predict_func
(
image
):
def
predict_func
(
image
):
...
@@ -87,7 +100,7 @@ def get_normlime_interpreter(img, model, dataset, num_samples=3000, batch_size=5
...
@@ -87,7 +100,7 @@ def get_normlime_interpreter(img, model, dataset, num_samples=3000, batch_size=5
image
[
i
]
=
cv2
.
cvtColor
(
image
[
i
],
cv2
.
COLOR_RGB2BGR
)
image
[
i
]
=
cv2
.
cvtColor
(
image
[
i
],
cv2
.
COLOR_RGB2BGR
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
tmp_transforms
=
copy
.
deepcopy
(
model
.
test_transforms
.
transforms
)
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
model
.
test_transforms
.
transforms
=
model
.
test_transforms
.
transforms
[
-
2
:]
out
=
model
.
interpretation_predict
(
image
)
out
=
interpretation_predict
(
model
,
image
)
model
.
test_transforms
.
transforms
=
tmp_transforms
model
.
test_transforms
.
transforms
=
tmp_transforms
return
out
[
0
]
return
out
[
0
]
labels_name
=
None
labels_name
=
None
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录