s2anet_head.py 41.5 KB
Newer Older
C
cnn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn.initializer import Normal, Constant
from ppdet.core.workspace import register
20
from ppdet.modeling import ops
C
cnn 已提交
21 22 23 24 25
from ppdet.modeling import bbox_utils
from ppdet.modeling.proposal_generator.target_layer import RBoxAssigner
import numpy as np


C
cnn 已提交
26
class S2ANetAnchorGenerator(nn.Layer):
C
cnn 已提交
27
    """
C
cnn 已提交
28
    AnchorGenerator by paddle
C
cnn 已提交
29 30
    """

C
cnn 已提交
31 32
    def __init__(self, base_size, scales, ratios, scale_major=True, ctr=None):
        super(S2ANetAnchorGenerator, self).__init__()
C
cnn 已提交
33
        self.base_size = base_size
C
cnn 已提交
34 35
        self.scales = paddle.to_tensor(scales)
        self.ratios = paddle.to_tensor(ratios)
C
cnn 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        self.scale_major = scale_major
        self.ctr = ctr
        self.base_anchors = self.gen_base_anchors()

    @property
    def num_base_anchors(self):
        return self.base_anchors.shape[0]

    def gen_base_anchors(self):
        w = self.base_size
        h = self.base_size
        if self.ctr is None:
            x_ctr = 0.5 * (w - 1)
            y_ctr = 0.5 * (h - 1)
        else:
            x_ctr, y_ctr = self.ctr

C
cnn 已提交
53
        h_ratios = paddle.sqrt(self.ratios)
C
cnn 已提交
54 55 56 57 58 59 60 61
        w_ratios = 1 / h_ratios
        if self.scale_major:
            ws = (w * w_ratios[:] * self.scales[:]).reshape([-1])
            hs = (h * h_ratios[:] * self.scales[:]).reshape([-1])
        else:
            ws = (w * self.scales[:] * w_ratios[:]).reshape([-1])
            hs = (h * self.scales[:] * h_ratios[:]).reshape([-1])

C
cnn 已提交
62
        base_anchors = paddle.stack(
C
cnn 已提交
63 64 65 66 67
            [
                x_ctr - 0.5 * (ws - 1), y_ctr - 0.5 * (hs - 1),
                x_ctr + 0.5 * (ws - 1), y_ctr + 0.5 * (hs - 1)
            ],
            axis=-1)
C
cnn 已提交
68
        base_anchors = paddle.round(base_anchors)
C
cnn 已提交
69 70 71
        return base_anchors

    def _meshgrid(self, x, y, row_major=True):
C
cnn 已提交
72
        yy, xx = paddle.meshgrid(y, x)
C
cnn 已提交
73 74
        yy = yy.reshape([-1])
        xx = xx.reshape([-1])
C
cnn 已提交
75 76 77 78 79
        if row_major:
            return xx, yy
        else:
            return yy, xx

C
cnn 已提交
80
    def forward(self, featmap_size, stride=16):
C
cnn 已提交
81
        # featmap_size*stride project it to original area
C
cnn 已提交
82 83 84 85 86

        feat_h = featmap_size[0]
        feat_w = featmap_size[1]
        shift_x = paddle.arange(0, feat_w, 1, 'int32') * stride
        shift_y = paddle.arange(0, feat_h, 1, 'int32') * stride
C
cnn 已提交
87
        shift_xx, shift_yy = self._meshgrid(shift_x, shift_y)
C
cnn 已提交
88
        shifts = paddle.stack([shift_xx, shift_yy, shift_xx, shift_yy], axis=-1)
C
cnn 已提交
89

C
cnn 已提交
90 91
        all_anchors = self.base_anchors[:, :] + shifts[:, :]
        all_anchors = all_anchors.reshape([feat_h * feat_w, 4])
C
cnn 已提交
92 93 94 95 96 97
        return all_anchors

    def valid_flags(self, featmap_size, valid_size):
        feat_h, feat_w = featmap_size
        valid_h, valid_w = valid_size
        assert valid_h <= feat_h and valid_w <= feat_w
C
cnn 已提交
98 99
        valid_x = paddle.zeros([feat_w], dtype='int32')
        valid_y = paddle.zeros([feat_h], dtype='int32')
C
cnn 已提交
100 101 102 103
        valid_x[:valid_w] = 1
        valid_y[:valid_h] = 1
        valid_xx, valid_yy = self._meshgrid(valid_x, valid_y)
        valid = valid_xx & valid_yy
C
cnn 已提交
104
        valid = paddle.reshape(valid, [-1, 1])
C
cnn 已提交
105
        valid = paddle.expand(valid, [-1, self.num_base_anchors]).reshape([-1])
C
cnn 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        return valid


class AlignConv(nn.Layer):
    def __init__(self, in_channels, out_channels, kernel_size=3, groups=1):
        super(AlignConv, self).__init__()
        self.kernel_size = kernel_size
        self.align_conv = paddle.vision.ops.DeformConv2D(
            in_channels,
            out_channels,
            kernel_size=self.kernel_size,
            padding=(self.kernel_size - 1) // 2,
            groups=groups,
            weight_attr=ParamAttr(initializer=Normal(0, 0.01)),
            bias_attr=None)

    @paddle.no_grad()
    def get_offset(self, anchors, featmap_size, stride):
        """
        Args:
            anchors: [M,5] xc,yc,w,h,angle
            featmap_size: (feat_h, feat_w)
            stride: 8
        Returns:

        """
        anchors = paddle.reshape(anchors, [-1, 5])  # (NA,5)
        dtype = anchors.dtype
C
cnn 已提交
134 135
        feat_h = featmap_size[0]
        feat_w = featmap_size[1]
C
cnn 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        pad = (self.kernel_size - 1) // 2
        idx = paddle.arange(-pad, pad + 1, dtype=dtype)

        yy, xx = paddle.meshgrid(idx, idx)
        xx = paddle.reshape(xx, [-1])
        yy = paddle.reshape(yy, [-1])

        # get sampling locations of default conv
        xc = paddle.arange(0, feat_w, dtype=dtype)
        yc = paddle.arange(0, feat_h, dtype=dtype)
        yc, xc = paddle.meshgrid(yc, xc)

        xc = paddle.reshape(xc, [-1, 1])
        yc = paddle.reshape(yc, [-1, 1])
        x_conv = xc + xx
        y_conv = yc + yy

        # get sampling locations of anchors
        # x_ctr, y_ctr, w, h, a = np.unbind(anchors, dim=1)
        x_ctr = anchors[:, 0]
        y_ctr = anchors[:, 1]
        w = anchors[:, 2]
        h = anchors[:, 3]
        a = anchors[:, 4]

C
cnn 已提交
161 162 163 164 165
        x_ctr = paddle.reshape(x_ctr, [-1, 1])
        y_ctr = paddle.reshape(y_ctr, [-1, 1])
        w = paddle.reshape(w, [-1, 1])
        h = paddle.reshape(h, [-1, 1])
        a = paddle.reshape(a, [-1, 1])
C
cnn 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

        x_ctr = x_ctr / stride
        y_ctr = y_ctr / stride
        w_s = w / stride
        h_s = h / stride
        cos, sin = paddle.cos(a), paddle.sin(a)
        dw, dh = w_s / self.kernel_size, h_s / self.kernel_size
        x, y = dw * xx, dh * yy
        xr = cos * x - sin * y
        yr = sin * x + cos * y
        x_anchor, y_anchor = xr + x_ctr, yr + y_ctr
        # get offset filed
        offset_x = x_anchor - x_conv
        offset_y = y_anchor - y_conv
        offset = paddle.stack([offset_y, offset_x], axis=-1)
C
cnn 已提交
181 182
        offset = paddle.reshape(
            offset, [feat_h * feat_w, self.kernel_size * self.kernel_size * 2])
C
cnn 已提交
183
        offset = paddle.transpose(offset, [1, 0])
C
cnn 已提交
184 185 186
        offset = paddle.reshape(
            offset,
            [1, self.kernel_size * self.kernel_size * 2, feat_h, feat_w])
C
cnn 已提交
187 188
        return offset

C
cnn 已提交
189
    def forward(self, x, refine_anchors, featmap_size, stride):
C
cnn 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        offset = self.get_offset(refine_anchors, featmap_size, stride)
        x = F.relu(self.align_conv(x, offset))
        return x


@register
class S2ANetHead(nn.Layer):
    """
    S2Anet head
    Args:
        stacked_convs (int): number of stacked_convs
        feat_in (int): input channels of feat
        feat_out (int): output channels of feat
        num_classes (int): num_classes
        anchor_strides (list): stride of anchors
        anchor_scales (list): scale of anchors
        anchor_ratios (list): ratios of anchors
        target_means (list): target_means
        target_stds (list): target_stds
        align_conv_type (str): align_conv_type ['Conv', 'AlignConv']
        align_conv_size (int): kernel size of align_conv
        use_sigmoid_cls (bool): use sigmoid_cls or not
C
cnn 已提交
212
        reg_loss_weight (list): loss weight for regression
C
cnn 已提交
213 214 215 216 217 218 219 220 221 222 223 224
    """
    __shared__ = ['num_classes']
    __inject__ = ['anchor_assign']

    def __init__(self,
                 stacked_convs=2,
                 feat_in=256,
                 feat_out=256,
                 num_classes=15,
                 anchor_strides=[8, 16, 32, 64, 128],
                 anchor_scales=[4],
                 anchor_ratios=[1.0],
C
cnn 已提交
225 226
                 target_means=0.0,
                 target_stds=1.0,
C
cnn 已提交
227 228 229 230
                 align_conv_type='AlignConv',
                 align_conv_size=3,
                 use_sigmoid_cls=True,
                 anchor_assign=RBoxAssigner().__dict__,
231
                 reg_loss_weight=[1.0, 1.0, 1.0, 1.0, 1.0],
C
cnn 已提交
232 233
                 cls_loss_weight=[1.0, 1.0],
                 reg_loss_type='l1'):
C
cnn 已提交
234 235 236 237 238 239 240 241
        super(S2ANetHead, self).__init__()
        self.stacked_convs = stacked_convs
        self.feat_in = feat_in
        self.feat_out = feat_out
        self.anchor_list = None
        self.anchor_scales = anchor_scales
        self.anchor_ratios = anchor_ratios
        self.anchor_strides = anchor_strides
C
cnn 已提交
242
        self.anchor_strides = paddle.to_tensor(anchor_strides)
C
cnn 已提交
243
        self.anchor_base_sizes = list(anchor_strides)
C
cnn 已提交
244 245
        self.means = paddle.ones(shape=[5]) * target_means
        self.stds = paddle.ones(shape=[5]) * target_stds
C
cnn 已提交
246
        assert align_conv_type in ['AlignConv', 'Conv', 'DCN']
C
cnn 已提交
247 248 249 250 251 252 253 254
        self.align_conv_type = align_conv_type
        self.align_conv_size = align_conv_size

        self.use_sigmoid_cls = use_sigmoid_cls
        self.cls_out_channels = num_classes if self.use_sigmoid_cls else 1
        self.sampling = False
        self.anchor_assign = anchor_assign
        self.reg_loss_weight = reg_loss_weight
255
        self.cls_loss_weight = cls_loss_weight
C
cnn 已提交
256 257 258
        self.alpha = 1.0
        self.beta = 1.0
        self.reg_loss_type = reg_loss_type
C
cnn 已提交
259 260 261 262 263 264

        self.s2anet_head_out = None

        # anchor
        self.anchor_generators = []
        for anchor_base in self.anchor_base_sizes:
C
cnn 已提交
265 266
            self.anchor_generators.append(
                S2ANetAnchorGenerator(anchor_base, anchor_scales,
C
cnn 已提交
267
                                      anchor_ratios))
C
cnn 已提交
268

C
cnn 已提交
269
        self.anchor_generators = nn.LayerList(self.anchor_generators)
C
cnn 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        self.fam_cls_convs = nn.Sequential()
        self.fam_reg_convs = nn.Sequential()

        for i in range(self.stacked_convs):
            chan_in = self.feat_in if i == 0 else self.feat_out

            self.fam_cls_convs.add_sublayer(
                'fam_cls_conv_{}'.format(i),
                nn.Conv2D(
                    in_channels=chan_in,
                    out_channels=self.feat_out,
                    kernel_size=3,
                    padding=1,
                    weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                    bias_attr=ParamAttr(initializer=Constant(0))))

            self.fam_cls_convs.add_sublayer('fam_cls_conv_{}_act'.format(i),
                                            nn.ReLU())

            self.fam_reg_convs.add_sublayer(
                'fam_reg_conv_{}'.format(i),
                nn.Conv2D(
                    in_channels=chan_in,
                    out_channels=self.feat_out,
                    kernel_size=3,
                    padding=1,
                    weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                    bias_attr=ParamAttr(initializer=Constant(0))))

            self.fam_reg_convs.add_sublayer('fam_reg_conv_{}_act'.format(i),
                                            nn.ReLU())

        self.fam_reg = nn.Conv2D(
            self.feat_out,
            5,
            1,
            weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
            bias_attr=ParamAttr(initializer=Constant(0)))
        prior_prob = 0.01
        bias_init = float(-np.log((1 - prior_prob) / prior_prob))
        self.fam_cls = nn.Conv2D(
            self.feat_out,
            self.cls_out_channels,
            1,
            weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
            bias_attr=ParamAttr(initializer=Constant(bias_init)))

        if self.align_conv_type == "AlignConv":
            self.align_conv = AlignConv(self.feat_out, self.feat_out,
                                        self.align_conv_size)
        elif self.align_conv_type == "Conv":
            self.align_conv = nn.Conv2D(
                self.feat_out,
                self.feat_out,
                self.align_conv_size,
                padding=(self.align_conv_size - 1) // 2,
                bias_attr=ParamAttr(initializer=Constant(0)))

C
cnn 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        elif self.align_conv_type == "DCN":
            self.align_conv_offset = nn.Conv2D(
                self.feat_out,
                2 * self.align_conv_size**2,
                1,
                weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                bias_attr=ParamAttr(initializer=Constant(0)))

            self.align_conv = paddle.vision.ops.DeformConv2D(
                self.feat_out,
                self.feat_out,
                self.align_conv_size,
                padding=(self.align_conv_size - 1) // 2,
                weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                bias_attr=False)

C
cnn 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        self.or_conv = nn.Conv2D(
            self.feat_out,
            self.feat_out,
            kernel_size=3,
            padding=1,
            weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
            bias_attr=ParamAttr(initializer=Constant(0)))

        # ODM
        self.odm_cls_convs = nn.Sequential()
        self.odm_reg_convs = nn.Sequential()

        for i in range(self.stacked_convs):
            ch_in = self.feat_out
            # ch_in = int(self.feat_out / 8) if i == 0 else self.feat_out

            self.odm_cls_convs.add_sublayer(
                'odm_cls_conv_{}'.format(i),
                nn.Conv2D(
                    in_channels=ch_in,
                    out_channels=self.feat_out,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                    bias_attr=ParamAttr(initializer=Constant(0))))

            self.odm_cls_convs.add_sublayer('odm_cls_conv_{}_act'.format(i),
                                            nn.ReLU())

            self.odm_reg_convs.add_sublayer(
                'odm_reg_conv_{}'.format(i),
                nn.Conv2D(
                    in_channels=self.feat_out,
                    out_channels=self.feat_out,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
                    bias_attr=ParamAttr(initializer=Constant(0))))

            self.odm_reg_convs.add_sublayer('odm_reg_conv_{}_act'.format(i),
                                            nn.ReLU())

        self.odm_cls = nn.Conv2D(
            self.feat_out,
            self.cls_out_channels,
            3,
            padding=1,
            weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
            bias_attr=ParamAttr(initializer=Constant(bias_init)))
        self.odm_reg = nn.Conv2D(
            self.feat_out,
            5,
            3,
            padding=1,
            weight_attr=ParamAttr(initializer=Normal(0.0, 0.01)),
            bias_attr=ParamAttr(initializer=Constant(0)))

C
cnn 已提交
403 404
        self.featmap_sizes = []
        self.base_anchors_list = []
C
cnn 已提交
405 406
        self.refine_anchor_list = []

C
cnn 已提交
407 408 409 410 411 412 413
    def forward(self, feats):
        fam_reg_branch_list = []
        fam_cls_branch_list = []

        odm_reg_branch_list = []
        odm_cls_branch_list = []

C
cnn 已提交
414 415
        self.featmap_sizes_list = []
        self.base_anchors_list = []
C
cnn 已提交
416 417
        self.refine_anchor_list = []

C
cnn 已提交
418 419
        for feat_idx in range(len(feats)):
            feat = feats[feat_idx]
C
cnn 已提交
420
            fam_cls_feat = self.fam_cls_convs(feat)
421

C
cnn 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
            fam_cls = self.fam_cls(fam_cls_feat)
            # [N, CLS, H, W] --> [N, H, W, CLS]
            fam_cls = fam_cls.transpose([0, 2, 3, 1])
            fam_cls_reshape = paddle.reshape(
                fam_cls, [fam_cls.shape[0], -1, self.cls_out_channels])
            fam_cls_branch_list.append(fam_cls_reshape)

            fam_reg_feat = self.fam_reg_convs(feat)

            fam_reg = self.fam_reg(fam_reg_feat)
            # [N, 5, H, W] --> [N, H, W, 5]
            fam_reg = fam_reg.transpose([0, 2, 3, 1])
            fam_reg_reshape = paddle.reshape(fam_reg, [fam_reg.shape[0], -1, 5])
            fam_reg_branch_list.append(fam_reg_reshape)

437
            # prepare anchor
C
cnn 已提交
438 439 440 441 442 443 444
            featmap_size = (paddle.shape(feat)[2], paddle.shape(feat)[3])
            self.featmap_sizes_list.append(featmap_size)
            init_anchors = self.anchor_generators[feat_idx](
                featmap_size, self.anchor_strides[feat_idx])

            init_anchors = paddle.to_tensor(init_anchors, dtype='float32')
            NA = featmap_size[0] * featmap_size[1]
C
cnn 已提交
445
            init_anchors = paddle.reshape(init_anchors, [NA, 4])
C
cnn 已提交
446 447 448 449 450 451 452
            init_anchors = self.rect2rbox(init_anchors)
            self.base_anchors_list.append(init_anchors)

            fam_reg1 = fam_reg
            fam_reg1.stop_gradient = True
            refine_anchor = self.bbox_decode(fam_reg1, init_anchors)
            #refine_anchor = self.bbox_decode(fam_reg.detach(), init_anchors)
453

C
cnn 已提交
454 455 456 457 458
            self.refine_anchor_list.append(refine_anchor)

            if self.align_conv_type == 'AlignConv':
                align_feat = self.align_conv(feat,
                                             refine_anchor.clone(),
C
cnn 已提交
459 460
                                             featmap_size,
                                             self.anchor_strides[feat_idx])
C
cnn 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            elif self.align_conv_type == 'DCN':
                align_offset = self.align_conv_offset(feat)
                align_feat = self.align_conv(feat, align_offset)
            elif self.align_conv_type == 'Conv':
                align_feat = self.align_conv(feat)

            or_feat = self.or_conv(align_feat)
            odm_reg_feat = or_feat
            odm_cls_feat = or_feat

            odm_reg_feat = self.odm_reg_convs(odm_reg_feat)
            odm_cls_feat = self.odm_cls_convs(odm_cls_feat)

            odm_cls_score = self.odm_cls(odm_cls_feat)
            # [N, CLS, H, W] --> [N, H, W, CLS]
            odm_cls_score = odm_cls_score.transpose([0, 2, 3, 1])
C
cnn 已提交
477
            odm_cls_score_shape = odm_cls_score.shape
C
cnn 已提交
478 479 480 481
            odm_cls_score_reshape = paddle.reshape(odm_cls_score, [
                odm_cls_score_shape[0], odm_cls_score_shape[1] *
                odm_cls_score_shape[2], self.cls_out_channels
            ])
C
cnn 已提交
482 483 484 485 486 487

            odm_cls_branch_list.append(odm_cls_score_reshape)

            odm_bbox_pred = self.odm_reg(odm_reg_feat)
            # [N, 5, H, W] --> [N, H, W, 5]
            odm_bbox_pred = odm_bbox_pred.transpose([0, 2, 3, 1])
C
cnn 已提交
488 489 490
            odm_bbox_pred_reshape = paddle.reshape(odm_bbox_pred, [-1, 5])
            odm_bbox_pred_reshape = paddle.unsqueeze(
                odm_bbox_pred_reshape, axis=0)
C
cnn 已提交
491 492 493 494 495 496
            odm_reg_branch_list.append(odm_bbox_pred_reshape)

        self.s2anet_head_out = (fam_cls_branch_list, fam_reg_branch_list,
                                odm_cls_branch_list, odm_reg_branch_list)
        return self.s2anet_head_out

C
cnn 已提交
497
    def get_prediction(self, nms_pre=2000):
C
cnn 已提交
498
        refine_anchors = self.refine_anchor_list
C
cnn 已提交
499 500 501 502
        fam_cls_branch_list = self.s2anet_head_out[0]
        fam_reg_branch_list = self.s2anet_head_out[1]
        odm_cls_branch_list = self.s2anet_head_out[2]
        odm_reg_branch_list = self.s2anet_head_out[3]
C
cnn 已提交
503
        pred_scores, pred_bboxes = self.get_bboxes(
C
cnn 已提交
504 505
            odm_cls_branch_list, odm_reg_branch_list, refine_anchors, nms_pre,
            self.cls_out_channels, self.use_sigmoid_cls)
C
cnn 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
        return pred_scores, pred_bboxes

    def smooth_l1_loss(self, pred, label, delta=1.0 / 9.0):
        """
        Args:
            pred: pred score
            label: label
            delta: delta
        Returns: loss
        """
        assert pred.shape == label.shape and label.numel() > 0
        assert delta > 0
        diff = paddle.abs(pred - label)
        loss = paddle.where(diff < delta, 0.5 * diff * diff / delta,
                            diff - 0.5 * delta)
        return loss

C
cnn 已提交
523
    def get_fam_loss(self, fam_target, s2anet_head_out, reg_loss_type='gwd'):
C
cnn 已提交
524 525
        (labels, label_weights, bbox_targets, bbox_weights, bbox_gt_bboxes,
         pos_inds, neg_inds) = fam_target
526 527 528 529 530
        fam_cls_branch_list, fam_reg_branch_list, odm_cls_branch_list, odm_reg_branch_list = s2anet_head_out

        fam_cls_losses = []
        fam_bbox_losses = []
        st_idx = 0
C
cnn 已提交
531 532 533 534
        num_total_samples = len(pos_inds) + len(
            neg_inds) if self.sampling else len(pos_inds)
        num_total_samples = max(1, num_total_samples)

C
cnn 已提交
535
        for idx, feat_size in enumerate(self.featmap_sizes_list):
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
            feat_anchor_num = feat_size[0] * feat_size[1]

            # step1:  get data
            feat_labels = labels[st_idx:st_idx + feat_anchor_num]
            feat_label_weights = label_weights[st_idx:st_idx + feat_anchor_num]

            feat_bbox_targets = bbox_targets[st_idx:st_idx + feat_anchor_num, :]
            feat_bbox_weights = bbox_weights[st_idx:st_idx + feat_anchor_num, :]

            # step2: calc cls loss
            feat_labels = feat_labels.reshape(-1)
            feat_label_weights = feat_label_weights.reshape(-1)

            fam_cls_score = fam_cls_branch_list[idx]
            fam_cls_score = paddle.squeeze(fam_cls_score, axis=0)
            fam_cls_score1 = fam_cls_score

            feat_labels = paddle.to_tensor(feat_labels)
C
cnn 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
            feat_labels_one_hot = paddle.nn.functional.one_hot(
                feat_labels, self.cls_out_channels + 1)
            feat_labels_one_hot = feat_labels_one_hot[:, 1:]
            feat_labels_one_hot.stop_gradient = True

            num_total_samples = paddle.to_tensor(
                num_total_samples, dtype='float32', stop_gradient=True)

            fam_cls = F.sigmoid_focal_loss(
                fam_cls_score1,
                feat_labels_one_hot,
                normalizer=num_total_samples,
                reduction='none')

            feat_label_weights = feat_label_weights.reshape(
                feat_label_weights.shape[0], 1)
            feat_label_weights = np.repeat(
                feat_label_weights, self.cls_out_channels, axis=1)
            feat_label_weights = paddle.to_tensor(
                feat_label_weights, stop_gradient=True)

            fam_cls = fam_cls * feat_label_weights
            fam_cls_total = paddle.sum(fam_cls)
577 578 579 580 581 582 583 584 585 586
            fam_cls_losses.append(fam_cls_total)

            # step3: regression loss
            feat_bbox_targets = paddle.to_tensor(
                feat_bbox_targets, dtype='float32', stop_gradient=True)
            feat_bbox_targets = paddle.reshape(feat_bbox_targets, [-1, 5])

            fam_bbox_pred = fam_reg_branch_list[idx]
            fam_bbox_pred = paddle.squeeze(fam_bbox_pred, axis=0)
            fam_bbox_pred = paddle.reshape(fam_bbox_pred, [-1, 5])
C
cnn 已提交
587 588 589 590 591 592 593
            fam_bbox = self.smooth_l1_loss(fam_bbox_pred, feat_bbox_targets)
            loss_weight = paddle.to_tensor(
                self.reg_loss_weight, dtype='float32', stop_gradient=True)
            fam_bbox = paddle.multiply(fam_bbox, loss_weight)
            feat_bbox_weights = paddle.to_tensor(
                feat_bbox_weights, stop_gradient=True)

C
cnn 已提交
594 595 596
            if reg_loss_type == 'l1':
                fam_bbox = fam_bbox * feat_bbox_weights
                fam_bbox_total = paddle.sum(fam_bbox) / num_total_samples
C
cnn 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
            elif reg_loss_type == 'iou' or reg_loss_type == 'gwd':
                fam_bbox = paddle.sum(fam_bbox, axis=-1)
                feat_bbox_weights = paddle.sum(feat_bbox_weights, axis=-1)
                try:
                    from rbox_iou_ops import rbox_iou
                except Exception as e:
                    print("import custom_ops error, try install rbox_iou_ops " \
                          "following ppdet/ext_op/README.md", e)
                    sys.stdout.flush()
                    sys.exit(-1)
                # calc iou
                fam_bbox_decode = self.delta2rbox(self.base_anchors_list[idx],
                                                  fam_bbox_pred)
                bbox_gt_bboxes = paddle.to_tensor(
                    bbox_gt_bboxes,
                    dtype=fam_bbox_decode.dtype,
                    place=fam_bbox_decode.place)
                bbox_gt_bboxes.stop_gradient = True
                iou = rbox_iou(fam_bbox_decode, bbox_gt_bboxes)
                iou = paddle.diag(iou)

                if reg_loss_type == 'iou':
                    EPS = paddle.to_tensor(
                        1e-8, dtype='float32', stop_gradient=True)
                    iou_factor = -1.0 * paddle.log(iou + EPS) / (fam_bbox + EPS)
                    iou_factor.stop_gradient = True
                    #fam_bbox = fam_bbox * iou_factor
                elif reg_loss_type == 'gwd':
                    bbox_gt_bboxes_level = bbox_gt_bboxes[st_idx:st_idx +
                                                          feat_anchor_num, :]
                    fam_bbox_total = self.gwd_loss(fam_bbox_decode,
                                                   bbox_gt_bboxes_level)
                    fam_bbox_total = fam_bbox_total * feat_bbox_weights
                    fam_bbox_total = paddle.sum(fam_bbox_total)
631 632

            fam_bbox_losses.append(fam_bbox_total)
C
cnn 已提交
633
            st_idx += feat_anchor_num
634 635

        fam_cls_loss = paddle.add_n(fam_cls_losses)
636 637
        fam_cls_loss_weight = paddle.to_tensor(
            self.cls_loss_weight[0], dtype='float32', stop_gradient=True)
638 639
        fam_cls_loss = fam_cls_loss * fam_cls_loss_weight
        fam_reg_loss = paddle.add_n(fam_bbox_losses)
C
cnn 已提交
640 641
        return fam_cls_loss, fam_reg_loss

C
cnn 已提交
642
    def get_odm_loss(self, odm_target, s2anet_head_out, reg_loss_type='gwd'):
C
cnn 已提交
643 644
        (labels, label_weights, bbox_targets, bbox_weights, bbox_gt_bboxes,
         pos_inds, neg_inds) = odm_target
645 646 647 648 649
        fam_cls_branch_list, fam_reg_branch_list, odm_cls_branch_list, odm_reg_branch_list = s2anet_head_out

        odm_cls_losses = []
        odm_bbox_losses = []
        st_idx = 0
C
cnn 已提交
650 651 652
        num_total_samples = len(pos_inds) + len(
            neg_inds) if self.sampling else len(pos_inds)
        num_total_samples = max(1, num_total_samples)
C
cnn 已提交
653

C
cnn 已提交
654
        for idx, feat_size in enumerate(self.featmap_sizes_list):
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
            feat_anchor_num = feat_size[0] * feat_size[1]

            # step1:  get data
            feat_labels = labels[st_idx:st_idx + feat_anchor_num]
            feat_label_weights = label_weights[st_idx:st_idx + feat_anchor_num]

            feat_bbox_targets = bbox_targets[st_idx:st_idx + feat_anchor_num, :]
            feat_bbox_weights = bbox_weights[st_idx:st_idx + feat_anchor_num, :]

            # step2: calc cls loss
            feat_labels = feat_labels.reshape(-1)
            feat_label_weights = feat_label_weights.reshape(-1)

            odm_cls_score = odm_cls_branch_list[idx]
            odm_cls_score = paddle.squeeze(odm_cls_score, axis=0)
            odm_cls_score1 = odm_cls_score

            feat_labels = paddle.to_tensor(feat_labels)
C
cnn 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
            feat_labels_one_hot = paddle.nn.functional.one_hot(
                feat_labels, self.cls_out_channels + 1)
            feat_labels_one_hot = feat_labels_one_hot[:, 1:]
            feat_labels_one_hot.stop_gradient = True

            num_total_samples = paddle.to_tensor(
                num_total_samples, dtype='float32', stop_gradient=True)
            odm_cls = F.sigmoid_focal_loss(
                odm_cls_score1,
                feat_labels_one_hot,
                normalizer=num_total_samples,
                reduction='none')

            feat_label_weights = feat_label_weights.reshape(
                feat_label_weights.shape[0], 1)
            feat_label_weights = np.repeat(
                feat_label_weights, self.cls_out_channels, axis=1)
            feat_label_weights = paddle.to_tensor(feat_label_weights)
            feat_label_weights.stop_gradient = True

            odm_cls = odm_cls * feat_label_weights
            odm_cls_total = paddle.sum(odm_cls)
695 696 697 698 699 700 701 702 703 704 705 706
            odm_cls_losses.append(odm_cls_total)

            # # step3: regression loss
            feat_bbox_targets = paddle.to_tensor(
                feat_bbox_targets, dtype='float32')
            feat_bbox_targets = paddle.reshape(feat_bbox_targets, [-1, 5])
            feat_bbox_targets.stop_gradient = True

            odm_bbox_pred = odm_reg_branch_list[idx]
            odm_bbox_pred = paddle.squeeze(odm_bbox_pred, axis=0)
            odm_bbox_pred = paddle.reshape(odm_bbox_pred, [-1, 5])
            odm_bbox = self.smooth_l1_loss(odm_bbox_pred, feat_bbox_targets)
C
cnn 已提交
707 708 709 710 711 712 713

            loss_weight = paddle.to_tensor(
                self.reg_loss_weight, dtype='float32', stop_gradient=True)
            odm_bbox = paddle.multiply(odm_bbox, loss_weight)
            feat_bbox_weights = paddle.to_tensor(
                feat_bbox_weights, stop_gradient=True)

C
cnn 已提交
714 715 716
            if reg_loss_type == 'l1':
                odm_bbox = odm_bbox * feat_bbox_weights
                odm_bbox_total = paddle.sum(odm_bbox) / num_total_samples
C
cnn 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
            elif reg_loss_type == 'iou' or reg_loss_type == 'gwd':
                odm_bbox = paddle.sum(odm_bbox, axis=-1)
                feat_bbox_weights = paddle.sum(feat_bbox_weights, axis=-1)
                try:
                    from rbox_iou_ops import rbox_iou
                except Exception as e:
                    print("import custom_ops error, try install rbox_iou_ops " \
                          "following ppdet/ext_op/README.md", e)
                    sys.stdout.flush()
                    sys.exit(-1)
                # calc iou
                odm_bbox_decode = self.delta2rbox(self.refine_anchor_list[idx],
                                                  odm_bbox_pred)
                bbox_gt_bboxes = paddle.to_tensor(
                    bbox_gt_bboxes,
                    dtype=odm_bbox_decode.dtype,
                    place=odm_bbox_decode.place)
                bbox_gt_bboxes.stop_gradient = True
                iou = rbox_iou(odm_bbox_decode, bbox_gt_bboxes)
                iou = paddle.diag(iou)

                if reg_loss_type == 'iou':
                    EPS = paddle.to_tensor(
                        1e-8, dtype='float32', stop_gradient=True)
                    iou_factor = -1.0 * paddle.log(iou + EPS) / (odm_bbox + EPS)
                    iou_factor.stop_gradient = True
                    # odm_bbox = odm_bbox * iou_factor
                elif reg_loss_type == 'gwd':
                    bbox_gt_bboxes_level = bbox_gt_bboxes[st_idx:st_idx +
                                                          feat_anchor_num, :]
                    odm_bbox_total = self.gwd_loss(odm_bbox_decode,
                                                   bbox_gt_bboxes_level)
                    odm_bbox_total = odm_bbox_total * feat_bbox_weights
                    odm_bbox_total = paddle.sum(odm_bbox_total)

752
            odm_bbox_losses.append(odm_bbox_total)
C
cnn 已提交
753
            st_idx += feat_anchor_num
754 755

        odm_cls_loss = paddle.add_n(odm_cls_losses)
756
        odm_cls_loss_weight = paddle.to_tensor(
757 758 759
            self.cls_loss_weight[1], dtype='float32', stop_gradient=True)
        odm_cls_loss = odm_cls_loss * odm_cls_loss_weight
        odm_reg_loss = paddle.add_n(odm_bbox_losses)
C
cnn 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
        return odm_cls_loss, odm_reg_loss

    def get_loss(self, inputs):
        # inputs: im_id image im_shape scale_factor gt_bbox gt_class is_crowd

        # compute loss
        fam_cls_loss_lst = []
        fam_reg_loss_lst = []
        odm_cls_loss_lst = []
        odm_reg_loss_lst = []

        im_shape = inputs['im_shape']
        for im_id in range(im_shape.shape[0]):
            np_im_shape = inputs['im_shape'][im_id].numpy()
            np_scale_factor = inputs['scale_factor'][im_id].numpy()
            # data_format: (xc, yc, w, h, theta)
            gt_bboxes = inputs['gt_rbox'][im_id].numpy()
            gt_labels = inputs['gt_class'][im_id].numpy()
            is_crowd = inputs['is_crowd'][im_id].numpy()
            gt_labels = gt_labels + 1

781
            # featmap_sizes
C
cnn 已提交
782
            anchors_list_all = np.concatenate(self.base_anchors_list)
783 784 785 786 787 788 789 790 791

            # get im_feat
            fam_cls_feats_list = [e[im_id] for e in self.s2anet_head_out[0]]
            fam_reg_feats_list = [e[im_id] for e in self.s2anet_head_out[1]]
            odm_cls_feats_list = [e[im_id] for e in self.s2anet_head_out[2]]
            odm_reg_feats_list = [e[im_id] for e in self.s2anet_head_out[3]]
            im_s2anet_head_out = (fam_cls_feats_list, fam_reg_feats_list,
                                  odm_cls_feats_list, odm_reg_feats_list)

C
cnn 已提交
792
            # FAM
793 794 795
            im_fam_target = self.anchor_assign(anchors_list_all, gt_bboxes,
                                               gt_labels, is_crowd)
            if im_fam_target is not None:
C
cnn 已提交
796
                im_fam_cls_loss, im_fam_reg_loss = self.get_fam_loss(
C
cnn 已提交
797
                    im_fam_target, im_s2anet_head_out, self.reg_loss_type)
C
cnn 已提交
798 799 800 801
                fam_cls_loss_lst.append(im_fam_cls_loss)
                fam_reg_loss_lst.append(im_fam_reg_loss)

            # ODM
C
cnn 已提交
802 803
            np_refine_anchors_list = paddle.concat(
                self.refine_anchor_list).numpy()
C
cnn 已提交
804 805
            np_refine_anchors_list = np.concatenate(np_refine_anchors_list)
            np_refine_anchors_list = np_refine_anchors_list.reshape(-1, 5)
C
cnn 已提交
806 807
            im_odm_target = self.anchor_assign(np_refine_anchors_list,
                                               gt_bboxes, gt_labels, is_crowd)
C
cnn 已提交
808

809
            if im_odm_target is not None:
C
cnn 已提交
810
                im_odm_cls_loss, im_odm_reg_loss = self.get_odm_loss(
C
cnn 已提交
811
                    im_odm_target, im_s2anet_head_out, self.reg_loss_type)
C
cnn 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
                odm_cls_loss_lst.append(im_odm_cls_loss)
                odm_reg_loss_lst.append(im_odm_reg_loss)
        fam_cls_loss = paddle.add_n(fam_cls_loss_lst)
        fam_reg_loss = paddle.add_n(fam_reg_loss_lst)
        odm_cls_loss = paddle.add_n(odm_cls_loss_lst)
        odm_reg_loss = paddle.add_n(odm_reg_loss_lst)
        return {
            'fam_cls_loss': fam_cls_loss,
            'fam_reg_loss': fam_reg_loss,
            'odm_cls_loss': odm_cls_loss,
            'odm_reg_loss': odm_reg_loss
        }

    def get_bboxes(self, cls_score_list, bbox_pred_list, mlvl_anchors, nms_pre,
                   cls_out_channels, use_sigmoid_cls):
        assert len(cls_score_list) == len(bbox_pred_list) == len(mlvl_anchors)

        mlvl_bboxes = []
        mlvl_scores = []

        idx = 0
        for cls_score, bbox_pred, anchors in zip(cls_score_list, bbox_pred_list,
                                                 mlvl_anchors):
            cls_score = paddle.reshape(cls_score, [-1, cls_out_channels])
            if use_sigmoid_cls:
                scores = F.sigmoid(cls_score)
            else:
                scores = F.softmax(cls_score, axis=-1)

            # bbox_pred = bbox_pred.permute(1, 2, 0).reshape(-1, 5)
            bbox_pred = paddle.transpose(bbox_pred, [1, 2, 0])
            bbox_pred = paddle.reshape(bbox_pred, [-1, 5])
            anchors = paddle.reshape(anchors, [-1, 5])

C
cnn 已提交
846
            if scores.shape[0] > nms_pre:
C
cnn 已提交
847 848 849 850 851 852 853 854 855 856 857
                # Get maximum scores for foreground classes.
                if use_sigmoid_cls:
                    max_scores = paddle.max(scores, axis=1)
                else:
                    max_scores = paddle.max(scores[:, 1:], axis=1)

                topk_val, topk_inds = paddle.topk(max_scores, nms_pre)
                anchors = paddle.gather(anchors, topk_inds)
                bbox_pred = paddle.gather(bbox_pred, topk_inds)
                scores = paddle.gather(scores, topk_inds)

C
cnn 已提交
858 859
            bbox_delta = paddle.reshape(bbox_pred, [-1, 5])
            bboxes = self.delta2rbox(anchors, bbox_delta)
C
cnn 已提交
860 861 862 863 864 865 866 867 868
            mlvl_bboxes.append(bboxes)
            mlvl_scores.append(scores)

            idx += 1

        mlvl_bboxes = paddle.concat(mlvl_bboxes, axis=0)
        mlvl_scores = paddle.concat(mlvl_scores)

        return mlvl_scores, mlvl_bboxes
C
cnn 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

    def rect2rbox(self, bboxes):
        """
        :param bboxes: shape (n, 4) (xmin, ymin, xmax, ymax)
        :return: dbboxes: shape (n, 5) (x_ctr, y_ctr, w, h, angle)
        """
        bboxes = paddle.reshape(bboxes, [-1, 4])
        num_boxes = paddle.shape(bboxes)[0]
        x_ctr = (bboxes[:, 2] + bboxes[:, 0]) / 2.0
        y_ctr = (bboxes[:, 3] + bboxes[:, 1]) / 2.0
        edges1 = paddle.abs(bboxes[:, 2] - bboxes[:, 0])
        edges2 = paddle.abs(bboxes[:, 3] - bboxes[:, 1])

        rbox_w = paddle.maximum(edges1, edges2)
        rbox_h = paddle.minimum(edges1, edges2)

        # set angle
        inds = edges1 < edges2
        inds = paddle.cast(inds, 'int32')
        rboxes_angle = inds * np.pi / 2.0

        rboxes = paddle.stack(
            (x_ctr, y_ctr, rbox_w, rbox_h, rboxes_angle), axis=1)
        return rboxes

    # deltas to rbox
    def delta2rbox(self, rrois, deltas, wh_ratio_clip=1e-6):
        """
        :param rrois: (cx, cy, w, h, theta)
        :param deltas: (dx, dy, dw, dh, dtheta)
        :param means: means of anchor
        :param stds: stds of anchor
        :param wh_ratio_clip: clip threshold of wh_ratio
        :return:
        """
        deltas = paddle.reshape(deltas, [-1, 5])
        rrois = paddle.reshape(rrois, [-1, 5])
        # fix dy2st bug denorm_deltas = deltas * self.stds + self.means
C
cnn 已提交
907 908
        denorm_deltas = paddle.add(
            paddle.multiply(deltas, self.stds), self.means)
C
cnn 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

        dx = denorm_deltas[:, 0]
        dy = denorm_deltas[:, 1]
        dw = denorm_deltas[:, 2]
        dh = denorm_deltas[:, 3]
        dangle = denorm_deltas[:, 4]
        max_ratio = np.abs(np.log(wh_ratio_clip))
        dw = paddle.clip(dw, min=-max_ratio, max=max_ratio)
        dh = paddle.clip(dh, min=-max_ratio, max=max_ratio)

        rroi_x = rrois[:, 0]
        rroi_y = rrois[:, 1]
        rroi_w = rrois[:, 2]
        rroi_h = rrois[:, 3]
        rroi_angle = rrois[:, 4]

        gx = dx * rroi_w * paddle.cos(rroi_angle) - dy * rroi_h * paddle.sin(
            rroi_angle) + rroi_x
        gy = dx * rroi_w * paddle.sin(rroi_angle) + dy * rroi_h * paddle.cos(
            rroi_angle) + rroi_y
        gw = rroi_w * dw.exp()
        gh = rroi_h * dh.exp()
        ga = np.pi * dangle + rroi_angle
        ga = (ga + np.pi / 4) % np.pi - np.pi / 4
        ga = paddle.to_tensor(ga)
        gw = paddle.to_tensor(gw, dtype='float32')
        gh = paddle.to_tensor(gh, dtype='float32')
        bboxes = paddle.stack([gx, gy, gw, gh, ga], axis=-1)
        return bboxes

C
cnn 已提交
939
    def bbox_decode(self, bbox_preds, anchors):
C
cnn 已提交
940 941 942 943 944 945 946 947 948 949 950
        """decode bbox from deltas
        Args:
            bbox_preds: [N,H,W,5]
            anchors: [H*W,5]
        return:
            bboxes: [N,H,W,5]
        """
        num_imgs, H, W, _ = bbox_preds.shape
        bbox_delta = paddle.reshape(bbox_preds, [-1, 5])
        bboxes = self.delta2rbox(anchors, bbox_delta)
        return bboxes
C
cnn 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

    def trace(self, A):
        tr = paddle.diagonal(A, axis1=-2, axis2=-1)
        tr = paddle.sum(tr, axis=-1)
        return tr

    def sqrt_newton_schulz_autograd(self, A, numIters):
        A_shape = A.shape
        batchSize = A_shape[0]
        dim = A_shape[1]

        normA = A * A
        normA = paddle.sum(normA, axis=1)
        normA = paddle.sum(normA, axis=1)
        normA = paddle.sqrt(normA)
        normA1 = normA.reshape([batchSize, 1, 1])
        Y = paddle.divide(A, paddle.expand_as(normA1, A))
        I = paddle.eye(dim, dim).reshape([1, dim, dim])
        l0 = []
        for i in range(batchSize):
            l0.append(I)
        I = paddle.concat(l0, axis=0)
        I.stop_gradient = False
        Z = paddle.eye(dim, dim).reshape([1, dim, dim])
        l1 = []
        for i in range(batchSize):
            l1.append(Z)
        Z = paddle.concat(l1, axis=0)
        Z.stop_gradient = False

        for i in range(numIters):
            T = 0.5 * (3.0 * I - Z.bmm(Y))
            Y = Y.bmm(T)
            Z = T.bmm(Z)
        sA = Y * paddle.sqrt(normA1).reshape([batchSize, 1, 1])
        sA = paddle.expand_as(sA, A)
        return sA

    def wasserstein_distance_sigma(sigma1, sigma2):
        wasserstein_distance_item2 = paddle.matmul(
            sigma1, sigma1) + paddle.matmul(
                sigma2, sigma2) - 2 * self.sqrt_newton_schulz_autograd(
                    paddle.matmul(
                        paddle.matmul(sigma1, paddle.matmul(sigma2, sigma2)),
                        sigma1), 10)
        wasserstein_distance_item2 = self.trace(wasserstein_distance_item2)

        return wasserstein_distance_item2

    def xywhr2xyrs(self, xywhr):
        xywhr = paddle.reshape(xywhr, [-1, 5])
        xy = xywhr[:, :2]
        wh = paddle.clip(xywhr[:, 2:4], min=1e-7, max=1e7)
        r = xywhr[:, 4]
        cos_r = paddle.cos(r)
        sin_r = paddle.sin(r)
        R = paddle.stack(
            (cos_r, -sin_r, sin_r, cos_r), axis=-1).reshape([-1, 2, 2])
        S = 0.5 * paddle.nn.functional.diag_embed(wh)
        return xy, R, S

    def gwd_loss(self,
                 pred,
                 target,
                 fun='log',
                 tau=1.0,
                 alpha=1.0,
                 normalize=False):

        xy_p, R_p, S_p = self.xywhr2xyrs(pred)
        xy_t, R_t, S_t = self.xywhr2xyrs(target)

        xy_distance = (xy_p - xy_t).square().sum(axis=-1)

        Sigma_p = R_p.matmul(S_p.square()).matmul(R_p.transpose([0, 2, 1]))
        Sigma_t = R_t.matmul(S_t.square()).matmul(R_t.transpose([0, 2, 1]))

        whr_distance = paddle.diagonal(
            S_p, axis1=-2, axis2=-1).square().sum(axis=-1)

        whr_distance = whr_distance + paddle.diagonal(
            S_t, axis1=-2, axis2=-1).square().sum(axis=-1)
        _t = Sigma_p.matmul(Sigma_t)

        _t_tr = paddle.diagonal(_t, axis1=-2, axis2=-1).sum(axis=-1)
        _t_det_sqrt = paddle.diagonal(S_p, axis1=-2, axis2=-1).prod(axis=-1)
        _t_det_sqrt = _t_det_sqrt * paddle.diagonal(
            S_t, axis1=-2, axis2=-1).prod(axis=-1)
        whr_distance = whr_distance + (-2) * (
            (_t_tr + 2 * _t_det_sqrt).clip(0).sqrt())

        distance = (xy_distance + alpha * alpha * whr_distance).clip(0)

        if normalize:
            wh_p = pred[..., 2:4].clip(min=1e-7, max=1e7)
            wh_t = target[..., 2:4].clip(min=1e-7, max=1e7)
            scale = ((wh_p.log() + wh_t.log()).sum(dim=-1) / 4).exp()
            distance = distance / scale

        if fun == 'log':
            distance = paddle.log1p(distance)

        if tau >= 1.0:
            return 1 - 1 / (tau + distance)

        return distance