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
6c7ff9c7
Commit
6c7ff9c7
authored
Aug 05, 2021
by
LDOUBLEV
Browse files
fix conflict
parents
ac91a9e1
9b8f587e
Changes
139
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
1526 additions
and
86 deletions
+1526
-86
ppocr/losses/combined_loss.py
ppocr/losses/combined_loss.py
+59
-0
ppocr/losses/distillation_loss.py
ppocr/losses/distillation_loss.py
+270
-0
ppocr/losses/rec_ctc_loss.py
ppocr/losses/rec_ctc_loss.py
+1
-1
ppocr/losses/table_att_loss.py
ppocr/losses/table_att_loss.py
+109
-0
ppocr/metrics/__init__.py
ppocr/metrics/__init__.py
+12
-9
ppocr/metrics/det_metric.py
ppocr/metrics/det_metric.py
+1
-0
ppocr/metrics/distillation_metric.py
ppocr/metrics/distillation_metric.py
+73
-0
ppocr/metrics/table_metric.py
ppocr/metrics/table_metric.py
+50
-0
ppocr/modeling/architectures/__init__.py
ppocr/modeling/architectures/__init__.py
+12
-4
ppocr/modeling/architectures/base_model.py
ppocr/modeling/architectures/base_model.py
+14
-6
ppocr/modeling/architectures/distillation_model.py
ppocr/modeling/architectures/distillation_model.py
+60
-0
ppocr/modeling/backbones/__init__.py
ppocr/modeling/backbones/__init__.py
+16
-9
ppocr/modeling/backbones/det_mobilenet_v3.py
ppocr/modeling/backbones/det_mobilenet_v3.py
+13
-32
ppocr/modeling/backbones/rec_mobilenet_v3.py
ppocr/modeling/backbones/rec_mobilenet_v3.py
+3
-6
ppocr/modeling/backbones/rec_mv1_enhance.py
ppocr/modeling/backbones/rec_mv1_enhance.py
+256
-0
ppocr/modeling/backbones/table_mobilenet_v3.py
ppocr/modeling/backbones/table_mobilenet_v3.py
+287
-0
ppocr/modeling/backbones/table_resnet_vd.py
ppocr/modeling/backbones/table_resnet_vd.py
+280
-0
ppocr/modeling/heads/__init__.py
ppocr/modeling/heads/__init__.py
+3
-1
ppocr/modeling/heads/cls_head.py
ppocr/modeling/heads/cls_head.py
+1
-1
ppocr/modeling/heads/det_db_head.py
ppocr/modeling/heads/det_db_head.py
+6
-17
No files found.
ppocr/losses/combined_loss.py
0 → 100644
View file @
6c7ff9c7
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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
paddle
import
paddle.nn
as
nn
from
.distillation_loss
import
DistillationCTCLoss
from
.distillation_loss
import
DistillationDMLLoss
from
.distillation_loss
import
DistillationDistanceLoss
,
DistillationDBLoss
,
DistillationDilaDBLoss
class
CombinedLoss
(
nn
.
Layer
):
"""
CombinedLoss:
a combionation of loss function
"""
def
__init__
(
self
,
loss_config_list
=
None
):
super
().
__init__
()
self
.
loss_func
=
[]
self
.
loss_weight
=
[]
assert
isinstance
(
loss_config_list
,
list
),
(
'operator config should be a list'
)
for
config
in
loss_config_list
:
assert
isinstance
(
config
,
dict
)
and
len
(
config
)
==
1
,
"yaml format error"
name
=
list
(
config
)[
0
]
param
=
config
[
name
]
assert
"weight"
in
param
,
"weight must be in param, but param just contains {}"
.
format
(
param
.
keys
())
self
.
loss_weight
.
append
(
param
.
pop
(
"weight"
))
self
.
loss_func
.
append
(
eval
(
name
)(
**
param
))
def
forward
(
self
,
input
,
batch
,
**
kargs
):
loss_dict
=
{}
loss_all
=
0.
for
idx
,
loss_func
in
enumerate
(
self
.
loss_func
):
loss
=
loss_func
(
input
,
batch
,
**
kargs
)
if
isinstance
(
loss
,
paddle
.
Tensor
):
loss
=
{
"loss_{}_{}"
.
format
(
str
(
loss
),
idx
):
loss
}
weight
=
self
.
loss_weight
[
idx
]
for
key
in
loss
.
keys
():
if
key
==
"loss"
:
loss_all
+=
loss
[
key
]
*
weight
else
:
loss_dict
[
"{}_{}"
.
format
(
key
,
idx
)]
=
loss
[
key
]
loss_dict
[
"loss"
]
=
loss_all
return
loss_dict
ppocr/losses/distillation_loss.py
0 → 100644
View file @
6c7ff9c7
#copyright (c) 2021 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
paddle
import
paddle.nn
as
nn
import
numpy
as
np
import
cv2
from
.rec_ctc_loss
import
CTCLoss
from
.basic_loss
import
DMLLoss
from
.basic_loss
import
DistanceLoss
from
.det_db_loss
import
DBLoss
from
.det_basic_loss
import
BalanceLoss
,
MaskL1Loss
,
DiceLoss
def
_sum_loss
(
loss_dict
):
if
"loss"
in
loss_dict
.
keys
():
return
loss_dict
else
:
loss_dict
[
"loss"
]
=
0.
for
k
,
value
in
loss_dict
.
items
():
if
k
==
"loss"
:
continue
else
:
loss_dict
[
"loss"
]
+=
value
return
loss_dict
class
DistillationDMLLoss
(
DMLLoss
):
"""
"""
def
__init__
(
self
,
model_name_pairs
=
[],
act
=
None
,
key
=
None
,
maps_name
=
None
,
name
=
"dml"
):
super
().
__init__
(
act
=
act
)
assert
isinstance
(
model_name_pairs
,
list
)
self
.
key
=
key
self
.
model_name_pairs
=
self
.
_check_model_name_pairs
(
model_name_pairs
)
self
.
name
=
name
self
.
maps_name
=
self
.
_check_maps_name
(
maps_name
)
def
_check_model_name_pairs
(
self
,
model_name_pairs
):
if
not
isinstance
(
model_name_pairs
,
list
):
return
[]
elif
isinstance
(
model_name_pairs
[
0
],
list
)
and
isinstance
(
model_name_pairs
[
0
][
0
],
str
):
return
model_name_pairs
else
:
return
[
model_name_pairs
]
def
_check_maps_name
(
self
,
maps_name
):
if
maps_name
is
None
:
return
None
elif
type
(
maps_name
)
==
str
:
return
[
maps_name
]
elif
type
(
maps_name
)
==
list
:
return
[
maps_name
]
else
:
return
None
def
_slice_out
(
self
,
outs
):
new_outs
=
{}
for
k
in
self
.
maps_name
:
if
k
==
"thrink_maps"
:
new_outs
[
k
]
=
outs
[:,
0
,
:,
:]
elif
k
==
"threshold_maps"
:
new_outs
[
k
]
=
outs
[:,
1
,
:,
:]
elif
k
==
"binary_maps"
:
new_outs
[
k
]
=
outs
[:,
2
,
:,
:]
else
:
continue
return
new_outs
def
forward
(
self
,
predicts
,
batch
):
loss_dict
=
dict
()
for
idx
,
pair
in
enumerate
(
self
.
model_name_pairs
):
out1
=
predicts
[
pair
[
0
]]
out2
=
predicts
[
pair
[
1
]]
if
self
.
key
is
not
None
:
out1
=
out1
[
self
.
key
]
out2
=
out2
[
self
.
key
]
if
self
.
maps_name
is
None
:
loss
=
super
().
forward
(
out1
,
out2
)
if
isinstance
(
loss
,
dict
):
for
key
in
loss
:
loss_dict
[
"{}_{}_{}_{}"
.
format
(
key
,
pair
[
0
],
pair
[
1
],
idx
)]
=
loss
[
key
]
else
:
loss_dict
[
"{}_{}"
.
format
(
self
.
name
,
idx
)]
=
loss
else
:
outs1
=
self
.
_slice_out
(
out1
)
outs2
=
self
.
_slice_out
(
out2
)
for
_c
,
k
in
enumerate
(
outs1
.
keys
()):
loss
=
super
().
forward
(
outs1
[
k
],
outs2
[
k
])
if
isinstance
(
loss
,
dict
):
for
key
in
loss
:
loss_dict
[
"{}_{}_{}_{}_{}"
.
format
(
key
,
pair
[
0
],
pair
[
1
],
map_name
,
idx
)]
=
loss
[
key
]
else
:
loss_dict
[
"{}_{}_{}"
.
format
(
self
.
name
,
self
.
maps_name
[
_c
],
idx
)]
=
loss
loss_dict
=
_sum_loss
(
loss_dict
)
return
loss_dict
class
DistillationCTCLoss
(
CTCLoss
):
def
__init__
(
self
,
model_name_list
=
[],
key
=
None
,
name
=
"loss_ctc"
):
super
().
__init__
()
self
.
model_name_list
=
model_name_list
self
.
key
=
key
self
.
name
=
name
def
forward
(
self
,
predicts
,
batch
):
loss_dict
=
dict
()
for
idx
,
model_name
in
enumerate
(
self
.
model_name_list
):
out
=
predicts
[
model_name
]
if
self
.
key
is
not
None
:
out
=
out
[
self
.
key
]
loss
=
super
().
forward
(
out
,
batch
)
if
isinstance
(
loss
,
dict
):
for
key
in
loss
:
loss_dict
[
"{}_{}_{}"
.
format
(
self
.
name
,
model_name
,
idx
)]
=
loss
[
key
]
else
:
loss_dict
[
"{}_{}"
.
format
(
self
.
name
,
model_name
)]
=
loss
return
loss_dict
class
DistillationDBLoss
(
DBLoss
):
def
__init__
(
self
,
model_name_list
=
[],
balance_loss
=
True
,
main_loss_type
=
'DiceLoss'
,
alpha
=
5
,
beta
=
10
,
ohem_ratio
=
3
,
eps
=
1e-6
,
name
=
"db"
,
**
kwargs
):
super
().
__init__
()
self
.
model_name_list
=
model_name_list
self
.
name
=
name
self
.
key
=
None
def
forward
(
self
,
predicts
,
batch
):
loss_dict
=
{}
for
idx
,
model_name
in
enumerate
(
self
.
model_name_list
):
out
=
predicts
[
model_name
]
if
self
.
key
is
not
None
:
out
=
out
[
self
.
key
]
loss
=
super
().
forward
(
out
,
batch
)
if
isinstance
(
loss
,
dict
):
for
key
in
loss
.
keys
():
if
key
==
"loss"
:
continue
name
=
"{}_{}_{}"
.
format
(
self
.
name
,
model_name
,
key
)
loss_dict
[
name
]
=
loss
[
key
]
else
:
loss_dict
[
"{}_{}"
.
format
(
self
.
name
,
model_name
)]
=
loss
loss_dict
=
_sum_loss
(
loss_dict
)
return
loss_dict
class
DistillationDilaDBLoss
(
DBLoss
):
def
__init__
(
self
,
model_name_pairs
=
[],
key
=
None
,
balance_loss
=
True
,
main_loss_type
=
'DiceLoss'
,
alpha
=
5
,
beta
=
10
,
ohem_ratio
=
3
,
eps
=
1e-6
,
name
=
"dila_dbloss"
):
super
().
__init__
()
self
.
model_name_pairs
=
model_name_pairs
self
.
name
=
name
self
.
key
=
key
def
forward
(
self
,
predicts
,
batch
):
loss_dict
=
dict
()
for
idx
,
pair
in
enumerate
(
self
.
model_name_pairs
):
stu_outs
=
predicts
[
pair
[
0
]]
tch_outs
=
predicts
[
pair
[
1
]]
if
self
.
key
is
not
None
:
stu_preds
=
stu_outs
[
self
.
key
]
tch_preds
=
tch_outs
[
self
.
key
]
stu_shrink_maps
=
stu_preds
[:,
0
,
:,
:]
stu_binary_maps
=
stu_preds
[:,
2
,
:,
:]
# dilation to teacher prediction
dilation_w
=
np
.
array
([[
1
,
1
],
[
1
,
1
]])
th_shrink_maps
=
tch_preds
[:,
0
,
:,
:]
th_shrink_maps
=
th_shrink_maps
.
numpy
()
>
0.3
# thresh = 0.3
dilate_maps
=
np
.
zeros_like
(
th_shrink_maps
).
astype
(
np
.
float32
)
for
i
in
range
(
th_shrink_maps
.
shape
[
0
]):
dilate_maps
[
i
]
=
cv2
.
dilate
(
th_shrink_maps
[
i
,
:,
:].
astype
(
np
.
uint8
),
dilation_w
)
th_shrink_maps
=
paddle
.
to_tensor
(
dilate_maps
)
label_threshold_map
,
label_threshold_mask
,
label_shrink_map
,
label_shrink_mask
=
batch
[
1
:]
# calculate the shrink map loss
bce_loss
=
self
.
alpha
*
self
.
bce_loss
(
stu_shrink_maps
,
th_shrink_maps
,
label_shrink_mask
)
loss_binary_maps
=
self
.
dice_loss
(
stu_binary_maps
,
th_shrink_maps
,
label_shrink_mask
)
# k = f"{self.name}_{pair[0]}_{pair[1]}"
k
=
"{}_{}_{}"
.
format
(
self
.
name
,
pair
[
0
],
pair
[
1
])
loss_dict
[
k
]
=
bce_loss
+
loss_binary_maps
loss_dict
=
_sum_loss
(
loss_dict
)
return
loss_dict
class
DistillationDistanceLoss
(
DistanceLoss
):
"""
"""
def
__init__
(
self
,
mode
=
"l2"
,
model_name_pairs
=
[],
key
=
None
,
name
=
"loss_distance"
,
**
kargs
):
super
().
__init__
(
mode
=
mode
,
**
kargs
)
assert
isinstance
(
model_name_pairs
,
list
)
self
.
key
=
key
self
.
model_name_pairs
=
model_name_pairs
self
.
name
=
name
+
"_l2"
def
forward
(
self
,
predicts
,
batch
):
loss_dict
=
dict
()
for
idx
,
pair
in
enumerate
(
self
.
model_name_pairs
):
out1
=
predicts
[
pair
[
0
]]
out2
=
predicts
[
pair
[
1
]]
if
self
.
key
is
not
None
:
out1
=
out1
[
self
.
key
]
out2
=
out2
[
self
.
key
]
loss
=
super
().
forward
(
out1
,
out2
)
if
isinstance
(
loss
,
dict
):
for
key
in
loss
:
loss_dict
[
"{}_{}_{}"
.
format
(
self
.
name
,
key
,
idx
)]
=
loss
[
key
]
else
:
loss_dict
[
"{}_{}_{}_{}"
.
format
(
self
.
name
,
pair
[
0
],
pair
[
1
],
idx
)]
=
loss
return
loss_dict
ppocr/losses/rec_ctc_loss.py
View file @
6c7ff9c7
...
@@ -25,7 +25,7 @@ class CTCLoss(nn.Layer):
...
@@ -25,7 +25,7 @@ class CTCLoss(nn.Layer):
super
(
CTCLoss
,
self
).
__init__
()
super
(
CTCLoss
,
self
).
__init__
()
self
.
loss_func
=
nn
.
CTCLoss
(
blank
=
0
,
reduction
=
'none'
)
self
.
loss_func
=
nn
.
CTCLoss
(
blank
=
0
,
reduction
=
'none'
)
def
__call__
(
self
,
predicts
,
batch
):
def
forward
(
self
,
predicts
,
batch
):
predicts
=
predicts
.
transpose
((
1
,
0
,
2
))
predicts
=
predicts
.
transpose
((
1
,
0
,
2
))
N
,
B
,
_
=
predicts
.
shape
N
,
B
,
_
=
predicts
.
shape
preds_lengths
=
paddle
.
to_tensor
([
N
]
*
B
,
dtype
=
'int64'
)
preds_lengths
=
paddle
.
to_tensor
([
N
]
*
B
,
dtype
=
'int64'
)
...
...
ppocr/losses/table_att_loss.py
0 → 100644
View file @
6c7ff9c7
# copyright (c) 2021 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.
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle
from
paddle
import
nn
from
paddle.nn
import
functional
as
F
from
paddle
import
fluid
class
TableAttentionLoss
(
nn
.
Layer
):
def
__init__
(
self
,
structure_weight
,
loc_weight
,
use_giou
=
False
,
giou_weight
=
1.0
,
**
kwargs
):
super
(
TableAttentionLoss
,
self
).
__init__
()
self
.
loss_func
=
nn
.
CrossEntropyLoss
(
weight
=
None
,
reduction
=
'none'
)
self
.
structure_weight
=
structure_weight
self
.
loc_weight
=
loc_weight
self
.
use_giou
=
use_giou
self
.
giou_weight
=
giou_weight
def
giou_loss
(
self
,
preds
,
bbox
,
eps
=
1e-7
,
reduction
=
'mean'
):
'''
:param preds:[[x1,y1,x2,y2], [x1,y1,x2,y2],,,]
:param bbox:[[x1,y1,x2,y2], [x1,y1,x2,y2],,,]
:return: loss
'''
ix1
=
fluid
.
layers
.
elementwise_max
(
preds
[:,
0
],
bbox
[:,
0
])
iy1
=
fluid
.
layers
.
elementwise_max
(
preds
[:,
1
],
bbox
[:,
1
])
ix2
=
fluid
.
layers
.
elementwise_min
(
preds
[:,
2
],
bbox
[:,
2
])
iy2
=
fluid
.
layers
.
elementwise_min
(
preds
[:,
3
],
bbox
[:,
3
])
iw
=
fluid
.
layers
.
clip
(
ix2
-
ix1
+
1e-3
,
0.
,
1e10
)
ih
=
fluid
.
layers
.
clip
(
iy2
-
iy1
+
1e-3
,
0.
,
1e10
)
# overlap
inters
=
iw
*
ih
# union
uni
=
(
preds
[:,
2
]
-
preds
[:,
0
]
+
1e-3
)
*
(
preds
[:,
3
]
-
preds
[:,
1
]
+
1e-3
)
+
(
bbox
[:,
2
]
-
bbox
[:,
0
]
+
1e-3
)
*
(
bbox
[:,
3
]
-
bbox
[:,
1
]
+
1e-3
)
-
inters
+
eps
# ious
ious
=
inters
/
uni
ex1
=
fluid
.
layers
.
elementwise_min
(
preds
[:,
0
],
bbox
[:,
0
])
ey1
=
fluid
.
layers
.
elementwise_min
(
preds
[:,
1
],
bbox
[:,
1
])
ex2
=
fluid
.
layers
.
elementwise_max
(
preds
[:,
2
],
bbox
[:,
2
])
ey2
=
fluid
.
layers
.
elementwise_max
(
preds
[:,
3
],
bbox
[:,
3
])
ew
=
fluid
.
layers
.
clip
(
ex2
-
ex1
+
1e-3
,
0.
,
1e10
)
eh
=
fluid
.
layers
.
clip
(
ey2
-
ey1
+
1e-3
,
0.
,
1e10
)
# enclose erea
enclose
=
ew
*
eh
+
eps
giou
=
ious
-
(
enclose
-
uni
)
/
enclose
loss
=
1
-
giou
if
reduction
==
'mean'
:
loss
=
paddle
.
mean
(
loss
)
elif
reduction
==
'sum'
:
loss
=
paddle
.
sum
(
loss
)
else
:
raise
NotImplementedError
return
loss
def
forward
(
self
,
predicts
,
batch
):
structure_probs
=
predicts
[
'structure_probs'
]
structure_targets
=
batch
[
1
].
astype
(
"int64"
)
structure_targets
=
structure_targets
[:,
1
:]
if
len
(
batch
)
==
6
:
structure_mask
=
batch
[
5
].
astype
(
"int64"
)
structure_mask
=
structure_mask
[:,
1
:]
structure_mask
=
paddle
.
reshape
(
structure_mask
,
[
-
1
])
structure_probs
=
paddle
.
reshape
(
structure_probs
,
[
-
1
,
structure_probs
.
shape
[
-
1
]])
structure_targets
=
paddle
.
reshape
(
structure_targets
,
[
-
1
])
structure_loss
=
self
.
loss_func
(
structure_probs
,
structure_targets
)
if
len
(
batch
)
==
6
:
structure_loss
=
structure_loss
*
structure_mask
# structure_loss = paddle.sum(structure_loss) * self.structure_weight
structure_loss
=
paddle
.
mean
(
structure_loss
)
*
self
.
structure_weight
loc_preds
=
predicts
[
'loc_preds'
]
loc_targets
=
batch
[
2
].
astype
(
"float32"
)
loc_targets_mask
=
batch
[
4
].
astype
(
"float32"
)
loc_targets
=
loc_targets
[:,
1
:,
:]
loc_targets_mask
=
loc_targets_mask
[:,
1
:,
:]
loc_loss
=
F
.
mse_loss
(
loc_preds
*
loc_targets_mask
,
loc_targets
)
*
self
.
loc_weight
if
self
.
use_giou
:
loc_loss_giou
=
self
.
giou_loss
(
loc_preds
*
loc_targets_mask
,
loc_targets
)
*
self
.
giou_weight
total_loss
=
structure_loss
+
loc_loss
+
loc_loss_giou
return
{
'loss'
:
total_loss
,
"structure_loss"
:
structure_loss
,
"loc_loss"
:
loc_loss
,
"loc_loss_giou"
:
loc_loss_giou
}
else
:
total_loss
=
structure_loss
+
loc_loss
return
{
'loss'
:
total_loss
,
"structure_loss"
:
structure_loss
,
"loc_loss"
:
loc_loss
}
\ No newline at end of file
ppocr/metrics/__init__.py
View file @
6c7ff9c7
...
@@ -19,20 +19,23 @@ from __future__ import unicode_literals
...
@@ -19,20 +19,23 @@ from __future__ import unicode_literals
import
copy
import
copy
__all__
=
[
'
build_metric
'
]
__all__
=
[
"
build_metric
"
]
from
.det_metric
import
DetMetric
from
.rec_metric
import
RecMetric
from
.cls_metric
import
ClsMetric
from
.e2e_metric
import
E2EMetric
from
.distillation_metric
import
DistillationMetric
from
.table_metric
import
TableMetric
def
build_metric
(
config
):
def
build_metric
(
config
):
from
.det_metric
import
DetMetric
support_dict
=
[
from
.rec_metric
import
RecMetric
"DetMetric"
,
"RecMetric"
,
"ClsMetric"
,
"E2EMetric"
,
"DistillationMetric"
,
"TableMetric"
from
.cls_metric
import
ClsMetric
]
from
.e2e_metric
import
E2EMetric
support_dict
=
[
'DetMetric'
,
'RecMetric'
,
'ClsMetric'
,
'E2EMetric'
]
config
=
copy
.
deepcopy
(
config
)
config
=
copy
.
deepcopy
(
config
)
module_name
=
config
.
pop
(
'
name
'
)
module_name
=
config
.
pop
(
"
name
"
)
assert
module_name
in
support_dict
,
Exception
(
assert
module_name
in
support_dict
,
Exception
(
'
metric only support {}
'
.
format
(
support_dict
))
"
metric only support {}
"
.
format
(
support_dict
))
module_class
=
eval
(
module_name
)(
**
config
)
module_class
=
eval
(
module_name
)(
**
config
)
return
module_class
return
module_class
ppocr/metrics/det_metric.py
View file @
6c7ff9c7
...
@@ -55,6 +55,7 @@ class DetMetric(object):
...
@@ -55,6 +55,7 @@ class DetMetric(object):
result
=
self
.
evaluator
.
evaluate_image
(
gt_info_list
,
det_info_list
)
result
=
self
.
evaluator
.
evaluate_image
(
gt_info_list
,
det_info_list
)
self
.
results
.
append
(
result
)
self
.
results
.
append
(
result
)
def
get_metric
(
self
):
def
get_metric
(
self
):
"""
"""
return metrics {
return metrics {
...
...
ppocr/metrics/distillation_metric.py
0 → 100644
View file @
6c7ff9c7
# 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
importlib
import
copy
from
.rec_metric
import
RecMetric
from
.det_metric
import
DetMetric
from
.e2e_metric
import
E2EMetric
from
.cls_metric
import
ClsMetric
class
DistillationMetric
(
object
):
def
__init__
(
self
,
key
=
None
,
base_metric_name
=
None
,
main_indicator
=
None
,
**
kwargs
):
self
.
main_indicator
=
main_indicator
self
.
key
=
key
self
.
main_indicator
=
main_indicator
self
.
base_metric_name
=
base_metric_name
self
.
kwargs
=
kwargs
self
.
metrics
=
None
def
_init_metrcis
(
self
,
preds
):
self
.
metrics
=
dict
()
mod
=
importlib
.
import_module
(
__name__
)
for
key
in
preds
:
self
.
metrics
[
key
]
=
getattr
(
mod
,
self
.
base_metric_name
)(
main_indicator
=
self
.
main_indicator
,
**
self
.
kwargs
)
self
.
metrics
[
key
].
reset
()
def
__call__
(
self
,
preds
,
batch
,
**
kwargs
):
assert
isinstance
(
preds
,
dict
)
if
self
.
metrics
is
None
:
self
.
_init_metrcis
(
preds
)
output
=
dict
()
for
key
in
preds
:
self
.
metrics
[
key
].
__call__
(
preds
[
key
],
batch
,
**
kwargs
)
def
get_metric
(
self
):
"""
return metrics {
'acc': 0,
'norm_edit_dis': 0,
}
"""
output
=
dict
()
for
key
in
self
.
metrics
:
metric
=
self
.
metrics
[
key
].
get_metric
()
# main indicator
if
key
==
self
.
key
:
output
.
update
(
metric
)
else
:
for
sub_key
in
metric
:
output
[
"{}_{}"
.
format
(
key
,
sub_key
)]
=
metric
[
sub_key
]
return
output
def
reset
(
self
):
for
key
in
self
.
metrics
:
self
.
metrics
[
key
].
reset
()
ppocr/metrics/table_metric.py
0 → 100644
View file @
6c7ff9c7
# 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
numpy
as
np
class
TableMetric
(
object
):
def
__init__
(
self
,
main_indicator
=
'acc'
,
**
kwargs
):
self
.
main_indicator
=
main_indicator
self
.
reset
()
def
__call__
(
self
,
pred
,
batch
,
*
args
,
**
kwargs
):
structure_probs
=
pred
[
'structure_probs'
].
numpy
()
structure_labels
=
batch
[
1
]
correct_num
=
0
all_num
=
0
structure_probs
=
np
.
argmax
(
structure_probs
,
axis
=
2
)
structure_labels
=
structure_labels
[:,
1
:]
batch_size
=
structure_probs
.
shape
[
0
]
for
bno
in
range
(
batch_size
):
all_num
+=
1
if
(
structure_probs
[
bno
]
==
structure_labels
[
bno
]).
all
():
correct_num
+=
1
self
.
correct_num
+=
correct_num
self
.
all_num
+=
all_num
return
{
'acc'
:
correct_num
*
1.0
/
all_num
,
}
def
get_metric
(
self
):
"""
return metrics {
'acc': 0,
}
"""
acc
=
1.0
*
self
.
correct_num
/
self
.
all_num
self
.
reset
()
return
{
'acc'
:
acc
}
def
reset
(
self
):
self
.
correct_num
=
0
self
.
all_num
=
0
ppocr/modeling/architectures/__init__.py
View file @
6c7ff9c7
...
@@ -13,12 +13,20 @@
...
@@ -13,12 +13,20 @@
# limitations under the License.
# limitations under the License.
import
copy
import
copy
import
importlib
from
.base_model
import
BaseModel
from
.distillation_model
import
DistillationModel
__all__
=
[
'build_model'
]
__all__
=
[
'build_model'
]
def
build_model
(
config
):
def
build_model
(
config
):
from
.base_model
import
BaseModel
config
=
copy
.
deepcopy
(
config
)
config
=
copy
.
deepcopy
(
config
)
module_class
=
BaseModel
(
config
)
if
not
"name"
in
config
:
return
module_class
arch
=
BaseModel
(
config
)
\ No newline at end of file
else
:
name
=
config
.
pop
(
"name"
)
mod
=
importlib
.
import_module
(
__name__
)
arch
=
getattr
(
mod
,
name
)(
config
)
return
arch
ppocr/modeling/architectures/base_model.py
View file @
6c7ff9c7
# Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
1
PaddlePaddle Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
...
@@ -32,7 +32,6 @@ class BaseModel(nn.Layer):
...
@@ -32,7 +32,6 @@ class BaseModel(nn.Layer):
config (dict): the super parameters for module.
config (dict): the super parameters for module.
"""
"""
super
(
BaseModel
,
self
).
__init__
()
super
(
BaseModel
,
self
).
__init__
()
in_channels
=
config
.
get
(
'in_channels'
,
3
)
in_channels
=
config
.
get
(
'in_channels'
,
3
)
model_type
=
config
[
'model_type'
]
model_type
=
config
[
'model_type'
]
# build transfrom,
# build transfrom,
...
@@ -68,14 +67,23 @@ class BaseModel(nn.Layer):
...
@@ -68,14 +67,23 @@ class BaseModel(nn.Layer):
config
[
"Head"
][
'in_channels'
]
=
in_channels
config
[
"Head"
][
'in_channels'
]
=
in_channels
self
.
head
=
build_head
(
config
[
"Head"
])
self
.
head
=
build_head
(
config
[
"Head"
])
self
.
return_all_feats
=
config
.
get
(
"return_all_feats"
,
False
)
def
forward
(
self
,
x
,
data
=
None
):
def
forward
(
self
,
x
,
data
=
None
):
y
=
dict
()
if
self
.
use_transform
:
if
self
.
use_transform
:
x
=
self
.
transform
(
x
)
x
=
self
.
transform
(
x
)
x
=
self
.
backbone
(
x
)
x
=
self
.
backbone
(
x
)
y
[
"backbone_out"
]
=
x
if
self
.
use_neck
:
if
self
.
use_neck
:
x
=
self
.
neck
(
x
)
x
=
self
.
neck
(
x
)
if
data
is
None
:
y
[
"neck_out"
]
=
x
x
=
self
.
head
(
x
)
x
=
self
.
head
(
x
,
targets
=
data
)
if
isinstance
(
x
,
dict
):
y
.
update
(
x
)
else
:
y
[
"head_out"
]
=
x
if
self
.
return_all_feats
:
return
y
else
:
else
:
x
=
self
.
head
(
x
,
data
)
return
x
return
x
ppocr/modeling/architectures/distillation_model.py
0 → 100644
View file @
6c7ff9c7
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
paddle
import
nn
from
ppocr.modeling.transforms
import
build_transform
from
ppocr.modeling.backbones
import
build_backbone
from
ppocr.modeling.necks
import
build_neck
from
ppocr.modeling.heads
import
build_head
from
.base_model
import
BaseModel
from
ppocr.utils.save_load
import
init_model
,
load_pretrained_params
__all__
=
[
'DistillationModel'
]
class
DistillationModel
(
nn
.
Layer
):
def
__init__
(
self
,
config
):
"""
the module for OCR distillation.
args:
config (dict): the super parameters for module.
"""
super
().
__init__
()
self
.
model_list
=
[]
self
.
model_name_list
=
[]
for
key
in
config
[
"Models"
]:
model_config
=
config
[
"Models"
][
key
]
freeze_params
=
False
pretrained
=
None
if
"freeze_params"
in
model_config
:
freeze_params
=
model_config
.
pop
(
"freeze_params"
)
if
"pretrained"
in
model_config
:
pretrained
=
model_config
.
pop
(
"pretrained"
)
model
=
BaseModel
(
model_config
)
if
pretrained
is
not
None
:
load_pretrained_params
(
model
,
pretrained
)
if
freeze_params
:
for
param
in
model
.
parameters
():
param
.
trainable
=
False
self
.
model_list
.
append
(
self
.
add_sublayer
(
key
,
model
))
self
.
model_name_list
.
append
(
key
)
def
forward
(
self
,
x
):
result_dict
=
dict
()
for
idx
,
model_name
in
enumerate
(
self
.
model_name_list
):
result_dict
[
model_name
]
=
self
.
model_list
[
idx
](
x
)
return
result_dict
ppocr/modeling/backbones/__init__.py
View file @
6c7ff9c7
...
@@ -12,29 +12,36 @@
...
@@ -12,29 +12,36 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
__all__
=
[
'
build_backbone
'
]
__all__
=
[
"
build_backbone
"
]
def
build_backbone
(
config
,
model_type
):
def
build_backbone
(
config
,
model_type
):
if
model_type
==
'
det
'
:
if
model_type
==
"
det
"
:
from
.det_mobilenet_v3
import
MobileNetV3
from
.det_mobilenet_v3
import
MobileNetV3
from
.det_resnet_vd
import
ResNet
from
.det_resnet_vd
import
ResNet
from
.det_resnet_vd_sast
import
ResNet_SAST
from
.det_resnet_vd_sast
import
ResNet_SAST
support_dict
=
[
'
MobileNetV3
'
,
'
ResNet
'
,
'
ResNet_SAST
'
]
support_dict
=
[
"
MobileNetV3
"
,
"
ResNet
"
,
"
ResNet_SAST
"
]
elif
model_type
==
'
rec
'
or
model_type
==
'
cls
'
:
elif
model_type
==
"
rec
"
or
model_type
==
"
cls
"
:
from
.rec_mobilenet_v3
import
MobileNetV3
from
.rec_mobilenet_v3
import
MobileNetV3
from
.rec_resnet_vd
import
ResNet
from
.rec_resnet_vd
import
ResNet
from
.rec_resnet_fpn
import
ResNetFPN
from
.rec_resnet_fpn
import
ResNetFPN
support_dict
=
[
'MobileNetV3'
,
'ResNet'
,
'ResNetFPN'
]
from
.rec_mv1_enhance
import
MobileNetV1Enhance
elif
model_type
==
'e2e'
:
support_dict
=
[
"MobileNetV1Enhance"
,
"MobileNetV3"
,
"ResNet"
,
"ResNetFPN"
]
elif
model_type
==
"e2e"
:
from
.e2e_resnet_vd_pg
import
ResNet
from
.e2e_resnet_vd_pg
import
ResNet
support_dict
=
[
'ResNet'
]
support_dict
=
[
"ResNet"
]
elif
model_type
==
"table"
:
from
.table_resnet_vd
import
ResNet
from
.table_mobilenet_v3
import
MobileNetV3
support_dict
=
[
"ResNet"
,
"MobileNetV3"
]
else
:
else
:
raise
NotImplementedError
raise
NotImplementedError
module_name
=
config
.
pop
(
'
name
'
)
module_name
=
config
.
pop
(
"
name
"
)
assert
module_name
in
support_dict
,
Exception
(
assert
module_name
in
support_dict
,
Exception
(
'
when model typs is {}, backbone only support {}
'
.
format
(
model_type
,
"
when model typs is {}, backbone only support {}
"
.
format
(
model_type
,
support_dict
))
support_dict
))
module_class
=
eval
(
module_name
)(
**
config
)
module_class
=
eval
(
module_name
)(
**
config
)
return
module_class
return
module_class
ppocr/modeling/backbones/det_mobilenet_v3.py
View file @
6c7ff9c7
...
@@ -102,8 +102,7 @@ class MobileNetV3(nn.Layer):
...
@@ -102,8 +102,7 @@ class MobileNetV3(nn.Layer):
padding
=
1
,
padding
=
1
,
groups
=
1
,
groups
=
1
,
if_act
=
True
,
if_act
=
True
,
act
=
'hardswish'
,
act
=
'hardswish'
)
name
=
'conv1'
)
self
.
stages
=
[]
self
.
stages
=
[]
self
.
out_channels
=
[]
self
.
out_channels
=
[]
...
@@ -125,8 +124,7 @@ class MobileNetV3(nn.Layer):
...
@@ -125,8 +124,7 @@ class MobileNetV3(nn.Layer):
kernel_size
=
k
,
kernel_size
=
k
,
stride
=
s
,
stride
=
s
,
use_se
=
se
,
use_se
=
se
,
act
=
nl
,
act
=
nl
))
name
=
"conv"
+
str
(
i
+
2
)))
inplanes
=
make_divisible
(
scale
*
c
)
inplanes
=
make_divisible
(
scale
*
c
)
i
+=
1
i
+=
1
block_list
.
append
(
block_list
.
append
(
...
@@ -138,8 +136,7 @@ class MobileNetV3(nn.Layer):
...
@@ -138,8 +136,7 @@ class MobileNetV3(nn.Layer):
padding
=
0
,
padding
=
0
,
groups
=
1
,
groups
=
1
,
if_act
=
True
,
if_act
=
True
,
act
=
'hardswish'
,
act
=
'hardswish'
))
name
=
'conv_last'
))
self
.
stages
.
append
(
nn
.
Sequential
(
*
block_list
))
self
.
stages
.
append
(
nn
.
Sequential
(
*
block_list
))
self
.
out_channels
.
append
(
make_divisible
(
scale
*
cls_ch_squeeze
))
self
.
out_channels
.
append
(
make_divisible
(
scale
*
cls_ch_squeeze
))
for
i
,
stage
in
enumerate
(
self
.
stages
):
for
i
,
stage
in
enumerate
(
self
.
stages
):
...
@@ -163,8 +160,7 @@ class ConvBNLayer(nn.Layer):
...
@@ -163,8 +160,7 @@ class ConvBNLayer(nn.Layer):
padding
,
padding
,
groups
=
1
,
groups
=
1
,
if_act
=
True
,
if_act
=
True
,
act
=
None
,
act
=
None
):
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
if_act
=
if_act
self
.
if_act
=
if_act
self
.
act
=
act
self
.
act
=
act
...
@@ -175,16 +171,9 @@ class ConvBNLayer(nn.Layer):
...
@@ -175,16 +171,9 @@ class ConvBNLayer(nn.Layer):
stride
=
stride
,
stride
=
stride
,
padding
=
padding
,
padding
=
padding
,
groups
=
groups
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
name
=
name
+
'_weights'
),
bias_attr
=
False
)
bias_attr
=
False
)
self
.
bn
=
nn
.
BatchNorm
(
self
.
bn
=
nn
.
BatchNorm
(
num_channels
=
out_channels
,
act
=
None
)
num_channels
=
out_channels
,
act
=
None
,
param_attr
=
ParamAttr
(
name
=
name
+
"_bn_scale"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_bn_offset"
),
moving_mean_name
=
name
+
"_bn_mean"
,
moving_variance_name
=
name
+
"_bn_variance"
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
conv
(
x
)
...
@@ -209,8 +198,7 @@ class ResidualUnit(nn.Layer):
...
@@ -209,8 +198,7 @@ class ResidualUnit(nn.Layer):
kernel_size
,
kernel_size
,
stride
,
stride
,
use_se
,
use_se
,
act
=
None
,
act
=
None
):
name
=
''
):
super
(
ResidualUnit
,
self
).
__init__
()
super
(
ResidualUnit
,
self
).
__init__
()
self
.
if_shortcut
=
stride
==
1
and
in_channels
==
out_channels
self
.
if_shortcut
=
stride
==
1
and
in_channels
==
out_channels
self
.
if_se
=
use_se
self
.
if_se
=
use_se
...
@@ -222,8 +210,7 @@ class ResidualUnit(nn.Layer):
...
@@ -222,8 +210,7 @@ class ResidualUnit(nn.Layer):
stride
=
1
,
stride
=
1
,
padding
=
0
,
padding
=
0
,
if_act
=
True
,
if_act
=
True
,
act
=
act
,
act
=
act
)
name
=
name
+
"_expand"
)
self
.
bottleneck_conv
=
ConvBNLayer
(
self
.
bottleneck_conv
=
ConvBNLayer
(
in_channels
=
mid_channels
,
in_channels
=
mid_channels
,
out_channels
=
mid_channels
,
out_channels
=
mid_channels
,
...
@@ -232,10 +219,9 @@ class ResidualUnit(nn.Layer):
...
@@ -232,10 +219,9 @@ class ResidualUnit(nn.Layer):
padding
=
int
((
kernel_size
-
1
)
//
2
),
padding
=
int
((
kernel_size
-
1
)
//
2
),
groups
=
mid_channels
,
groups
=
mid_channels
,
if_act
=
True
,
if_act
=
True
,
act
=
act
,
act
=
act
)
name
=
name
+
"_depthwise"
)
if
self
.
if_se
:
if
self
.
if_se
:
self
.
mid_se
=
SEModule
(
mid_channels
,
name
=
name
+
"_se"
)
self
.
mid_se
=
SEModule
(
mid_channels
)
self
.
linear_conv
=
ConvBNLayer
(
self
.
linear_conv
=
ConvBNLayer
(
in_channels
=
mid_channels
,
in_channels
=
mid_channels
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
...
@@ -243,8 +229,7 @@ class ResidualUnit(nn.Layer):
...
@@ -243,8 +229,7 @@ class ResidualUnit(nn.Layer):
stride
=
1
,
stride
=
1
,
padding
=
0
,
padding
=
0
,
if_act
=
False
,
if_act
=
False
,
act
=
None
,
act
=
None
)
name
=
name
+
"_linear"
)
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
x
=
self
.
expand_conv
(
inputs
)
x
=
self
.
expand_conv
(
inputs
)
...
@@ -258,7 +243,7 @@ class ResidualUnit(nn.Layer):
...
@@ -258,7 +243,7 @@ class ResidualUnit(nn.Layer):
class
SEModule
(
nn
.
Layer
):
class
SEModule
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
reduction
=
4
,
name
=
""
):
def
__init__
(
self
,
in_channels
,
reduction
=
4
):
super
(
SEModule
,
self
).
__init__
()
super
(
SEModule
,
self
).
__init__
()
self
.
avg_pool
=
nn
.
AdaptiveAvgPool2D
(
1
)
self
.
avg_pool
=
nn
.
AdaptiveAvgPool2D
(
1
)
self
.
conv1
=
nn
.
Conv2D
(
self
.
conv1
=
nn
.
Conv2D
(
...
@@ -266,17 +251,13 @@ class SEModule(nn.Layer):
...
@@ -266,17 +251,13 @@ class SEModule(nn.Layer):
out_channels
=
in_channels
//
reduction
,
out_channels
=
in_channels
//
reduction
,
kernel_size
=
1
,
kernel_size
=
1
,
stride
=
1
,
stride
=
1
,
padding
=
0
,
padding
=
0
)
weight_attr
=
ParamAttr
(
name
=
name
+
"_1_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_1_offset"
))
self
.
conv2
=
nn
.
Conv2D
(
self
.
conv2
=
nn
.
Conv2D
(
in_channels
=
in_channels
//
reduction
,
in_channels
=
in_channels
//
reduction
,
out_channels
=
in_channels
,
out_channels
=
in_channels
,
kernel_size
=
1
,
kernel_size
=
1
,
stride
=
1
,
stride
=
1
,
padding
=
0
,
padding
=
0
)
weight_attr
=
ParamAttr
(
name
+
"_2_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_2_offset"
))
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
outputs
=
self
.
avg_pool
(
inputs
)
outputs
=
self
.
avg_pool
(
inputs
)
...
...
ppocr/modeling/backbones/rec_mobilenet_v3.py
View file @
6c7ff9c7
...
@@ -96,8 +96,7 @@ class MobileNetV3(nn.Layer):
...
@@ -96,8 +96,7 @@ class MobileNetV3(nn.Layer):
padding
=
1
,
padding
=
1
,
groups
=
1
,
groups
=
1
,
if_act
=
True
,
if_act
=
True
,
act
=
'hardswish'
,
act
=
'hardswish'
)
name
=
'conv1'
)
i
=
0
i
=
0
block_list
=
[]
block_list
=
[]
inplanes
=
make_divisible
(
inplanes
*
scale
)
inplanes
=
make_divisible
(
inplanes
*
scale
)
...
@@ -110,8 +109,7 @@ class MobileNetV3(nn.Layer):
...
@@ -110,8 +109,7 @@ class MobileNetV3(nn.Layer):
kernel_size
=
k
,
kernel_size
=
k
,
stride
=
s
,
stride
=
s
,
use_se
=
se
,
use_se
=
se
,
act
=
nl
,
act
=
nl
))
name
=
'conv'
+
str
(
i
+
2
)))
inplanes
=
make_divisible
(
scale
*
c
)
inplanes
=
make_divisible
(
scale
*
c
)
i
+=
1
i
+=
1
self
.
blocks
=
nn
.
Sequential
(
*
block_list
)
self
.
blocks
=
nn
.
Sequential
(
*
block_list
)
...
@@ -124,8 +122,7 @@ class MobileNetV3(nn.Layer):
...
@@ -124,8 +122,7 @@ class MobileNetV3(nn.Layer):
padding
=
0
,
padding
=
0
,
groups
=
1
,
groups
=
1
,
if_act
=
True
,
if_act
=
True
,
act
=
'hardswish'
,
act
=
'hardswish'
)
name
=
'conv_last'
)
self
.
pool
=
nn
.
MaxPool2D
(
kernel_size
=
2
,
stride
=
2
,
padding
=
0
)
self
.
pool
=
nn
.
MaxPool2D
(
kernel_size
=
2
,
stride
=
2
,
padding
=
0
)
self
.
out_channels
=
make_divisible
(
scale
*
cls_ch_squeeze
)
self
.
out_channels
=
make_divisible
(
scale
*
cls_ch_squeeze
)
...
...
ppocr/modeling/backbones/rec_mv1_enhance.py
0 → 100644
View file @
6c7ff9c7
# 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.
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
numpy
as
np
import
paddle
from
paddle
import
ParamAttr
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle.nn
import
Conv2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2D
,
MaxPool2D
,
AvgPool2D
from
paddle.nn.initializer
import
KaimingNormal
import
math
import
numpy
as
np
import
paddle
from
paddle
import
ParamAttr
,
reshape
,
transpose
,
concat
,
split
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle.nn
import
Conv2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2D
,
MaxPool2D
,
AvgPool2D
from
paddle.nn.initializer
import
KaimingNormal
import
math
from
paddle.nn.functional
import
hardswish
,
hardsigmoid
from
paddle.regularizer
import
L2Decay
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
filter_size
,
num_filters
,
stride
,
padding
,
channels
=
None
,
num_groups
=
1
,
act
=
'hard_swish'
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
Conv2D
(
in_channels
=
num_channels
,
out_channels
=
num_filters
,
kernel_size
=
filter_size
,
stride
=
stride
,
padding
=
padding
,
groups
=
num_groups
,
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
()),
bias_attr
=
False
)
self
.
_batch_norm
=
BatchNorm
(
num_filters
,
act
=
act
,
param_attr
=
ParamAttr
(
regularizer
=
L2Decay
(
0.0
)),
bias_attr
=
ParamAttr
(
regularizer
=
L2Decay
(
0.0
)))
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
y
=
self
.
_batch_norm
(
y
)
return
y
class
DepthwiseSeparable
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters1
,
num_filters2
,
num_groups
,
stride
,
scale
,
dw_size
=
3
,
padding
=
1
,
use_se
=
False
):
super
(
DepthwiseSeparable
,
self
).
__init__
()
self
.
use_se
=
use_se
self
.
_depthwise_conv
=
ConvBNLayer
(
num_channels
=
num_channels
,
num_filters
=
int
(
num_filters1
*
scale
),
filter_size
=
dw_size
,
stride
=
stride
,
padding
=
padding
,
num_groups
=
int
(
num_groups
*
scale
))
if
use_se
:
self
.
_se
=
SEModule
(
int
(
num_filters1
*
scale
))
self
.
_pointwise_conv
=
ConvBNLayer
(
num_channels
=
int
(
num_filters1
*
scale
),
filter_size
=
1
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
1
,
padding
=
0
)
def
forward
(
self
,
inputs
):
y
=
self
.
_depthwise_conv
(
inputs
)
if
self
.
use_se
:
y
=
self
.
_se
(
y
)
y
=
self
.
_pointwise_conv
(
y
)
return
y
class
MobileNetV1Enhance
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
=
3
,
scale
=
0.5
,
**
kwargs
):
super
().
__init__
()
self
.
scale
=
scale
self
.
block_list
=
[]
self
.
conv1
=
ConvBNLayer
(
num_channels
=
3
,
filter_size
=
3
,
channels
=
3
,
num_filters
=
int
(
32
*
scale
),
stride
=
2
,
padding
=
1
)
conv2_1
=
DepthwiseSeparable
(
num_channels
=
int
(
32
*
scale
),
num_filters1
=
32
,
num_filters2
=
64
,
num_groups
=
32
,
stride
=
1
,
scale
=
scale
)
self
.
block_list
.
append
(
conv2_1
)
conv2_2
=
DepthwiseSeparable
(
num_channels
=
int
(
64
*
scale
),
num_filters1
=
64
,
num_filters2
=
128
,
num_groups
=
64
,
stride
=
1
,
scale
=
scale
)
self
.
block_list
.
append
(
conv2_2
)
conv3_1
=
DepthwiseSeparable
(
num_channels
=
int
(
128
*
scale
),
num_filters1
=
128
,
num_filters2
=
128
,
num_groups
=
128
,
stride
=
1
,
scale
=
scale
)
self
.
block_list
.
append
(
conv3_1
)
conv3_2
=
DepthwiseSeparable
(
num_channels
=
int
(
128
*
scale
),
num_filters1
=
128
,
num_filters2
=
256
,
num_groups
=
128
,
stride
=
(
2
,
1
),
scale
=
scale
)
self
.
block_list
.
append
(
conv3_2
)
conv4_1
=
DepthwiseSeparable
(
num_channels
=
int
(
256
*
scale
),
num_filters1
=
256
,
num_filters2
=
256
,
num_groups
=
256
,
stride
=
1
,
scale
=
scale
)
self
.
block_list
.
append
(
conv4_1
)
conv4_2
=
DepthwiseSeparable
(
num_channels
=
int
(
256
*
scale
),
num_filters1
=
256
,
num_filters2
=
512
,
num_groups
=
256
,
stride
=
(
2
,
1
),
scale
=
scale
)
self
.
block_list
.
append
(
conv4_2
)
for
_
in
range
(
5
):
conv5
=
DepthwiseSeparable
(
num_channels
=
int
(
512
*
scale
),
num_filters1
=
512
,
num_filters2
=
512
,
num_groups
=
512
,
stride
=
1
,
dw_size
=
5
,
padding
=
2
,
scale
=
scale
,
use_se
=
False
)
self
.
block_list
.
append
(
conv5
)
conv5_6
=
DepthwiseSeparable
(
num_channels
=
int
(
512
*
scale
),
num_filters1
=
512
,
num_filters2
=
1024
,
num_groups
=
512
,
stride
=
(
2
,
1
),
dw_size
=
5
,
padding
=
2
,
scale
=
scale
,
use_se
=
True
)
self
.
block_list
.
append
(
conv5_6
)
conv6
=
DepthwiseSeparable
(
num_channels
=
int
(
1024
*
scale
),
num_filters1
=
1024
,
num_filters2
=
1024
,
num_groups
=
1024
,
stride
=
1
,
dw_size
=
5
,
padding
=
2
,
use_se
=
True
,
scale
=
scale
)
self
.
block_list
.
append
(
conv6
)
self
.
block_list
=
nn
.
Sequential
(
*
self
.
block_list
)
self
.
pool
=
nn
.
MaxPool2D
(
kernel_size
=
2
,
stride
=
2
,
padding
=
0
)
self
.
out_channels
=
int
(
1024
*
scale
)
def
forward
(
self
,
inputs
):
y
=
self
.
conv1
(
inputs
)
y
=
self
.
block_list
(
y
)
y
=
self
.
pool
(
y
)
return
y
class
SEModule
(
nn
.
Layer
):
def
__init__
(
self
,
channel
,
reduction
=
4
):
super
(
SEModule
,
self
).
__init__
()
self
.
avg_pool
=
AdaptiveAvgPool2D
(
1
)
self
.
conv1
=
Conv2D
(
in_channels
=
channel
,
out_channels
=
channel
//
reduction
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
weight_attr
=
ParamAttr
(),
bias_attr
=
ParamAttr
())
self
.
conv2
=
Conv2D
(
in_channels
=
channel
//
reduction
,
out_channels
=
channel
,
kernel_size
=
1
,
stride
=
1
,
padding
=
0
,
weight_attr
=
ParamAttr
(),
bias_attr
=
ParamAttr
())
def
forward
(
self
,
inputs
):
outputs
=
self
.
avg_pool
(
inputs
)
outputs
=
self
.
conv1
(
outputs
)
outputs
=
F
.
relu
(
outputs
)
outputs
=
self
.
conv2
(
outputs
)
outputs
=
hardsigmoid
(
outputs
)
return
paddle
.
multiply
(
x
=
inputs
,
y
=
outputs
)
ppocr/modeling/backbones/table_mobilenet_v3.py
0 → 100644
View file @
6c7ff9c7
This diff is collapsed.
Click to expand it.
ppocr/modeling/backbones/table_resnet_vd.py
0 → 100644
View file @
6c7ff9c7
This diff is collapsed.
Click to expand it.
ppocr/modeling/heads/__init__.py
View file @
6c7ff9c7
...
@@ -31,8 +31,10 @@ def build_head(config):
...
@@ -31,8 +31,10 @@ def build_head(config):
from
.cls_head
import
ClsHead
from
.cls_head
import
ClsHead
support_dict
=
[
support_dict
=
[
'DBHead'
,
'EASTHead'
,
'SASTHead'
,
'CTCHead'
,
'ClsHead'
,
'AttentionHead'
,
'DBHead'
,
'EASTHead'
,
'SASTHead'
,
'CTCHead'
,
'ClsHead'
,
'AttentionHead'
,
'SRNHead'
,
'PGHead'
]
'SRNHead'
,
'PGHead'
,
'TableAttentionHead'
]
#table head
from
.table_att_head
import
TableAttentionHead
module_name
=
config
.
pop
(
'name'
)
module_name
=
config
.
pop
(
'name'
)
assert
module_name
in
support_dict
,
Exception
(
'head only support {}'
.
format
(
assert
module_name
in
support_dict
,
Exception
(
'head only support {}'
.
format
(
...
...
ppocr/modeling/heads/cls_head.py
View file @
6c7ff9c7
...
@@ -43,7 +43,7 @@ class ClsHead(nn.Layer):
...
@@ -43,7 +43,7 @@ class ClsHead(nn.Layer):
initializer
=
nn
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
initializer
=
nn
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
),
)
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
),
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
,
targets
=
None
):
x
=
self
.
pool
(
x
)
x
=
self
.
pool
(
x
)
x
=
paddle
.
reshape
(
x
,
shape
=
[
x
.
shape
[
0
],
x
.
shape
[
1
]])
x
=
paddle
.
reshape
(
x
,
shape
=
[
x
.
shape
[
0
],
x
.
shape
[
1
]])
x
=
self
.
fc
(
x
)
x
=
self
.
fc
(
x
)
...
...
ppocr/modeling/heads/det_db_head.py
View file @
6c7ff9c7
This diff is collapsed.
Click to expand it.
Prev
1
2
3
4
5
6
7
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