Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
a4d2c8ea
M
mindarmour
项目概览
MindSpore
/
mindarmour
通知
4
Star
2
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindarmour
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a4d2c8ea
编写于
9月 02, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
9月 02, 2020
浏览文件
操作
浏览文件
下载
差异文件
!106 Remove name of GradOperation and change model predict logic of detector
Merge pull request !106 from pkuliuliu/master
上级
d99219c2
aa113c33
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
22 addition
and
25 deletion
+22
-25
mindarmour/detectors/mag_net.py
mindarmour/detectors/mag_net.py
+9
-9
mindarmour/detectors/spatial_smoothing.py
mindarmour/detectors/spatial_smoothing.py
+9
-9
mindarmour/diff_privacy/train/model.py
mindarmour/diff_privacy/train/model.py
+2
-2
mindarmour/utils/util.py
mindarmour/utils/util.py
+2
-5
未找到文件。
mindarmour/detectors/mag_net.py
浏览文件 @
a4d2c8ea
...
...
@@ -121,8 +121,8 @@ class ErrorBasedDetector(Detector):
float, the distance between reconstructed and original samples.
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
x_trans
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
diff
=
np
.
abs
(
inputs
-
x_trans
.
asnumpy
()
)
x_trans
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
diff
=
np
.
abs
(
inputs
-
x_trans
)
dims
=
tuple
(
np
.
arange
(
len
(
inputs
.
shape
))[
1
:])
marks
=
np
.
mean
(
np
.
power
(
diff
,
2
),
axis
=
dims
)
return
marks
...
...
@@ -138,10 +138,10 @@ class ErrorBasedDetector(Detector):
numpy.ndarray, reconstructed images.
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
x_trans
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
x_trans
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
if
self
.
_bounds
is
not
None
:
clip_min
,
clip_max
=
self
.
_bounds
x_trans
=
np
.
clip
(
x_trans
.
asnumpy
()
,
clip_min
,
clip_max
)
x_trans
=
np
.
clip
(
x_trans
,
clip_min
,
clip_max
)
return
x_trans
def
set_threshold
(
self
,
threshold
):
...
...
@@ -214,12 +214,12 @@ class DivergenceBasedDetector(ErrorBasedDetector):
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
x_len
=
inputs
.
shape
[
0
]
x_transformed
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
x_origin
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
x_trans
=
self
.
_model
.
predict
(
x_transformed
)
x_transformed
=
self
.
_auto_encoder
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
x_origin
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
x_trans
=
self
.
_model
.
predict
(
Tensor
(
x_transformed
)).
asnumpy
(
)
y_pred
=
softmax
(
x_origin
.
asnumpy
()
/
self
.
_t
,
axis
=
1
)
y_trans_pred
=
softmax
(
x_trans
.
asnumpy
()
/
self
.
_t
,
axis
=
1
)
y_pred
=
softmax
(
x_origin
/
self
.
_t
,
axis
=
1
)
y_trans_pred
=
softmax
(
x_trans
/
self
.
_t
,
axis
=
1
)
if
self
.
_option
==
'jsd'
:
marks
=
[
_jsd
(
y_pred
[
i
],
y_trans_pred
[
i
])
for
i
in
range
(
x_len
)]
...
...
mindarmour/detectors/spatial_smoothing.py
浏览文件 @
a4d2c8ea
...
...
@@ -83,10 +83,10 @@ class SpatialSmoothing(Detector):
as positive, i.e. adversarial.
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
.
asnumpy
()
dist
=
self
.
_dist
(
raw_pred
.
asnumpy
(),
smoothing_pred
.
asnumpy
()
)
dist
=
self
.
_dist
(
raw_pred
,
smoothing_pred
)
index
=
int
(
len
(
dist
)
*
(
1
-
self
.
_fpr
))
threshold
=
np
.
sort
(
dist
,
axis
=
None
)[
index
]
self
.
_threshold
=
threshold
...
...
@@ -104,9 +104,9 @@ class SpatialSmoothing(Detector):
input sample with index i is adversarial.
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
dist
=
self
.
_dist
(
raw_pred
.
asnumpy
(),
smoothing_pred
.
asnumpy
()
)
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
.
asnumpy
()
dist
=
self
.
_dist
(
raw_pred
,
smoothing_pred
)
res
=
[
0
]
*
len
(
dist
)
for
i
,
elem
in
enumerate
(
dist
):
...
...
@@ -127,9 +127,9 @@ class SpatialSmoothing(Detector):
float, distance.
"""
inputs
=
check_numpy_param
(
'inputs'
,
inputs
)
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
dist
=
self
.
_dist
(
raw_pred
.
asnumpy
(),
smoothing_pred
.
asnumpy
()
)
raw_pred
=
self
.
_model
.
predict
(
Tensor
(
inputs
))
.
asnumpy
()
smoothing_pred
=
self
.
_model
.
predict
(
Tensor
(
self
.
transform
(
inputs
)))
.
asnumpy
()
dist
=
self
.
_dist
(
raw_pred
,
smoothing_pred
)
return
dist
def
transform
(
self
,
inputs
):
...
...
mindarmour/diff_privacy/train/model.py
浏览文件 @
a4d2c8ea
...
...
@@ -383,7 +383,7 @@ class _TrainOneStepWithLossScaleCell(Cell):
self
.
network
.
add_flags
(
defer_inline
=
True
)
self
.
weights
=
ParameterTuple
(
network
.
trainable_params
())
self
.
optimizer
=
optimizer
self
.
grad
=
C
.
GradOperation
(
'grad'
,
get_by_list
=
True
,
sens_param
=
True
)
self
.
grad
=
C
.
GradOperation
(
get_by_list
=
True
,
sens_param
=
True
)
self
.
hyper_map
=
C
.
HyperMap
()
if
context
.
get_context
(
"device_target"
)
==
"GPU"
:
self
.
gpu_target
=
True
...
...
@@ -602,7 +602,7 @@ class _TrainOneStepCell(Cell):
self
.
network
.
add_flags
(
defer_inline
=
True
)
self
.
weights
=
optimizer
.
parameters
self
.
optimizer
=
optimizer
self
.
grad
=
C
.
GradOperation
(
'grad'
,
get_by_list
=
True
,
sens_param
=
True
)
self
.
grad
=
C
.
GradOperation
(
get_by_list
=
True
,
sens_param
=
True
)
self
.
sens
=
sens
self
.
reducer_flag
=
False
self
.
grad_reducer
=
None
...
...
mindarmour/utils/util.py
浏览文件 @
a4d2c8ea
...
...
@@ -108,9 +108,7 @@ class GradWrapWithLoss(Cell):
def
__init__
(
self
,
network
):
super
(
GradWrapWithLoss
,
self
).
__init__
()
self
.
_grad_all
=
GradOperation
(
name
=
"get_all"
,
get_all
=
True
,
sens_param
=
False
)
self
.
_grad_all
=
GradOperation
(
get_all
=
True
,
sens_param
=
False
)
self
.
_network
=
network
def
construct
(
self
,
inputs
,
labels
):
...
...
@@ -149,8 +147,7 @@ class GradWrap(Cell):
def
__init__
(
self
,
network
):
super
(
GradWrap
,
self
).
__init__
()
self
.
grad
=
GradOperation
(
name
=
"grad"
,
get_all
=
False
,
sens_param
=
True
)
self
.
grad
=
GradOperation
(
get_all
=
False
,
sens_param
=
True
)
self
.
network
=
network
def
construct
(
self
,
inputs
,
weight
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录