kie_unet_sdmgr.py 5.9 KB
Newer Older
L
add kie  
LDOUBLEV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# 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.

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

import paddle
from paddle import nn
L
debug  
LDOUBLEV 已提交
21 22
import numpy as np
import cv2
L
add kie  
LDOUBLEV 已提交
23 24 25 26 27 28 29 30

__all__ = ["Kie_backbone"]


class Encoder(nn.Layer):
    def __init__(self, num_channels, num_filters):
        super(Encoder, self).__init__()
        self.conv1 = nn.Conv2D(
L
debug  
LDOUBLEV 已提交
31 32 33 34 35 36
            num_channels,
            num_filters,
            kernel_size=3,
            stride=1,
            padding=1,
            bias_attr=False)
L
add kie  
LDOUBLEV 已提交
37 38 39
        self.bn1 = nn.BatchNorm(num_filters, act='relu')

        self.conv2 = nn.Conv2D(
L
debug  
LDOUBLEV 已提交
40 41 42 43 44 45
            num_filters,
            num_filters,
            kernel_size=3,
            stride=1,
            padding=1,
            bias_attr=False)
L
add kie  
LDOUBLEV 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        self.bn2 = nn.BatchNorm(num_filters, act='relu')

        self.pool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)

    def forward(self, inputs):
        x = self.conv1(inputs)
        x = self.bn1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x_pooled = self.pool(x)
        return x, x_pooled


class Decoder(nn.Layer):
    def __init__(self, num_channels, num_filters):
        super(Decoder, self).__init__()
L
debug  
LDOUBLEV 已提交
62

L
add kie  
LDOUBLEV 已提交
63
        self.conv1 = nn.Conv2D(
L
debug  
LDOUBLEV 已提交
64 65 66 67 68 69
            num_channels,
            num_filters,
            kernel_size=3,
            stride=1,
            padding=1,
            bias_attr=False)
L
add kie  
LDOUBLEV 已提交
70 71 72
        self.bn1 = nn.BatchNorm(num_filters, act='relu')

        self.conv2 = nn.Conv2D(
L
debug  
LDOUBLEV 已提交
73 74 75 76 77 78
            num_filters,
            num_filters,
            kernel_size=3,
            stride=1,
            padding=1,
            bias_attr=False)
L
add kie  
LDOUBLEV 已提交
79 80
        self.bn2 = nn.BatchNorm(num_filters, act='relu')

L
debug  
LDOUBLEV 已提交
81 82 83 84 85 86 87 88 89
        self.conv0 = nn.Conv2D(
            num_channels,
            num_filters,
            kernel_size=1,
            stride=1,
            padding=0,
            bias_attr=False)
        self.bn0 = nn.BatchNorm(num_filters, act='relu')

L
add kie  
LDOUBLEV 已提交
90
    def forward(self, inputs_prev, inputs):
L
debug  
LDOUBLEV 已提交
91 92 93 94
        x = self.conv0(inputs)
        x = self.bn0(x)
        x = paddle.nn.functional.interpolate(
            x, scale_factor=2, mode='bilinear', align_corners=False)
L
add kie  
LDOUBLEV 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        x = paddle.concat([inputs_prev, x], axis=1)
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        return x


class UNet(nn.Layer):
    def __init__(self):
        super(UNet, self).__init__()
        self.down1 = Encoder(num_channels=3, num_filters=16)
        self.down2 = Encoder(num_channels=16, num_filters=32)
        self.down3 = Encoder(num_channels=32, num_filters=64)
        self.down4 = Encoder(num_channels=64, num_filters=128)
        self.down5 = Encoder(num_channels=128, num_filters=256)

        self.up1 = Decoder(32, 16)
L
debug  
LDOUBLEV 已提交
113 114 115
        self.up2 = Decoder(64, 32)
        self.up3 = Decoder(128, 64)
        self.up4 = Decoder(256, 128)
L
add kie  
LDOUBLEV 已提交
116 117 118
        self.out_channels = 16

    def forward(self, inputs):
L
debug  
LDOUBLEV 已提交
119 120 121 122 123
        x1, _ = self.down1(inputs)
        _, x2 = self.down2(x1)
        _, x3 = self.down3(x2)
        _, x4 = self.down4(x3)
        _, x5 = self.down5(x4)
L
add kie  
LDOUBLEV 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

        x = self.up4(x4, x5)
        x = self.up3(x3, x)
        x = self.up2(x2, x)
        x = self.up1(x1, x)
        return x


class Kie_backbone(nn.Layer):
    def __init__(self, in_channels, **kwargs):
        super(Kie_backbone, self).__init__()
        self.out_channels = 16
        self.img_feat = UNet()
        self.maxpool = nn.MaxPool2D(kernel_size=7)

    def bbox2roi(self, bbox_list):
        rois_list = []
        rois_num = []
        for img_id, bboxes in enumerate(bbox_list):
            rois_num.append(bboxes.shape[0])
            rois_list.append(bboxes)
        rois = paddle.concat(rois_list, 0)
        rois_num = paddle.to_tensor(rois_num, dtype='int32')
        return rois, rois_num

L
debug  
LDOUBLEV 已提交
149 150 151 152
    def pre_process(self, img, relations, texts, gt_bboxes, tag, img_size):
        img, relations, texts, gt_bboxes, tag, img_size = img.numpy(
        ), relations.numpy(), texts.numpy(), gt_bboxes.numpy(), tag.numpy(
        ).tolist(), img_size.numpy()
L
add kie  
LDOUBLEV 已提交
153
        temp_relations, temp_texts, temp_gt_bboxes = [], [], []
L
debug  
LDOUBLEV 已提交
154 155
        h, w = int(np.max(img_size[:, 0])), int(np.max(img_size[:, 1]))
        img = paddle.to_tensor(img[:, :, :h, :w])
L
add kie  
LDOUBLEV 已提交
156 157 158 159 160 161 162 163 164 165 166 167
        batch = len(tag)
        for i in range(batch):
            num, recoder_len = tag[i][0], tag[i][1]
            temp_relations.append(
                paddle.to_tensor(
                    relations[i, :num, :num, :], dtype='float32'))
            temp_texts.append(
                paddle.to_tensor(
                    texts[i, :num, :recoder_len], dtype='float32'))
            temp_gt_bboxes.append(
                paddle.to_tensor(
                    gt_bboxes[i, :num, ...], dtype='float32'))
L
debug  
LDOUBLEV 已提交
168
        return img, temp_relations, temp_texts, temp_gt_bboxes
L
add kie  
LDOUBLEV 已提交
169

L
LDOUBLEV 已提交
170 171 172 173
    def forward(self, images, inputs):
        img = images
        relations, texts, gt_bboxes, tag, img_size = inputs[0], inputs[
            1], inputs[2], inputs[4], inputs[-1]
L
debug  
LDOUBLEV 已提交
174 175
        img, relations, texts, gt_bboxes = self.pre_process(
            img, relations, texts, gt_bboxes, tag, img_size)
L
add kie  
LDOUBLEV 已提交
176 177 178 179 180 181 182 183 184 185 186
        x = self.img_feat(img)
        boxes, rois_num = self.bbox2roi(gt_bboxes)
        feats = paddle.fluid.layers.roi_align(
            x,
            boxes,
            spatial_scale=1.0,
            pooled_height=7,
            pooled_width=7,
            rois_num=rois_num)
        feats = self.maxpool(feats).squeeze(-1).squeeze(-1)
        return [relations, texts, feats]