Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
aa0ab8c9
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
aa0ab8c9
编写于
8月 21, 2018
作者:
W
whs
提交者:
GitHub
8月 21, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make ocr and icnet support for python3 (#1178)
* Make ocr and icnet support for python3 * Restore default arguments.
上级
beb4dfbe
变更
13
显示空白变更内容
内联
并排
Showing
13 changed file
with
89 addition
and
65 deletion
+89
-65
fluid/icnet/cityscape.py
fluid/icnet/cityscape.py
+5
-2
fluid/icnet/eval.py
fluid/icnet/eval.py
+3
-4
fluid/icnet/icnet.py
fluid/icnet/icnet.py
+21
-18
fluid/icnet/infer.py
fluid/icnet/infer.py
+2
-1
fluid/icnet/train.py
fluid/icnet/train.py
+9
-6
fluid/icnet/utils.py
fluid/icnet/utils.py
+2
-1
fluid/ocr_recognition/attention_model.py
fluid/ocr_recognition/attention_model.py
+3
-0
fluid/ocr_recognition/crnn_ctc_model.py
fluid/ocr_recognition/crnn_ctc_model.py
+6
-2
fluid/ocr_recognition/data_reader.py
fluid/ocr_recognition/data_reader.py
+5
-2
fluid/ocr_recognition/eval.py
fluid/ocr_recognition/eval.py
+3
-4
fluid/ocr_recognition/infer.py
fluid/ocr_recognition/infer.py
+7
-6
fluid/ocr_recognition/train.py
fluid/ocr_recognition/train.py
+15
-12
fluid/ocr_recognition/utility.py
fluid/ocr_recognition/utility.py
+8
-7
未找到文件。
fluid/icnet/cityscape.py
浏览文件 @
aa0ab8c9
"""Reader for Cityscape dataset.
"""Reader for Cityscape dataset.
"""
"""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
os
import
cv2
import
cv2
import
numpy
as
np
import
numpy
as
np
...
@@ -173,8 +176,8 @@ class DataGenerater:
...
@@ -173,8 +176,8 @@ class DataGenerater:
"""
"""
Scale label according to factor.
Scale label according to factor.
"""
"""
h
=
label
.
shape
[
0
]
/
factor
h
=
label
.
shape
[
0
]
/
/
factor
w
=
label
.
shape
[
1
]
/
factor
w
=
label
.
shape
[
1
]
/
/
factor
return
cv2
.
resize
(
return
cv2
.
resize
(
label
,
(
h
,
w
),
interpolation
=
cv2
.
INTER_NEAREST
)[:,
:,
np
.
newaxis
]
label
,
(
h
,
w
),
interpolation
=
cv2
.
INTER_NEAREST
)[:,
:,
np
.
newaxis
]
...
...
fluid/icnet/eval.py
浏览文件 @
aa0ab8c9
...
@@ -64,7 +64,7 @@ def eval(args):
...
@@ -64,7 +64,7 @@ def eval(args):
exe
.
run
(
fluid
.
default_startup_program
())
exe
.
run
(
fluid
.
default_startup_program
())
assert
os
.
path
.
exists
(
args
.
model_path
)
assert
os
.
path
.
exists
(
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
args
.
model_path
)
print
"loaded model from: %s"
%
args
.
model_path
print
(
"loaded model from: %s"
%
args
.
model_path
)
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
fetch_vars
=
[
iou
,
out_w
,
out_r
]
fetch_vars
=
[
iou
,
out_w
,
out_r
]
...
@@ -80,11 +80,10 @@ def eval(args):
...
@@ -80,11 +80,10 @@ def eval(args):
fetch_list
=
fetch_vars
)
fetch_list
=
fetch_vars
)
out_wrong
+=
result
[
1
]
out_wrong
+=
result
[
1
]
out_right
+=
result
[
2
]
out_right
+=
result
[
2
]
print
"count: %s; current iou: %.3f;
\r
"
%
(
count
,
result
[
0
]),
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
iou
=
cal_mean_iou
(
out_wrong
,
out_right
)
iou
=
cal_mean_iou
(
out_wrong
,
out_right
)
print
"
\n
mean iou: %.3f"
%
iou
print
(
"
\n
mean iou: %.3f"
%
iou
)
print
"kpis test_acc %f"
%
iou
print
(
"kpis test_acc %f"
%
iou
)
def
main
():
def
main
():
...
...
fluid/icnet/icnet.py
浏览文件 @
aa0ab8c9
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
numpy
as
np
import
numpy
as
np
import
sys
import
sys
...
@@ -20,8 +23,8 @@ def conv(input,
...
@@ -20,8 +23,8 @@ def conv(input,
if
padding
==
"SAME"
:
if
padding
==
"SAME"
:
padding_h
=
max
(
k_h
-
s_h
,
0
)
padding_h
=
max
(
k_h
-
s_h
,
0
)
padding_w
=
max
(
k_w
-
s_w
,
0
)
padding_w
=
max
(
k_w
-
s_w
,
0
)
padding_top
=
padding_h
/
2
padding_top
=
padding_h
/
/
2
padding_left
=
padding_w
/
2
padding_left
=
padding_w
/
/
2
padding_bottom
=
padding_h
-
padding_top
padding_bottom
=
padding_h
-
padding_top
padding_right
=
padding_w
-
padding_left
padding_right
=
padding_w
-
padding_left
padding
=
[
padding
=
[
...
@@ -57,8 +60,8 @@ def atrous_conv(input,
...
@@ -57,8 +60,8 @@ def atrous_conv(input,
if
padding
==
"SAME"
:
if
padding
==
"SAME"
:
padding_h
=
max
(
k_h
-
s_h
,
0
)
padding_h
=
max
(
k_h
-
s_h
,
0
)
padding_w
=
max
(
k_w
-
s_w
,
0
)
padding_w
=
max
(
k_w
-
s_w
,
0
)
padding_top
=
padding_h
/
2
padding_top
=
padding_h
/
/
2
padding_left
=
padding_w
/
2
padding_left
=
padding_w
/
/
2
padding_bottom
=
padding_h
-
padding_top
padding_bottom
=
padding_h
-
padding_top
padding_right
=
padding_w
-
padding_left
padding_right
=
padding_w
-
padding_left
padding
=
[
padding
=
[
...
@@ -141,15 +144,15 @@ def dilation_convs(input):
...
@@ -141,15 +144,15 @@ def dilation_convs(input):
def
pyramis_pooling
(
input
,
input_shape
):
def
pyramis_pooling
(
input
,
input_shape
):
shape
=
np
.
ceil
(
input_shape
/
32
).
astype
(
"int32"
)
shape
=
np
.
ceil
(
input_shape
/
/
32
).
astype
(
"int32"
)
h
,
w
=
shape
h
,
w
=
shape
pool1
=
avg_pool
(
input
,
h
,
w
,
h
,
w
)
pool1
=
avg_pool
(
input
,
h
,
w
,
h
,
w
)
pool1_interp
=
interp
(
pool1
,
shape
)
pool1_interp
=
interp
(
pool1
,
shape
)
pool2
=
avg_pool
(
input
,
h
/
2
,
w
/
2
,
h
/
2
,
w
/
2
)
pool2
=
avg_pool
(
input
,
h
/
/
2
,
w
//
2
,
h
//
2
,
w
/
/
2
)
pool2_interp
=
interp
(
pool2
,
shape
)
pool2_interp
=
interp
(
pool2
,
shape
)
pool3
=
avg_pool
(
input
,
h
/
3
,
w
/
3
,
h
/
3
,
w
/
3
)
pool3
=
avg_pool
(
input
,
h
/
/
3
,
w
//
3
,
h
//
3
,
w
/
/
3
)
pool3_interp
=
interp
(
pool3
,
shape
)
pool3_interp
=
interp
(
pool3
,
shape
)
pool4
=
avg_pool
(
input
,
h
/
4
,
w
/
4
,
h
/
4
,
w
/
4
)
pool4
=
avg_pool
(
input
,
h
/
/
4
,
w
//
4
,
h
//
4
,
w
/
/
4
)
pool4_interp
=
interp
(
pool4
,
shape
)
pool4_interp
=
interp
(
pool4
,
shape
)
conv5_3_sum
=
input
+
pool4_interp
+
pool3_interp
+
pool2_interp
+
pool1_interp
conv5_3_sum
=
input
+
pool4_interp
+
pool3_interp
+
pool2_interp
+
pool1_interp
return
conv5_3_sum
return
conv5_3_sum
...
@@ -172,14 +175,14 @@ def shared_convs(image):
...
@@ -172,14 +175,14 @@ def shared_convs(image):
def
res_block
(
input
,
filter_num
,
padding
=
0
,
dilation
=
None
,
name
=
None
):
def
res_block
(
input
,
filter_num
,
padding
=
0
,
dilation
=
None
,
name
=
None
):
tmp
=
conv
(
input
,
1
,
1
,
filter_num
/
4
,
1
,
1
,
name
=
name
+
"_1_1_reduce"
)
tmp
=
conv
(
input
,
1
,
1
,
filter_num
/
/
4
,
1
,
1
,
name
=
name
+
"_1_1_reduce"
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
zero_padding
(
tmp
,
padding
=
padding
)
tmp
=
zero_padding
(
tmp
,
padding
=
padding
)
if
dilation
is
None
:
if
dilation
is
None
:
tmp
=
conv
(
tmp
,
3
,
3
,
filter_num
/
4
,
1
,
1
,
name
=
name
+
"_3_3"
)
tmp
=
conv
(
tmp
,
3
,
3
,
filter_num
/
/
4
,
1
,
1
,
name
=
name
+
"_3_3"
)
else
:
else
:
tmp
=
atrous_conv
(
tmp
=
atrous_conv
(
tmp
,
3
,
3
,
filter_num
/
4
,
dilation
,
name
=
name
+
"_3_3"
)
tmp
,
3
,
3
,
filter_num
/
/
4
,
dilation
,
name
=
name
+
"_3_3"
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
conv
(
tmp
,
1
,
1
,
filter_num
,
1
,
1
,
name
=
name
+
"_1_1_increase"
)
tmp
=
conv
(
tmp
,
1
,
1
,
filter_num
,
1
,
1
,
name
=
name
+
"_1_1_increase"
)
tmp
=
bn
(
tmp
,
relu
=
False
)
tmp
=
bn
(
tmp
,
relu
=
False
)
...
@@ -195,7 +198,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
...
@@ -195,7 +198,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
proj_bn
=
bn
(
proj
,
relu
=
False
)
proj_bn
=
bn
(
proj
,
relu
=
False
)
tmp
=
conv
(
tmp
=
conv
(
input
,
1
,
1
,
filter_num
/
4
,
stride
,
stride
,
name
=
name
+
"_1_1_reduce"
)
input
,
1
,
1
,
filter_num
/
/
4
,
stride
,
stride
,
name
=
name
+
"_1_1_reduce"
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
zero_padding
(
tmp
,
padding
=
padding
)
tmp
=
zero_padding
(
tmp
,
padding
=
padding
)
...
@@ -208,7 +211,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
...
@@ -208,7 +211,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
tmp
,
tmp
,
3
,
3
,
3
,
3
,
filter_num
/
4
,
filter_num
/
/
4
,
1
,
1
,
1
,
1
,
padding
=
padding
,
padding
=
padding
,
...
@@ -218,7 +221,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
...
@@ -218,7 +221,7 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
tmp
,
tmp
,
3
,
3
,
3
,
3
,
filter_num
/
4
,
filter_num
/
/
4
,
dilation
,
dilation
,
padding
=
padding
,
padding
=
padding
,
name
=
name
+
"_3_3"
)
name
=
name
+
"_3_3"
)
...
@@ -232,12 +235,12 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
...
@@ -232,12 +235,12 @@ def proj_block(input, filter_num, padding=0, dilation=None, stride=1,
def
sub_net_4
(
input
,
input_shape
):
def
sub_net_4
(
input
,
input_shape
):
tmp
=
interp
(
input
,
out_shape
=
np
.
ceil
(
input_shape
/
32
))
tmp
=
interp
(
input
,
out_shape
=
np
.
ceil
(
input_shape
/
/
32
))
tmp
=
dilation_convs
(
tmp
)
tmp
=
dilation_convs
(
tmp
)
tmp
=
pyramis_pooling
(
tmp
,
input_shape
)
tmp
=
pyramis_pooling
(
tmp
,
input_shape
)
tmp
=
conv
(
tmp
,
1
,
1
,
256
,
1
,
1
,
name
=
"conv5_4_k1"
)
tmp
=
conv
(
tmp
,
1
,
1
,
256
,
1
,
1
,
name
=
"conv5_4_k1"
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
bn
(
tmp
,
relu
=
True
)
tmp
=
interp
(
tmp
,
input_shape
/
16
)
tmp
=
interp
(
tmp
,
input_shape
/
/
16
)
return
tmp
return
tmp
...
@@ -265,7 +268,7 @@ def CCF24(sub2_out, sub4_out, input_shape):
...
@@ -265,7 +268,7 @@ def CCF24(sub2_out, sub4_out, input_shape):
tmp
=
bn
(
tmp
,
relu
=
False
)
tmp
=
bn
(
tmp
,
relu
=
False
)
tmp
=
tmp
+
sub2_out
tmp
=
tmp
+
sub2_out
tmp
=
fluid
.
layers
.
relu
(
tmp
)
tmp
=
fluid
.
layers
.
relu
(
tmp
)
tmp
=
interp
(
tmp
,
input_shape
/
8
)
tmp
=
interp
(
tmp
,
input_shape
/
/
8
)
return
tmp
return
tmp
...
@@ -275,7 +278,7 @@ def CCF124(sub1_out, sub24_out, input_shape):
...
@@ -275,7 +278,7 @@ def CCF124(sub1_out, sub24_out, input_shape):
tmp
=
bn
(
tmp
,
relu
=
False
)
tmp
=
bn
(
tmp
,
relu
=
False
)
tmp
=
tmp
+
sub1_out
tmp
=
tmp
+
sub1_out
tmp
=
fluid
.
layers
.
relu
(
tmp
)
tmp
=
fluid
.
layers
.
relu
(
tmp
)
tmp
=
interp
(
tmp
,
input_shape
/
4
)
tmp
=
interp
(
tmp
,
input_shape
/
/
4
)
return
tmp
return
tmp
...
...
fluid/icnet/infer.py
浏览文件 @
aa0ab8c9
"""Infer for ICNet model."""
"""Infer for ICNet model."""
from
__future__
import
print_function
import
cityscape
import
cityscape
import
argparse
import
argparse
import
functools
import
functools
...
@@ -101,7 +102,7 @@ def infer(args):
...
@@ -101,7 +102,7 @@ def infer(args):
exe
.
run
(
fluid
.
default_startup_program
())
exe
.
run
(
fluid
.
default_startup_program
())
assert
os
.
path
.
exists
(
args
.
model_path
)
assert
os
.
path
.
exists
(
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
args
.
model_path
)
print
"loaded model from: %s"
%
args
.
model_path
print
(
"loaded model from: %s"
%
args
.
model_path
)
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
if
not
os
.
path
.
isdir
(
args
.
out_path
):
if
not
os
.
path
.
isdir
(
args
.
out_path
):
...
...
fluid/icnet/train.py
浏览文件 @
aa0ab8c9
"""Trainer for ICNet model."""
"""Trainer for ICNet model."""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
icnet
import
icnet
from
icnet
import
icnet
import
cityscape
import
cityscape
import
argparse
import
argparse
...
@@ -94,7 +97,7 @@ def train(args):
...
@@ -94,7 +97,7 @@ def train(args):
exe
.
run
(
fluid
.
default_startup_program
())
exe
.
run
(
fluid
.
default_startup_program
())
if
args
.
init_model
is
not
None
:
if
args
.
init_model
is
not
None
:
print
"load model from: %s"
%
args
.
init_model
print
(
"load model from: %s"
%
args
.
init_model
)
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
fluid
.
io
.
load_params
(
exe
,
args
.
init_model
)
fluid
.
io
.
load_params
(
exe
,
args
.
init_model
)
...
@@ -111,7 +114,7 @@ def train(args):
...
@@ -111,7 +114,7 @@ def train(args):
for
data
in
train_reader
():
for
data
in
train_reader
():
if
iter_id
>
TOTAL_STEP
:
if
iter_id
>
TOTAL_STEP
:
end_time
=
time
.
time
()
end_time
=
time
.
time
()
print
"kpis train_duration %f"
%
(
end_time
-
start_time
)
print
(
"kpis train_duration %f"
%
(
end_time
-
start_time
)
)
return
return
iter_id
+=
1
iter_id
+=
1
results
=
exe
.
run
(
results
=
exe
.
run
(
...
@@ -123,10 +126,10 @@ def train(args):
...
@@ -123,10 +126,10 @@ def train(args):
sub124_loss
+=
results
[
3
]
sub124_loss
+=
results
[
3
]
# training log
# training log
if
iter_id
%
LOG_PERIOD
==
0
:
if
iter_id
%
LOG_PERIOD
==
0
:
print
"Iter[%d]; train loss: %.3f; sub4_loss: %.3f; sub24_loss: %.3f; sub124_loss: %.3f"
%
(
print
(
"Iter[%d]; train loss: %.3f; sub4_loss: %.3f; sub24_loss: %.3f; sub124_loss: %.3f"
%
(
iter_id
,
t_loss
/
LOG_PERIOD
,
sub4_loss
/
LOG_PERIOD
,
iter_id
,
t_loss
/
LOG_PERIOD
,
sub4_loss
/
LOG_PERIOD
,
sub24_loss
/
LOG_PERIOD
,
sub124_loss
/
LOG_PERIOD
)
sub24_loss
/
LOG_PERIOD
,
sub124_loss
/
LOG_PERIOD
)
)
print
"kpis train_cost %f"
%
(
t_loss
/
LOG_PERIOD
)
print
(
"kpis train_cost %f"
%
(
t_loss
/
LOG_PERIOD
)
)
t_loss
=
0.
t_loss
=
0.
sub4_loss
=
0.
sub4_loss
=
0.
...
@@ -137,7 +140,7 @@ def train(args):
...
@@ -137,7 +140,7 @@ def train(args):
if
iter_id
%
CHECKPOINT_PERIOD
==
0
and
args
.
checkpoint_path
is
not
None
:
if
iter_id
%
CHECKPOINT_PERIOD
==
0
and
args
.
checkpoint_path
is
not
None
:
dir_name
=
args
.
checkpoint_path
+
"/"
+
str
(
iter_id
)
dir_name
=
args
.
checkpoint_path
+
"/"
+
str
(
iter_id
)
fluid
.
io
.
save_persistables
(
exe
,
dirname
=
dir_name
)
fluid
.
io
.
save_persistables
(
exe
,
dirname
=
dir_name
)
print
"Saved checkpoint: %s"
%
(
dir_name
)
print
(
"Saved checkpoint: %s"
%
(
dir_name
)
)
def
main
():
def
main
():
...
...
fluid/icnet/utils.py
浏览文件 @
aa0ab8c9
...
@@ -19,6 +19,7 @@ from __future__ import print_function
...
@@ -19,6 +19,7 @@ from __future__ import print_function
import
distutils.util
import
distutils.util
import
numpy
as
np
import
numpy
as
np
from
paddle.fluid
import
core
from
paddle.fluid
import
core
import
six
def
print_arguments
(
args
):
def
print_arguments
(
args
):
...
@@ -37,7 +38,7 @@ def print_arguments(args):
...
@@ -37,7 +38,7 @@ def print_arguments(args):
:type args: argparse.Namespace
:type args: argparse.Namespace
"""
"""
print
(
"----------- Configuration Arguments -----------"
)
print
(
"----------- Configuration Arguments -----------"
)
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
(
)):
for
arg
,
value
in
sorted
(
six
.
iteritems
(
vars
(
args
)
)):
print
(
"%s: %s"
%
(
arg
,
value
))
print
(
"%s: %s"
%
(
arg
,
value
))
print
(
"------------------------------------------------"
)
print
(
"------------------------------------------------"
)
...
...
fluid/ocr_recognition/attention_model.py
浏览文件 @
aa0ab8c9
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
decoder_size
=
128
decoder_size
=
128
...
...
fluid/ocr_recognition/crnn_ctc_model.py
浏览文件 @
aa0ab8c9
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle.fluid.layers.learning_rate_scheduler
import
_decay_step_counter
from
paddle.fluid.layers.learning_rate_scheduler
import
_decay_step_counter
from
paddle.fluid.initializer
import
init_on_cpu
from
paddle.fluid.initializer
import
init_on_cpu
import
math
import
math
import
six
def
conv_bn_pool
(
input
,
def
conv_bn_pool
(
input
,
...
@@ -15,7 +19,7 @@ def conv_bn_pool(input,
...
@@ -15,7 +19,7 @@ def conv_bn_pool(input,
pooling
=
True
,
pooling
=
True
,
use_cudnn
=
False
):
use_cudnn
=
False
):
tmp
=
input
tmp
=
input
for
i
in
xrange
(
group
):
for
i
in
six
.
moves
.
xrange
(
group
):
tmp
=
fluid
.
layers
.
conv2d
(
tmp
=
fluid
.
layers
.
conv2d
(
input
=
tmp
,
input
=
tmp
,
num_filters
=
out_ch
[
i
],
num_filters
=
out_ch
[
i
],
...
@@ -192,7 +196,7 @@ def ctc_train_net(args, data_shape, num_classes):
...
@@ -192,7 +196,7 @@ def ctc_train_net(args, data_shape, num_classes):
inference_program
=
fluid
.
default_main_program
().
clone
(
for_test
=
True
)
inference_program
=
fluid
.
default_main_program
().
clone
(
for_test
=
True
)
if
learning_rate_decay
==
"piecewise_decay"
:
if
learning_rate_decay
==
"piecewise_decay"
:
learning_rate
=
fluid
.
layers
.
piecewise_decay
([
learning_rate
=
fluid
.
layers
.
piecewise_decay
([
args
.
total_step
/
4
,
args
.
total_step
/
2
,
args
.
total_step
*
3
/
4
args
.
total_step
/
/
4
,
args
.
total_step
//
2
,
args
.
total_step
*
3
/
/
4
],
[
LR
,
LR
*
0.1
,
LR
*
0.01
,
LR
*
0.001
])
],
[
LR
,
LR
*
0.1
,
LR
*
0.01
,
LR
*
0.001
])
else
:
else
:
learning_rate
=
LR
learning_rate
=
LR
...
...
fluid/ocr_recognition/data_reader.py
浏览文件 @
aa0ab8c9
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
os
import
os
import
cv2
import
cv2
import
tarfile
import
tarfile
...
@@ -67,11 +70,11 @@ class DataGenerator(object):
...
@@ -67,11 +70,11 @@ class DataGenerator(object):
batchsize
batchsize
)
+
"; i++) print $(4*i+1)
\"
\"
$(4*i+2)
\"
\"
$(4*i+3)
\"
\"
$(4*i+4);}}' > "
+
to_file
)
+
"; i++) print $(4*i+1)
\"
\"
$(4*i+2)
\"
\"
$(4*i+3)
\"
\"
$(4*i+4);}}' > "
+
to_file
os
.
system
(
cmd
)
os
.
system
(
cmd
)
print
"finish batch shuffle"
print
(
"finish batch shuffle"
)
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
def
reader
():
def
reader
():
sizes
=
len
(
img_label_lines
)
/
batchsize
sizes
=
len
(
img_label_lines
)
/
/
batchsize
if
sizes
==
0
:
if
sizes
==
0
:
raise
ValueError
(
'Batch size is bigger than the dataset size.'
)
raise
ValueError
(
'Batch size is bigger than the dataset size.'
)
while
True
:
while
True
:
...
...
fluid/ocr_recognition/eval.py
浏览文件 @
aa0ab8c9
...
@@ -55,17 +55,16 @@ def evaluate(args):
...
@@ -55,17 +55,16 @@ def evaluate(args):
model_dir
=
os
.
path
.
dirname
(
args
.
model_path
)
model_dir
=
os
.
path
.
dirname
(
args
.
model_path
)
model_file_name
=
os
.
path
.
basename
(
args
.
model_path
)
model_file_name
=
os
.
path
.
basename
(
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
print
"Init model from: %s."
%
args
.
model_path
print
(
"Init model from: %s."
%
args
.
model_path
)
evaluator
.
reset
(
exe
)
evaluator
.
reset
(
exe
)
count
=
0
count
=
0
for
data
in
test_reader
():
for
data
in
test_reader
():
count
+=
1
count
+=
1
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
get_feeder_data
(
data
,
place
))
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
get_feeder_data
(
data
,
place
))
print
"Read %d samples;
\r
"
%
count
,
avg_distance
,
avg_seq_error
=
evaluator
.
eval
(
exe
)
avg_distance
,
avg_seq_error
=
evaluator
.
eval
(
exe
)
print
"Read %d samples; avg_distance: %s; avg_seq_error: %s"
%
(
print
(
"Read %d samples; avg_distance: %s; avg_seq_error: %s"
%
(
count
,
avg_distance
,
avg_seq_error
)
count
,
avg_distance
,
avg_seq_error
)
)
def
main
():
def
main
():
...
...
fluid/ocr_recognition/infer.py
浏览文件 @
aa0ab8c9
from
__future__
import
print_function
import
paddle.v2
as
paddle
import
paddle.v2
as
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
utility
import
add_arguments
,
print_arguments
,
to_lodtensor
,
get_ctc_feeder_data
,
get_attention_feeder_for_infer
from
utility
import
add_arguments
,
print_arguments
,
to_lodtensor
,
get_ctc_feeder_data
,
get_attention_feeder_for_infer
...
@@ -64,7 +65,7 @@ def inference(args):
...
@@ -64,7 +65,7 @@ def inference(args):
with
open
(
args
.
dict
)
as
dict_file
:
with
open
(
args
.
dict
)
as
dict_file
:
for
i
,
word
in
enumerate
(
dict_file
):
for
i
,
word
in
enumerate
(
dict_file
):
dict_map
[
i
]
=
word
.
strip
()
dict_map
[
i
]
=
word
.
strip
()
print
"Loaded dict from %s"
%
args
.
dict
print
(
"Loaded dict from %s"
%
args
.
dict
)
# load init model
# load init model
model_dir
=
args
.
model_path
model_dir
=
args
.
model_path
...
@@ -73,7 +74,7 @@ def inference(args):
...
@@ -73,7 +74,7 @@ def inference(args):
model_dir
=
os
.
path
.
dirname
(
args
.
model_path
)
model_dir
=
os
.
path
.
dirname
(
args
.
model_path
)
model_file_name
=
os
.
path
.
basename
(
args
.
model_path
)
model_file_name
=
os
.
path
.
basename
(
args
.
model_path
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
print
"Init model from: %s."
%
args
.
model_path
print
(
"Init model from: %s."
%
args
.
model_path
)
batch_times
=
[]
batch_times
=
[]
iters
=
0
iters
=
0
...
@@ -96,17 +97,17 @@ def inference(args):
...
@@ -96,17 +97,17 @@ def inference(args):
fps
=
args
.
batch_size
/
batch_time
fps
=
args
.
batch_size
/
batch_time
batch_times
.
append
(
batch_time
)
batch_times
.
append
(
batch_time
)
if
dict_map
is
not
None
:
if
dict_map
is
not
None
:
print
"Iteration %d, latency: %.5f s, fps: %f, result: %s"
%
(
print
(
"Iteration %d, latency: %.5f s, fps: %f, result: %s"
%
(
iters
,
iters
,
batch_time
,
batch_time
,
fps
,
fps
,
[
dict_map
[
index
]
for
index
in
indexes
],
)
[
dict_map
[
index
]
for
index
in
indexes
],
)
)
else
:
else
:
print
"Iteration %d, latency: %.5f s, fps: %f, result: %s"
%
(
print
(
"Iteration %d, latency: %.5f s, fps: %f, result: %s"
%
(
iters
,
iters
,
batch_time
,
batch_time
,
fps
,
fps
,
indexes
,
)
indexes
,
)
)
iters
+=
1
iters
+=
1
...
...
fluid/ocr_recognition/train.py
浏览文件 @
aa0ab8c9
"""Trainer for OCR CTC or attention model."""
"""Trainer for OCR CTC or attention model."""
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
utility
import
add_arguments
,
print_arguments
,
to_lodtensor
,
get_ctc_feeder_data
,
get_attention_feeder_data
from
utility
import
add_arguments
,
print_arguments
,
to_lodtensor
,
get_ctc_feeder_data
,
get_attention_feeder_data
import
paddle.fluid.profiler
as
profiler
import
paddle.fluid.profiler
as
profiler
...
@@ -85,7 +88,7 @@ def train(args):
...
@@ -85,7 +88,7 @@ def train(args):
model_dir
=
os
.
path
.
dirname
(
args
.
init_model
)
model_dir
=
os
.
path
.
dirname
(
args
.
init_model
)
model_file_name
=
os
.
path
.
basename
(
args
.
init_model
)
model_file_name
=
os
.
path
.
basename
(
args
.
init_model
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
fluid
.
io
.
load_params
(
exe
,
dirname
=
model_dir
,
filename
=
model_file_name
)
print
"Init model from: %s."
%
args
.
init_model
print
(
"Init model from: %s."
%
args
.
init_model
)
train_exe
=
exe
train_exe
=
exe
error_evaluator
.
reset
(
exe
)
error_evaluator
.
reset
(
exe
)
...
@@ -112,18 +115,18 @@ def train(args):
...
@@ -112,18 +115,18 @@ def train(args):
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
))
_
,
test_seq_error
=
error_evaluator
.
eval
(
exe
)
_
,
test_seq_error
=
error_evaluator
.
eval
(
exe
)
print
"
\n
Time: %s; Iter[%d]; Test seq error: %s.
\n
"
%
(
print
(
"
\n
Time: %s; Iter[%d]; Test seq error: %s.
\n
"
%
(
time
.
time
(),
iter_num
,
str
(
test_seq_error
[
0
]))
time
.
time
(),
iter_num
,
str
(
test_seq_error
[
0
]))
)
#Note: The following logs are special for CE monitoring.
#Note: The following logs are special for CE monitoring.
#Other situations do not need to care about these logs.
#Other situations do not need to care about these logs.
print
"kpis test_acc %f"
%
(
1
-
test_seq_error
[
0
]
)
print
(
"kpis test_acc %f"
%
(
1
-
test_seq_error
[
0
])
)
def
save_model
(
args
,
exe
,
iter_num
):
def
save_model
(
args
,
exe
,
iter_num
):
filename
=
"model_%05d"
%
iter_num
filename
=
"model_%05d"
%
iter_num
fluid
.
io
.
save_params
(
fluid
.
io
.
save_params
(
exe
,
dirname
=
args
.
save_model_dir
,
filename
=
filename
)
exe
,
dirname
=
args
.
save_model_dir
,
filename
=
filename
)
print
"Saved model to: %s/%s."
%
(
args
.
save_model_dir
,
filename
)
print
(
"Saved model to: %s/%s."
%
(
args
.
save_model_dir
,
filename
)
)
iter_num
=
0
iter_num
=
0
stop
=
False
stop
=
False
...
@@ -152,14 +155,14 @@ def train(args):
...
@@ -152,14 +155,14 @@ def train(args):
iter_num
+=
1
iter_num
+=
1
# training log
# training log
if
iter_num
%
args
.
log_period
==
0
:
if
iter_num
%
args
.
log_period
==
0
:
print
"
\n
Time: %s; Iter[%d]; Avg loss: %.3f; Avg seq err: %.3f"
%
(
print
(
"
\n
Time: %s; Iter[%d]; Avg loss: %.3f; Avg seq err: %.3f"
%
(
time
.
time
(),
iter_num
,
time
.
time
(),
iter_num
,
total_loss
/
(
args
.
log_period
*
args
.
batch_size
),
total_loss
/
(
args
.
log_period
*
args
.
batch_size
),
total_seq_error
/
(
args
.
log_period
*
args
.
batch_size
))
total_seq_error
/
(
args
.
log_period
*
args
.
batch_size
))
)
print
"kpis train_cost %f"
%
(
total_loss
/
(
args
.
log_period
*
print
(
"kpis train_cost %f"
%
(
total_loss
/
(
args
.
log_period
*
args
.
batch_size
))
args
.
batch_size
))
)
print
"kpis train_acc %f"
%
(
print
(
"kpis train_acc %f"
%
(
1
-
total_seq_error
/
(
args
.
log_period
*
args
.
batch_size
))
1
-
total_seq_error
/
(
args
.
log_period
*
args
.
batch_size
))
)
total_loss
=
0.0
total_loss
=
0.0
total_seq_error
=
0.0
total_seq_error
=
0.0
...
@@ -179,7 +182,7 @@ def train(args):
...
@@ -179,7 +182,7 @@ def train(args):
else
:
else
:
save_model
(
args
,
exe
,
iter_num
)
save_model
(
args
,
exe
,
iter_num
)
end_time
=
time
.
time
()
end_time
=
time
.
time
()
print
"kpis train_duration %f"
%
(
end_time
-
start_time
)
print
(
"kpis train_duration %f"
%
(
end_time
-
start_time
)
)
# Postprocess benchmark data
# Postprocess benchmark data
latencies
=
batch_times
[
args
.
skip_batch_num
:]
latencies
=
batch_times
[
args
.
skip_batch_num
:]
latency_avg
=
np
.
average
(
latencies
)
latency_avg
=
np
.
average
(
latencies
)
...
...
fluid/ocr_recognition/utility.py
浏览文件 @
aa0ab8c9
...
@@ -20,6 +20,7 @@ import distutils.util
...
@@ -20,6 +20,7 @@ import distutils.util
import
numpy
as
np
import
numpy
as
np
from
paddle.fluid
import
core
from
paddle.fluid
import
core
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
six
def
print_arguments
(
args
):
def
print_arguments
(
args
):
...
@@ -38,7 +39,7 @@ def print_arguments(args):
...
@@ -38,7 +39,7 @@ def print_arguments(args):
:type args: argparse.Namespace
:type args: argparse.Namespace
"""
"""
print
(
"----------- Configuration Arguments -----------"
)
print
(
"----------- Configuration Arguments -----------"
)
for
arg
,
value
in
sorted
(
vars
(
args
).
iteritems
(
)):
for
arg
,
value
in
sorted
(
six
.
iteritems
(
vars
(
args
)
)):
print
(
"%s: %s"
%
(
arg
,
value
))
print
(
"%s: %s"
%
(
arg
,
value
))
print
(
"------------------------------------------------"
)
print
(
"------------------------------------------------"
)
...
@@ -82,9 +83,9 @@ def get_ctc_feeder_data(data, place, need_label=True):
...
@@ -82,9 +83,9 @@ def get_ctc_feeder_data(data, place, need_label=True):
pixel_tensor
=
core
.
LoDTensor
()
pixel_tensor
=
core
.
LoDTensor
()
pixel_data
=
None
pixel_data
=
None
pixel_data
=
np
.
concatenate
(
pixel_data
=
np
.
concatenate
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
),
axis
=
0
).
astype
(
"float32"
)
list
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
)
),
axis
=
0
).
astype
(
"float32"
)
pixel_tensor
.
set
(
pixel_data
,
place
)
pixel_tensor
.
set
(
pixel_data
,
place
)
label_tensor
=
to_lodtensor
(
map
(
lambda
x
:
x
[
1
],
data
),
place
)
label_tensor
=
to_lodtensor
(
list
(
map
(
lambda
x
:
x
[
1
],
data
)
),
place
)
if
need_label
:
if
need_label
:
return
{
"pixel"
:
pixel_tensor
,
"label"
:
label_tensor
}
return
{
"pixel"
:
pixel_tensor
,
"label"
:
label_tensor
}
else
:
else
:
...
@@ -95,10 +96,10 @@ def get_attention_feeder_data(data, place, need_label=True):
...
@@ -95,10 +96,10 @@ def get_attention_feeder_data(data, place, need_label=True):
pixel_tensor
=
core
.
LoDTensor
()
pixel_tensor
=
core
.
LoDTensor
()
pixel_data
=
None
pixel_data
=
None
pixel_data
=
np
.
concatenate
(
pixel_data
=
np
.
concatenate
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
),
axis
=
0
).
astype
(
"float32"
)
list
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
)
),
axis
=
0
).
astype
(
"float32"
)
pixel_tensor
.
set
(
pixel_data
,
place
)
pixel_tensor
.
set
(
pixel_data
,
place
)
label_in_tensor
=
to_lodtensor
(
map
(
lambda
x
:
x
[
1
],
data
),
place
)
label_in_tensor
=
to_lodtensor
(
list
(
map
(
lambda
x
:
x
[
1
],
data
)
),
place
)
label_out_tensor
=
to_lodtensor
(
map
(
lambda
x
:
x
[
2
],
data
),
place
)
label_out_tensor
=
to_lodtensor
(
list
(
map
(
lambda
x
:
x
[
2
],
data
)
),
place
)
if
need_label
:
if
need_label
:
return
{
return
{
"pixel"
:
pixel_tensor
,
"pixel"
:
pixel_tensor
,
...
@@ -126,7 +127,7 @@ def get_attention_feeder_for_infer(data, place):
...
@@ -126,7 +127,7 @@ def get_attention_feeder_for_infer(data, place):
pixel_tensor
=
core
.
LoDTensor
()
pixel_tensor
=
core
.
LoDTensor
()
pixel_data
=
None
pixel_data
=
None
pixel_data
=
np
.
concatenate
(
pixel_data
=
np
.
concatenate
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
),
axis
=
0
).
astype
(
"float32"
)
list
(
map
(
lambda
x
:
x
[
0
][
np
.
newaxis
,
:],
data
)
),
axis
=
0
).
astype
(
"float32"
)
pixel_tensor
.
set
(
pixel_data
,
place
)
pixel_tensor
.
set
(
pixel_data
,
place
)
return
{
return
{
"pixel"
:
pixel_tensor
,
"pixel"
:
pixel_tensor
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录