Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_41840029
PaddleOCR
提交
a4b0241a
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看板
提交
a4b0241a
编写于
9月 01, 2021
作者:
W
WenmuZhou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use paddlepaddle license
上级
835e7178
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
52 addition
and
27 deletion
+52
-27
ppocr/losses/det_pse_loss.py
ppocr/losses/det_pse_loss.py
+52
-27
未找到文件。
ppocr/losses/det_pse_loss.py
浏览文件 @
a4b0241a
# -*- coding: utf-8 -*-
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
# @Time : 3/29/19 11:03 AM
#
# @Author : zhoujun
# 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
from
paddle
import
nn
from
paddle
import
nn
from
paddle.nn
import
functional
as
F
from
paddle.nn
import
functional
as
F
...
@@ -9,7 +20,12 @@ from ppocr.utils.iou import iou
...
@@ -9,7 +20,12 @@ from ppocr.utils.iou import iou
class
PSELoss
(
nn
.
Layer
):
class
PSELoss
(
nn
.
Layer
):
def
__init__
(
self
,
alpha
,
ohem_ratio
=
3
,
kernel_sample_mask
=
'pred'
,
reduction
=
'sum'
,
**
kwargs
):
def
__init__
(
self
,
alpha
,
ohem_ratio
=
3
,
kernel_sample_mask
=
'pred'
,
reduction
=
'sum'
,
**
kwargs
):
"""Implement PSE Loss.
"""Implement PSE Loss.
"""
"""
super
(
PSELoss
,
self
).
__init__
()
super
(
PSELoss
,
self
).
__init__
()
...
@@ -31,32 +47,32 @@ class PSELoss(nn.Layer):
...
@@ -31,32 +47,32 @@ class PSELoss(nn.Layer):
selected_masks
=
self
.
ohem_batch
(
texts
,
gt_texts
,
training_masks
)
selected_masks
=
self
.
ohem_batch
(
texts
,
gt_texts
,
training_masks
)
loss_text
=
self
.
dice_loss
(
texts
,
gt_texts
,
selected_masks
)
loss_text
=
self
.
dice_loss
(
texts
,
gt_texts
,
selected_masks
)
iou_text
=
iou
((
texts
>
0
).
astype
(
'int64'
),
gt_texts
,
training_masks
,
reduce
=
False
)
iou_text
=
iou
((
texts
>
0
).
astype
(
'int64'
),
losses
=
dict
(
gt_texts
,
loss_text
=
loss_text
,
training_masks
,
iou_text
=
iou_text
reduce
=
False
)
)
losses
=
dict
(
loss_text
=
loss_text
,
iou_text
=
iou_text
)
# kernel loss
# kernel loss
loss_kernels
=
[]
loss_kernels
=
[]
if
self
.
kernel_sample_mask
==
'gt'
:
if
self
.
kernel_sample_mask
==
'gt'
:
selected_masks
=
gt_texts
*
training_masks
selected_masks
=
gt_texts
*
training_masks
elif
self
.
kernel_sample_mask
==
'pred'
:
elif
self
.
kernel_sample_mask
==
'pred'
:
selected_masks
=
(
F
.
sigmoid
(
texts
)
>
0.5
).
astype
(
'float32'
)
*
training_masks
selected_masks
=
(
F
.
sigmoid
(
texts
)
>
0.5
).
astype
(
'float32'
)
*
training_masks
for
i
in
range
(
kernels
.
shape
[
1
]):
for
i
in
range
(
kernels
.
shape
[
1
]):
kernel_i
=
kernels
[:,
i
,
:,
:]
kernel_i
=
kernels
[:,
i
,
:,
:]
gt_kernel_i
=
gt_kernels
[:,
i
,
:,
:]
gt_kernel_i
=
gt_kernels
[:,
i
,
:,
:]
loss_kernel_i
=
self
.
dice_loss
(
kernel_i
,
gt_kernel_i
,
selected_masks
)
loss_kernel_i
=
self
.
dice_loss
(
kernel_i
,
gt_kernel_i
,
selected_masks
)
loss_kernels
.
append
(
loss_kernel_i
)
loss_kernels
.
append
(
loss_kernel_i
)
loss_kernels
=
paddle
.
mean
(
paddle
.
stack
(
loss_kernels
,
axis
=
1
),
axis
=
1
)
loss_kernels
=
paddle
.
mean
(
paddle
.
stack
(
loss_kernels
,
axis
=
1
),
axis
=
1
)
iou_kernel
=
iou
(
iou_kernel
=
iou
((
kernels
[:,
-
1
,
:,
:]
>
0
).
astype
(
'int64'
),
(
kernels
[:,
-
1
,
:,
:]
>
0
).
astype
(
'int64'
),
gt_kernels
[:,
-
1
,
:,
:],
training_masks
*
gt_texts
,
gt_kernels
[:,
-
1
,
:,
:],
training_masks
*
gt_texts
,
reduce
=
False
)
reduce
=
False
)
losses
.
update
(
dict
(
losses
.
update
(
dict
(
loss_kernels
=
loss_kernels
,
iou_kernel
=
iou_kernel
))
loss_kernels
=
loss_kernels
,
iou_kernel
=
iou_kernel
))
loss
=
self
.
alpha
*
loss_text
+
(
1
-
self
.
alpha
)
*
loss_kernels
loss
=
self
.
alpha
*
loss_text
+
(
1
-
self
.
alpha
)
*
loss_kernels
losses
[
'loss'
]
=
loss
losses
[
'loss'
]
=
loss
if
self
.
reduction
==
'sum'
:
if
self
.
reduction
==
'sum'
:
...
@@ -83,12 +99,15 @@ class PSELoss(nn.Layer):
...
@@ -83,12 +99,15 @@ class PSELoss(nn.Layer):
def
ohem_single
(
self
,
score
,
gt_text
,
training_mask
,
ohem_ratio
=
3
):
def
ohem_single
(
self
,
score
,
gt_text
,
training_mask
,
ohem_ratio
=
3
):
pos_num
=
int
(
paddle
.
sum
((
gt_text
>
0.5
).
astype
(
'float32'
)))
-
int
(
pos_num
=
int
(
paddle
.
sum
((
gt_text
>
0.5
).
astype
(
'float32'
)))
-
int
(
paddle
.
sum
(
paddle
.
logical_and
((
gt_text
>
0.5
),
(
training_mask
<=
0.5
)).
astype
(
'float32'
)))
paddle
.
sum
(
paddle
.
logical_and
((
gt_text
>
0.5
),
(
training_mask
<=
0.5
))
.
astype
(
'float32'
)))
if
pos_num
==
0
:
if
pos_num
==
0
:
# selected_mask = gt_text.copy() * 0 # may be not good
# selected_mask = gt_text.copy() * 0 # may be not good
selected_mask
=
training_mask
selected_mask
=
training_mask
selected_mask
=
selected_mask
.
reshape
([
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]]).
astype
(
selected_mask
=
selected_mask
.
reshape
(
[
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]]).
astype
(
'float32'
)
'float32'
)
return
selected_mask
return
selected_mask
...
@@ -97,23 +116,29 @@ class PSELoss(nn.Layer):
...
@@ -97,23 +116,29 @@ class PSELoss(nn.Layer):
if
neg_num
==
0
:
if
neg_num
==
0
:
selected_mask
=
training_mask
selected_mask
=
training_mask
selected_mask
=
selected_mask
.
view
(
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]).
astype
(
'float32'
)
selected_mask
=
selected_mask
.
view
(
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]).
astype
(
'float32'
)
return
selected_mask
return
selected_mask
neg_score
=
paddle
.
masked_select
(
score
,
gt_text
<=
0.5
)
neg_score
=
paddle
.
masked_select
(
score
,
gt_text
<=
0.5
)
neg_score_sorted
=
paddle
.
sort
(
-
neg_score
)
neg_score_sorted
=
paddle
.
sort
(
-
neg_score
)
threshold
=
-
neg_score_sorted
[
neg_num
-
1
]
threshold
=
-
neg_score_sorted
[
neg_num
-
1
]
selected_mask
=
paddle
.
logical_and
(
paddle
.
logical_or
((
score
>=
threshold
),
(
gt_text
>
0.5
)),
selected_mask
=
paddle
.
logical_and
(
paddle
.
logical_or
((
score
>=
threshold
),
(
gt_text
>
0.5
)),
(
training_mask
>
0.5
))
(
training_mask
>
0.5
))
selected_mask
=
selected_mask
.
reshape
([
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]]).
astype
(
'float32'
)
selected_mask
=
selected_mask
.
reshape
(
[
1
,
selected_mask
.
shape
[
0
],
selected_mask
.
shape
[
1
]]).
astype
(
'float32'
)
return
selected_mask
return
selected_mask
def
ohem_batch
(
self
,
scores
,
gt_texts
,
training_masks
,
ohem_ratio
=
3
):
def
ohem_batch
(
self
,
scores
,
gt_texts
,
training_masks
,
ohem_ratio
=
3
):
selected_masks
=
[]
selected_masks
=
[]
for
i
in
range
(
scores
.
shape
[
0
]):
for
i
in
range
(
scores
.
shape
[
0
]):
selected_masks
.
append
(
selected_masks
.
append
(
self
.
ohem_single
(
scores
[
i
,
:,
:],
gt_texts
[
i
,
:,
:],
training_masks
[
i
,
:,
:],
ohem_ratio
))
self
.
ohem_single
(
scores
[
i
,
:,
:],
gt_texts
[
i
,
:,
:],
training_masks
[
i
,
:,
:],
ohem_ratio
))
selected_masks
=
paddle
.
concat
(
selected_masks
,
0
).
astype
(
'float32'
)
selected_masks
=
paddle
.
concat
(
selected_masks
,
0
).
astype
(
'float32'
)
return
selected_masks
return
selected_masks
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录