Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
080748fc
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
080748fc
编写于
9月 10, 2020
作者:
Q
qingqing01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update OCR model
上级
df33864a
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
80 addition
and
84 deletion
+80
-84
examples/ocr/eval.py
examples/ocr/eval.py
+30
-32
examples/ocr/predict.py
examples/ocr/predict.py
+14
-12
examples/ocr/seq2seq_attn.py
examples/ocr/seq2seq_attn.py
+5
-8
examples/ocr/train.py
examples/ocr/train.py
+24
-26
examples/ocr/utility.py
examples/ocr/utility.py
+7
-6
未找到文件。
examples/ocr/eval.py
浏览文件 @
080748fc
...
@@ -16,11 +16,11 @@ from __future__ import print_function
...
@@ -16,11 +16,11 @@ from __future__ import print_function
import
argparse
import
argparse
import
functools
import
functools
import
paddle
.fluid.profiler
as
profiler
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle.
incubate.hapi.model
import
Input
,
set_device
from
paddle.
static
import
InputSpec
as
Input
from
paddle.
incubate.hapi.
vision.transforms
import
BatchCompose
from
paddle.vision.transforms
import
BatchCompose
from
utility
import
add_arguments
,
print_arguments
from
utility
import
add_arguments
,
print_arguments
from
utility
import
SeqAccuracy
,
LoggerCallBack
,
SeqBeamAccuracy
from
utility
import
SeqAccuracy
,
LoggerCallBack
,
SeqBeamAccuracy
...
@@ -46,13 +46,8 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.
...
@@ -46,13 +46,8 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.
def
main
(
FLAGS
):
def
main
(
FLAGS
):
device
=
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
device
=
paddle
.
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
model
=
Seq2SeqAttModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
)
# yapf: disable
# yapf: disable
inputs
=
[
inputs
=
[
...
@@ -64,13 +59,16 @@ def main(FLAGS):
...
@@ -64,13 +59,16 @@ def main(FLAGS):
Input
([
None
,
None
],
"float32"
,
name
=
"mask"
)
Input
([
None
,
None
],
"float32"
,
name
=
"mask"
)
]
]
# yapf: enable
# yapf: enable
model
=
paddle
.
Model
(
model
.
prepare
(
Seq2SeqAttModel
(
loss_function
=
WeightCrossEntropy
(),
encoder_size
=
FLAGS
.
encoder_size
,
metrics
=
SeqAccuracy
(),
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
),
inputs
=
inputs
,
inputs
=
inputs
,
labels
=
labels
,
labels
=
labels
)
device
=
device
)
model
.
prepare
(
loss
=
WeightCrossEntropy
(),
metrics
=
SeqAccuracy
())
model
.
load
(
FLAGS
.
init_model
)
model
.
load
(
FLAGS
.
init_model
)
test_dataset
=
data
.
test
()
test_dataset
=
data
.
test
()
...
@@ -97,29 +95,29 @@ def main(FLAGS):
...
@@ -97,29 +95,29 @@ def main(FLAGS):
def
beam_search
(
FLAGS
):
def
beam_search
(
FLAGS
):
device
=
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
device
=
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
model
=
Seq2SeqAttInferModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
,
beam_size
=
FLAGS
.
beam_size
)
# yapf: disable
inputs
=
[
inputs
=
[
Input
(
Input
([
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
[
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
Input
(
Input
([
None
,
None
],
"int64"
,
name
=
"label_in"
)
[
None
,
None
],
"int64"
,
name
=
"label_in"
)
]
]
labels
=
[
labels
=
[
Input
(
Input
([
None
,
None
],
"int64"
,
name
=
"label_out"
),
[
None
,
None
],
"int64"
,
name
=
"label_out"
),
Input
(
Input
([
None
,
None
],
"float32"
,
name
=
"mask"
)
[
None
,
None
],
"float32"
,
name
=
"mask"
)
]
]
model
.
prepare
(
# yapf: enable
loss_function
=
None
,
metrics
=
SeqBeamAccuracy
(),
model
=
paddle
.
Model
(
Seq2SeqAttInferModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
,
beam_size
=
FLAGS
.
beam_size
),
inputs
=
inputs
,
inputs
=
inputs
,
labels
=
labels
,
labels
=
labels
)
device
=
device
)
model
.
prepare
(
loss_function
=
None
,
metrics
=
SeqBeamAccuracy
())
model
.
load
(
FLAGS
.
init_model
)
model
.
load
(
FLAGS
.
init_model
)
test_dataset
=
data
.
test
()
test_dataset
=
data
.
test
()
...
...
examples/ocr/predict.py
浏览文件 @
080748fc
...
@@ -22,12 +22,12 @@ import argparse
...
@@ -22,12 +22,12 @@ import argparse
import
functools
import
functools
from
PIL
import
Image
from
PIL
import
Image
import
paddle
.fluid.profiler
as
profiler
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle.
incubate.hapi.model
import
Input
,
set_device
from
paddle.
static
import
InputSpec
as
Input
from
paddle.
incubate.hapi
.datasets.folder
import
ImageFolder
from
paddle.
vision
.datasets.folder
import
ImageFolder
from
paddle.
incubate.hapi.
vision.transforms
import
BatchCompose
from
paddle.vision.transforms
import
BatchCompose
from
utility
import
add_arguments
,
print_arguments
from
utility
import
add_arguments
,
print_arguments
from
utility
import
postprocess
,
index2word
from
utility
import
postprocess
,
index2word
...
@@ -52,18 +52,20 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.")
...
@@ -52,18 +52,20 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.")
def
main
(
FLAGS
):
def
main
(
FLAGS
):
device
=
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
device
=
paddle
.
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
model
=
Seq2SeqAttInferModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
,
beam_size
=
FLAGS
.
beam_size
)
inputs
=
[
Input
([
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
]
inputs
=
[
Input
([
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
]
model
=
paddle
.
Model
(
Seq2SeqAttInferModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
,
beam_size
=
FLAGS
.
beam_size
),
inputs
)
model
.
prepare
(
inputs
=
inputs
,
device
=
device
)
model
.
prepare
()
model
.
load
(
FLAGS
.
init_model
)
model
.
load
(
FLAGS
.
init_model
)
fn
=
lambda
p
:
Image
.
open
(
p
).
convert
(
'L'
)
fn
=
lambda
p
:
Image
.
open
(
p
).
convert
(
'L'
)
...
...
examples/ocr/seq2seq_attn.py
浏览文件 @
080748fc
...
@@ -19,9 +19,7 @@ import paddle.fluid as fluid
...
@@ -19,9 +19,7 @@ import paddle.fluid as fluid
import
paddle.fluid.layers
as
layers
import
paddle.fluid.layers
as
layers
from
paddle.fluid.layers
import
BeamSearchDecoder
from
paddle.fluid.layers
import
BeamSearchDecoder
from
paddle.incubate.hapi.text
import
RNNCell
,
RNN
,
DynamicDecode
from
paddle.text
import
RNNCell
,
RNN
,
DynamicDecode
from
paddle.incubate.hapi.model
import
Model
from
paddle.incubate.hapi.loss
import
Loss
class
ConvBNPool
(
fluid
.
dygraph
.
Layer
):
class
ConvBNPool
(
fluid
.
dygraph
.
Layer
):
...
@@ -249,7 +247,7 @@ class Decoder(fluid.dygraph.Layer):
...
@@ -249,7 +247,7 @@ class Decoder(fluid.dygraph.Layer):
return
pred
return
pred
class
Seq2SeqAttModel
(
Model
):
class
Seq2SeqAttModel
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
def
__init__
(
self
,
self
,
in_channle
=
1
,
in_channle
=
1
,
...
@@ -322,12 +320,11 @@ class Seq2SeqAttInferModel(Seq2SeqAttModel):
...
@@ -322,12 +320,11 @@ class Seq2SeqAttInferModel(Seq2SeqAttModel):
return
rs
return
rs
class
WeightCrossEntropy
(
Loss
):
class
WeightCrossEntropy
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
WeightCrossEntropy
,
self
).
__init__
(
average
=
False
)
super
(
WeightCrossEntropy
,
self
).
__init__
()
def
forward
(
self
,
outputs
,
labels
):
def
forward
(
self
,
predict
,
label
,
mask
):
predict
,
(
label
,
mask
)
=
outputs
[
0
],
labels
loss
=
layers
.
cross_entropy
(
predict
,
label
=
label
)
loss
=
layers
.
cross_entropy
(
predict
,
label
=
label
)
loss
=
layers
.
elementwise_mul
(
loss
,
mask
,
axis
=
0
)
loss
=
layers
.
elementwise_mul
(
loss
,
mask
,
axis
=
0
)
loss
=
layers
.
reduce_sum
(
loss
)
loss
=
layers
.
reduce_sum
(
loss
)
...
...
examples/ocr/train.py
浏览文件 @
080748fc
...
@@ -21,11 +21,11 @@ import numpy as np
...
@@ -21,11 +21,11 @@ import numpy as np
import
argparse
import
argparse
import
functools
import
functools
import
paddle
.fluid.profiler
as
profiler
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle.
incubate.hapi.model
import
Input
,
set_device
from
paddle.
static
import
InputSpec
as
Input
from
paddle.
incubate.hapi.
vision.transforms
import
BatchCompose
from
paddle.vision.transforms
import
BatchCompose
from
utility
import
add_arguments
,
print_arguments
from
utility
import
add_arguments
,
print_arguments
from
utility
import
SeqAccuracy
,
LoggerCallBack
from
utility
import
SeqAccuracy
,
LoggerCallBack
...
@@ -58,14 +58,28 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.")
...
@@ -58,14 +58,28 @@ add_arg('dynamic', bool, False, "Whether to use dygraph.")
def
main
(
FLAGS
):
def
main
(
FLAGS
):
device
=
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
device
=
paddle
.
set_device
(
"gpu"
if
FLAGS
.
use_gpu
else
"cpu"
)
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
fluid
.
enable_dygraph
(
device
)
if
FLAGS
.
dynamic
else
None
model
=
Seq2SeqAttModel
(
# yapf: disable
encoder_size
=
FLAGS
.
encoder_size
,
inputs
=
[
decoder_size
=
FLAGS
.
decoder_size
,
Input
([
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
emb_dim
=
FLAGS
.
embedding_dim
,
Input
([
None
,
None
],
"int64"
,
name
=
"label_in"
),
num_classes
=
FLAGS
.
num_classes
)
]
labels
=
[
Input
([
None
,
None
],
"int64"
,
name
=
"label_out"
),
Input
([
None
,
None
],
"float32"
,
name
=
"mask"
),
]
# yapf: enable
model
=
paddle
.
Model
(
Seq2SeqAttModel
(
encoder_size
=
FLAGS
.
encoder_size
,
decoder_size
=
FLAGS
.
decoder_size
,
emb_dim
=
FLAGS
.
embedding_dim
,
num_classes
=
FLAGS
.
num_classes
),
inputs
,
labels
)
lr
=
FLAGS
.
lr
lr
=
FLAGS
.
lr
if
FLAGS
.
lr_decay_strategy
==
"piecewise_decay"
:
if
FLAGS
.
lr_decay_strategy
==
"piecewise_decay"
:
...
@@ -79,23 +93,7 @@ def main(FLAGS):
...
@@ -79,23 +93,7 @@ def main(FLAGS):
parameter_list
=
model
.
parameters
(),
parameter_list
=
model
.
parameters
(),
grad_clip
=
grad_clip
)
grad_clip
=
grad_clip
)
# yapf: disable
model
.
prepare
(
optimizer
,
WeightCrossEntropy
(),
SeqAccuracy
())
inputs
=
[
Input
([
None
,
1
,
48
,
384
],
"float32"
,
name
=
"pixel"
),
Input
([
None
,
None
],
"int64"
,
name
=
"label_in"
),
]
labels
=
[
Input
([
None
,
None
],
"int64"
,
name
=
"label_out"
),
Input
([
None
,
None
],
"float32"
,
name
=
"mask"
),
]
# yapf: enable
model
.
prepare
(
optimizer
,
WeightCrossEntropy
(),
SeqAccuracy
(),
inputs
=
inputs
,
labels
=
labels
)
train_dataset
=
data
.
train
()
train_dataset
=
data
.
train
()
train_collate_fn
=
BatchCompose
(
train_collate_fn
=
BatchCompose
(
...
...
examples/ocr/utility.py
浏览文件 @
080748fc
...
@@ -18,11 +18,12 @@ from __future__ import division
...
@@ -18,11 +18,12 @@ from __future__ import division
from
__future__
import
print_function
from
__future__
import
print_function
import
distutils.util
import
distutils.util
import
numpy
as
np
import
numpy
as
np
import
paddle.fluid
as
fluid
import
six
import
six
from
paddle.incubate.hapi.metrics
import
Metric
import
paddle
from
paddle.incubate.hapi.callbacks
import
ProgBarLogger
import
paddle.fluid
as
fluid
from
paddle.metric
import
Metric
def
print_arguments
(
args
):
def
print_arguments
(
args
):
...
@@ -72,7 +73,7 @@ class SeqAccuracy(Metric):
...
@@ -72,7 +73,7 @@ class SeqAccuracy(Metric):
self
.
_name
=
'seq_acc'
self
.
_name
=
'seq_acc'
self
.
reset
()
self
.
reset
()
def
add_metric_op
(
self
,
output
,
label
,
mask
,
*
args
,
**
kwargs
):
def
compute
(
self
,
output
,
label
,
mask
,
*
args
,
**
kwargs
):
pred
=
fluid
.
layers
.
flatten
(
output
,
axis
=
2
)
pred
=
fluid
.
layers
.
flatten
(
output
,
axis
=
2
)
score
,
topk
=
fluid
.
layers
.
topk
(
pred
,
1
)
score
,
topk
=
fluid
.
layers
.
topk
(
pred
,
1
)
return
topk
,
label
,
mask
return
topk
,
label
,
mask
...
@@ -102,7 +103,7 @@ class SeqAccuracy(Metric):
...
@@ -102,7 +103,7 @@ class SeqAccuracy(Metric):
return
self
.
_name
return
self
.
_name
class
LoggerCallBack
(
ProgBarLogger
):
class
LoggerCallBack
(
paddle
.
callbacks
.
ProgBarLogger
):
def
__init__
(
self
,
log_freq
=
1
,
verbose
=
2
,
train_bs
=
None
,
eval_bs
=
None
):
def
__init__
(
self
,
log_freq
=
1
,
verbose
=
2
,
train_bs
=
None
,
eval_bs
=
None
):
super
(
LoggerCallBack
,
self
).
__init__
(
log_freq
,
verbose
)
super
(
LoggerCallBack
,
self
).
__init__
(
log_freq
,
verbose
)
self
.
train_bs
=
train_bs
self
.
train_bs
=
train_bs
...
@@ -153,7 +154,7 @@ class SeqBeamAccuracy(Metric):
...
@@ -153,7 +154,7 @@ class SeqBeamAccuracy(Metric):
self
.
_name
=
'seq_acc'
self
.
_name
=
'seq_acc'
self
.
reset
()
self
.
reset
()
def
add_metric_op
(
self
,
output
,
label
,
mask
,
*
args
,
**
kwargs
):
def
compute
(
self
,
output
,
label
,
mask
,
*
args
,
**
kwargs
):
return
output
,
label
,
mask
return
output
,
label
,
mask
def
update
(
self
,
preds
,
labels
,
masks
,
*
args
,
**
kwargs
):
def
update
(
self
,
preds
,
labels
,
masks
,
*
args
,
**
kwargs
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录