Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
29992c10
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看板
提交
29992c10
编写于
3月 20, 2018
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add parallel training option.
上级
c7a1c889
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
59 addition
and
23 deletion
+59
-23
fluid/ocr_recognition/crnn_ctc_model.py
fluid/ocr_recognition/crnn_ctc_model.py
+56
-16
fluid/ocr_recognition/ctc_train.py
fluid/ocr_recognition/ctc_train.py
+3
-7
未找到文件。
fluid/ocr_recognition/crnn_ctc_model.py
浏览文件 @
29992c10
...
...
@@ -26,7 +26,12 @@ def conv_bn_pool(input,
bias_attr
=
bias
,
is_test
=
is_test
)
tmp
=
fluid
.
layers
.
pool2d
(
input
=
tmp
,
pool_size
=
2
,
pool_type
=
'max'
,
pool_stride
=
2
,
use_cudnn
=
True
)
input
=
tmp
,
pool_size
=
2
,
pool_type
=
'max'
,
pool_stride
=
2
,
use_cudnn
=
True
,
ceil_mode
=
True
)
return
tmp
...
...
@@ -136,26 +141,61 @@ def encoder_net(images,
def
ctc_train_net
(
images
,
label
,
args
,
num_classes
):
regularizer
=
fluid
.
regularizer
.
L2Decay
(
args
.
l2
)
gradient_clip
=
None
if
args
.
parallel
:
places
=
fluid
.
layers
.
get_places
()
pd
=
fluid
.
layers
.
ParallelDo
(
places
)
with
pd
.
do
():
images_
=
pd
.
read_input
(
images
)
label_
=
pd
.
read_input
(
label
)
fc_out
=
encoder_net
(
images
,
images_
,
num_classes
,
regularizer
=
regularizer
,
gradient_clip
=
gradient_clip
)
cost
=
fluid
.
layers
.
warpctc
(
input
=
fc_out
,
label
=
label
,
blank
=
num_classes
,
norm_by_times
=
True
)
input
=
fc_out
,
label
=
label_
,
blank
=
num_classes
,
norm_by_times
=
True
)
sum_cost
=
fluid
.
layers
.
reduce_sum
(
cost
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
args
.
learning_rate
,
momentum
=
args
.
momentum
)
optimizer
.
minimize
(
sum_cost
)
decoded_out
=
fluid
.
layers
.
ctc_greedy_decoder
(
input
=
fc_out
,
blank
=
num_classes
)
pd
.
write_output
(
sum_cost
)
pd
.
write_output
(
decoded_out
)
sum_cost
,
decoded_out
=
pd
()
sum_cost
=
fluid
.
layers
.
reduce_sum
(
sum_cost
)
else
:
fc_out
=
encoder_net
(
images
,
num_classes
,
regularizer
=
regularizer
,
gradient_clip
=
gradient_clip
)
cost
=
fluid
.
layers
.
warpctc
(
input
=
fc_out
,
label
=
label
,
blank
=
num_classes
,
norm_by_times
=
True
)
sum_cost
=
fluid
.
layers
.
reduce_sum
(
cost
)
decoded_out
=
fluid
.
layers
.
ctc_greedy_decoder
(
input
=
fc_out
,
blank
=
num_classes
)
casted_label
=
fluid
.
layers
.
cast
(
x
=
label
,
dtype
=
'int64'
)
error_evaluator
=
fluid
.
evaluator
.
EditDistance
(
input
=
decoded_out
,
label
=
casted_label
)
return
sum_cost
,
error_evaluator
inference_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
inference_program
):
inference_program
=
fluid
.
io
.
get_inference_program
(
error_evaluator
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
args
.
learning_rate
,
momentum
=
args
.
momentum
)
_
,
params_grads
=
optimizer
.
minimize
(
sum_cost
)
return
sum_cost
,
error_evaluator
,
inference_program
def
ctc_infer
(
images
,
num_classes
):
...
...
fluid/ocr_recognition/ctc_train.py
浏览文件 @
29992c10
"""Trainer for OCR CTC model."""
import
paddle.v2
as
paddle
import
paddle.fluid
as
fluid
import
dummy_reader
import
ctc_reader
...
...
@@ -24,12 +23,12 @@ add_arg('momentum', float, 0.9, "Momentum.")
add_arg
(
'rnn_hidden_size'
,
int
,
200
,
"Hidden size of rnn layers."
)
add_arg
(
'device'
,
int
,
0
,
"Device id.'-1' means running on CPU"
"while '0' means GPU-0."
)
add_arg
(
'parallel'
,
bool
,
True
,
"Whether use parallel training."
)
# yapf: disable
def
load_parameter
(
place
):
params
=
load_param
(
'./name.map'
,
'./data/model/results_without_avg_window/pass-00000/'
)
for
name
in
params
:
# print "param: %s" % name
t
=
fluid
.
global_scope
().
find_var
(
name
).
get_tensor
()
t
.
set
(
params
[
name
],
place
)
...
...
@@ -41,7 +40,8 @@ def train(args, data_reader=dummy_reader):
# define network
images
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
data_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int32'
,
lod_level
=
1
)
sum_cost
,
error_evaluator
=
ctc_train_net
(
images
,
label
,
args
,
num_classes
)
sum_cost
,
error_evaluator
,
inference_program
=
ctc_train_net
(
images
,
label
,
args
,
num_classes
)
# data reader
train_reader
=
data_reader
.
train
(
args
.
batch_size
)
test_reader
=
data_reader
.
test
()
...
...
@@ -51,11 +51,8 @@ def train(args, data_reader=dummy_reader):
place
=
fluid
.
CUDAPlace
(
args
.
device
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
#load_parameter(place)
inference_program
=
fluid
.
io
.
get_inference_program
(
error_evaluator
)
for
pass_id
in
range
(
args
.
pass_num
):
error_evaluator
.
reset
(
exe
)
batch_id
=
1
...
...
@@ -78,7 +75,6 @@ def train(args, data_reader=dummy_reader):
sys
.
stdout
.
flush
()
batch_id
+=
1
# evaluate model on test data
error_evaluator
.
reset
(
exe
)
for
data
in
test_reader
():
exe
.
run
(
inference_program
,
feed
=
get_feeder_data
(
data
,
place
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录