Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
97724c2a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
97724c2a
编写于
1月 09, 2018
作者:
G
gx_wind
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bugs and modify func param name
上级
bf1e0372
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
13 addition
and
18 deletion
+13
-18
adversarial/advbox/attacks/base.py
adversarial/advbox/attacks/base.py
+5
-5
adversarial/advbox/attacks/gradientsign.py
adversarial/advbox/attacks/gradientsign.py
+5
-4
adversarial/advbox/models/base.py
adversarial/advbox/models/base.py
+1
-2
adversarial/advbox/models/paddle.py
adversarial/advbox/models/paddle.py
+1
-1
adversarial/fluid_mnist.py
adversarial/fluid_mnist.py
+1
-6
未找到文件。
adversarial/advbox/attacks/base.py
浏览文件 @
97724c2a
...
...
@@ -18,22 +18,22 @@ class Attack(object):
def
__init__
(
self
,
model
):
self
.
model
=
model
def
__call__
(
self
,
image_
batch
):
def
__call__
(
self
,
image_
label
):
"""
Generate the adversarial sample.
Args:
image_
batch(list): The image and label tuple lis
t.
image_
label(list): The image and label tuple list with one elemen
t.
"""
adv_img
=
self
.
_apply
(
image_
batch
)
adv_img
=
self
.
_apply
(
image_
label
)
return
adv_img
@
abstractmethod
def
_apply
(
self
,
image_
batch
):
def
_apply
(
self
,
image_
label
):
"""
Search an adversarial example.
Args:
image_batch(list): The image and label tuple list.
image_batch(list): The image and label tuple list
with one element
.
"""
raise
NotImplementedError
adversarial/advbox/attacks/gradientsign.py
浏览文件 @
97724c2a
...
...
@@ -15,18 +15,19 @@ class GradientSignAttack(Attack):
Paper link: https://arxiv.org/abs/1412.6572
"""
def
_apply
(
self
,
image_batch
,
epsilons
=
1000
):
pre_label
=
np
.
argmax
(
self
.
model
.
predict
(
image_batch
))
def
_apply
(
self
,
image_label
,
epsilons
=
1000
):
assert
len
(
image_label
)
==
1
pre_label
=
np
.
argmax
(
self
.
model
.
predict
(
image_label
))
min_
,
max_
=
self
.
model
.
bounds
()
gradient
=
self
.
model
.
gradient
(
image_
batch
)
gradient
=
self
.
model
.
gradient
(
image_
label
)
gradient_sign
=
np
.
sign
(
gradient
)
*
(
max_
-
min_
)
if
not
isinstance
(
epsilons
,
Iterable
):
epsilons
=
np
.
linspace
(
0
,
1
,
num
=
epsilons
+
1
)
for
epsilon
in
epsilons
:
adv_img
=
image_
batch
[
0
][
0
].
reshape
(
adv_img
=
image_
label
[
0
][
0
].
reshape
(
gradient_sign
.
shape
)
+
epsilon
*
gradient_sign
adv_img
=
np
.
clip
(
adv_img
,
min_
,
max_
)
adv_label
=
np
.
argmax
(
self
.
model
.
predict
([(
adv_img
,
0
)]))
...
...
adversarial/advbox/models/base.py
浏览文件 @
97724c2a
...
...
@@ -81,8 +81,7 @@ class Model(object):
Calculate the gradient of the cross-entropy loss w.r.t the image.
Args:
image(numpy.ndarray): image with shape (height, width, channel)
label(int): image label used to cal gradient.
image_batch(list): The image and label tuple list.
Return:
numpy.ndarray: gradient of the cross-entropy loss w.r.t the image with
...
...
adversarial/advbox/models/paddle.py
浏览文件 @
97724c2a
...
...
@@ -49,7 +49,7 @@ class PaddleModel(Model):
loss
=
self
.
_program
.
block
(
0
).
var
(
self
.
_cost_name
)
param_grads
=
fluid
.
backward
.
append_backward
(
loss
,
parameter_list
=
[
self
.
_input_name
])
self
.
_gradient
=
param_grads
[
0
][
1
]
self
.
_gradient
=
dict
(
param_grads
)[
self
.
_input_name
]
def
predict
(
self
,
image_batch
):
"""
...
...
adversarial/fluid_mnist.py
浏览文件 @
97724c2a
...
...
@@ -15,7 +15,6 @@ def mnist_cnn_model(img):
Returns:
Variable: the label prediction
"""
#conv1 = fluid.nets.conv2d()
conv_pool_1
=
fluid
.
nets
.
simple_img_conv_pool
(
input
=
img
,
num_filters
=
20
,
...
...
@@ -73,19 +72,15 @@ def main():
pass_acc
=
accuracy
.
eval
(
exe
)
print
(
"pass_id="
+
str
(
pass_id
)
+
" acc="
+
str
(
acc
)
+
" pass_acc="
+
str
(
pass_acc
))
# print loss, acc
if
loss
<
LOSS_THRESHOLD
and
pass_acc
>
ACC_THRESHOLD
:
# if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good.
break
# exit(0)
pass_acc
=
accuracy
.
eval
(
exe
)
print
(
"pass_id="
+
str
(
pass_id
)
+
" pass_acc="
+
str
(
pass_acc
))
fluid
.
io
.
save_params
(
exe
,
dirname
=
'./mnist'
,
main_program
=
fluid
.
default_main_program
())
print
(
'train mnist done'
)
exit
(
1
)
if
__name__
==
'__main__'
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录