def forward_refine(self, memory, mlvl_masks, refine_targets, losses,
                       img_metas):
        """Forward function.

        Args:
            mlvl_masks (tuple[Tensor]): The key_padding_mask from
                different level used for encoder and decoder,
                each is a 3D-tensor with shape (bs, H, W).
            losses (dict[str, Tensor]): A dictionary of loss components.
            img_metas (list[dict]): List of image information.

        Returns:
            dict[str, Tensor]: A dictionary of loss components.
        """
        kpt_preds, kpt_targets, area_targets, kpt_weights = refine_targets
        pos_inds = kpt_weights.sum(-1) > 0
        if not pos_inds.any():
            pos_kpt_preds = paddle.zeros_like(kpt_preds[:1])
            pos_img_inds = paddle.zeros([1], dtype="int64")
        else:
            pos_kpt_preds = kpt_preds[pos_inds]
            pos_img_inds = (pos_inds.nonzero() /
                            self.num_query).squeeze(1).astype("int64")
        hs, init_reference, inter_references = self.transformer.forward_refine(
            mlvl_masks,
            memory,
            pos_kpt_preds.detach(),
            pos_img_inds,
            kpt_branches=self.refine_kpt_branches
            if self.with_kpt_refine else None,  # noqa:E501
        )

        outputs_kpts = []

        for lvl in range(hs.shape[0]):
            if lvl == 0:
                reference = init_reference
            else:
                reference = inter_references[lvl - 1]
            reference = inverse_sigmoid(reference)
            tmp_kpt = self.refine_kpt_branches[lvl](hs[lvl])
            assert reference.shape[-1] == 2
            tmp_kpt += reference
            outputs_kpt = F.sigmoid(tmp_kpt)
            outputs_kpts.append(outputs_kpt)
        outputs_kpts = paddle.stack(outputs_kpts)

        if not self.training:
            return outputs_kpts

        num_valid_kpt = paddle.clip(
            reduce_mean(kpt_weights.sum()), min=1).item()
        num_total_pos = paddle.to_tensor(
            [outputs_kpts.shape[1]], dtype=kpt_weights.dtype)
        num_total_pos = paddle.clip(reduce_mean(num_total_pos), min=1).item()

        if not pos_inds.any():
            for i, kpt_refine_preds in enumerate(outputs_kpts):
                loss_kpt = loss_oks = kpt_refine_preds.sum() * 0
                losses[f'd{i}.loss_kpt_refine'] = loss_kpt
                losses[f'd{i}.loss_oks_refine'] = loss_oks
                continue
            return losses

        batch_size = mlvl_masks[0].shape[0]
        factors = []
        for img_id in range(batch_size):
            img_h, img_w, _ = img_metas[img_id]['img_shape']
            factor = paddle.to_tensor(
                [img_w, img_h, img_w, img_h],
                dtype="float32").squeeze(-1).unsqueeze(0).tile(
                    (self.num_query, 1))
            factors.append(factor)
        factors = paddle.concat(factors, 0)
        factors = factors[pos_inds][:, :2].tile((1, kpt_preds.shape[-1] // 2))

        pos_kpt_weights = kpt_weights[pos_inds]
        pos_kpt_targets = kpt_targets[pos_inds]
        pos_kpt_targets_scaled = pos_kpt_targets * factors
        pos_areas = area_targets[pos_inds]
        pos_valid = kpt_weights[pos_inds][:, 0::2]
        for i, kpt_refine_preds in enumerate(outputs_kpts):
            if not pos_inds.any():
                print("refine kpt and oks skip")
                loss_kpt = loss_oks = kpt_refine_preds.sum() * 0
                losses[f'd{i}.loss_kpt_refine'] = loss_kpt
                losses[f'd{i}.loss_oks_refine'] = loss_oks
                continue

            # kpt L1 Loss
            pos_refine_preds = kpt_refine_preds.reshape(
                (kpt_refine_preds.shape[0], -1))
            loss_kpt = self.loss_kpt_refine(
                pos_refine_preds,
                pos_kpt_targets,
                pos_kpt_weights,
                avg_factor=num_valid_kpt)
            losses[f'd{i}.loss_kpt_refine'] = loss_kpt
            # kpt oks loss
            pos_refine_preds_scaled = pos_refine_preds * factors
            assert (pos_areas > 0).all()
            loss_oks = self.loss_oks_refine(
                pos_refine_preds_scaled,
                pos_kpt_targets_scaled,
                pos_valid,
                pos_areas,
                avg_factor=num_total_pos)
            losses[f'd{i}.loss_oks_refine'] = loss_oks
        return losses
Forward function.

        Args:
            mlvl_masks (tuple[Tensor]): The key_padding_mask from
                different level used for encoder and decoder,
                each is a 3D-tensor with shape (bs, H, W).
            losses (dict[str, Tensor]): A dictionary of loss components.
            img_metas (list[dict]): List of image information.

        Returns:
            dict[str, Tensor]: A dictionary of loss components.
        
ppdet/modeling/heads/petr_head.py