Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
9aa068ad
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9aa068ad
编写于
1月 27, 2018
作者:
wgzqz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add deepfool.
上级
555389db
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
78 addition
and
0 deletion
+78
-0
fluid/adversarial/advbox/attacks/deepfool.py
fluid/adversarial/advbox/attacks/deepfool.py
+78
-0
未找到文件。
fluid/adversarial/advbox/attacks/deepfool.py
0 → 100644
浏览文件 @
9aa068ad
"""
This module provide the attack method for deepfool. Deepfool is a simple and
accurate adversarial attack.
"""
from
__future__
import
division
import
logging
import
numpy
as
np
from
.base
import
Attack
class
DeepFoolAttack
(
Attack
):
"""
DeepFool: a simple and accurate method to fool deep neural networks",
Seyed-Mohsen Moosavi-Dezfooli, Alhussein Fawzi, Pascal Frossard,
https://arxiv.org/abs/1511.04599
"""
def
_apply
(
self
,
adversary
,
iterations
=
100
,
overshoot
=
0.02
):
"""
Apply the deep fool attack.
Args:
adversary(Adversary): The Adversary object.
iterations(int): The iterations.
overshoot(float): We add (1+overshoot)*pert every iteration.
Return:
adversary: The Adversary object.
"""
assert
adversary
is
not
None
pre_label
=
adversary
.
original_label
min_
,
max_
=
self
.
model
.
bounds
()
f
=
self
.
model
.
predict
([(
adversary
.
original
,
0
)])
if
adversary
.
is_targeted_attack
:
labels
=
[
adversary
.
target_label
]
else
:
max_class_count
=
10
if
len
(
f
)
>
max_class_count
:
labels
=
np
.
argsort
(
f
)[
-
(
max_class_count
+
1
):
-
1
]
else
:
labels
=
np
.
arange
(
len
(
f
))
gradient
=
self
.
model
.
gradient
([(
adversary
.
original
,
pre_label
)])
x
=
adversary
.
original
.
reshape
(
gradient
.
shape
)
for
iteration
in
xrange
(
iterations
):
w
=
np
.
inf
w_norm
=
np
.
inf
pert
=
np
.
inf
for
k
in
labels
:
if
k
==
pre_label
:
continue
gradient_k
=
self
.
model
.
gradient
([(
x
,
k
)])
w_k
=
gradient_k
-
gradient
f_k
=
f
[
k
]
-
f
[
pre_label
]
w_k_norm
=
np
.
linalg
.
norm
(
w_k
)
+
1e-8
pert_k
=
(
np
.
abs
(
f_k
)
+
1e-8
)
/
w_k_norm
if
pert_k
<
pert
:
pert
=
pert_k
w
=
w_k
w_norm
=
w_k_norm
r_i
=
-
w
*
pert
/
w_norm
# The gradient is -gradient in the paper.
x
=
x
+
(
1
+
overshoot
)
*
r_i
x
=
np
.
clip
(
x
,
min_
,
max_
)
f
=
self
.
model
.
predict
([(
x
,
0
)])
gradient
=
self
.
model
.
gradient
([(
x
,
pre_label
)])
adv_label
=
np
.
argmax
(
f
)
logging
.
info
(
'iteration = {}, f = {}, pre_label = {}'
', adv_label={}'
.
format
(
iteration
,
f
[
pre_label
],
pre_label
,
adv_label
))
if
adversary
.
try_accept_the_example
(
x
,
adv_label
):
return
adversary
return
adversary
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录