Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_41840029
PaddleOCR
提交
f4b62551
P
PaddleOCR
项目概览
weixin_41840029
/
PaddleOCR
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleOCR
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f4b62551
编写于
5月 18, 2022
作者:
littletomatodonkey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add support for svtr static training (#6328)
上级
1bb03b4d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
14 deletion
+32
-14
ppocr/modeling/architectures/__init__.py
ppocr/modeling/architectures/__init__.py
+22
-4
ppocr/modeling/heads/rec_sar_head.py
ppocr/modeling/heads/rec_sar_head.py
+10
-10
未找到文件。
ppocr/modeling/architectures/__init__.py
浏览文件 @
f4b62551
...
...
@@ -40,11 +40,29 @@ def apply_to_static(model, config, logger):
return
model
assert
"image_shape"
in
config
[
"Global"
],
"image_shape must be assigned for static training mode..."
supported_list
=
[
"DB"
]
assert
config
[
"Architecture"
][
"algorithm"
]
in
supported_list
,
f
"algorithms that supports static training must in in
{
supported_list
}
but got
{
config
[
'Architecture'
][
'algorithm'
]
}
"
supported_list
=
[
"DB"
,
"SVTR"
]
if
config
[
"Architecture"
][
"algorithm"
]
in
[
"Distillation"
]:
algo
=
list
(
config
[
"Architecture"
][
"Models"
].
values
())[
0
][
"algorithm"
]
else
:
algo
=
config
[
"Architecture"
][
"algorithm"
]
assert
algo
in
supported_list
,
f
"algorithms that supports static training must in in
{
supported_list
}
but got
{
algo
}
"
specs
=
[
InputSpec
(
[
None
]
+
config
[
"Global"
][
"image_shape"
],
dtype
=
'float32'
)
]
if
algo
==
"SVTR"
:
specs
.
append
([
InputSpec
(
[
None
,
config
[
"Global"
][
"max_text_length"
]],
dtype
=
'int64'
),
InputSpec
(
[
None
,
config
[
"Global"
][
"max_text_length"
]],
dtype
=
'int64'
),
InputSpec
(
[
None
],
dtype
=
'int64'
),
InputSpec
(
[
None
],
dtype
=
'float64'
)
])
specs
=
[
InputSpec
([
None
]
+
config
[
"Global"
][
"image_shape"
])]
model
=
to_static
(
model
,
input_spec
=
specs
)
logger
.
info
(
"Successfully to apply @to_static with specs: {}"
.
format
(
specs
))
return
model
ppocr/modeling/heads/rec_sar_head.py
浏览文件 @
f4b62551
...
...
@@ -83,7 +83,7 @@ class SAREncoder(nn.Layer):
def
forward
(
self
,
feat
,
img_metas
=
None
):
if
img_metas
is
not
None
:
assert
len
(
img_metas
[
0
])
==
feat
.
shape
[
0
]
assert
len
(
img_metas
[
0
])
==
paddle
.
shape
(
feat
)
[
0
]
valid_ratios
=
None
if
img_metas
is
not
None
and
self
.
mask
:
...
...
@@ -98,9 +98,10 @@ class SAREncoder(nn.Layer):
if
valid_ratios
is
not
None
:
valid_hf
=
[]
T
=
holistic_feat
.
shape
[
1
]
for
i
in
range
(
len
(
valid_ratios
)):
valid_step
=
min
(
T
,
math
.
ceil
(
T
*
valid_ratios
[
i
]))
-
1
T
=
paddle
.
shape
(
holistic_feat
)[
1
]
for
i
in
range
(
paddle
.
shape
(
valid_ratios
)[
0
]):
valid_step
=
paddle
.
minimum
(
T
,
paddle
.
ceil
(
valid_ratios
[
i
]
*
T
).
astype
(
'int32'
))
-
1
valid_hf
.
append
(
holistic_feat
[
i
,
valid_step
,
:])
valid_hf
=
paddle
.
stack
(
valid_hf
,
axis
=
0
)
else
:
...
...
@@ -247,13 +248,14 @@ class ParallelSARDecoder(BaseDecoder):
# bsz * (seq_len + 1) * h * w * attn_size
attn_weight
=
self
.
conv1x1_2
(
attn_weight
)
# bsz * (seq_len + 1) * h * w * 1
bsz
,
T
,
h
,
w
,
c
=
attn_weight
.
shape
bsz
,
T
,
h
,
w
,
c
=
paddle
.
shape
(
attn_weight
)
assert
c
==
1
if
valid_ratios
is
not
None
:
# cal mask of attention weight
for
i
in
range
(
len
(
valid_ratios
)):
valid_width
=
min
(
w
,
math
.
ceil
(
w
*
valid_ratios
[
i
]))
for
i
in
range
(
paddle
.
shape
(
valid_ratios
)[
0
]):
valid_width
=
paddle
.
minimum
(
w
,
paddle
.
ceil
(
valid_ratios
[
i
]
*
w
).
astype
(
"int32"
))
if
valid_width
<
w
:
attn_weight
[
i
,
:,
:,
valid_width
:,
:]
=
float
(
'-inf'
)
...
...
@@ -288,7 +290,7 @@ class ParallelSARDecoder(BaseDecoder):
img_metas: [label, valid_ratio]
'''
if
img_metas
is
not
None
:
assert
len
(
img_metas
[
0
])
==
feat
.
shape
[
0
]
assert
paddle
.
shape
(
img_metas
[
0
])[
0
]
==
paddle
.
shape
(
feat
)
[
0
]
valid_ratios
=
None
if
img_metas
is
not
None
and
self
.
mask
:
...
...
@@ -302,7 +304,6 @@ class ParallelSARDecoder(BaseDecoder):
# bsz * (seq_len + 1) * C
out_dec
=
self
.
_2d_attention
(
in_dec
,
feat
,
out_enc
,
valid_ratios
=
valid_ratios
)
# bsz * (seq_len + 1) * num_classes
return
out_dec
[:,
1
:,
:]
# bsz * seq_len * num_classes
...
...
@@ -395,7 +396,6 @@ class SARHead(nn.Layer):
if
self
.
training
:
label
=
targets
[
0
]
# label
label
=
paddle
.
to_tensor
(
label
,
dtype
=
'int64'
)
final_out
=
self
.
decoder
(
feat
,
holistic_feat
,
label
,
img_metas
=
targets
)
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录