Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
d824116b
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看板
提交
d824116b
编写于
1月 24, 2018
作者:
wgzqz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add targeted attack methods.
上级
f2f6daa2
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
193 addition
and
0 deletion
+193
-0
fluid/adversarial/advbox/adversary.py
fluid/adversarial/advbox/adversary.py
+184
-0
fluid/adversarial/advbox/attacks/__init__.py
fluid/adversarial/advbox/attacks/__init__.py
+9
-0
未找到文件。
fluid/adversarial/advbox/adversary.py
0 → 100644
浏览文件 @
d824116b
"""
Defines a class that contains the original object, the target and the
adversarial example.
"""
class
Adversary
(
object
):
"""
Adversary contains the original object, the target and the adversarial
example.
"""
def
__init__
(
self
,
original
,
original_label
=
None
):
"""
:param original: The original instance, such as an image.
:param original_label: The original instance's label.
"""
assert
original
is
not
None
self
.
__original
=
original
self
.
__original_label
=
original_label
self
.
__target_label
=
None
self
.
__target
=
None
self
.
__is_targeted_attack
=
False
self
.
__adversarial_example
=
None
self
.
__adversarial_label
=
None
def
set_target
(
self
,
is_targeted_attack
,
target
=
None
,
target_label
=
None
):
"""
Set the target be targeted or untargeted.
:param is_targeted_attack: bool
:param target: The target.
:param target_label: If is_targeted_attack is true and target_label is
None, self.target_label will be set by the Attack class.
If is_targeted_attack is false, target_label must be None.
"""
assert
(
target_label
is
None
)
or
is_targeted_attack
self
.
__is_targeted_attack
=
is_targeted_attack
self
.
__target_label
=
target_label
self
.
__target
=
target
if
not
is_targeted_attack
:
self
.
__target_label
=
None
self
.
__target
=
None
def
set_original
(
self
,
original
,
original_label
=
None
):
"""
Reset the original.
:param original: Original instance.
:param original_label: Original instance's label.
"""
if
original
!=
self
.
__original
:
self
.
__original
=
original
self
.
__original_label
=
original_label
self
.
__adversarial_example
=
None
if
original
is
None
:
self
.
__original_label
=
None
def
_is_successful
(
self
,
adversarial_label
):
"""
Is the adversarial_label is the expected adversarial label.
:param adversarial_label: adversarial label.
:return: bool
"""
if
self
.
__target_label
is
not
None
:
return
adversarial_label
==
self
.
__target_label
else
:
return
(
adversarial_label
is
not
None
)
and
\
(
adversarial_label
!=
self
.
__original_label
)
def
is_successful
(
self
):
"""
Has the adversarial example been found.
:return: bool
"""
return
self
.
_is_successful
(
self
.
__adversarial_label
)
def
try_accept_the_example
(
self
,
adversarial_example
,
adversarial_label
):
"""
If adversarial_label the target label that we are finding.
The adversarial_example and adversarial_label will be accepted and
True will be returned.
:return: bool
"""
assert
adversarial_example
.
shape
==
self
.
__original
.
shape
ok
=
self
.
_is_successful
(
adversarial_label
)
if
ok
:
self
.
__adversarial_example
=
adversarial_example
self
.
__adversarial_label
=
adversarial_label
return
ok
def
perturbation
(
self
,
multiplying_factor
=
1.0
):
"""
The perturbation that the adversarial_example is added.
:param multiplying_factor: float.
:return: The perturbation that is multiplied by multiplying_factor.
"""
assert
self
.
__original
is
not
None
assert
self
.
__adversarial_example
is
not
None
return
multiplying_factor
*
(
self
.
__adversarial_example
-
self
.
__original
)
@
property
def
is_targeted_attack
(
self
):
"""
:property: is_targeted_attack
"""
return
self
.
__is_targeted_attack
@
property
def
target_label
(
self
):
"""
:property: target_label
"""
return
self
.
__target_label
@
target_label
.
setter
def
target_label
(
self
):
"""
:property: target_label
"""
return
self
.
__target_label
@
property
def
target
(
self
):
"""
:property: target
"""
return
self
.
__target
@
property
def
original
(
self
):
"""
:property: original
"""
return
self
.
__original
@
property
def
original_label
(
self
):
"""
:property: original
"""
return
self
.
__original_label
@
original_label
.
setter
def
original_label
(
self
,
label
):
"""
original_label setter
"""
self
.
__original_label
=
label
@
property
def
adversarial_example
(
self
):
"""
:property: adversarial_example
"""
return
self
.
__adversarial_example
@
adversarial_example
.
setter
def
adversarial_example
(
self
,
example
):
"""
adversarial_example setter
"""
self
.
__adversarial_example
=
example
@
property
def
adversarial_label
(
self
):
"""
:property: adversarial_label
"""
return
self
.
__adversarial_label
@
adversarial_label
.
setter
def
adversarial_label
(
self
,
label
):
"""
adversarial_label setter
"""
self
.
__adversarial_label
=
label
fluid/adversarial/advbox/attacks/__init__.py
0 → 100644
浏览文件 @
d824116b
"""
Attack methods
"""
from
.base
import
Attack
from
.gradientsign
import
FGSM
from
.gradientsign
import
GradientSignAttack
from
.iterator_gradientsign
import
IFGSM
from
.iterator_gradientsign
import
IteratorGradientSignAttack
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录