Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
0a83aa46
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看板
未验证
提交
0a83aa46
编写于
2月 10, 2018
作者:
wgzqz
提交者:
GitHub
2月 10, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request
#1
from PaddlePaddle/develop
Merge from upstream
上级
ae418490
08f169cb
变更
14
展开全部
隐藏空白更改
内联
并排
Showing
14 changed file
with
1727 addition
and
266 deletion
+1727
-266
fluid/DeepASR/data_utils/augmentor/tests/__init__.py
fluid/DeepASR/data_utils/augmentor/tests/__init__.py
+7
-0
fluid/DeepASR/data_utils/data_reader.py
fluid/DeepASR/data_utils/data_reader.py
+416
-239
fluid/DeepASR/data_utils/util.py
fluid/DeepASR/data_utils/util.py
+44
-0
fluid/DeepASR/infer.py
fluid/DeepASR/infer.py
+112
-0
fluid/DeepASR/model_utils/__init__.py
fluid/DeepASR/model_utils/__init__.py
+0
-0
fluid/DeepASR/model_utils/model.py
fluid/DeepASR/model_utils/model.py
+105
-0
fluid/DeepASR/tools/_init_paths.py
fluid/DeepASR/tools/_init_paths.py
+19
-0
fluid/DeepASR/tools/profile.py
fluid/DeepASR/tools/profile.py
+189
-0
fluid/DeepASR/train.py
fluid/DeepASR/train.py
+229
-0
fluid/adversarial/advbox/attacks/saliency.py
fluid/adversarial/advbox/attacks/saliency.py
+146
-0
fluid/adversarial/mnist_tutorial_jsma.py
fluid/adversarial/mnist_tutorial_jsma.py
+97
-0
fluid/image_classification/mobilenet.py
fluid/image_classification/mobilenet.py
+218
-0
fluid/image_classification/se_resnext.py
fluid/image_classification/se_resnext.py
+48
-27
fluid/ocr_recognition/ctc_reader.py
fluid/ocr_recognition/ctc_reader.py
+97
-0
未找到文件。
fluid/DeepASR/data_utils/augmentor/tests/__init__.py
0 → 100644
浏览文件 @
0a83aa46
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
data_utils.augmentor.trans_mean_variance_norm
as
trans_mean_variance_norm
import
data_utils.augmentor.trans_add_delta
as
trans_add_delta
import
data_utils.augmentor.trans_splice
as
trans_splice
fluid/DeepASR/data_utils/data_reader.py
浏览文件 @
0a83aa46
此差异已折叠。
点击以展开。
fluid/DeepASR/data_utils/util.py
浏览文件 @
0a83aa46
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
division
from
__future__
import
print_function
from
__future__
import
print_function
import
sys
from
six
import
reraise
from
tblib
import
Traceback
import
numpy
as
np
def
to_lodtensor
(
data
,
place
):
def
to_lodtensor
(
data
,
place
):
...
@@ -28,3 +33,42 @@ def lodtensor_to_ndarray(lod_tensor):
...
@@ -28,3 +33,42 @@ def lodtensor_to_ndarray(lod_tensor):
for
i
in
xrange
(
np
.
product
(
dims
)):
for
i
in
xrange
(
np
.
product
(
dims
)):
ret
.
ravel
()[
i
]
=
lod_tensor
.
get_float_element
(
i
)
ret
.
ravel
()[
i
]
=
lod_tensor
.
get_float_element
(
i
)
return
ret
,
lod_tensor
.
lod
()
return
ret
,
lod_tensor
.
lod
()
class
CriticalException
(
Exception
):
pass
def
suppress_signal
(
signo
,
stack_frame
):
pass
def
suppress_complaints
(
verbose
,
notify
=
None
):
def
decorator_maker
(
func
):
def
suppress_warpper
(
*
args
,
**
kwargs
):
try
:
func
(
*
args
,
**
kwargs
)
except
:
et
,
ev
,
tb
=
sys
.
exc_info
()
if
notify
is
not
None
:
notify
(
except_type
=
et
,
except_value
=
ev
,
traceback
=
tb
)
if
verbose
==
1
or
isinstance
(
ev
,
CriticalException
):
reraise
(
et
,
ev
,
Traceback
(
tb
).
as_traceback
())
return
suppress_warpper
return
decorator_maker
class
ForceExitWrapper
(
object
):
def
__init__
(
self
,
exit_flag
):
self
.
_exit_flag
=
exit_flag
@
suppress_complaints
(
verbose
=
0
)
def
__call__
(
self
,
*
args
,
**
kwargs
):
self
.
_exit_flag
.
value
=
True
def
__eq__
(
self
,
flag
):
return
self
.
_exit_flag
.
value
==
flag
fluid/DeepASR/infer.py
0 → 100644
浏览文件 @
0a83aa46
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
argparse
import
paddle.v2.fluid
as
fluid
import
data_utils.augmentor.trans_mean_variance_norm
as
trans_mean_variance_norm
import
data_utils.augmentor.trans_add_delta
as
trans_add_delta
import
data_utils.augmentor.trans_splice
as
trans_splice
import
data_utils.data_reader
as
reader
from
data_utils.util
import
lodtensor_to_ndarray
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"Inference for stacked LSTMP model."
)
parser
.
add_argument
(
'--batch_size'
,
type
=
int
,
default
=
32
,
help
=
'The sequence number of a batch data. (default: %(default)d)'
)
parser
.
add_argument
(
'--device'
,
type
=
str
,
default
=
'GPU'
,
choices
=
[
'CPU'
,
'GPU'
],
help
=
'The device type. (default: %(default)s)'
)
parser
.
add_argument
(
'--mean_var'
,
type
=
str
,
default
=
'data/global_mean_var_search26kHr'
,
help
=
"The path for feature's global mean and variance. "
"(default: %(default)s)"
)
parser
.
add_argument
(
'--infer_feature_lst'
,
type
=
str
,
default
=
'data/infer_feature.lst'
,
help
=
'The feature list path for inference. (default: %(default)s)'
)
parser
.
add_argument
(
'--infer_label_lst'
,
type
=
str
,
default
=
'data/infer_label.lst'
,
help
=
'The label list path for inference. (default: %(default)s)'
)
parser
.
add_argument
(
'--model_save_path'
,
type
=
str
,
default
=
'./checkpoints/deep_asr.pass_0.model/'
,
help
=
'The directory for saving model. (default: %(default)s)'
)
args
=
parser
.
parse_args
()
return
args
def
print_arguments
(
args
):
print
(
'----------- Configuration Arguments -----------'
)
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
()):
print
(
'%s: %s'
%
(
arg
,
value
))
print
(
'------------------------------------------------'
)
def
split_infer_result
(
infer_seq
,
lod
):
infer_batch
=
[]
for
i
in
xrange
(
0
,
len
(
lod
[
0
])
-
1
):
infer_batch
.
append
(
infer_seq
[
lod
[
0
][
i
]:
lod
[
0
][
i
+
1
]])
return
infer_batch
def
infer
(
args
):
""" Gets one batch of feature data and predicts labels for each sample.
"""
if
not
os
.
path
.
exists
(
args
.
model_save_path
):
raise
IOError
(
"Invalid model path!"
)
place
=
fluid
.
CUDAPlace
(
0
)
if
args
.
device
==
'GPU'
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
# load model
[
infer_program
,
feed_dict
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
args
.
model_save_path
,
exe
)
ltrans
=
[
trans_add_delta
.
TransAddDelta
(
2
,
2
),
trans_mean_variance_norm
.
TransMeanVarianceNorm
(
args
.
mean_var
),
trans_splice
.
TransSplice
()
]
infer_data_reader
=
reader
.
DataReader
(
args
.
infer_feature_lst
,
args
.
infer_label_lst
)
infer_data_reader
.
set_transformers
(
ltrans
)
feature_t
=
fluid
.
LoDTensor
()
one_batch
=
infer_data_reader
.
batch_iterator
(
args
.
batch_size
,
1
).
next
()
(
features
,
labels
,
lod
)
=
one_batch
feature_t
.
set
(
features
,
place
)
feature_t
.
set_lod
([
lod
])
results
=
exe
.
run
(
infer_program
,
feed
=
{
feed_dict
[
0
]:
feature_t
},
fetch_list
=
fetch_targets
,
return_numpy
=
False
)
probs
,
lod
=
lodtensor_to_ndarray
(
results
[
0
])
preds
=
probs
.
argmax
(
axis
=
1
)
infer_batch
=
split_infer_result
(
preds
,
lod
)
for
index
,
sample
in
enumerate
(
infer_batch
):
print
(
"result %d: "
%
index
,
sample
,
'
\n
'
)
if
__name__
==
'__main__'
:
args
=
parse_args
()
print_arguments
(
args
)
infer
(
args
)
fluid/DeepASR/model_utils/__init__.py
0 → 100644
浏览文件 @
0a83aa46
fluid/DeepASR/model_utils/model.py
0 → 100644
浏览文件 @
0a83aa46
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
def
stacked_lstmp_model
(
hidden_dim
,
proj_dim
,
stacked_num
,
class_num
,
parallel
=
False
,
is_train
=
True
):
""" The model for DeepASR. The main structure is composed of stacked
identical LSTMP (LSTM with recurrent projection) layers.
When running in training and validation phase, the feeding dictionary
is {'feature', 'label'}, fed by the LodTensor for feature data and
label data respectively. And in inference, only `feature` is needed.
Args:
hidden_dim(int): The hidden state's dimension of the LSTMP layer.
proj_dim(int): The projection size of the LSTMP layer.
stacked_num(int): The number of stacked LSTMP layers.
parallel(bool): Run in parallel or not, default `False`.
is_train(bool): Run in training phase or not, default `True`.
class_dim(int): The number of output classes.
"""
# network configuration
def
_net_conf
(
feature
,
label
):
seq_conv1
=
fluid
.
layers
.
sequence_conv
(
input
=
feature
,
num_filters
=
1024
,
filter_size
=
3
,
filter_stride
=
1
,
bias_attr
=
True
)
bn1
=
fluid
.
layers
.
batch_norm
(
input
=
seq_conv1
,
act
=
"sigmoid"
,
is_test
=
not
is_train
,
momentum
=
0.9
,
epsilon
=
1e-05
,
data_layout
=
'NCHW'
)
stack_input
=
bn1
for
i
in
range
(
stacked_num
):
fc
=
fluid
.
layers
.
fc
(
input
=
stack_input
,
size
=
hidden_dim
*
4
,
bias_attr
=
True
)
proj
,
cell
=
fluid
.
layers
.
dynamic_lstmp
(
input
=
fc
,
size
=
hidden_dim
*
4
,
proj_size
=
proj_dim
,
bias_attr
=
True
,
use_peepholes
=
True
,
is_reverse
=
False
,
cell_activation
=
"tanh"
,
proj_activation
=
"tanh"
)
bn
=
fluid
.
layers
.
batch_norm
(
input
=
proj
,
act
=
"sigmoid"
,
is_test
=
not
is_train
,
momentum
=
0.9
,
epsilon
=
1e-05
,
data_layout
=
'NCHW'
)
stack_input
=
bn
prediction
=
fluid
.
layers
.
fc
(
input
=
stack_input
,
size
=
class_num
,
act
=
'softmax'
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
acc
=
fluid
.
layers
.
accuracy
(
input
=
prediction
,
label
=
label
)
return
prediction
,
avg_cost
,
acc
# data feeder
feature
=
fluid
.
layers
.
data
(
name
=
"feature"
,
shape
=
[
-
1
,
120
*
11
],
dtype
=
"float32"
,
lod_level
=
1
)
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
-
1
,
1
],
dtype
=
"int64"
,
lod_level
=
1
)
if
parallel
:
# When the execution place is specified to CUDAPlace, the program will
# run on all $CUDA_VISIBLE_DEVICES GPUs. Otherwise the program will
# run on all CPU devices.
places
=
fluid
.
layers
.
get_places
()
pd
=
fluid
.
layers
.
ParallelDo
(
places
)
with
pd
.
do
():
feat_
=
pd
.
read_input
(
feature
)
label_
=
pd
.
read_input
(
label
)
prediction
,
avg_cost
,
acc
=
_net_conf
(
feat_
,
label_
)
for
out
in
[
avg_cost
,
acc
]:
pd
.
write_output
(
out
)
# get mean loss and acc through every devices.
avg_cost
,
acc
=
pd
()
avg_cost
=
fluid
.
layers
.
mean
(
x
=
avg_cost
)
acc
=
fluid
.
layers
.
mean
(
x
=
acc
)
else
:
prediction
,
avg_cost
,
acc
=
_net_conf
(
feature
,
label
)
return
prediction
,
avg_cost
,
acc
fluid/DeepASR/tools/_init_paths.py
0 → 100644
浏览文件 @
0a83aa46
"""Add the parent directory to $PYTHONPATH"""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os.path
import
sys
def
add_path
(
path
):
if
path
not
in
sys
.
path
:
sys
.
path
.
insert
(
0
,
path
)
this_dir
=
os
.
path
.
dirname
(
__file__
)
# Add project path to PYTHONPATH
proj_path
=
os
.
path
.
join
(
this_dir
,
'..'
)
add_path
(
proj_path
)
fluid/DeepASR/tools/profile.py
0 → 100644
浏览文件 @
0a83aa46
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
sys
import
numpy
as
np
import
argparse
import
time
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid.profiler
as
profiler
import
_init_paths
import
data_utils.augmentor.trans_mean_variance_norm
as
trans_mean_variance_norm
import
data_utils.augmentor.trans_add_delta
as
trans_add_delta
import
data_utils.augmentor.trans_splice
as
trans_splice
import
data_utils.data_reader
as
reader
from
model_utils.model
import
stacked_lstmp_model
from
data_utils.util
import
lodtensor_to_ndarray
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"Profiling for the stacked LSTMP model."
)
parser
.
add_argument
(
'--batch_size'
,
type
=
int
,
default
=
32
,
help
=
'The sequence number of a batch data. (default: %(default)d)'
)
parser
.
add_argument
(
'--minimum_batch_size'
,
type
=
int
,
default
=
1
,
help
=
'The minimum sequence number of a batch data. '
'(default: %(default)d)'
)
parser
.
add_argument
(
'--stacked_num'
,
type
=
int
,
default
=
5
,
help
=
'Number of lstmp layers to stack. (default: %(default)d)'
)
parser
.
add_argument
(
'--proj_dim'
,
type
=
int
,
default
=
512
,
help
=
'Project size of lstmp unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--hidden_dim'
,
type
=
int
,
default
=
1024
,
help
=
'Hidden size of lstmp unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--learning_rate'
,
type
=
float
,
default
=
0.002
,
help
=
'Learning rate used to train. (default: %(default)f)'
)
parser
.
add_argument
(
'--device'
,
type
=
str
,
default
=
'GPU'
,
choices
=
[
'CPU'
,
'GPU'
],
help
=
'The device type. (default: %(default)s)'
)
parser
.
add_argument
(
'--parallel'
,
action
=
'store_true'
,
help
=
'If set, run in parallel.'
)
parser
.
add_argument
(
'--mean_var'
,
type
=
str
,
default
=
'data/global_mean_var_search26kHr'
,
help
=
'mean var path'
)
parser
.
add_argument
(
'--feature_lst'
,
type
=
str
,
default
=
'data/feature.lst'
,
help
=
'feature list path.'
)
parser
.
add_argument
(
'--label_lst'
,
type
=
str
,
default
=
'data/label.lst'
,
help
=
'label list path.'
)
parser
.
add_argument
(
'--max_batch_num'
,
type
=
int
,
default
=
10
,
help
=
'Maximum number of batches for profiling. (default: %(default)d)'
)
parser
.
add_argument
(
'--first_batches_to_skip'
,
type
=
int
,
default
=
1
,
help
=
'Number of first batches to skip for profiling. '
'(default: %(default)d)'
)
parser
.
add_argument
(
'--print_train_acc'
,
action
=
'store_true'
,
help
=
'If set, output training accuray.'
)
parser
.
add_argument
(
'--sorted_key'
,
type
=
str
,
default
=
'total'
,
choices
=
[
'None'
,
'total'
,
'calls'
,
'min'
,
'max'
,
'ave'
],
help
=
'Different types of time to sort the profiling report. '
'(default: %(default)s)'
)
args
=
parser
.
parse_args
()
return
args
def
print_arguments
(
args
):
print
(
'----------- Configuration Arguments -----------'
)
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
()):
print
(
'%s: %s'
%
(
arg
,
value
))
print
(
'------------------------------------------------'
)
def
profile
(
args
):
"""profile the training process.
"""
if
not
args
.
first_batches_to_skip
<
args
.
max_batch_num
:
raise
ValueError
(
"arg 'first_batches_to_skip' must be smaller than "
"'max_batch_num'."
)
if
not
args
.
first_batches_to_skip
>=
0
:
raise
ValueError
(
"arg 'first_batches_to_skip' must not be smaller than 0."
)
_
,
avg_cost
,
accuracy
=
stacked_lstmp_model
(
hidden_dim
=
args
.
hidden_dim
,
proj_dim
=
args
.
proj_dim
,
stacked_num
=
args
.
stacked_num
,
class_num
=
1749
,
parallel
=
args
.
parallel
)
adam_optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
adam_optimizer
.
minimize
(
avg_cost
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
ltrans
=
[
trans_add_delta
.
TransAddDelta
(
2
,
2
),
trans_mean_variance_norm
.
TransMeanVarianceNorm
(
args
.
mean_var
),
trans_splice
.
TransSplice
()
]
data_reader
=
reader
.
DataReader
(
args
.
feature_lst
,
args
.
label_lst
)
data_reader
.
set_transformers
(
ltrans
)
feature_t
=
fluid
.
LoDTensor
()
label_t
=
fluid
.
LoDTensor
()
sorted_key
=
None
if
args
.
sorted_key
is
'None'
else
args
.
sorted_key
with
profiler
.
profiler
(
args
.
device
,
sorted_key
)
as
prof
:
frames_seen
,
start_time
=
0
,
0.0
for
batch_id
,
batch_data
in
enumerate
(
data_reader
.
batch_iterator
(
args
.
batch_size
,
args
.
minimum_batch_size
)):
if
batch_id
>=
args
.
max_batch_num
:
break
if
args
.
first_batches_to_skip
==
batch_id
:
profiler
.
reset_profiler
()
start_time
=
time
.
time
()
frames_seen
=
0
# load_data
(
features
,
labels
,
lod
)
=
batch_data
feature_t
.
set
(
features
,
place
)
feature_t
.
set_lod
([
lod
])
label_t
.
set
(
labels
,
place
)
label_t
.
set_lod
([
lod
])
frames_seen
+=
lod
[
-
1
]
outs
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"feature"
:
feature_t
,
"label"
:
label_t
},
fetch_list
=
[
avg_cost
,
accuracy
],
return_numpy
=
False
)
if
args
.
print_train_acc
:
print
(
"Batch %d acc: %f"
%
(
batch_id
,
lodtensor_to_ndarray
(
outs
[
1
])[
0
]))
else
:
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
time_consumed
=
time
.
time
()
-
start_time
frames_per_sec
=
frames_seen
/
time_consumed
print
(
"
\n
Time consumed: %f s, performance: %f frames/s."
%
(
time_consumed
,
frames_per_sec
))
if
__name__
==
'__main__'
:
args
=
parse_args
()
print_arguments
(
args
)
profile
(
args
)
fluid/DeepASR/
stacked_dynamic_lstm
.py
→
fluid/DeepASR/
train
.py
浏览文件 @
0a83aa46
...
@@ -2,26 +2,34 @@ from __future__ import absolute_import
...
@@ -2,26 +2,34 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
division
from
__future__
import
print_function
from
__future__
import
print_function
import
sys
import
os
import
numpy
as
np
import
numpy
as
np
import
argparse
import
argparse
import
time
import
time
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid.profiler
as
profiler
import
data_utils.augmentor.trans_mean_variance_norm
as
trans_mean_variance_norm
import
data_utils.trans_mean_variance_norm
as
trans_mean_variance_norm
import
data_utils.augmentor.trans_add_delta
as
trans_add_delta
import
data_utils.trans_add_delta
as
trans_add_delta
import
data_utils.augmentor.trans_splice
as
trans_splice
import
data_utils.trans_splice
as
trans_splice
import
data_utils.data_reader
as
reader
import
data_utils.data_reader
as
reader
from
data_utils.util
import
lodtensor_to_ndarray
from
model_utils.model
import
stacked_lstmp_model
def
parse_args
():
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"
LSTM model benchmark
."
)
parser
=
argparse
.
ArgumentParser
(
"
Training for stacked LSTMP model
."
)
parser
.
add_argument
(
parser
.
add_argument
(
'--batch_size'
,
'--batch_size'
,
type
=
int
,
type
=
int
,
default
=
32
,
default
=
32
,
help
=
'The sequence number of a batch data. (default: %(default)d)'
)
help
=
'The sequence number of a batch data. (default: %(default)d)'
)
parser
.
add_argument
(
'--minimum_batch_size'
,
type
=
int
,
default
=
1
,
help
=
'The minimum sequence number of a batch data. '
'(default: %(default)d)'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--stacked_num'
,
'--stacked_num'
,
type
=
int
,
type
=
int
,
...
@@ -42,6 +50,11 @@ def parse_args():
...
@@ -42,6 +50,11 @@ def parse_args():
type
=
int
,
type
=
int
,
default
=
100
,
default
=
100
,
help
=
'Epoch number to train. (default: %(default)d)'
)
help
=
'Epoch number to train. (default: %(default)d)'
)
parser
.
add_argument
(
'--print_per_batches'
,
type
=
int
,
default
=
100
,
help
=
'Interval to print training accuracy. (default: %(default)d)'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--learning_rate'
,
'--learning_rate'
,
type
=
float
,
type
=
float
,
...
@@ -54,107 +67,68 @@ def parse_args():
...
@@ -54,107 +67,68 @@ def parse_args():
choices
=
[
'CPU'
,
'GPU'
],
choices
=
[
'CPU'
,
'GPU'
],
help
=
'The device type. (default: %(default)s)'
)
help
=
'The device type. (default: %(default)s)'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--infer_only'
,
action
=
'store_true'
,
help
=
'If set, run forward only.'
)
'--parallel'
,
action
=
'store_true'
,
help
=
'If set, run in parallel.'
)
parser
.
add_argument
(
'--mean_var'
,
type
=
str
,
default
=
'data/global_mean_var_search26kHr'
,
help
=
"The path for feature's global mean and variance. "
"(default: %(default)s)"
)
parser
.
add_argument
(
'--train_feature_lst'
,
type
=
str
,
default
=
'data/feature.lst'
,
help
=
'The feature list path for training. (default: %(default)s)'
)
parser
.
add_argument
(
'--train_label_lst'
,
type
=
str
,
default
=
'data/label.lst'
,
help
=
'The label list path for training. (default: %(default)s)'
)
parser
.
add_argument
(
'--val_feature_lst'
,
type
=
str
,
default
=
'data/val_feature.lst'
,
help
=
'The feature list path for validation. (default: %(default)s)'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--use_cprof'
,
action
=
'store_true'
,
help
=
'If set, use cProfile.'
)
'--val_label_lst'
,
type
=
str
,
default
=
'data/val_label.lst'
,
help
=
'The label list path for validation. (default: %(default)s)'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--use_nvprof'
,
'--model_save_dir'
,
action
=
'store_true'
,
type
=
str
,
help
=
'If set, use nvprof for CUDA.'
)
default
=
'./checkpoints'
,
parser
.
add_argument
(
'--mean_var'
,
type
=
str
,
help
=
'mean var path'
)
help
=
"The directory for saving model. Do not save model if set to "
parser
.
add_argument
(
'--feature_lst'
,
type
=
str
,
help
=
'mean var path'
)
"''. (default: %(default)s)"
)
parser
.
add_argument
(
'--label_lst'
,
type
=
str
,
help
=
'mean var path'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
return
args
return
args
def
print_arguments
(
args
):
def
print_arguments
(
args
):
vars
(
args
)[
'use_nvprof'
]
=
(
vars
(
args
)[
'use_nvprof'
]
and
vars
(
args
)[
'device'
]
==
'GPU'
)
print
(
'----------- Configuration Arguments -----------'
)
print
(
'----------- Configuration Arguments -----------'
)
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
()):
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
()):
print
(
'%s: %s'
%
(
arg
,
value
))
print
(
'%s: %s'
%
(
arg
,
value
))
print
(
'------------------------------------------------'
)
print
(
'------------------------------------------------'
)
def
dynamic_lstmp_model
(
hidden_dim
,
proj_dim
,
stacked_num
,
class_num
=
1749
,
is_train
=
True
):
feature
=
fluid
.
layers
.
data
(
name
=
"feature"
,
shape
=
[
-
1
,
120
*
11
],
dtype
=
"float32"
,
lod_level
=
1
)
seq_conv1
=
fluid
.
layers
.
sequence_conv
(
input
=
feature
,
num_filters
=
1024
,
filter_size
=
3
,
filter_stride
=
1
,
bias_attr
=
True
)
bn1
=
fluid
.
layers
.
batch_norm
(
input
=
seq_conv1
,
act
=
"sigmoid"
,
is_test
=
False
,
momentum
=
0.9
,
epsilon
=
1e-05
,
data_layout
=
'NCHW'
)
stack_input
=
bn1
for
i
in
range
(
stacked_num
):
fc
=
fluid
.
layers
.
fc
(
input
=
stack_input
,
size
=
hidden_dim
*
4
,
bias_attr
=
True
)
proj
,
cell
=
fluid
.
layers
.
dynamic_lstmp
(
input
=
fc
,
size
=
hidden_dim
*
4
,
proj_size
=
proj_dim
,
bias_attr
=
True
,
use_peepholes
=
True
,
is_reverse
=
False
,
cell_activation
=
"tanh"
,
proj_activation
=
"tanh"
)
bn
=
fluid
.
layers
.
batch_norm
(
input
=
proj
,
act
=
"sigmoid"
,
is_test
=
False
,
momentum
=
0.9
,
epsilon
=
1e-05
,
data_layout
=
'NCHW'
)
stack_input
=
bn
prediction
=
fluid
.
layers
.
fc
(
input
=
stack_input
,
size
=
class_num
,
act
=
'softmax'
)
if
not
is_train
:
return
feature
,
prediction
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
-
1
,
1
],
dtype
=
"int64"
,
lod_level
=
1
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
return
prediction
,
label
,
avg_cost
def
train
(
args
):
def
train
(
args
):
if
args
.
use_cprof
:
"""train in loop.
pr
=
cProfile
.
Profile
()
"""
pr
.
enable
()
prediction
,
label
,
avg_cost
=
dynamic_lstmp_model
(
prediction
,
avg_cost
,
accuracy
=
stacked_lstmp_model
(
args
.
hidden_dim
,
args
.
proj_dim
,
args
.
stacked_num
)
hidden_dim
=
args
.
hidden_dim
,
proj_dim
=
args
.
proj_dim
,
stacked_num
=
args
.
stacked_num
,
class_num
=
1749
,
parallel
=
args
.
parallel
)
adam_optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
adam_optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
adam_optimizer
.
minimize
(
avg_cost
)
adam_optimizer
.
minimize
(
avg_cost
)
accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
prediction
,
label
=
label
)
# program for test
test_program
=
fluid
.
default_main_program
().
clone
()
# clone from default main program
with
fluid
.
program_guard
(
test_program
):
inference_program
=
fluid
.
default_main_program
().
clone
()
test_program
=
fluid
.
io
.
get_inference_program
([
avg_cost
,
accuracy
])
with
fluid
.
program_guard
(
inference_program
):
test_accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
prediction
,
label
=
label
)
test_target
=
[
avg_cost
]
+
test_accuracy
.
metrics
+
test_accuracy
.
states
inference_program
=
fluid
.
io
.
get_inference_program
(
test_target
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
=
fluid
.
Executor
(
place
)
...
@@ -166,62 +140,90 @@ def train(args):
...
@@ -166,62 +140,90 @@ def train(args):
trans_splice
.
TransSplice
()
trans_splice
.
TransSplice
()
]
]
data_reader
=
reader
.
DataRead
(
args
.
feature_lst
,
args
.
label_lst
)
feature_t
=
fluid
.
LoDTensor
()
data_reader
.
set_trans
(
ltrans
)
label_t
=
fluid
.
LoDTensor
()
res_feature
=
fluid
.
LoDTensor
()
# validation
res_label
=
fluid
.
LoDTensor
()
def
test
(
exe
):
# If test data not found, return invalid cost and accuracy
if
not
(
os
.
path
.
exists
(
args
.
val_feature_lst
)
and
os
.
path
.
exists
(
args
.
val_label_lst
)):
return
-
1.0
,
-
1.0
# test data reader
test_data_reader
=
reader
.
DataReader
(
args
.
val_feature_lst
,
args
.
val_label_lst
)
test_data_reader
.
set_transformers
(
ltrans
)
test_costs
,
test_accs
=
[],
[]
for
batch_id
,
batch_data
in
enumerate
(
test_data_reader
.
batch_iterator
(
args
.
batch_size
,
args
.
minimum_batch_size
)):
# load_data
(
features
,
labels
,
lod
)
=
batch_data
feature_t
.
set
(
features
,
place
)
feature_t
.
set_lod
([
lod
])
label_t
.
set
(
labels
,
place
)
label_t
.
set_lod
([
lod
])
cost
,
acc
=
exe
.
run
(
test_program
,
feed
=
{
"feature"
:
feature_t
,
"label"
:
label_t
},
fetch_list
=
[
avg_cost
,
accuracy
],
return_numpy
=
False
)
test_costs
.
append
(
lodtensor_to_ndarray
(
cost
)[
0
])
test_accs
.
append
(
lodtensor_to_ndarray
(
acc
)[
0
])
return
np
.
mean
(
test_costs
),
np
.
mean
(
test_accs
)
# train data reader
train_data_reader
=
reader
.
DataReader
(
args
.
train_feature_lst
,
args
.
train_label_lst
,
-
1
)
train_data_reader
.
set_transformers
(
ltrans
)
# train
for
pass_id
in
xrange
(
args
.
pass_num
):
for
pass_id
in
xrange
(
args
.
pass_num
):
pass_start_time
=
time
.
time
()
pass_start_time
=
time
.
time
()
words_seen
=
0
for
batch_id
,
batch_data
in
enumerate
(
accuracy
.
reset
(
exe
)
train_data_reader
.
batch_iterator
(
args
.
batch_size
,
batch_id
=
0
args
.
minimum_batch_size
)):
while
True
:
# load_data
# load_data
one_batch
=
data_reader
.
get_one_batch
(
args
.
batch_size
)
(
features
,
labels
,
lod
)
=
batch_data
if
one_batch
==
None
:
feature_t
.
set
(
features
,
place
)
break
feature_t
.
set_lod
([
lod
])
(
bat_feature
,
bat_label
,
lod
)
=
one_batch
label_t
.
set
(
labels
,
place
)
res_feature
.
set
(
bat_feature
,
place
)
label_t
.
set_lod
([
lod
])
res_feature
.
set_lod
([
lod
])
res_label
.
set
(
bat_label
,
place
)
cost
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
res_label
.
set_lod
([
lod
])
feed
=
{
"feature"
:
feature_t
,
"label"
:
label_t
},
batch_id
+=
1
fetch_list
=
[
avg_cost
,
accuracy
],
return_numpy
=
False
)
words_seen
+=
lod
[
-
1
]
if
batch_id
>
0
and
(
batch_id
%
args
.
print_per_batches
==
0
):
loss
,
acc
=
exe
.
run
(
print
(
"
\n
Batch %d, train cost: %f, train acc: %f"
%
fluid
.
default_main_program
(),
(
batch_id
,
lodtensor_to_ndarray
(
cost
)[
0
],
feed
=
{
"feature"
:
res_feature
,
lodtensor_to_ndarray
(
acc
)[
0
]))
"label"
:
res_label
},
else
:
fetch_list
=
[
avg_cost
]
+
accuracy
.
metrics
,
sys
.
stdout
.
write
(
'.'
)
return_numpy
=
False
)
sys
.
stdout
.
flush
()
train_acc
=
accuracy
.
eval
(
exe
)
# run test
print
(
"acc:"
,
lodtensor_to_ndarray
(
loss
))
val_cost
,
val_acc
=
test
(
exe
)
# save model
if
args
.
model_save_dir
!=
''
:
model_path
=
os
.
path
.
join
(
args
.
model_save_dir
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".model"
)
fluid
.
io
.
save_inference_model
(
model_path
,
[
"feature"
],
[
prediction
],
exe
)
# cal pass time
pass_end_time
=
time
.
time
()
pass_end_time
=
time
.
time
()
time_consumed
=
pass_end_time
-
pass_start_time
time_consumed
=
pass_end_time
-
pass_start_time
words_per_sec
=
words_seen
/
time_consumed
# print info at pass end
print
(
"
\n
Pass %d, time consumed: %f s, val cost: %f, val acc: %f
\n
"
%
(
pass_id
,
time_consumed
,
val_cost
,
val_acc
))
def
lodtensor_to_ndarray
(
lod_tensor
):
dims
=
lod_tensor
.
get_dims
()
ret
=
np
.
zeros
(
shape
=
dims
).
astype
(
'float32'
)
for
i
in
xrange
(
np
.
product
(
dims
)):
ret
.
ravel
()[
i
]
=
lod_tensor
.
get_float_element
(
i
)
return
ret
,
lod_tensor
.
lod
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
args
=
parse_args
()
args
=
parse_args
()
print_arguments
(
args
)
print_arguments
(
args
)
if
args
.
infer_only
:
if
args
.
model_save_dir
!=
''
and
not
os
.
path
.
exists
(
args
.
model_save_dir
):
pass
os
.
mkdir
(
args
.
model_save_dir
)
else
:
if
args
.
use_nvprof
and
args
.
device
==
'GPU'
:
train
(
args
)
with
profiler
.
cuda_profiler
(
"cuda_profiler.txt"
,
'csv'
)
as
nvprof
:
train
(
args
)
else
:
train
(
args
)
fluid/adversarial/advbox/attacks/saliency.py
0 → 100644
浏览文件 @
0a83aa46
"""
This module provide the attack method for JSMA's implement.
"""
from
__future__
import
division
import
logging
import
random
import
numpy
as
np
from
.base
import
Attack
class
SaliencyMapAttack
(
Attack
):
"""
Implements the Saliency Map Attack.
The Jacobian-based Saliency Map Approach (Papernot et al. 2016).
Paper link: https://arxiv.org/pdf/1511.07528.pdf
"""
def
_apply
(
self
,
adversary
,
max_iter
=
2000
,
fast
=
True
,
theta
=
0.1
,
max_perturbations_per_pixel
=
7
):
"""
Apply the JSMA attack.
Args:
adversary(Adversary): The Adversary object.
max_iter(int): The max iterations.
fast(bool): Whether evaluate the pixel influence on sum of residual classes.
theta(float): Perturbation per pixel relative to [min, max] range.
max_perturbations_per_pixel(int): The max count of perturbation per pixel.
Return:
adversary: The Adversary object.
"""
assert
adversary
is
not
None
if
not
adversary
.
is_targeted_attack
or
(
adversary
.
target_label
is
None
):
target_labels
=
self
.
_generate_random_target
(
adversary
.
original_label
)
else
:
target_labels
=
[
adversary
.
target_label
]
for
target
in
target_labels
:
original_image
=
adversary
.
original
# the mask defines the search domain
# each modified pixel with border value is set to zero in mask
mask
=
np
.
ones_like
(
original_image
)
# count tracks how often each pixel was changed
counts
=
np
.
zeros_like
(
original_image
)
labels
=
range
(
self
.
model
.
num_classes
())
adv_img
=
original_image
.
copy
()
min_
,
max_
=
self
.
model
.
bounds
()
for
step
in
range
(
max_iter
):
adv_img
=
np
.
clip
(
adv_img
,
min_
,
max_
)
adv_label
=
np
.
argmax
(
self
.
model
.
predict
(
adv_img
))
if
adversary
.
try_accept_the_example
(
adv_img
,
adv_label
):
return
adversary
# stop if mask is all zero
if
not
any
(
mask
.
flatten
()):
return
adversary
logging
.
info
(
'step = {}, original_label = {}, adv_label={}'
.
format
(
step
,
adversary
.
original_label
,
adv_label
))
# get pixel location with highest influence on class
idx
,
p_sign
=
self
.
_saliency_map
(
adv_img
,
target
,
labels
,
mask
,
fast
=
fast
)
# apply perturbation
adv_img
[
idx
]
+=
-
p_sign
*
theta
*
(
max_
-
min_
)
# tracks number of updates for each pixel
counts
[
idx
]
+=
1
# remove pixel from search domain if it hits the bound
if
adv_img
[
idx
]
<=
min_
or
adv_img
[
idx
]
>=
max_
:
mask
[
idx
]
=
0
# remove pixel if it was changed too often
if
counts
[
idx
]
>=
max_perturbations_per_pixel
:
mask
[
idx
]
=
0
adv_img
=
np
.
clip
(
adv_img
,
min_
,
max_
)
def
_generate_random_target
(
self
,
original_label
):
"""
Draw random target labels all of which are different and not the original label.
Args:
original_label(int): Original label.
Return:
target_labels(list): random target labels
"""
num_random_target
=
1
num_classes
=
self
.
model
.
num_classes
()
assert
num_random_target
<=
num_classes
-
1
target_labels
=
random
.
sample
(
range
(
num_classes
),
num_random_target
+
1
)
target_labels
=
[
t
for
t
in
target_labels
if
t
!=
original_label
]
target_labels
=
target_labels
[:
num_random_target
]
return
target_labels
def
_saliency_map
(
self
,
image
,
target
,
labels
,
mask
,
fast
=
False
):
"""
Get pixel location with highest influence on class.
Args:
image(numpy.ndarray): Image with shape (height, width, channels).
target(int): The target label.
labels(int): The number of classes of the output label.
mask(list): Each modified pixel with border value is set to zero in mask.
fast(bool): Whether evaluate the pixel influence on sum of residual classes.
Return:
idx: The index of optimal pixel.
pix_sign: The direction of perturbation
"""
# pixel influence on target class
alphas
=
self
.
model
.
gradient
(
image
,
target
)
*
mask
# pixel influence on sum of residual classes(don't evaluate if fast == True)
if
fast
:
betas
=
-
np
.
ones_like
(
alphas
)
else
:
betas
=
np
.
sum
([
self
.
model
.
gradient
(
image
,
label
)
*
mask
-
alphas
for
label
in
labels
],
0
)
# compute saliency map (take into account both pos. & neg. perturbations)
sal_map
=
np
.
abs
(
alphas
)
*
np
.
abs
(
betas
)
*
np
.
sign
(
alphas
*
betas
)
# find optimal pixel & direction of perturbation
idx
=
np
.
argmin
(
sal_map
)
idx
=
np
.
unravel_index
(
idx
,
mask
.
shape
)
pix_sign
=
np
.
sign
(
alphas
)[
idx
]
return
idx
,
pix_sign
JSMA
=
SaliencyMapAttack
fluid/adversarial/mnist_tutorial_jsma.py
0 → 100644
浏览文件 @
0a83aa46
"""
FGSM demos on mnist using advbox tool.
"""
import
matplotlib.pyplot
as
plt
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
import
numpy
as
np
from
advbox
import
Adversary
from
advbox.attacks.saliency
import
SaliencyMapAttack
from
advbox.models.paddle
import
PaddleModel
def
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
():
"""
Advbox demo which demonstrate how to use advbox.
"""
IMG_NAME
=
'img'
LABEL_NAME
=
'label'
img
=
fluid
.
layers
.
data
(
name
=
IMG_NAME
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
# gradient should flow
img
.
stop_gradient
=
False
label
=
fluid
.
layers
.
data
(
name
=
LABEL_NAME
,
shape
=
[
1
],
dtype
=
'int64'
)
logits
=
cnn_model
(
img
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
logits
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
BATCH_SIZE
=
1
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
500
),
batch_size
=
BATCH_SIZE
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
IMG_NAME
,
LABEL_NAME
],
place
=
place
,
program
=
fluid
.
default_main_program
())
fluid
.
io
.
load_params
(
exe
,
"./mnist/"
,
main_program
=
fluid
.
default_main_program
())
# advbox demo
m
=
PaddleModel
(
fluid
.
default_main_program
(),
IMG_NAME
,
LABEL_NAME
,
logits
.
name
,
avg_cost
.
name
,
(
-
1
,
1
))
attack
=
SaliencyMapAttack
(
m
)
total_num
=
0
success_num
=
0
for
data
in
train_reader
():
total_num
+=
1
# adversary.set_target(True, target_label=target_label)
jsma_attack
=
attack
(
Adversary
(
data
[
0
][
0
],
data
[
0
][
1
]))
if
jsma_attack
is
not
None
and
jsma_attack
.
is_successful
():
# plt.imshow(jsma_attack.target, cmap='Greys_r')
# plt.show()
success_num
+=
1
print
(
'original_label=%d, adversary examples label =%d'
%
(
data
[
0
][
1
],
jsma_attack
.
adversarial_label
))
# np.save('adv_img', jsma_attack.adversarial_example)
print
(
'total num = %d, success num = %d '
%
(
total_num
,
success_num
))
if
total_num
==
100
:
break
if
__name__
==
'__main__'
:
main
()
fluid/image_classification/mobilenet.py
0 → 100644
浏览文件 @
0a83aa46
import
os
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
from
paddle.v2.fluid.initializer
import
MSRA
from
paddle.v2.fluid.param_attr
import
ParamAttr
parameter_attr
=
ParamAttr
(
initializer
=
MSRA
())
def
conv_bn_layer
(
input
,
filter_size
,
num_filters
,
stride
,
padding
,
channels
=
None
,
num_groups
=
1
,
act
=
'relu'
,
use_cudnn
=
True
):
conv
=
fluid
.
layers
.
conv2d
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
padding
,
groups
=
num_groups
,
act
=
None
,
use_cudnn
=
use_cudnn
,
param_attr
=
parameter_attr
,
bias_attr
=
False
)
return
fluid
.
layers
.
batch_norm
(
input
=
conv
,
act
=
act
)
def
depthwise_separable
(
input
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
):
"""
"""
depthwise_conv
=
conv_bn_layer
(
input
=
input
,
filter_size
=
3
,
num_filters
=
int
(
num_filters1
*
scale
),
stride
=
stride
,
padding
=
1
,
num_groups
=
int
(
num_groups
*
scale
),
use_cudnn
=
False
)
pointwise_conv
=
conv_bn_layer
(
input
=
depthwise_conv
,
filter_size
=
1
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
1
,
padding
=
0
)
return
pointwise_conv
def
mobile_net
(
img
,
class_dim
,
scale
=
1.0
):
# conv1: 112x112
tmp
=
conv_bn_layer
(
img
,
filter_size
=
3
,
channels
=
3
,
num_filters
=
int
(
32
*
scale
),
stride
=
2
,
padding
=
1
)
# 56x56
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
32
,
num_filters2
=
64
,
num_groups
=
32
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
64
,
num_filters2
=
128
,
num_groups
=
64
,
stride
=
2
,
scale
=
scale
)
# 28x28
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
128
,
num_filters2
=
128
,
num_groups
=
128
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
128
,
num_filters2
=
256
,
num_groups
=
128
,
stride
=
2
,
scale
=
scale
)
# 14x14
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
256
,
num_filters2
=
256
,
num_groups
=
256
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
256
,
num_filters2
=
512
,
num_groups
=
256
,
stride
=
2
,
scale
=
scale
)
# 14x14
for
i
in
range
(
5
):
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
512
,
num_filters2
=
512
,
num_groups
=
512
,
stride
=
1
,
scale
=
scale
)
# 7x7
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
512
,
num_filters2
=
1024
,
num_groups
=
512
,
stride
=
2
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
1024
,
num_filters2
=
1024
,
num_groups
=
1024
,
stride
=
1
,
scale
=
scale
)
tmp
=
fluid
.
layers
.
pool2d
(
input
=
tmp
,
pool_size
=
0
,
pool_stride
=
1
,
pool_type
=
'avg'
,
global_pooling
=
True
)
tmp
=
fluid
.
layers
.
fc
(
input
=
tmp
,
size
=
class_dim
,
act
=
'softmax'
,
param_attr
=
parameter_attr
)
return
tmp
def
train
(
learning_rate
,
batch_size
,
num_passes
,
model_save_dir
=
'model'
):
class_dim
=
102
image_shape
=
[
3
,
224
,
224
]
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
out
=
mobile_net
(
image
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
learning_rate
,
momentum
=
0.9
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
5
*
1e-5
))
opts
=
optimizer
.
minimize
(
avg_cost
)
accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
out
,
label
=
label
)
inference_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
inference_program
):
test_accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
out
,
label
=
label
)
test_target
=
[
avg_cost
]
+
test_accuracy
.
metrics
+
test_accuracy
.
states
inference_program
=
fluid
.
io
.
get_inference_program
(
test_target
)
place
=
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
test
(),
batch_size
=
batch_size
)
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
for
pass_id
in
range
(
num_passes
):
accuracy
.
reset
(
exe
)
for
batch_id
,
data
in
enumerate
(
train_reader
()):
loss
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
accuracy
.
metrics
)
print
(
"Pass {0}, batch {1}, loss {2}, acc {3}"
.
format
(
pass_id
,
batch_id
,
loss
[
0
],
acc
[
0
]))
pass_acc
=
accuracy
.
eval
(
exe
)
test_accuracy
.
reset
(
exe
)
for
data
in
test_reader
():
loss
,
acc
=
exe
.
run
(
inference_program
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
test_accuracy
.
metrics
)
test_pass_acc
=
test_accuracy
.
eval
(
exe
)
print
(
"End pass {0}, train_acc {1}, test_acc {2}"
.
format
(
pass_id
,
pass_acc
,
test_pass_acc
))
if
pass_id
%
10
==
0
:
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
print
'save models to %s'
%
(
model_path
)
fluid
.
io
.
save_inference_model
(
model_path
,
[
'image'
],
[
out
],
exe
)
if
__name__
==
'__main__'
:
train
(
learning_rate
=
0.005
,
batch_size
=
40
,
num_passes
=
300
)
fluid/image_classification/se_resnext.py
浏览文件 @
0a83aa46
...
@@ -103,66 +103,87 @@ def train(learning_rate,
...
@@ -103,66 +103,87 @@ def train(learning_rate,
batch_size
,
batch_size
,
num_passes
,
num_passes
,
init_model
=
None
,
init_model
=
None
,
model_save_dir
=
'model'
):
model_save_dir
=
'model'
,
parallel
=
True
):
class_dim
=
1000
class_dim
=
1000
image_shape
=
[
3
,
224
,
224
]
image_shape
=
[
3
,
224
,
224
]
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
)
if
parallel
:
places
=
fluid
.
layers
.
get_places
()
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
pd
=
fluid
.
layers
.
ParallelDo
(
places
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
with
pd
.
do
():
image_
=
pd
.
read_input
(
image
)
label_
=
pd
.
read_input
(
label
)
out
=
SE_ResNeXt
(
input
=
image_
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label_
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label_
)
pd
.
write_output
(
avg_cost
)
pd
.
write_output
(
accuracy
)
avg_cost
,
accuracy
=
pd
()
avg_cost
=
fluid
.
layers
.
mean
(
x
=
avg_cost
)
accuracy
=
fluid
.
layers
.
mean
(
x
=
accuracy
)
else
:
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
learning_rate
,
learning_rate
=
learning_rate
,
momentum
=
0.9
,
momentum
=
0.9
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
1e-4
))
regularization
=
fluid
.
regularizer
.
L2Decay
(
1e-4
))
opts
=
optimizer
.
minimize
(
avg_cost
)
opts
=
optimizer
.
minimize
(
avg_cost
)
accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
out
,
label
=
label
)
inference_program
=
fluid
.
default_main_program
().
clone
()
inference_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
inference_program
):
with
fluid
.
program_guard
(
inference_program
):
test_accuracy
=
fluid
.
evaluator
.
Accuracy
(
input
=
out
,
label
=
label
)
inference_program
=
fluid
.
io
.
get_inference_program
([
avg_cost
,
accuracy
])
test_target
=
[
avg_cost
]
+
test_accuracy
.
metrics
+
test_accuracy
.
states
inference_program
=
fluid
.
io
.
get_inference_program
(
test_target
)
place
=
fluid
.
CUDAPlace
(
0
)
place
=
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
exe
.
run
(
fluid
.
default_startup_program
())
if
init_model
is
not
None
:
if
init_model
is
not
None
:
fluid
.
io
.
load_persistables
_if_exist
(
exe
,
init_model
)
fluid
.
io
.
load_persistables
(
exe
,
init_model
)
train_reader
=
paddle
.
batch
(
reader
.
train
(),
batch_size
=
batch_size
)
train_reader
=
paddle
.
batch
(
reader
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
reader
.
test
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
reader
.
test
(),
batch_size
=
batch_size
)
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
for
pass_id
in
range
(
num_passes
):
for
pass_id
in
range
(
num_passes
):
accuracy
.
reset
(
exe
)
for
batch_id
,
data
in
enumerate
(
train_reader
()):
for
batch_id
,
data
in
enumerate
(
train_reader
()):
loss
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
loss
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
accuracy
.
metrics
)
fetch_list
=
[
avg_cost
])
print
(
"Pass {0}, batch {1}, loss {2}, acc {3}"
.
format
(
print
(
"Pass {0}, batch {1}, loss {2}"
.
format
(
pass_id
,
batch_id
,
pass_id
,
batch_id
,
loss
[
0
],
acc
[
0
]))
float
(
loss
[
0
])))
pass_acc
=
accuracy
.
eval
(
exe
)
total_loss
=
0.0
test_accuracy
.
reset
(
exe
)
total_acc
=
0.0
total_batch
=
0
for
data
in
test_reader
():
for
data
in
test_reader
():
loss
,
acc
=
exe
.
run
(
inference_program
,
loss
,
acc
=
exe
.
run
(
inference_program
,
feed
=
feeder
.
feed
(
data
),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
test_accuracy
.
metrics
)
fetch_list
=
[
avg_cost
,
accuracy
])
test_pass_acc
=
test_accuracy
.
eval
(
exe
)
total_loss
+=
float
(
loss
)
print
(
"End pass {0}, train_acc {1}, test_acc {2}"
.
format
(
total_acc
+=
float
(
acc
)
pass_id
,
pass_acc
,
test_pass_acc
))
total_batch
+=
1
print
(
"End pass {0}, test_loss {1}, test_acc {2}"
.
format
(
pass_id
,
total_loss
/
total_batch
,
total_acc
/
total_batch
))
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
if
not
os
.
path
.
isdir
(
model_path
):
fluid
.
io
.
save_inference_model
(
model_path
,
[
'image'
],
[
out
],
exe
)
os
.
makedirs
(
model_path
)
fluid
.
io
.
save_persistables
(
exe
,
model_path
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
train
(
learning_rate
=
0.1
,
batch_size
=
8
,
num_passes
=
100
,
init_model
=
None
)
train
(
learning_rate
=
0.1
,
batch_size
=
8
,
num_passes
=
100
,
init_model
=
None
,
parallel
=
False
)
fluid/ocr_recognition/ctc_reader.py
0 → 100644
浏览文件 @
0a83aa46
import
os
import
cv2
import
numpy
as
np
from
PIL
import
Image
from
paddle.v2.image
import
load_image
class
DataGenerator
(
object
):
def
__init__
(
self
):
pass
def
train_reader
(
self
,
img_root_dir
,
img_label_list
,
batchsize
):
'''
Reader interface for training.
:param img_root_dir: The root path of the image for training.
:type file_list: str
:param img_label_list: The path of the <image_name, label> file for training.
:type file_list: str
'''
img_label_lines
=
[]
if
batchsize
==
1
:
to_file
=
"tmp.txt"
cmd
=
"cat "
+
img_label_list
+
" | awk '{print $1,$2,$3,$4;}' | shuf > "
+
to_file
print
"cmd: "
+
cmd
os
.
system
(
cmd
)
print
"finish batch shuffle"
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
else
:
to_file
=
"tmp.txt"
#cmd1: partial shuffle
cmd
=
"cat "
+
img_label_list
+
" | awk '{printf(
\"
%04d%.4f %s
\\
n
\"
, $1, rand(), $0)}' | sort | sed 1,$((1 + RANDOM % 100))d | "
#cmd2: batch merge and shuffle
cmd
+=
"awk '{printf $2
\"
\"
$3
\"
\"
$4
\"
\"
$5
\"
\"
; if(NR % "
+
str
(
batchsize
)
+
" == 0) print
\"\"
;}' | shuf | "
#cmd3: batch split
cmd
+=
"awk '{if(NF == "
+
str
(
batchsize
)
+
" * 4) {for(i = 0; i < "
+
str
(
batchsize
)
+
"; i++) print $(4*i+1)
\"
\"
$(4*i+2)
\"
\"
$(4*i+3)
\"
\"
$(4*i+4);}}' > "
+
to_file
print
"cmd: "
+
cmd
os
.
system
(
cmd
)
print
"finish batch shuffle"
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
def
reader
():
sizes
=
len
(
img_label_lines
)
/
batchsize
for
i
in
range
(
sizes
):
result
=
[]
sz
=
[
0
,
0
]
for
j
in
range
(
batchsize
):
line
=
img_label_lines
[
i
*
batchsize
+
j
]
# h, w, img_name, labels
items
=
line
.
split
(
' '
)
label
=
[
int
(
c
)
for
c
in
items
[
-
1
].
split
(
','
)]
img
=
Image
.
open
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
])).
convert
(
'L'
)
#zhuanhuidu
if
j
==
0
:
sz
=
img
.
size
img
=
img
.
resize
((
sz
[
0
],
sz
[
1
]))
img
=
np
.
array
(
img
)
-
127.5
img
=
img
[
np
.
newaxis
,
...]
result
.
append
([
img
,
label
])
yield
result
return
reader
def
test_reader
(
self
,
img_root_dir
,
img_label_list
):
'''
Reader interface for inference.
:param img_root_dir: The root path of the images for training.
:type file_list: str
:param img_label_list: The path of the <image_name, label> file for testing.
:type file_list: list
'''
def
reader
():
for
line
in
open
(
img_label_list
):
# h, w, img_name, labels
items
=
line
.
split
(
' '
)
label
=
[
int
(
c
)
for
c
in
items
[
-
1
].
split
(
','
)]
img
=
Image
.
open
(
os
.
path
.
join
(
img_root_dir
,
items
[
2
])).
convert
(
'L'
)
img
=
np
.
array
(
img
)
-
127.5
img
=
img
[
np
.
newaxis
,
...]
yield
img
,
label
return
reader
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录