det_db_head.py 4.5 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


W
WenmuZhou 已提交
26 27 28 29 30
def get_bias_attr(k, name):
    stdv = 1.0 / math.sqrt(k * 1.0)
    initializer = paddle.nn.initializer.Uniform(-stdv, stdv)
    bias_attr = ParamAttr(initializer=initializer, name=name + "_b_attr")
    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,
W
WenmuZhou 已提交
41
            weight_attr=ParamAttr(name=name_list[0] + '.w_0'),
L
LDOUBLEV 已提交
42
            bias_attr=False)
W
WenmuZhou 已提交
43 44 45 46 47 48 49 50 51 52 53
        self.conv_bn1 = nn.BatchNorm(
            num_channels=in_channels // 4,
            param_attr=ParamAttr(
                name=name_list[1] + '.w_0',
                initializer=paddle.nn.initializer.Constant(value=1.0)),
            bias_attr=ParamAttr(
                name=name_list[1] + '.b_0',
                initializer=paddle.nn.initializer.Constant(value=1e-4)),
            moving_mean_name=name_list[1] + '.w_1',
            moving_variance_name=name_list[1] + '.w_2',
            act='relu')
D
dyning 已提交
54
        self.conv2 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
55 56 57
            in_channels=in_channels // 4,
            out_channels=in_channels // 4,
            kernel_size=2,
L
LDOUBLEV 已提交
58
            stride=2,
W
WenmuZhou 已提交
59 60
            weight_attr=ParamAttr(
                name=name_list[2] + '.w_0',
W
WenmuZhou 已提交
61
                initializer=paddle.nn.initializer.KaimingUniform()),
W
WenmuZhou 已提交
62 63 64 65 66 67 68 69 70 71 72
            bias_attr=get_bias_attr(in_channels // 4, name_list[-1] + "conv2"))
        self.conv_bn2 = nn.BatchNorm(
            num_channels=in_channels // 4,
            param_attr=ParamAttr(
                name=name_list[3] + '.w_0',
                initializer=paddle.nn.initializer.Constant(value=1.0)),
            bias_attr=ParamAttr(
                name=name_list[3] + '.b_0',
                initializer=paddle.nn.initializer.Constant(value=1e-4)),
            moving_mean_name=name_list[3] + '.w_1',
            moving_variance_name=name_list[3] + '.w_2',
L
LDOUBLEV 已提交
73
            act="relu")
D
dyning 已提交
74
        self.conv3 = nn.Conv2DTranspose(
W
WenmuZhou 已提交
75 76 77
            in_channels=in_channels // 4,
            out_channels=1,
            kernel_size=2,
L
LDOUBLEV 已提交
78
            stride=2,
W
WenmuZhou 已提交
79 80
            weight_attr=ParamAttr(
                name=name_list[4] + '.w_0',
W
WenmuZhou 已提交
81
                initializer=paddle.nn.initializer.KaimingUniform()),
W
WenmuZhou 已提交
82 83
            bias_attr=get_bias_attr(in_channels // 4, name_list[-1] + "conv3"),
        )
L
LDOUBLEV 已提交
84

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


W
WenmuZhou 已提交
95 96 97 98 99 100 101
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 已提交
102

W
WenmuZhou 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
    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 已提交
116

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

W
WenmuZhou 已提交
120 121 122 123
    def forward(self, x):
        shrink_maps = self.binarize(x)
        if not self.training:
            return shrink_maps
L
LDOUBLEV 已提交
124

W
WenmuZhou 已提交
125
        threshold_maps = self.thresh(x)
L
LDOUBLEV 已提交
126
        binary_maps = self.step_function(shrink_maps, threshold_maps)
W
WenmuZhou 已提交
127 128
        y = paddle.concat([shrink_maps, threshold_maps, binary_maps], axis=1)
        return y