det_db_head.py 4.1 KB
Newer Older
W
WenmuZhou 已提交
1
# copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
L
LDOUBLEV 已提交
2
#
W
WenmuZhou 已提交
3 4 5
# 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
L
LDOUBLEV 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
W
WenmuZhou 已提交
9 10 11 12 13
# 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.
L
LDOUBLEV 已提交
14 15 16 17 18 19

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
W
WenmuZhou 已提交
20 21 22 23
import paddle
from paddle import nn
import paddle.nn.functional as F
from paddle import ParamAttr
L
LDOUBLEV 已提交
24 25


littletomatodonkey's avatar
littletomatodonkey 已提交
26
def get_bias_attr(k):
W
WenmuZhou 已提交
27 28
    stdv = 1.0 / math.sqrt(k * 1.0)
    initializer = paddle.nn.initializer.Uniform(-stdv, stdv)
littletomatodonkey's avatar
littletomatodonkey 已提交
29
    bias_attr = ParamAttr(initializer=initializer)
W
WenmuZhou 已提交
30
    return bias_attr
L
LDOUBLEV 已提交
31 32


W
WenmuZhou 已提交
33
class Head(nn.Layer):
L
LDOUBLEV 已提交
34
    def __init__(self, in_channels, name_list, kernel_list=[3, 2, 2], **kwargs):
W
WenmuZhou 已提交
35
        super(Head, self).__init__()
L
LDOUBLEV 已提交
36

D
dyning 已提交
37
        self.conv1 = nn.Conv2D(
W
WenmuZhou 已提交
38 39
            in_channels=in_channels,
            out_channels=in_channels // 4,
L
fix  
LDOUBLEV 已提交
40 41
            kernel_size=kernel_list[0],
            padding=int(kernel_list[0] // 2),
littletomatodonkey's avatar
littletomatodonkey 已提交
42
            weight_attr=ParamAttr(),
L
LDOUBLEV 已提交
43
            bias_attr=False)
W
WenmuZhou 已提交
44 45 46 47 48 49 50
        self.conv_bn1 = nn.BatchNorm(
            num_channels=in_channels // 4,
            param_attr=ParamAttr(
                initializer=paddle.nn.initializer.Constant(value=1.0)),
            bias_attr=ParamAttr(
                initializer=paddle.nn.initializer.Constant(value=1e-4)),
            act='relu')
D
dyning 已提交
51
        self.conv2 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
52 53
            in_channels=in_channels // 4,
            out_channels=in_channels // 4,
L
fix  
LDOUBLEV 已提交
54
            kernel_size=kernel_list[1],
L
LDOUBLEV 已提交
55
            stride=2,
W
WenmuZhou 已提交
56
            weight_attr=ParamAttr(
W
WenmuZhou 已提交
57
                initializer=paddle.nn.initializer.KaimingUniform()),
littletomatodonkey's avatar
littletomatodonkey 已提交
58
            bias_attr=get_bias_attr(in_channels // 4))
W
WenmuZhou 已提交
59 60 61 62 63 64
        self.conv_bn2 = nn.BatchNorm(
            num_channels=in_channels // 4,
            param_attr=ParamAttr(
                initializer=paddle.nn.initializer.Constant(value=1.0)),
            bias_attr=ParamAttr(
                initializer=paddle.nn.initializer.Constant(value=1e-4)),
L
LDOUBLEV 已提交
65
            act="relu")
D
dyning 已提交
66
        self.conv3 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
67 68
            in_channels=in_channels // 4,
            out_channels=1,
L
fix  
LDOUBLEV 已提交
69
            kernel_size=kernel_list[2],
L
LDOUBLEV 已提交
70
            stride=2,
W
WenmuZhou 已提交
71
            weight_attr=ParamAttr(
W
WenmuZhou 已提交
72
                initializer=paddle.nn.initializer.KaimingUniform()),
littletomatodonkey's avatar
littletomatodonkey 已提交
73
            bias_attr=get_bias_attr(in_channels // 4), )
L
LDOUBLEV 已提交
74

W
WenmuZhou 已提交
75 76 77 78 79 80 81 82
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv_bn1(x)
        x = self.conv2(x)
        x = self.conv_bn2(x)
        x = self.conv3(x)
        x = F.sigmoid(x)
        return x
L
LDOUBLEV 已提交
83 84


W
WenmuZhou 已提交
85 86 87 88 89 90 91
class DBHead(nn.Layer):
    """
    Differentiable Binarization (DB) for text detection:
        see https://arxiv.org/abs/1911.08947
    args:
        params(dict): super parameters for build DB network
    """
L
LDOUBLEV 已提交
92

W
WenmuZhou 已提交
93 94 95 96 97 98 99 100 101 102 103
    def __init__(self, in_channels, k=50, **kwargs):
        super(DBHead, self).__init__()
        self.k = k
        binarize_name_list = [
            'conv2d_56', 'batch_norm_47', 'conv2d_transpose_0', 'batch_norm_48',
            'conv2d_transpose_1', 'binarize'
        ]
        thresh_name_list = [
            'conv2d_57', 'batch_norm_49', 'conv2d_transpose_2', 'batch_norm_50',
            'conv2d_transpose_3', 'thresh'
        ]
L
LDOUBLEV 已提交
104 105
        self.binarize = Head(in_channels, binarize_name_list, **kwargs)
        self.thresh = Head(in_channels, thresh_name_list, **kwargs)
L
LDOUBLEV 已提交
106

W
WenmuZhou 已提交
107 108
    def step_function(self, x, y):
        return paddle.reciprocal(1 + paddle.exp(-self.k * (x - y)))
L
LDOUBLEV 已提交
109

M
refine  
MissPenguin 已提交
110
    def forward(self, x, targets=None):
W
WenmuZhou 已提交
111 112
        shrink_maps = self.binarize(x)
        if not self.training:
W
WenmuZhou 已提交
113
            return {'maps': shrink_maps}
L
LDOUBLEV 已提交
114

W
WenmuZhou 已提交
115
        threshold_maps = self.thresh(x)
L
LDOUBLEV 已提交
116
        binary_maps = self.step_function(shrink_maps, threshold_maps)
W
WenmuZhou 已提交
117
        y = paddle.concat([shrink_maps, threshold_maps, binary_maps], axis=1)
W
WenmuZhou 已提交
118
        return {'maps': y}