Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
8e8e5a89
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8e8e5a89
编写于
1月 08, 2018
作者:
G
gx_wind
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix coding standard
上级
bbb03fce
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
31 addition
and
27 deletion
+31
-27
adversarial/README.md
adversarial/README.md
+7
-1
adversarial/advbox/__init__.py
adversarial/advbox/__init__.py
+0
-1
adversarial/advbox/attacks/base.py
adversarial/advbox/attacks/base.py
+1
-0
adversarial/advbox/attacks/gradientsign.py
adversarial/advbox/attacks/gradientsign.py
+6
-3
adversarial/advbox/models/base.py
adversarial/advbox/models/base.py
+1
-1
adversarial/advbox/models/paddle.py
adversarial/advbox/models/paddle.py
+16
-21
未找到文件。
adversarial/README.md
浏览文件 @
8e8e5a89
# Advbox
# Advbox
Advbox is a Python toolbox to create adversarial examples that fool neural networks. It requires Python and paddle.
Advbox is a Python toolbox to create adversarial examples that fool neural networks. It requires Python and paddle.
## How to use
1.
train a model and save it's parameters. (like fluid_mnist.py)
2.
load the parameters which is trained in step1, then reconstruct the model.(like mnist_tutorial_fgsm.py)
3.
use advbox to generate the adversarial sample.
adversarial/advbox/__init__.py
浏览文件 @
8e8e5a89
...
@@ -11,7 +11,6 @@
...
@@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
"""
"""
A set of tools for generating adversarial example on paddle platform
A set of tools for generating adversarial example on paddle platform
"""
"""
adversarial/advbox/attacks/base.py
浏览文件 @
8e8e5a89
...
@@ -7,6 +7,7 @@ import abc
...
@@ -7,6 +7,7 @@ import abc
abstractmethod
=
abc
.
abstractmethod
abstractmethod
=
abc
.
abstractmethod
class
Attack
(
object
):
class
Attack
(
object
):
"""
"""
Abstract base class for adversarial attacks. `Attack` represent an adversarial attack
Abstract base class for adversarial attacks. `Attack` represent an adversarial attack
...
...
adversarial/advbox/attacks/gradientsign.py
浏览文件 @
8e8e5a89
...
@@ -6,6 +6,7 @@ import numpy as np
...
@@ -6,6 +6,7 @@ import numpy as np
from
collections
import
Iterable
from
collections
import
Iterable
from
.base
import
Attack
from
.base
import
Attack
class
GradientSignAttack
(
Attack
):
class
GradientSignAttack
(
Attack
):
"""
"""
This attack was originally implemented by Goodfellow et al. (2015) with the
This attack was originally implemented by Goodfellow et al. (2015) with the
...
@@ -22,10 +23,11 @@ class GradientSignAttack(Attack):
...
@@ -22,10 +23,11 @@ class GradientSignAttack(Attack):
gradient_sign
=
np
.
sign
(
gradient
)
*
(
max_
-
min_
)
gradient_sign
=
np
.
sign
(
gradient
)
*
(
max_
-
min_
)
if
not
isinstance
(
epsilons
,
Iterable
):
if
not
isinstance
(
epsilons
,
Iterable
):
epsilons
=
np
.
linspace
(
0
,
1
,
num
=
epsilons
+
1
)
epsilons
=
np
.
linspace
(
0
,
1
,
num
=
epsilons
+
1
)
for
epsilon
in
epsilons
:
for
epsilon
in
epsilons
:
adv_img
=
image_batch
[
0
][
0
].
reshape
(
gradient_sign
.
shape
)
+
epsilon
*
gradient_sign
adv_img
=
image_batch
[
0
][
0
].
reshape
(
gradient_sign
.
shape
)
+
epsilon
*
gradient_sign
adv_img
=
np
.
clip
(
adv_img
,
min_
,
max_
)
adv_img
=
np
.
clip
(
adv_img
,
min_
,
max_
)
adv_label
=
np
.
argmax
(
self
.
model
.
predict
([(
adv_img
,
0
)]))
adv_label
=
np
.
argmax
(
self
.
model
.
predict
([(
adv_img
,
0
)]))
#print("pre_label="+str(pre_label)+ " adv_label="+str(adv_label))
#print("pre_label="+str(pre_label)+ " adv_label="+str(adv_label))
...
@@ -33,4 +35,5 @@ class GradientSignAttack(Attack):
...
@@ -33,4 +35,5 @@ class GradientSignAttack(Attack):
#print(epsilon, pre_label, adv_label)
#print(epsilon, pre_label, adv_label)
return
adv_img
return
adv_img
FGSM
=
GradientSignAttack
FGSM
=
GradientSignAttack
adversarial/advbox/models/base.py
浏览文件 @
8e8e5a89
...
@@ -6,8 +6,8 @@ import abc
...
@@ -6,8 +6,8 @@ import abc
abstractmethod
=
abc
.
abstractmethod
abstractmethod
=
abc
.
abstractmethod
class
Model
(
object
):
class
Model
(
object
):
"""
"""
Base class of model to provide attack.
Base class of model to provide attack.
...
...
adversarial/advbox/models/paddle.py
浏览文件 @
8e8e5a89
...
@@ -7,6 +7,7 @@ from paddle.v2.fluid.framework import program_guard
...
@@ -7,6 +7,7 @@ from paddle.v2.fluid.framework import program_guard
from
.base
import
Model
from
.base
import
Model
class
PaddleModel
(
Model
):
class
PaddleModel
(
Model
):
"""
"""
Create a PaddleModel instance.
Create a PaddleModel instance.
...
@@ -30,9 +31,7 @@ class PaddleModel(Model):
...
@@ -30,9 +31,7 @@ class PaddleModel(Model):
channel_axis
=
3
,
channel_axis
=
3
,
preprocess
=
None
):
preprocess
=
None
):
super
(
PaddleModel
,
self
).
__init__
(
super
(
PaddleModel
,
self
).
__init__
(
bounds
=
bounds
,
bounds
=
bounds
,
channel_axis
=
channel_axis
,
preprocess
=
preprocess
)
channel_axis
=
channel_axis
,
preprocess
=
preprocess
)
if
preprocess
is
None
:
if
preprocess
is
None
:
preprocess
=
(
0
,
1
)
preprocess
=
(
0
,
1
)
...
@@ -48,7 +47,8 @@ class PaddleModel(Model):
...
@@ -48,7 +47,8 @@ class PaddleModel(Model):
# gradient
# gradient
loss
=
self
.
_program
.
block
(
0
).
var
(
self
.
_cost_name
)
loss
=
self
.
_program
.
block
(
0
).
var
(
self
.
_cost_name
)
param_grads
=
fluid
.
backward
.
append_backward
(
loss
,
parameter_list
=
[
self
.
_input_name
])
param_grads
=
fluid
.
backward
.
append_backward
(
loss
,
parameter_list
=
[
self
.
_input_name
])
self
.
_gradient
=
param_grads
[
0
][
1
]
self
.
_gradient
=
param_grads
[
0
][
1
]
def
predict
(
self
,
image_batch
):
def
predict
(
self
,
image_batch
):
...
@@ -63,14 +63,11 @@ class PaddleModel(Model):
...
@@ -63,14 +63,11 @@ class PaddleModel(Model):
feeder
=
fluid
.
DataFeeder
(
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
self
.
_input_name
,
self
.
_logits_name
],
feed_list
=
[
self
.
_input_name
,
self
.
_logits_name
],
place
=
self
.
_place
,
place
=
self
.
_place
,
program
=
self
.
_program
program
=
self
.
_program
)
)
predict_var
=
self
.
_program
.
block
(
0
).
var
(
self
.
_predict_name
)
predict_var
=
self
.
_program
.
block
(
0
).
var
(
self
.
_predict_name
)
predict
=
self
.
_exe
.
run
(
predict
=
self
.
_exe
.
run
(
self
.
_program
,
self
.
_program
,
feed
=
feeder
.
feed
(
image_batch
),
feed
=
feeder
.
feed
(
image_batch
),
fetch_list
=
[
predict_var
]
fetch_list
=
[
predict_var
])
)
return
predict
return
predict
def
num_classes
(
self
):
def
num_classes
(
self
):
...
@@ -96,11 +93,9 @@ class PaddleModel(Model):
...
@@ -96,11 +93,9 @@ class PaddleModel(Model):
feeder
=
fluid
.
DataFeeder
(
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
self
.
_input_name
,
self
.
_logits_name
],
feed_list
=
[
self
.
_input_name
,
self
.
_logits_name
],
place
=
self
.
_place
,
place
=
self
.
_place
,
program
=
self
.
_program
program
=
self
.
_program
)
)
grad
,
=
self
.
_exe
.
run
(
grad
,
=
self
.
_exe
.
run
(
self
.
_program
,
self
.
_program
,
feed
=
feeder
.
feed
(
image_batch
),
feed
=
feeder
.
feed
(
image_batch
),
fetch_list
=
[
self
.
_gradient
])
fetch_list
=
[
self
.
_gradient
])
return
grad
return
grad
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录