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,
...
@@ -26,7 +26,12 @@ def conv_bn_pool(input,
bias_attr
=
bias
,
bias_attr
=
bias
,
is_test
=
is_test
)
is_test
=
is_test
)
tmp
=
fluid
.
layers
.
pool2d
(
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
return
tmp
...
@@ -136,26 +141,61 @@ def encoder_net(images,
...
@@ -136,26 +141,61 @@ def encoder_net(images,
def
ctc_train_net
(
images
,
label
,
args
,
num_classes
):
def
ctc_train_net
(
images
,
label
,
args
,
num_classes
):
regularizer
=
fluid
.
regularizer
.
L2Decay
(
args
.
l2
)
regularizer
=
fluid
.
regularizer
.
L2Decay
(
args
.
l2
)
gradient_clip
=
None
gradient_clip
=
None
fc_out
=
encoder_net
(
if
args
.
parallel
:
images
,
places
=
fluid
.
layers
.
get_places
()
num_classes
,
pd
=
fluid
.
layers
.
ParallelDo
(
places
)
regularizer
=
regularizer
,
with
pd
.
do
():
gradient_clip
=
gradient_clip
)
images_
=
pd
.
read_input
(
images
)
label_
=
pd
.
read_input
(
label
)
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
)
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
)
cost
=
fluid
.
layers
.
warpctc
(
casted_label
=
fluid
.
layers
.
cast
(
x
=
label
,
dtype
=
'int64'
)
input
=
fc_out
,
label
=
label
,
blank
=
num_classes
,
norm_by_times
=
True
)
error_evaluator
=
fluid
.
evaluator
.
EditDistance
(
sum_cost
=
fluid
.
layers
.
reduce_sum
(
cost
)
input
=
decoded_out
,
label
=
casted_label
)
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
(
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
args
.
learning_rate
,
momentum
=
args
.
momentum
)
learning_rate
=
args
.
learning_rate
,
momentum
=
args
.
momentum
)
optimizer
.
minimize
(
sum_cost
)
_
,
params_grads
=
optimizer
.
minimize
(
sum_cost
)
decoded_out
=
fluid
.
layers
.
ctc_greedy_decoder
(
return
sum_cost
,
error_evaluator
,
inference_program
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
def
ctc_infer
(
images
,
num_classes
):
def
ctc_infer
(
images
,
num_classes
):
...
...
fluid/ocr_recognition/ctc_train.py
浏览文件 @
29992c10
"""Trainer for OCR CTC model."""
"""Trainer for OCR CTC model."""
import
paddle.v2
as
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
dummy_reader
import
dummy_reader
import
ctc_reader
import
ctc_reader
...
@@ -24,12 +23,12 @@ add_arg('momentum', float, 0.9, "Momentum.")
...
@@ -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
(
'rnn_hidden_size'
,
int
,
200
,
"Hidden size of rnn layers."
)
add_arg
(
'device'
,
int
,
0
,
"Device id.'-1' means running on CPU"
add_arg
(
'device'
,
int
,
0
,
"Device id.'-1' means running on CPU"
"while '0' means GPU-0."
)
"while '0' means GPU-0."
)
add_arg
(
'parallel'
,
bool
,
True
,
"Whether use parallel training."
)
# yapf: disable
# yapf: disable
def
load_parameter
(
place
):
def
load_parameter
(
place
):
params
=
load_param
(
'./name.map'
,
'./data/model/results_without_avg_window/pass-00000/'
)
params
=
load_param
(
'./name.map'
,
'./data/model/results_without_avg_window/pass-00000/'
)
for
name
in
params
:
for
name
in
params
:
# print "param: %s" % name
t
=
fluid
.
global_scope
().
find_var
(
name
).
get_tensor
()
t
=
fluid
.
global_scope
().
find_var
(
name
).
get_tensor
()
t
.
set
(
params
[
name
],
place
)
t
.
set
(
params
[
name
],
place
)
...
@@ -41,7 +40,8 @@ def train(args, data_reader=dummy_reader):
...
@@ -41,7 +40,8 @@ def train(args, data_reader=dummy_reader):
# define network
# define network
images
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
data_shape
,
dtype
=
'float32'
)
images
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
data_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int32'
,
lod_level
=
1
)
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
# data reader
train_reader
=
data_reader
.
train
(
args
.
batch_size
)
train_reader
=
data_reader
.
train
(
args
.
batch_size
)
test_reader
=
data_reader
.
test
()
test_reader
=
data_reader
.
test
()
...
@@ -51,11 +51,8 @@ def train(args, data_reader=dummy_reader):
...
@@ -51,11 +51,8 @@ def train(args, data_reader=dummy_reader):
place
=
fluid
.
CUDAPlace
(
args
.
device
)
place
=
fluid
.
CUDAPlace
(
args
.
device
)
exe
=
fluid
.
Executor
(
place
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
exe
.
run
(
fluid
.
default_startup_program
())
#load_parameter(place)
#load_parameter(place)
inference_program
=
fluid
.
io
.
get_inference_program
(
error_evaluator
)
for
pass_id
in
range
(
args
.
pass_num
):
for
pass_id
in
range
(
args
.
pass_num
):
error_evaluator
.
reset
(
exe
)
error_evaluator
.
reset
(
exe
)
batch_id
=
1
batch_id
=
1
...
@@ -78,7 +75,6 @@ def train(args, data_reader=dummy_reader):
...
@@ -78,7 +75,6 @@ def train(args, data_reader=dummy_reader):
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
batch_id
+=
1
batch_id
+=
1
# evaluate model on test data
error_evaluator
.
reset
(
exe
)
error_evaluator
.
reset
(
exe
)
for
data
in
test_reader
():
for
data
in
test_reader
():
exe
.
run
(
inference_program
,
feed
=
get_feeder_data
(
data
,
place
))
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录