Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
2a5cb2ec
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2a5cb2ec
编写于
6月 04, 2018
作者:
Y
Yancey
提交者:
GitHub
6月 04, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11066 from Yancey1989/dist_recordio
support recordio in dist train
上级
a2103008
97b2f6f5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
165 addition
and
2 deletion
+165
-2
doc/fluid/howto/cluster/fluid_recordio.md
doc/fluid/howto/cluster/fluid_recordio.md
+127
-0
python/paddle/fluid/recordio_writer.py
python/paddle/fluid/recordio_writer.py
+37
-2
tools/codestyle/.gitignore
tools/codestyle/.gitignore
+1
-0
未找到文件。
doc/fluid/howto/cluster/fluid_recordio.md
0 → 100644
浏览文件 @
2a5cb2ec
# How to use RecordIO in Fluid
If you want to use RecordIO as your training data format, you need to convert to your training data
to RecordIO files and reading them in the process of training, PaddlePaddle Fluid provides some
interface to deal with the RecordIO files.
## Generate RecordIO File
Before start training with RecordIO files, you need to convert your training data
to RecordIO format by
`fluid.recordio_writer.convert_reader_to_recordio_file`
, the sample codes
as follows:
```
python
reader
=
paddle
.
batch
(
mnist
.
train
(),
batch_size
=
1
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
# order is image and label
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
[
784
]),
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
),
],
place
=
fluid
.
CPUPlace
())
fluid
.
recordio_writer
.
convert_reader_to_recordio_file
(
'./mnist.recordio'
,
reader
,
feeder
)
```
The above code snippet would generate a RecordIO
`./mnist.recordio`
on your host.
**NOTE**
: we recommend users to set
`batch_size=1`
when generating the recordio files so that users can
adjust it flexibly while reading it.
## Use the RecordIO file in a Local Training Job
PaddlePaddle Fluid provides an interface
`fluid.layers.io.open_recordio_file`
to load your RecordIO file
and then you can use them as a Layer in your network configuration, the sample codes as follows:
```
python
data_file
=
fluid
.
layers
.
io
.
open_recordio_file
(
filename
=
"./mnist.recordio"
,
shapes
=
[(
-
1
,
784
),(
-
1
,
1
)],
lod_levels
=
[
0
,
0
],
dtypes
=
[
"float32"
,
"int32"
])
data_file
=
fluid
.
layers
.
io
.
batch
(
data_file
,
batch_size
=
4
)
img
,
label
=
fluid
.
layers
.
io
.
read_file
(
data_file
)
hidden
=
fluid
.
layers
.
fc
(
input
=
img
,
size
=
100
,
act
=
'tanh'
)
prediction
=
fluid
.
layers
.
fc
(
input
=
hidden
,
size
=
10
,
act
=
'softmax'
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
fluid
.
optimizer
.
Adam
(
learning_rate
=
1e-3
).
minimize
(
avg_loss
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
avg_loss_np
=
[]
# train a pass
batch_id
=
0
while
True
:
tmp
,
=
exe
.
run
(
fetch_list
=
[
avg_loss
])
avg_loss_np
.
append
(
tmp
)
print
(
batch_id
)
batch_id
+=
1
```
## Use the RecordIO files in Distributed Training
1.
generate multiple RecordIO files
For a distributed training job, you may have multiple trainer nodes,
and one or more RecordIO files for one trainer node, you can use the interface
`fluid.recordio_writer.convert_reader_to_recordio_files`
to convert your training data
into multiple RecordIO files, the sample codes as follows:
```
python
reader
=
paddle
.
batch
(
mnist
.
train
(),
batch_size
=
1
)
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
# order is image and label
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
[
784
]),
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
),
],
place
=
fluid
.
CPUPlace
())
fluid
.
recordio_writer
.
convert_reader_to_recordio_files
(
filename_suffix
=
'./mnist.recordio'
,
batch_per_file
=
100
,
reader
,
feeder
)
```
The above codes would generate multiple RecordIO files on your host like:
```
bash
.
\_
mnist-00000.recordio
|-mnist-00001.recordio
|-mnist-00002.recordio
|-mnist-00003.recordio
|-mnist-00004.recordio
```
2.
open multiple RecordIO files by
`fluid.layers.io.open_files`
For a distributed training job, the distributed operator system will schedule trainer process on multiple nodes,
each trainer process reads parts of the whole training data, we usually take the following approach to make the training
data allocated by each trainer process as uniform as possiable:
```
python
def
gen_train_list
(
file_pattern
,
trainers
,
trainer_id
):
file_list
=
glob
.
glob
(
file_pattern
)
ret_list
=
[]
for
idx
,
f
in
enumerate
(
file_list
):
if
(
idx
+
trainers
)
%
trainers
==
trainer_id
:
ret_list
.
append
(
f
)
return
ret_list
trainers
=
int
(
os
.
getenv
(
"TRAINERS"
))
trainer_id
=
int
(
os
.
getenv
(
"PADDLE_INIT_TRAINER_ID"
))
data_file
=
fluid
.
layers
.
io
.
open_files
(
filenames
=
gen_train_list
(
"./mnist-[0-9]*.recordio"
,
2
,
0
),
thread_num
=
1
,
shapes
=
[(
-
1
,
784
),(
-
1
,
1
)],
lod_levels
=
[
0
,
0
],
dtypes
=
[
"float32"
,
"int32"
])
img
,
label
=
fluid
.
layers
.
io
.
read_file
(
data_files
)
...
```
python/paddle/fluid/recordio_writer.py
浏览文件 @
2a5cb2ec
...
...
@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
core
import
contextlib
__all__
=
[
'convert_reader_to_recordio_file'
]
__all__
=
[
'convert_reader_to_recordio_file'
,
'convert_reader_to_recordio_files'
]
@
contextlib
.
contextmanager
...
...
@@ -46,3 +48,36 @@ def convert_reader_to_recordio_file(
writer
.
complete_append_tensor
()
counter
+=
1
return
counter
def
convert_reader_to_recordio_files
(
filename
,
batch_per_file
,
reader_creator
,
feeder
,
compressor
=
core
.
RecordIOWriter
.
Compressor
.
Snappy
,
max_num_records
=
1000
,
feed_order
=
None
):
if
feed_order
is
None
:
feed_order
=
feeder
.
feed_names
f_name
,
f_ext
=
os
.
path
.
splitext
(
filename
)
assert
(
f_ext
==
".recordio"
)
lines
=
[]
f_idx
=
0
counter
=
0
for
idx
,
batch
in
enumerate
(
reader_creator
()):
lines
.
append
(
batch
)
if
idx
>=
batch_per_file
and
idx
%
batch_per_file
==
0
:
filename
=
"%s-%05d%s"
%
(
f_name
,
f_idx
,
f_ext
)
with
create_recordio_writer
(
filename
,
compressor
,
max_num_records
)
as
writer
:
for
l
in
lines
:
res
=
feeder
.
feed
(
l
)
for
each
in
feed_order
:
writer
.
append_tensor
(
res
[
each
])
writer
.
complete_append_tensor
()
counter
+=
1
lines
=
[]
f_idx
+=
1
return
counter
tools/codestyle/.gitignore
0 → 100644
浏览文件 @
2a5cb2ec
*.pyc
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录