Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wangsen
paddle_dbnet
Commits
ec0de454
"vscode:/vscode.git/clone" did not exist on "aa6e29cf049c0b6cb00f7a39e7011e174c0f98e7"
Commit
ec0de454
authored
Jan 05, 2021
by
Leif
Browse files
Merge remote-tracking branch 'upstream/dygraph' into dy1
parents
a127340c
c3e5522c
Changes
59
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
197 additions
and
95 deletions
+197
-95
doc/doc_en/recognition_en.md
doc/doc_en/recognition_en.md
+4
-4
paddleocr.py
paddleocr.py
+2
-2
ppocr/data/imaug/operators.py
ppocr/data/imaug/operators.py
+2
-2
ppocr/losses/det_basic_loss.py
ppocr/losses/det_basic_loss.py
+0
-1
ppocr/losses/det_sast_loss.py
ppocr/losses/det_sast_loss.py
+22
-22
ppocr/modeling/transforms/tps.py
ppocr/modeling/transforms/tps.py
+5
-1
ppocr/optimizer/learning_rate.py
ppocr/optimizer/learning_rate.py
+47
-4
ppocr/optimizer/lr_scheduler.py
ppocr/optimizer/lr_scheduler.py
+49
-0
setup.py
setup.py
+1
-1
tools/infer/predict_cls.py
tools/infer/predict_cls.py
+7
-10
tools/infer/predict_det.py
tools/infer/predict_det.py
+10
-8
tools/infer/predict_rec.py
tools/infer/predict_rec.py
+6
-10
tools/infer/predict_system.py
tools/infer/predict_system.py
+2
-0
tools/infer/utility.py
tools/infer/utility.py
+18
-16
tools/infer_cls.py
tools/infer_cls.py
+2
-0
tools/infer_det.py
tools/infer_det.py
+2
-0
tools/infer_rec.py
tools/infer_rec.py
+2
-0
tools/program.py
tools/program.py
+15
-13
tools/train.py
tools/train.py
+1
-1
No files found.
doc/doc_en/recognition_en.md
View file @
ec0de454
...
...
@@ -317,11 +317,11 @@ Eval:
<a
name=
"EVALUATION"
></a>
### EVALUATION
The evaluation data
set can be
modified via
`configs/rec/rec_icdar15_reader.yml`
setting of
`label_file_path`
in EvalReader
.
The evaluation dataset can be
set by modifying the
`Eval.dataset.label_file_list`
field in the
`configs/rec/rec_icdar15_train.yml`
file
.
```
# GPU evaluation, Global.checkpoints is the weight to be tested
python3 -m paddle.distributed.launch --gpus '0' tools/eval.py -c configs/rec/rec_icdar15_
reader
.yml -o Global.checkpoints={path/to/weights}/best_accuracy
python3 -m paddle.distributed.launch --gpus '0' tools/eval.py -c configs/rec/rec_icdar15_
train
.yml -o Global.checkpoints={path/to/weights}/best_accuracy
```
<a
name=
"PREDICTION"
></a>
...
...
@@ -336,7 +336,7 @@ The default prediction picture is stored in `infer_img`, and the weight is speci
```
# Predict English results
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.
checkpoints
={path/to/weights}/best_accuracy
TestReader
.infer_img=doc/imgs_words/en/word_1.jpg
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.
pretrained_model
={path/to/weights}/best_accuracy
Global.load_static_weights=false Global
.infer_img=doc/imgs_words/en/word_1.jpg
```
Input image:
...
...
@@ -354,7 +354,7 @@ The configuration file used for prediction must be consistent with the training.
```
# Predict Chinese results
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.
checkpoints
={path/to/weights}/best_accuracy
TestReader
.infer_img=doc/imgs_words/ch/word_1.jpg
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.
pretrained_model
={path/to/weights}/best_accuracy
Global.load_static_weights=false Global
.infer_img=doc/imgs_words/ch/word_1.jpg
```
Input image:
...
...
paddleocr.py
View file @
ec0de454
...
...
@@ -262,8 +262,8 @@ class PaddleOCR(predict_system.TextSystem):
logger
.
error
(
'rec_algorithm must in {}'
.
format
(
SUPPORT_REC_MODEL
))
sys
.
exit
(
0
)
postprocess_params
.
rec_char_dict_path
=
Path
(
__file__
).
parent
/
postprocess_params
.
rec_char_dict_path
postprocess_params
.
rec_char_dict_path
=
str
(
Path
(
__file__
).
parent
/
postprocess_params
.
rec_char_dict_path
)
# init det_model and rec_model
super
().
__init__
(
postprocess_params
)
...
...
ppocr/data/imaug/operators.py
View file @
ec0de454
...
...
@@ -119,10 +119,10 @@ class DetResizeForTest(object):
if
'image_shape'
in
kwargs
:
self
.
image_shape
=
kwargs
[
'image_shape'
]
self
.
resize_type
=
1
if
'limit_side_len'
in
kwargs
:
el
if
'limit_side_len'
in
kwargs
:
self
.
limit_side_len
=
kwargs
[
'limit_side_len'
]
self
.
limit_type
=
kwargs
.
get
(
'limit_type'
,
'min'
)
if
'resize_long'
in
kwargs
:
el
if
'resize_long'
in
kwargs
:
self
.
resize_type
=
2
self
.
resize_long
=
kwargs
.
get
(
'resize_long'
,
960
)
else
:
...
...
ppocr/losses/det_basic_loss.py
View file @
ec0de454
...
...
@@ -45,7 +45,6 @@ class BalanceLoss(nn.Layer):
self
.
balance_loss
=
balance_loss
self
.
main_loss_type
=
main_loss_type
self
.
negative_ratio
=
negative_ratio
self
.
main_loss_type
=
main_loss_type
self
.
return_origin
=
return_origin
self
.
eps
=
eps
...
...
ppocr/losses/det_sast_loss.py
View file @
ec0de454
...
...
@@ -19,7 +19,6 @@ from __future__ import print_function
import
paddle
from
paddle
import
nn
from
.det_basic_loss
import
DiceLoss
import
paddle.fluid
as
fluid
import
numpy
as
np
...
...
@@ -27,9 +26,7 @@ class SASTLoss(nn.Layer):
"""
"""
def
__init__
(
self
,
eps
=
1e-6
,
**
kwargs
):
def
__init__
(
self
,
eps
=
1e-6
,
**
kwargs
):
super
(
SASTLoss
,
self
).
__init__
()
self
.
dice_loss
=
DiceLoss
(
eps
=
eps
)
...
...
@@ -39,7 +36,7 @@ class SASTLoss(nn.Layer):
tcl_mask: N x 128 x 1
tcl_label: N x X list or LoDTensor
"""
f_score
=
predicts
[
'f_score'
]
f_border
=
predicts
[
'f_border'
]
f_tvo
=
predicts
[
'f_tvo'
]
...
...
@@ -53,15 +50,17 @@ class SASTLoss(nn.Layer):
score_loss
=
1.0
-
2
*
intersection
/
(
union
+
1e-5
)
#border loss
l_border_split
,
l_border_norm
=
paddle
.
split
(
l_border
,
num_or_sections
=
[
4
,
1
],
axis
=
1
)
l_border_split
,
l_border_norm
=
paddle
.
split
(
l_border
,
num_or_sections
=
[
4
,
1
],
axis
=
1
)
f_border_split
=
f_border
border_ex_shape
=
l_border_norm
.
shape
*
np
.
array
([
1
,
4
,
1
,
1
])
l_border_norm_split
=
paddle
.
expand
(
x
=
l_border_norm
,
shape
=
border_ex_shape
)
l_border_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
border_ex_shape
)
l_border_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
border_ex_shape
)
l_border_norm_split
=
paddle
.
expand
(
x
=
l_border_norm
,
shape
=
border_ex_shape
)
l_border_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
border_ex_shape
)
l_border_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
border_ex_shape
)
border_diff
=
l_border_split
-
f_border_split
abs_border_diff
=
paddle
.
abs
(
border_diff
)
abs_border_diff
=
paddle
.
abs
(
border_diff
)
border_sign
=
abs_border_diff
<
1.0
border_sign
=
paddle
.
cast
(
border_sign
,
dtype
=
'float32'
)
border_sign
.
stop_gradient
=
True
...
...
@@ -72,15 +71,16 @@ class SASTLoss(nn.Layer):
(
paddle
.
sum
(
l_border_score
*
l_border_mask
)
+
1e-5
)
#tvo_loss
l_tvo_split
,
l_tvo_norm
=
paddle
.
split
(
l_tvo
,
num_or_sections
=
[
8
,
1
],
axis
=
1
)
l_tvo_split
,
l_tvo_norm
=
paddle
.
split
(
l_tvo
,
num_or_sections
=
[
8
,
1
],
axis
=
1
)
f_tvo_split
=
f_tvo
tvo_ex_shape
=
l_tvo_norm
.
shape
*
np
.
array
([
1
,
8
,
1
,
1
])
l_tvo_norm_split
=
paddle
.
expand
(
x
=
l_tvo_norm
,
shape
=
tvo_ex_shape
)
l_tvo_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
tvo_ex_shape
)
l_tvo_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
tvo_ex_shape
)
l_tvo_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
tvo_ex_shape
)
l_tvo_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
tvo_ex_shape
)
#
tvo_geo_diff
=
l_tvo_split
-
f_tvo_split
abs_tvo_geo_diff
=
paddle
.
abs
(
tvo_geo_diff
)
abs_tvo_geo_diff
=
paddle
.
abs
(
tvo_geo_diff
)
tvo_sign
=
abs_tvo_geo_diff
<
1.0
tvo_sign
=
paddle
.
cast
(
tvo_sign
,
dtype
=
'float32'
)
tvo_sign
.
stop_gradient
=
True
...
...
@@ -91,15 +91,16 @@ class SASTLoss(nn.Layer):
(
paddle
.
sum
(
l_tvo_score
*
l_tvo_mask
)
+
1e-5
)
#tco_loss
l_tco_split
,
l_tco_norm
=
paddle
.
split
(
l_tco
,
num_or_sections
=
[
2
,
1
],
axis
=
1
)
l_tco_split
,
l_tco_norm
=
paddle
.
split
(
l_tco
,
num_or_sections
=
[
2
,
1
],
axis
=
1
)
f_tco_split
=
f_tco
tco_ex_shape
=
l_tco_norm
.
shape
*
np
.
array
([
1
,
2
,
1
,
1
])
l_tco_norm_split
=
paddle
.
expand
(
x
=
l_tco_norm
,
shape
=
tco_ex_shape
)
l_tco_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
tco_ex_shape
)
l_tco_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
tco_ex_shape
)
l_tco_score
=
paddle
.
expand
(
x
=
l_score
,
shape
=
tco_ex_shape
)
l_tco_mask
=
paddle
.
expand
(
x
=
l_mask
,
shape
=
tco_ex_shape
)
tco_geo_diff
=
l_tco_split
-
f_tco_split
abs_tco_geo_diff
=
paddle
.
abs
(
tco_geo_diff
)
abs_tco_geo_diff
=
paddle
.
abs
(
tco_geo_diff
)
tco_sign
=
abs_tco_geo_diff
<
1.0
tco_sign
=
paddle
.
cast
(
tco_sign
,
dtype
=
'float32'
)
tco_sign
.
stop_gradient
=
True
...
...
@@ -109,13 +110,12 @@ class SASTLoss(nn.Layer):
tco_loss
=
paddle
.
sum
(
tco_out_loss
*
l_tco_score
*
l_tco_mask
)
/
\
(
paddle
.
sum
(
l_tco_score
*
l_tco_mask
)
+
1e-5
)
# total loss
tvo_lw
,
tco_lw
=
1.5
,
1.5
score_lw
,
border_lw
=
1.0
,
1.0
total_loss
=
score_loss
*
score_lw
+
border_loss
*
border_lw
+
\
tvo_loss
*
tvo_lw
+
tco_loss
*
tco_lw
losses
=
{
'loss'
:
total_loss
,
"score_loss"
:
score_loss
,
\
"border_loss"
:
border_loss
,
'tvo_loss'
:
tvo_loss
,
'tco_loss'
:
tco_loss
}
return
losses
\ No newline at end of file
return
losses
ppocr/modeling/transforms/tps.py
View file @
ec0de454
...
...
@@ -16,6 +16,7 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
math
import
paddle
from
paddle
import
nn
,
ParamAttr
from
paddle.nn
import
functional
as
F
...
...
@@ -88,11 +89,14 @@ class LocalizationNetwork(nn.Layer):
in_channels
=
num_filters
self
.
block_list
.
append
(
pool
)
name
=
"loc_fc1"
stdv
=
1.0
/
math
.
sqrt
(
num_filters_list
[
-
1
]
*
1.0
)
self
.
fc1
=
nn
.
Linear
(
in_channels
,
fc_dim
,
weight_attr
=
ParamAttr
(
learning_rate
=
loc_lr
,
name
=
name
+
"_w"
),
learning_rate
=
loc_lr
,
name
=
name
+
"_w"
,
initializer
=
nn
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
name
+
'.b_0'
),
name
=
name
)
...
...
ppocr/optimizer/learning_rate.py
View file @
ec0de454
...
...
@@ -18,6 +18,7 @@ from __future__ import print_function
from
__future__
import
unicode_literals
from
paddle.optimizer
import
lr
from
.lr_scheduler
import
CyclicalCosineDecay
class
Linear
(
object
):
...
...
@@ -46,7 +47,7 @@ class Linear(object):
self
.
end_lr
=
end_lr
self
.
power
=
power
self
.
last_epoch
=
last_epoch
self
.
warmup_epoch
=
warmup_epoch
*
step_each_epoch
self
.
warmup_epoch
=
round
(
warmup_epoch
*
step_each_epoch
)
def
__call__
(
self
):
learning_rate
=
lr
.
PolynomialDecay
(
...
...
@@ -87,7 +88,7 @@ class Cosine(object):
self
.
learning_rate
=
learning_rate
self
.
T_max
=
step_each_epoch
*
epochs
self
.
last_epoch
=
last_epoch
self
.
warmup_epoch
=
warmup_epoch
*
step_each_epoch
self
.
warmup_epoch
=
round
(
warmup_epoch
*
step_each_epoch
)
def
__call__
(
self
):
learning_rate
=
lr
.
CosineAnnealingDecay
(
...
...
@@ -129,7 +130,7 @@ class Step(object):
self
.
learning_rate
=
learning_rate
self
.
gamma
=
gamma
self
.
last_epoch
=
last_epoch
self
.
warmup_epoch
=
warmup_epoch
*
step_each_epoch
self
.
warmup_epoch
=
round
(
warmup_epoch
*
step_each_epoch
)
def
__call__
(
self
):
learning_rate
=
lr
.
StepDecay
(
...
...
@@ -168,7 +169,7 @@ class Piecewise(object):
self
.
boundaries
=
[
step_each_epoch
*
e
for
e
in
decay_epochs
]
self
.
values
=
values
self
.
last_epoch
=
last_epoch
self
.
warmup_epoch
=
warmup_epoch
*
step_each_epoch
self
.
warmup_epoch
=
round
(
warmup_epoch
*
step_each_epoch
)
def
__call__
(
self
):
learning_rate
=
lr
.
PiecewiseDecay
(
...
...
@@ -183,3 +184,45 @@ class Piecewise(object):
end_lr
=
self
.
values
[
0
],
last_epoch
=
self
.
last_epoch
)
return
learning_rate
class
CyclicalCosine
(
object
):
"""
Cyclical cosine learning rate decay
Args:
learning_rate(float): initial learning rate
step_each_epoch(int): steps each epoch
epochs(int): total training epochs
cycle(int): period of the cosine learning rate
last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
"""
def
__init__
(
self
,
learning_rate
,
step_each_epoch
,
epochs
,
cycle
,
warmup_epoch
=
0
,
last_epoch
=-
1
,
**
kwargs
):
super
(
CyclicalCosine
,
self
).
__init__
()
self
.
learning_rate
=
learning_rate
self
.
T_max
=
step_each_epoch
*
epochs
self
.
last_epoch
=
last_epoch
self
.
warmup_epoch
=
round
(
warmup_epoch
*
step_each_epoch
)
self
.
cycle
=
round
(
cycle
*
step_each_epoch
)
def
__call__
(
self
):
learning_rate
=
CyclicalCosineDecay
(
learning_rate
=
self
.
learning_rate
,
T_max
=
self
.
T_max
,
cycle
=
self
.
cycle
,
last_epoch
=
self
.
last_epoch
)
if
self
.
warmup_epoch
>
0
:
learning_rate
=
lr
.
LinearWarmup
(
learning_rate
=
learning_rate
,
warmup_steps
=
self
.
warmup_epoch
,
start_lr
=
0.0
,
end_lr
=
self
.
learning_rate
,
last_epoch
=
self
.
last_epoch
)
return
learning_rate
ppocr/optimizer/lr_scheduler.py
0 → 100644
View file @
ec0de454
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
math
from
paddle.optimizer.lr
import
LRScheduler
class
CyclicalCosineDecay
(
LRScheduler
):
def
__init__
(
self
,
learning_rate
,
T_max
,
cycle
=
1
,
last_epoch
=-
1
,
eta_min
=
0.0
,
verbose
=
False
):
"""
Cyclical cosine learning rate decay
A learning rate which can be referred in https://arxiv.org/pdf/2012.12645.pdf
Args:
learning rate(float): learning rate
T_max(int): maximum epoch num
cycle(int): period of the cosine decay
last_epoch (int, optional): The index of last epoch. Can be set to restart training. Default: -1, means initial learning rate.
eta_min(float): minimum learning rate during training
verbose(bool): whether to print learning rate for each epoch
"""
super
(
CyclicalCosineDecay
,
self
).
__init__
(
learning_rate
,
last_epoch
,
verbose
)
self
.
cycle
=
cycle
self
.
eta_min
=
eta_min
def
get_lr
(
self
):
if
self
.
last_epoch
==
0
:
return
self
.
base_lr
reletive_epoch
=
self
.
last_epoch
%
self
.
cycle
lr
=
self
.
eta_min
+
0.5
*
(
self
.
base_lr
-
self
.
eta_min
)
*
\
(
1
+
math
.
cos
(
math
.
pi
*
reletive_epoch
/
self
.
cycle
))
return
lr
setup.py
View file @
ec0de454
...
...
@@ -32,7 +32,7 @@ setup(
package_dir
=
{
'paddleocr'
:
''
},
include_package_data
=
True
,
entry_points
=
{
"console_scripts"
:
[
"paddleocr= paddleocr.paddleocr:main"
]},
version
=
'2.0.
1
'
,
version
=
'2.0.
2
'
,
install_requires
=
requirements
,
license
=
'Apache License 2.0'
,
description
=
'Awesome OCR toolkits based on PaddlePaddle (8.6M ultra-lightweight pre-trained model, support training and deployment among server, mobile, embeded and IoT devices'
,
...
...
tools/infer/predict_cls.py
View file @
ec0de454
...
...
@@ -18,13 +18,14 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
cv2
import
copy
import
numpy
as
np
import
math
import
time
import
traceback
import
paddle.fluid
as
fluid
import
tools.infer.utility
as
utility
from
ppocr.postprocess
import
build_post_process
...
...
@@ -39,7 +40,6 @@ class TextClassifier(object):
self
.
cls_image_shape
=
[
int
(
v
)
for
v
in
args
.
cls_image_shape
.
split
(
","
)]
self
.
cls_batch_num
=
args
.
cls_batch_num
self
.
cls_thresh
=
args
.
cls_thresh
self
.
use_zero_copy_run
=
args
.
use_zero_copy_run
postprocess_params
=
{
'name'
:
'ClsPostProcess'
,
"label_list"
:
args
.
label_list
,
...
...
@@ -99,12 +99,8 @@ class TextClassifier(object):
norm_img_batch
=
norm_img_batch
.
copy
()
starttime
=
time
.
time
()
if
self
.
use_zero_copy_run
:
self
.
input_tensor
.
copy_from_cpu
(
norm_img_batch
)
self
.
predictor
.
zero_copy_run
()
else
:
norm_img_batch
=
fluid
.
core
.
PaddleTensor
(
norm_img_batch
)
self
.
predictor
.
run
([
norm_img_batch
])
self
.
input_tensor
.
copy_from_cpu
(
norm_img_batch
)
self
.
predictor
.
run
()
prob_out
=
self
.
output_tensors
[
0
].
copy_to_cpu
()
cls_result
=
self
.
postprocess_op
(
prob_out
)
elapse
+=
time
.
time
()
-
starttime
...
...
@@ -143,10 +139,11 @@ def main(args):
"Please set --rec_image_shape='3,32,100' and --rec_char_type='en' "
)
exit
()
for
ino
in
range
(
len
(
img_list
)):
logger
.
info
(
"Predicts of {}:{}"
.
format
(
valid_image_file_list
[
ino
],
cls_res
[
ino
]))
logger
.
info
(
"Predicts of {}:{}"
.
format
(
valid_image_file_list
[
ino
],
cls_res
[
ino
]))
logger
.
info
(
"Total predict time for {} images, cost: {:.3f}"
.
format
(
len
(
img_list
),
predict_time
))
if
__name__
==
"__main__"
:
main
(
utility
.
parse_args
())
tools/infer/predict_det.py
View file @
ec0de454
...
...
@@ -18,11 +18,12 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
cv2
import
numpy
as
np
import
time
import
sys
import
paddle
import
tools.infer.utility
as
utility
from
ppocr.utils.logging
import
get_logger
...
...
@@ -35,8 +36,8 @@ logger = get_logger()
class
TextDetector
(
object
):
def
__init__
(
self
,
args
):
self
.
args
=
args
self
.
det_algorithm
=
args
.
det_algorithm
self
.
use_zero_copy_run
=
args
.
use_zero_copy_run
pre_process_list
=
[{
'DetResizeForTest'
:
{
'limit_side_len'
:
args
.
det_limit_side_len
,
...
...
@@ -70,6 +71,11 @@ class TextDetector(object):
postprocess_params
[
"cover_thresh"
]
=
args
.
det_east_cover_thresh
postprocess_params
[
"nms_thresh"
]
=
args
.
det_east_nms_thresh
elif
self
.
det_algorithm
==
"SAST"
:
pre_process_list
[
0
]
=
{
'DetResizeForTest'
:
{
'resize_long'
:
args
.
det_limit_side_len
}
}
postprocess_params
[
'name'
]
=
'SASTPostProcess'
postprocess_params
[
"score_thresh"
]
=
args
.
det_sast_score_thresh
postprocess_params
[
"nms_thresh"
]
=
args
.
det_sast_nms_thresh
...
...
@@ -157,12 +163,8 @@ class TextDetector(object):
img
=
img
.
copy
()
starttime
=
time
.
time
()
if
self
.
use_zero_copy_run
:
self
.
input_tensor
.
copy_from_cpu
(
img
)
self
.
predictor
.
zero_copy_run
()
else
:
im
=
paddle
.
fluid
.
core
.
PaddleTensor
(
img
)
self
.
predictor
.
run
([
im
])
self
.
input_tensor
.
copy_from_cpu
(
img
)
self
.
predictor
.
run
()
outputs
=
[]
for
output_tensor
in
self
.
output_tensors
:
output
=
output_tensor
.
copy_to_cpu
()
...
...
tools/infer/predict_rec.py
View file @
ec0de454
...
...
@@ -18,12 +18,13 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
cv2
import
numpy
as
np
import
math
import
time
import
traceback
import
paddle.fluid
as
fluid
import
tools.infer.utility
as
utility
from
ppocr.postprocess
import
build_post_process
...
...
@@ -39,7 +40,6 @@ class TextRecognizer(object):
self
.
character_type
=
args
.
rec_char_type
self
.
rec_batch_num
=
args
.
rec_batch_num
self
.
rec_algorithm
=
args
.
rec_algorithm
self
.
use_zero_copy_run
=
args
.
use_zero_copy_run
postprocess_params
=
{
'name'
:
'CTCLabelDecode'
,
"character_type"
:
args
.
rec_char_type
,
...
...
@@ -101,12 +101,8 @@ class TextRecognizer(object):
norm_img_batch
=
np
.
concatenate
(
norm_img_batch
)
norm_img_batch
=
norm_img_batch
.
copy
()
starttime
=
time
.
time
()
if
self
.
use_zero_copy_run
:
self
.
input_tensor
.
copy_from_cpu
(
norm_img_batch
)
self
.
predictor
.
zero_copy_run
()
else
:
norm_img_batch
=
fluid
.
core
.
PaddleTensor
(
norm_img_batch
)
self
.
predictor
.
run
([
norm_img_batch
])
self
.
input_tensor
.
copy_from_cpu
(
norm_img_batch
)
self
.
predictor
.
run
()
outputs
=
[]
for
output_tensor
in
self
.
output_tensors
:
output
=
output_tensor
.
copy_to_cpu
()
...
...
@@ -145,8 +141,8 @@ def main(args):
"Please set --rec_image_shape='3,32,100' and --rec_char_type='en' "
)
exit
()
for
ino
in
range
(
len
(
img_list
)):
logger
.
info
(
"Predicts of {}:{}"
.
format
(
valid_image_file_list
[
ino
],
rec_res
[
ino
]))
logger
.
info
(
"Predicts of {}:{}"
.
format
(
valid_image_file_list
[
ino
],
rec_res
[
ino
]))
logger
.
info
(
"Total predict time for {} images, cost: {:.3f}"
.
format
(
len
(
img_list
),
predict_time
))
...
...
tools/infer/predict_system.py
View file @
ec0de454
...
...
@@ -18,6 +18,8 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'../..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
cv2
import
copy
import
numpy
as
np
...
...
tools/infer/utility.py
View file @
ec0de454
...
...
@@ -20,8 +20,7 @@ import numpy as np
import
json
from
PIL
import
Image
,
ImageDraw
,
ImageFont
import
math
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
create_paddle_predictor
from
paddle
import
inference
def
parse_args
():
...
...
@@ -33,7 +32,8 @@ def parse_args():
parser
.
add_argument
(
"--use_gpu"
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
"--ir_optim"
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
"--use_tensorrt"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--gpu_mem"
,
type
=
int
,
default
=
8000
)
parser
.
add_argument
(
"--use_fp16"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--gpu_mem"
,
type
=
int
,
default
=
500
)
# params for text detector
parser
.
add_argument
(
"--image_dir"
,
type
=
str
)
...
...
@@ -46,7 +46,7 @@ def parse_args():
parser
.
add_argument
(
"--det_db_thresh"
,
type
=
float
,
default
=
0.3
)
parser
.
add_argument
(
"--det_db_box_thresh"
,
type
=
float
,
default
=
0.5
)
parser
.
add_argument
(
"--det_db_unclip_ratio"
,
type
=
float
,
default
=
1.6
)
parser
.
add_argument
(
"--max_batch_size"
,
type
=
int
,
default
=
10
)
# EAST parmas
parser
.
add_argument
(
"--det_east_score_thresh"
,
type
=
float
,
default
=
0.8
)
parser
.
add_argument
(
"--det_east_cover_thresh"
,
type
=
float
,
default
=
0.1
)
...
...
@@ -78,12 +78,10 @@ def parse_args():
parser
.
add_argument
(
"--cls_model_dir"
,
type
=
str
)
parser
.
add_argument
(
"--cls_image_shape"
,
type
=
str
,
default
=
"3, 48, 192"
)
parser
.
add_argument
(
"--label_list"
,
type
=
list
,
default
=
[
'0'
,
'180'
])
parser
.
add_argument
(
"--cls_batch_num"
,
type
=
int
,
default
=
30
)
parser
.
add_argument
(
"--cls_batch_num"
,
type
=
int
,
default
=
6
)
parser
.
add_argument
(
"--cls_thresh"
,
type
=
float
,
default
=
0.9
)
parser
.
add_argument
(
"--enable_mkldnn"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--use_zero_copy_run"
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
"--use_pdserving"
,
type
=
str2bool
,
default
=
False
)
return
parser
.
parse_args
()
...
...
@@ -109,10 +107,15 @@ def create_predictor(args, mode, logger):
logger
.
info
(
"not find params file path {}"
.
format
(
params_file_path
))
sys
.
exit
(
0
)
config
=
Analysis
Config
(
model_file_path
,
params_file_path
)
config
=
inference
.
Config
(
model_file_path
,
params_file_path
)
if
args
.
use_gpu
:
config
.
enable_use_gpu
(
args
.
gpu_mem
,
0
)
if
args
.
use_tensorrt
:
config
.
enable_tensorrt_engine
(
precision_mode
=
inference
.
PrecisionType
.
Half
if
args
.
use_fp16
else
inference
.
PrecisionType
.
Float32
,
max_batch_size
=
args
.
max_batch_size
)
else
:
config
.
disable_gpu
()
config
.
set_cpu_math_library_num_threads
(
6
)
...
...
@@ -120,24 +123,23 @@ def create_predictor(args, mode, logger):
# cache 10 different shapes for mkldnn to avoid memory leak
config
.
set_mkldnn_cache_capacity
(
10
)
config
.
enable_mkldnn
()
args
.
rec_batch_num
=
1
# config.enable_memory_optim()
config
.
disable_glog_info
()
if
args
.
use_zero_copy_run
:
config
.
delete_pass
(
"conv_transpose_eltwiseadd_bn_fuse_pass"
)
config
.
switch_use_feed_fetch_ops
(
False
)
else
:
config
.
switch_use_feed_fetch_ops
(
True
)
config
.
delete_pass
(
"conv_transpose_eltwiseadd_bn_fuse_pass"
)
config
.
switch_use_feed_fetch_ops
(
False
)
predictor
=
create_paddle_predictor
(
config
)
# create predictor
predictor
=
inference
.
create_predictor
(
config
)
input_names
=
predictor
.
get_input_names
()
for
name
in
input_names
:
input_tensor
=
predictor
.
get_input_
tensor
(
name
)
input_tensor
=
predictor
.
get_input_
handle
(
name
)
output_names
=
predictor
.
get_output_names
()
output_tensors
=
[]
for
output_name
in
output_names
:
output_tensor
=
predictor
.
get_output_
tensor
(
output_name
)
output_tensor
=
predictor
.
get_output_
handle
(
output_name
)
output_tensors
.
append
(
output_tensor
)
return
predictor
,
input_tensor
,
output_tensors
...
...
tools/infer_cls.py
View file @
ec0de454
...
...
@@ -25,6 +25,8 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
paddle
from
ppocr.data
import
create_operators
,
transform
...
...
tools/infer_det.py
View file @
ec0de454
...
...
@@ -25,6 +25,8 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
cv2
import
json
import
paddle
...
...
tools/infer_rec.py
View file @
ec0de454
...
...
@@ -25,6 +25,8 @@ __dir__ = os.path.dirname(os.path.abspath(__file__))
sys
.
path
.
append
(
__dir__
)
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
__dir__
,
'..'
)))
os
.
environ
[
"FLAGS_allocator_strategy"
]
=
'auto_growth'
import
paddle
from
ppocr.data
import
create_operators
,
transform
...
...
tools/program.py
View file @
ec0de454
...
...
@@ -131,7 +131,7 @@ def check_gpu(use_gpu):
"model on CPU"
try
:
if
use_gpu
and
not
paddle
.
fluid
.
is_compiled_with_cuda
():
if
use_gpu
and
not
paddle
.
is_compiled_with_cuda
():
print
(
err
)
sys
.
exit
(
1
)
except
Exception
as
e
:
...
...
@@ -179,9 +179,9 @@ def train(config,
if
'start_epoch'
in
best_model_dict
:
start_epoch
=
best_model_dict
[
'start_epoch'
]
else
:
start_epoch
=
0
start_epoch
=
1
for
epoch
in
range
(
start_epoch
,
epoch_num
):
for
epoch
in
range
(
start_epoch
,
epoch_num
+
1
):
if
epoch
>
0
:
train_dataloader
=
build_dataloader
(
config
,
'Train'
,
device
,
logger
)
train_batch_cost
=
0.0
...
...
@@ -332,7 +332,7 @@ def eval(model, valid_dataloader, post_process_class, eval_class):
return
metirc
def
preprocess
():
def
preprocess
(
is_train
=
False
):
FLAGS
=
ArgsParser
().
parse_args
()
config
=
load_config
(
FLAGS
.
config
)
merge_config
(
FLAGS
.
opt
)
...
...
@@ -350,15 +350,17 @@ def preprocess():
device
=
paddle
.
set_device
(
device
)
config
[
'Global'
][
'distributed'
]
=
dist
.
get_world_size
()
!=
1
# save_config
save_model_dir
=
config
[
'Global'
][
'save_model_dir'
]
os
.
makedirs
(
save_model_dir
,
exist_ok
=
True
)
with
open
(
os
.
path
.
join
(
save_model_dir
,
'config.yml'
),
'w'
)
as
f
:
yaml
.
dump
(
dict
(
config
),
f
,
default_flow_style
=
False
,
sort_keys
=
False
)
logger
=
get_logger
(
name
=
'root'
,
log_file
=
'{}/train.log'
.
format
(
save_model_dir
))
if
is_train
:
# save_config
save_model_dir
=
config
[
'Global'
][
'save_model_dir'
]
os
.
makedirs
(
save_model_dir
,
exist_ok
=
True
)
with
open
(
os
.
path
.
join
(
save_model_dir
,
'config.yml'
),
'w'
)
as
f
:
yaml
.
dump
(
dict
(
config
),
f
,
default_flow_style
=
False
,
sort_keys
=
False
)
log_file
=
'{}/train.log'
.
format
(
save_model_dir
)
else
:
log_file
=
None
logger
=
get_logger
(
name
=
'root'
,
log_file
=
log_file
)
if
config
[
'Global'
][
'use_visualdl'
]:
from
visualdl
import
LogWriter
vdl_writer_path
=
'{}/vdl/'
.
format
(
save_model_dir
)
...
...
tools/train.py
View file @
ec0de454
...
...
@@ -110,6 +110,6 @@ def test_reader(config, device, logger):
if
__name__
==
'__main__'
:
config
,
device
,
logger
,
vdl_writer
=
program
.
preprocess
()
config
,
device
,
logger
,
vdl_writer
=
program
.
preprocess
(
is_train
=
True
)
main
(
config
,
device
,
logger
,
vdl_writer
)
# test_reader(config, device, logger)
Prev
1
2
3
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment