Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
f3c14dbf
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看板
提交
f3c14dbf
编写于
7月 19, 2019
作者:
L
LiufangSang
提交者:
whs
7月 19, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ocr fix issue 2744 (#2837)
* fix2744 * remove useless comment * make code less ugly
上级
7be8ffa9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
49 addition
and
19 deletion
+49
-19
PaddleCV/ocr_recognition/data_reader.py
PaddleCV/ocr_recognition/data_reader.py
+49
-19
未找到文件。
PaddleCV/ocr_recognition/data_reader.py
浏览文件 @
f3c14dbf
...
...
@@ -9,6 +9,7 @@ from PIL import Image
from
os
import
path
from
paddle.dataset.image
import
load_image
import
paddle
import
random
try
:
input
=
raw_input
...
...
@@ -53,28 +54,57 @@ class DataGenerator(object):
:param cycle: If number of iterations is greater than dataset_size / batch_size
it reiterates dataset over as many times as necessary.
:type cycle: bool
'''
img_label_lines
=
[]
to_file
=
"tmp.txt"
if
not
shuffle
:
cmd
=
"cat "
+
img_label_list
+
" | awk '{print $1,$2,$3,$4;}' > "
+
to_file
elif
batchsize
==
1
:
cmd
=
"cat "
+
img_label_list
+
" | awk '{print $1,$2,$3,$4;}' | shuf > "
+
to_file
else
:
#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
os
.
system
(
cmd
)
def
_shuffle_data
(
input_file_path
,
output_file_path
,
shuffle
,
batchsize
):
def
_write_file
(
file_path
,
lines_to_write
):
open
(
file_path
,
'w'
).
writelines
(
[
"{}
\n
"
.
format
(
item
)
for
item
in
lines_to_write
])
input_file
=
open
(
input_file_path
,
'r'
)
lines_to_shuf
=
[
line
.
strip
()
for
line
in
input_file
.
readlines
()]
if
not
shuffle
:
_write_file
(
output_file_path
,
lines_to_shuf
)
elif
batchsize
==
1
:
random
.
shuffle
(
lines_to_shuf
)
_write_file
(
output_file_path
,
lines_to_shuf
)
else
:
#partial shuffle
for
i
in
range
(
len
(
lines_to_shuf
)):
str_i
=
lines_to_shuf
[
i
]
list_i
=
str_i
.
strip
().
split
(
' '
)
str_i_
=
"%04d%.4f "
%
(
int
(
list_i
[
0
]),
random
.
random
()
)
+
str_i
lines_to_shuf
[
i
]
=
str_i_
lines_to_shuf
.
sort
()
delete_num
=
random
.
randint
(
1
,
100
)
del
lines_to_shuf
[
0
:
delete_num
]
#batch merge and shuffle
lines_concat
=
[]
for
i
in
range
(
0
,
len
(
lines_to_shuf
),
batchsize
):
lines_concat
.
append
(
' '
.
join
(
lines_to_shuf
[
i
:
i
+
batchsize
]))
random
.
shuffle
(
lines_concat
)
#batch split
out_file
=
open
(
output_file_path
,
'w'
)
for
i
in
range
(
len
(
lines_concat
)):
tmp_list
=
lines_concat
[
i
].
split
(
' '
)
for
j
in
range
(
int
(
len
(
tmp_list
)
/
5
)):
out_file
.
write
(
"{} {} {} {}
\n
"
.
format
(
tmp_list
[
5
*
j
+
1
],
tmp_list
[
5
*
j
+
2
],
tmp_list
[
5
*
j
+
3
],
tmp_list
[
5
*
j
+
4
]))
out_file
.
close
()
input_file
.
close
()
_shuffle_data
(
img_label_list
,
to_file
,
shuffle
,
batchsize
)
print
(
"finish batch shuffle"
)
img_label_lines
=
open
(
to_file
,
'r'
).
readlines
()
...
...
@@ -148,7 +178,7 @@ class DataGenerator(object):
was None. If img_label_list was set to None, it will read image path
from stdin.
:type img_root_dir: str
:param cycle: If number of iterations is greater than dataset_size /
batch_size it reiterates dataset over as many times as necessary.
:type cycle: bool
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录