Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
35210a04
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
提交
35210a04
编写于
1月 06, 2018
作者:
G
gx_wind
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
delete unused files
上级
2b3ba40e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
0 addition
and
145 deletion
+0
-145
adversarial/advbox/tutorials/tutorial_model.py
adversarial/advbox/tutorials/tutorial_model.py
+0
-32
adversarial/mnist_fgsm.py
adversarial/mnist_fgsm.py
+0
-113
未找到文件。
adversarial/advbox/tutorials/tutorial_model.py
已删除
100644 → 0
浏览文件 @
2b3ba40e
################################################################################
#
# Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
#
################################################################################
"""
A pure Paddlepaddle implementation of a neural network.
"""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
__future__
import
unicode_literals
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
from
advbox
import
Model
def
main
():
"""
example main function
"""
model_dir
=
"./mnist_model"
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
program
,
feed_var_names
,
fetch_vars
=
fluid
.
io
.
load_inferfence_model
(
model_dir
,
exe
)
print
(
program
)
if
__name__
==
"__main__"
:
main
()
adversarial/mnist_fgsm.py
已删除
100644 → 0
浏览文件 @
2b3ba40e
"""
This attack was originally implemented by Goodfellow et al. (2015) with the
infinity norm (and is known as the "Fast Gradient Sign Method"). This is therefore called
the Fast Gradient Method.
Paper link: https://arxiv.org/abs/1412.6572
"""
import
numpy
as
np
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
BATCH_SIZE
=
50
PASS_NUM
=
1
EPS
=
0.3
CLIP_MIN
=
-
1
CLIP_MAX
=
1
PASS_NUM
=
1
def
mnist_cnn_model
(
img
):
"""
Mnist cnn model
Args:
img(Varaible): the input image to be recognized
Returns:
Variable: the label prediction
"""
#conv1 = fluid.nets.conv2d()
conv_pool_1
=
fluid
.
nets
.
simple_img_conv_pool
(
input
=
img
,
num_filters
=
20
,
filter_size
=
5
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
'relu'
)
conv_pool_2
=
fluid
.
nets
.
simple_img_conv_pool
(
input
=
conv_pool_1
,
num_filters
=
50
,
filter_size
=
5
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
'relu'
)
logits
=
fluid
.
layers
.
fc
(
input
=
conv_pool_2
,
size
=
10
,
act
=
'softmax'
)
return
logits
def
main
():
"""
Generate adverserial example and evaluate accuracy on mnist using FGSM
"""
images
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
# The gradient should flow
images
.
stop_gradient
=
False
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
predict
=
mnist_cnn_model
(
images
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
# Cal gradient of input
params_grads
=
fluid
.
backward
.
append_backward_ops
(
avg_cost
,
parameter_list
=
[
'pixel'
])
# data batch
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
500
),
batch_size
=
BATCH_SIZE
)
accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
predict
,
label
=
label
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
accuracy
.
reset
(
exe
)
#exe.run(fluid.default_startup_program())
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
images
,
label
],
place
=
place
)
for
pass_id
in
range
(
PASS_NUM
):
fluid
.
io
.
load_params
(
exe
,
"./mnist/"
,
main_program
=
fluid
.
default_main_program
())
for
data
in
train_reader
():
# cal gradient and eval accuracy
ps
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
params_grads
[
0
][
1
]]
+
accuracy
.
metrics
)
labels
=
[]
for
idx
,
_
in
enumerate
(
data
):
labels
.
append
(
data
[
idx
][
1
])
# generate adversarial example
batch_num
=
ps
.
shape
[
0
]
new_data
=
[]
for
i
in
range
(
batch_num
):
adv_img
=
np
.
reshape
(
data
[
0
][
0
],
(
1
,
28
,
28
))
+
EPS
*
np
.
sign
(
ps
[
i
])
adv_img
=
np
.
clip
(
adv_img
,
CLIP_MIN
,
CLIP_MAX
)
#adv_imgs.append(adv_img)
t
=
(
adv_img
,
data
[
0
][
1
])
new_data
.
append
(
t
)
# predict label
predict_label
,
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
new_data
),
fetch_list
=
[
predict
])
adv_labels
=
np
.
argmax
(
predict_label
,
axis
=
1
)
batch_accuracy
=
np
.
mean
(
np
.
equal
(
labels
,
adv_labels
))
print
"pass_id="
+
str
(
pass_id
)
+
" acc="
+
str
(
acc
)
+
" adv_acc="
+
str
(
batch_accuracy
)
if
__name__
==
"__main__"
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录