det_db_head.py 4.0 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 34 35
class Head(nn.Layer):
    def __init__(self, in_channels, name_list):
        super(Head, self).__init__()
D
dyning 已提交
36
        self.conv1 = nn.Conv2D(
W
WenmuZhou 已提交
37 38 39
            in_channels=in_channels,
            out_channels=in_channels // 4,
            kernel_size=3,
L
LDOUBLEV 已提交
40
            padding=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
41
            weight_attr=ParamAttr(),
L
LDOUBLEV 已提交
42
            bias_attr=False)
W
WenmuZhou 已提交
43 44 45 46 47 48 49
        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 已提交
50
        self.conv2 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
51 52 53
            in_channels=in_channels // 4,
            out_channels=in_channels // 4,
            kernel_size=2,
L
LDOUBLEV 已提交
54
            stride=2,
W
WenmuZhou 已提交
55
            weight_attr=ParamAttr(
W
WenmuZhou 已提交
56
                initializer=paddle.nn.initializer.KaimingUniform()),
littletomatodonkey's avatar
littletomatodonkey 已提交
57
            bias_attr=get_bias_attr(in_channels // 4))
W
WenmuZhou 已提交
58 59 60 61 62 63
        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 已提交
64
            act="relu")
D
dyning 已提交
65
        self.conv3 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
66 67 68
            in_channels=in_channels // 4,
            out_channels=1,
            kernel_size=2,
L
LDOUBLEV 已提交
69
            stride=2,
W
WenmuZhou 已提交
70
            weight_attr=ParamAttr(
W
WenmuZhou 已提交
71
                initializer=paddle.nn.initializer.KaimingUniform()),
littletomatodonkey's avatar
littletomatodonkey 已提交
72
            bias_attr=get_bias_attr(in_channels // 4), )
L
LDOUBLEV 已提交
73

W
WenmuZhou 已提交
74 75 76 77 78 79 80 81
    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 已提交
82 83


W
WenmuZhou 已提交
84 85 86 87 88 89 90
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 已提交
91

W
WenmuZhou 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
    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'
        ]
        self.binarize = Head(in_channels, binarize_name_list)
        self.thresh = Head(in_channels, thresh_name_list)
L
LDOUBLEV 已提交
105

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

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

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