Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
mindarmour
提交
c36bb5bc
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看板
提交
c36bb5bc
编写于
4月 23, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
4月 23, 2020
浏览文件
操作
浏览文件
下载
差异文件
!11 remove duplicate code.
Merge pull request !11 from zheng-huanhuan/master
上级
248b479d
74e17deb
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
21 addition
and
46 deletion
+21
-46
mindarmour/attacks/black/natural_evolutionary_strategy.py
mindarmour/attacks/black/natural_evolutionary_strategy.py
+0
-6
mindarmour/attacks/black/pointwise_attack.py
mindarmour/attacks/black/pointwise_attack.py
+15
-35
mindarmour/attacks/lbfgs.py
mindarmour/attacks/lbfgs.py
+6
-5
未找到文件。
mindarmour/attacks/black/natural_evolutionary_strategy.py
浏览文件 @
c36bb5bc
...
...
@@ -29,12 +29,6 @@ LOGGER = LogUtil.get_instance()
TAG
=
'NES'
def
_one_hot
(
index
,
total
):
arr
=
np
.
zeros
((
total
))
arr
[
index
]
=
1.0
return
arr
def
_bound
(
image
,
epislon
):
lower
=
np
.
clip
(
image
-
epislon
,
0
,
1
)
upper
=
np
.
clip
(
image
+
epislon
,
0
,
1
)
...
...
mindarmour/attacks/black/pointwise_attack.py
浏览文件 @
c36bb5bc
...
...
@@ -167,10 +167,9 @@ class PointWiseAttack(Attack):
'is: {}'
.
format
(
unperturbed_img
.
dtype
,
perturbed_img
.
dtype
)
LOGGER
.
error
(
TAG
,
msg
)
raise
ValueError
(
msg
)
LOGGER
.
debug
(
TAG
,
'Before optimize, the mse distance between original '
'sample and adversarial sample is: {}'
.
format
(
self
.
_distance
(
perturbed_img
,
unperturbed_img
)))
l2_dis
=
np
.
linalg
.
norm
(
perturbed_img
-
unperturbed_img
)
LOGGER
.
debug
(
TAG
,
'Before optimize, the l2 distance between original '
'sample and adversarial sample is: {}'
.
format
(
l2_dis
))
# recover pixel if image is adversarial
for
_
in
range
(
self
.
_max_iter
):
is_improve
=
False
...
...
@@ -190,8 +189,9 @@ class PointWiseAttack(Attack):
break
else
:
recover
[
ite_ind
]
=
perturbed_img
[
ite_ind
]
if
not
is_improve
or
(
self
.
_distance
(
perturbed_img
,
unperturbed_img
)
<=
self
.
_get_threthod
()):
l2_dis
=
np
.
linalg
.
norm
(
perturbed_img
-
unperturbed_img
)
if
not
is_improve
or
(
np
.
square
(
l2_dis
)
/
np
.
sqrt
(
len
(
pixels_ind
))
<=
self
.
_get_threthod
()):
break
LOGGER
.
debug
(
TAG
,
'first round: Query count {}'
.
format
(
query_count
))
LOGGER
.
debug
(
TAG
,
'Starting binary searches.'
)
...
...
@@ -213,12 +213,10 @@ class PointWiseAttack(Attack):
is_improve
=
True
mask
[
ite_ind
]
=
True
perturbed_img
[
ite_ind
]
=
recover
[
ite_ind
]
l2_dis
=
np
.
linalg
.
norm
(
perturbed_img
-
unperturbed_img
)
LOGGER
.
debug
(
TAG
,
'Reset {}th pixel value to original, '
'mse distance: {}.'
.
format
(
ite_ind
,
self
.
_distance
(
perturbed_img
,
unperturbed_img
)))
'l2 distance: {}.'
.
format
(
ite_ind
,
l2_dis
))
break
else
:
# use binary searches
...
...
@@ -232,15 +230,15 @@ class PointWiseAttack(Attack):
is_improve
=
True
mask
[
ite_ind
]
=
True
perturbed_img
[
ite_ind
]
=
optimized_value
l2_dis
=
np
.
linalg
.
norm
(
perturbed_img
-
unperturbed_img
)
LOGGER
.
debug
(
TAG
,
'Reset {}th pixel value to original, '
'mse distance: {}.'
.
format
(
ite_ind
,
self
.
_distance
(
perturbed_img
,
unperturbed_img
)))
'l2 distance: {}.'
.
format
(
ite_ind
,
l2_dis
))
break
if
not
is_improve
or
(
self
.
_distance
(
perturbed_img
,
unperturbed_img
)
<=
self
.
_get_threthod
()):
l2_dis
=
np
.
linalg
.
norm
(
perturbed_img
-
unperturbed_img
)
if
not
is_improve
or
(
np
.
square
(
l2_dis
)
/
np
.
sqrt
(
len
(
pixels_ind
))
<=
self
.
_get_threthod
()):
LOGGER
.
debug
(
TAG
,
'second optimized finish.'
)
break
LOGGER
.
info
(
TAG
,
'Optimized finished, query count is {}'
.
format
(
query_count
))
...
...
@@ -295,32 +293,14 @@ class PointWiseAttack(Attack):
is_adv
,
start_adv
,
query_c
=
self
.
_init_attack
.
generate
(
inputs
,
labels
)
return
is_adv
,
start_adv
,
query_c
def
_distance
(
self
,
perturbed_img
,
unperturbed_img
):
"""
Calculate Mean Squared Error (MSE) to evaluate the optimized process.
Args:
perturbed_img (numpy.ndarray): Adversarial sample to be optimized.
unperturbed_img (numpy.ndarray): As a reference benigh sample.
Returns:
float, Calculation of Mean Squared Error (MSE).
"""
return
np
.
square
(
np
.
subtract
(
perturbed_img
,
unperturbed_img
)).
mean
()
def
_get_threthod
(
self
,
method
=
'MSE'
):
def
_get_threthod
(
self
):
"""
Return a float number, when distance small than this number,
optimize will abort early.
Args:
method: distance method. Default: MSE.
Returns:
float, the optimized level, the smaller of number, the better
of adversarial sample.
"""
predefined_threshold
=
0.01
if
method
==
'MSE'
:
return
predefined_threshold
return
predefined_threshold
mindarmour/attacks/lbfgs.py
浏览文件 @
c36bb5bc
...
...
@@ -127,12 +127,13 @@ class LBFGS(Attack):
def
_loss
(
self
,
cur_input
,
start_input
,
cur_eps
,
shape
,
labels
):
"""
The l-bfgs-b loss
used is Mean Square Error distance from original
input plus cross
entropy loss.
The l-bfgs-b loss
is the sum of l2 distances to the original input plus
the cross-
entropy loss.
"""
cur_input
=
cur_input
.
astype
(
self
.
_dtype
)
mse_distance
=
np
.
mean
(
np
.
square
(
start_input
-
cur_input
))
/
\
((
self
.
_box_max
-
self
.
_box_min
)
**
2
)
l2_distance
=
np
.
linalg
.
norm
(
cur_input
.
reshape
(
(
cur_input
.
shape
[
0
],
-
1
))
-
start_input
.
reshape
(
(
start_input
.
shape
[
0
],
-
1
)))
logits
=
self
.
_forward_one
(
cur_input
.
reshape
(
shape
)).
flatten
()
logits
=
logits
-
np
.
max
(
logits
)
if
self
.
_sparse
:
...
...
@@ -146,7 +147,7 @@ class LBFGS(Attack):
crossentropy
=
logits
[
target_class
]
-
np
.
log
(
np
.
sum
(
np
.
exp
(
logits
)))
gradient
=
-
self
.
_gradient
(
cur_input
,
labels
,
shape
).
flatten
()
return
(
mse
_distance
+
cur_eps
*
crossentropy
).
astype
(
self
.
_dtype
),
\
return
(
l2
_distance
+
cur_eps
*
crossentropy
).
astype
(
self
.
_dtype
),
\
gradient
.
astype
(
np
.
float64
)
def
_lbfgsb
(
self
,
start_input
,
cur_eps
,
shape
,
labels
,
bounds
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录