Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleX
提交
7fcfbf80
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7fcfbf80
编写于
5月 20, 2020
作者:
S
sunyanfang01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove download
上级
441634a1
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
49 addition
and
22 deletion
+49
-22
paddlex/interpret/__init__.py
paddlex/interpret/__init__.py
+2
-1
paddlex/interpret/core/_session_preparation.py
paddlex/interpret/core/_session_preparation.py
+8
-11
paddlex/interpret/core/interpretation_algorithms.py
paddlex/interpret/core/interpretation_algorithms.py
+18
-2
paddlex/interpret/core/normlime_base.py
paddlex/interpret/core/normlime_base.py
+21
-8
未找到文件。
paddlex/interpret/__init__.py
浏览文件 @
7fcfbf80
...
...
@@ -15,4 +15,5 @@
from
__future__
import
absolute_import
from
.
import
visualize
visualize
=
visualize
.
visualize
\ No newline at end of file
lime
=
visualize
.
lime
normlime
=
visualize
.
normlime
paddlex/interpret/core/_session_preparation.py
浏览文件 @
7fcfbf80
...
...
@@ -28,17 +28,6 @@ def gen_user_home():
return
os
.
path
.
expanduser
(
'~'
)
root_path
=
gen_user_home
()
root_path
=
osp
.
join
(
root_path
,
'.paddlex'
)
h_pre_models
=
osp
.
join
(
root_path
,
"pre_models"
)
if
not
osp
.
exists
(
h_pre_models
):
if
not
osp
.
exists
(
root_path
):
os
.
makedirs
(
root_path
)
url
=
"https://bj.bcebos.com/paddlex/interpret/pre_models.tar.gz"
pdx
.
utils
.
download_and_decompress
(
url
,
path
=
root_path
)
h_pre_models_kmeans
=
osp
.
join
(
h_pre_models
,
"kmeans_model.pkl"
)
def
paddle_get_fc_weights
(
var_name
=
"fc_0.w_0"
):
fc_weights
=
fluid
.
global_scope
().
find_var
(
var_name
).
get_tensor
()
return
np
.
array
(
fc_weights
)
...
...
@@ -50,6 +39,14 @@ def paddle_resize(extracted_features, outsize):
def
compute_features_for_kmeans
(
data_content
):
root_path
=
gen_user_home
()
root_path
=
osp
.
join
(
root_path
,
'.paddlex'
)
h_pre_models
=
osp
.
join
(
root_path
,
"pre_models"
)
if
not
osp
.
exists
(
h_pre_models
):
if
not
osp
.
exists
(
root_path
):
os
.
makedirs
(
root_path
)
url
=
"https://bj.bcebos.com/paddlex/interpret/pre_models.tar.gz"
pdx
.
utils
.
download_and_decompress
(
url
,
path
=
root_path
)
def
conv_bn_layer
(
input
,
num_filters
,
filter_size
,
...
...
paddlex/interpret/core/interpretation_algorithms.py
浏览文件 @
7fcfbf80
...
...
@@ -13,11 +13,12 @@
#limitations under the License.
import
os
import
os.path
as
osp
import
numpy
as
np
import
time
from
.
import
lime_base
from
._session_preparation
import
paddle_get_fc_weights
,
compute_features_for_kmeans
,
h_pre_models_kmeans
from
._session_preparation
import
paddle_get_fc_weights
,
compute_features_for_kmeans
,
gen_user_home
from
.normlime_base
import
combine_normlime_and_lime
,
get_feature_for_kmeans
,
load_kmeans_model
from
paddlex.interpret.as_data_reader.readers
import
read_image
...
...
@@ -215,6 +216,15 @@ class LIME(object):
class
NormLIME
(
object
):
def
__init__
(
self
,
predict_fn
,
label_names
,
num_samples
=
3000
,
batch_size
=
50
,
kmeans_model_for_normlime
=
None
,
normlime_weights
=
None
):
root_path
=
gen_user_home
()
root_path
=
osp
.
join
(
root_path
,
'.paddlex'
)
h_pre_models
=
osp
.
join
(
root_path
,
"pre_models"
)
if
not
osp
.
exists
(
h_pre_models
):
if
not
osp
.
exists
(
root_path
):
os
.
makedirs
(
root_path
)
url
=
"https://bj.bcebos.com/paddlex/interpret/pre_models.tar.gz"
pdx
.
utils
.
download_and_decompress
(
url
,
path
=
root_path
)
h_pre_models_kmeans
=
osp
.
join
(
h_pre_models
,
"kmeans_model.pkl"
)
if
kmeans_model_for_normlime
is
None
:
try
:
self
.
kmeans_model
=
load_kmeans_model
(
h_pre_models_kmeans
)
...
...
@@ -242,7 +252,13 @@ class NormLIME(object):
self
.
label_names
=
label_names
def
predict_cluster_labels
(
self
,
feature_map
,
segments
):
return
self
.
kmeans_model
.
predict
(
get_feature_for_kmeans
(
feature_map
,
segments
))
X
=
get_feature_for_kmeans
(
feature_map
,
segments
)
try
:
cluster_labels
=
self
.
kmeans_model
.
predict
(
X
)
except
AttributeError
:
from
sklearn.metrics
import
pairwise_distances_argmin_min
cluster_labels
,
_
=
pairwise_distances_argmin_min
(
X
,
self
.
kmeans_model
.
cluster_centers_
)
return
cluster_labels
def
predict_using_normlime_weights
(
self
,
pred_labels
,
predicted_cluster_labels
):
# global weights
...
...
paddlex/interpret/core/normlime_base.py
浏览文件 @
7fcfbf80
...
...
@@ -13,12 +13,14 @@
#limitations under the License.
import
os
import
os.path
as
osp
import
numpy
as
np
import
glob
from
paddlex.interpret.as_data_reader.readers
import
read_image
import
paddlex.utils.logging
as
logging
from
.
import
lime_base
from
._session_preparation
import
compute_features_for_kmeans
,
h_pre_models_kmeans
from
._session_preparation
import
compute_features_for_kmeans
,
gen_user_home
def
load_kmeans_model
(
fname
):
...
...
@@ -102,6 +104,15 @@ def save_one_lime_predict_and_kmean_labels(lime_all_weights, image_pred_labels,
def
precompute_lime_weights
(
list_data_
,
predict_fn
,
num_samples
,
batch_size
,
save_dir
):
root_path
=
gen_user_home
()
root_path
=
osp
.
join
(
root_path
,
'.paddlex'
)
h_pre_models
=
osp
.
join
(
root_path
,
"pre_models"
)
if
not
osp
.
exists
(
h_pre_models
):
if
not
osp
.
exists
(
root_path
):
os
.
makedirs
(
root_path
)
url
=
"https://bj.bcebos.com/paddlex/interpret/pre_models.tar.gz"
pdx
.
utils
.
download_and_decompress
(
url
,
path
=
root_path
)
h_pre_models_kmeans
=
osp
.
join
(
h_pre_models
,
"kmeans_model.pkl"
)
kmeans_model
=
load_kmeans_model
(
h_pre_models_kmeans
)
for
data_index
,
each_data_
in
enumerate
(
list_data_
):
...
...
@@ -113,11 +124,10 @@ def precompute_lime_weights(list_data_, predict_fn, num_samples, batch_size, sav
save_path
=
os
.
path
.
join
(
save_dir
,
save_path
)
if
os
.
path
.
exists
(
save_path
):
print
(
f
'
{
save_path
}
exists, not computing this one.'
)
logging
.
info
(
save_path
+
' exists, not computing this one.'
,
use_color
=
True
)
continue
print
(
'processing'
,
each_data_
if
isinstance
(
each_data_
,
str
)
else
data_index
,
f
',
{
data_index
}
/
{
len
(
list_data_
)
}
'
)
img_file_name
=
each_data_
if
isinstance
(
each_data_
,
str
)
else
data_index
logging
.
info
(
'processing '
+
img_file_name
+
' [{}/{}]'
.
format
(
data_index
,
len
(
list_data_
)),
use_color
=
True
)
image_show
=
read_image
(
each_data_
)
result
=
predict_fn
(
image_show
)
...
...
@@ -149,9 +159,12 @@ def precompute_lime_weights(list_data_, predict_fn, num_samples, batch_size, sav
interpreter
=
algo
.
interpret_instance
(
image_show
[
0
],
predict_fn
,
pred_label
,
0
,
num_samples
=
num_samples
,
batch_size
=
batch_size
)
cluster_labels
=
kmeans_model
.
predict
(
get_feature_for_kmeans
(
compute_features_for_kmeans
(
image_show
).
transpose
((
1
,
2
,
0
)),
interpreter
.
segments
)
)
X
=
get_feature_for_kmeans
(
compute_features_for_kmeans
(
image_show
).
transpose
((
1
,
2
,
0
)),
interpreter
.
segments
)
try
:
cluster_labels
=
kmeans_model
.
predict
(
X
)
except
AttributeError
:
from
sklearn.metrics
import
pairwise_distances_argmin_min
cluster_labels
,
_
=
pairwise_distances_argmin_min
(
X
,
kmeans_model
.
cluster_centers_
)
save_one_lime_predict_and_kmean_labels
(
interpreter
.
local_weights
,
pred_label
,
cluster_labels
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录