docstr: Compute ABCNet loss.
  
  Args:
      inputs (tuple(tensor)): Raw predictions from model, containing
          ``cls_scores``, ``bbox_preds``, ``beizer_preds`` and
          ``centernesses``.
          Each is a tensor of shape :math:`(N, H, W)`.
      data_samples (list[TextDetDataSample]): The data samples.
  
  Returns:
      dict: The dict for abcnet-det losses with loss_cls, loss_bbox,
      loss_centerness and loss_bezier.
docstr_tok: ["Compute","ABCNet","loss",".","Args",":","inputs","(","tuple","(","tensor",")",")",":","Raw","predictions","from","model",",","containing","``","cls_scores","``",",","``","bbox_preds","``",",","``","beizer_preds","``","and","``","centernesses","``",".","Each","is","a","tensor","of","shape",":","math",":","`","(","N",",","H",",","W",")","`",".","data_samples","(","list","[","TextDetDataSample","]",")",":","The","data","samples",".","Returns",":","dict",":","The","dict","for","abcnet-det","losses","with","loss_cls",",","loss_bbox",",","loss_centerness","and","loss_bezier","."]
code: def forward(
      self, inputs: Tuple[Tensor], data_samples: DetSampleList
  ) -> Dict:
      """Compute ABCNet loss.
  
      Args:
          inputs (tuple(tensor)): Raw predictions from model, containing
              ``cls_scores``, ``bbox_preds``, ``beizer_preds`` and
              ``centernesses``.
              Each is a tensor of shape :math:`(N, H, W)`.
          data_samples (list[TextDetDataSample]): The data samples.
  
      Returns:
          dict: The dict for abcnet-det losses with loss_cls, loss_bbox,
          loss_centerness and loss_bezier.
      """
      cls_scores, bbox_preds, centernesses, beizer_preds = inputs
      assert (
          len(cls_scores)
          == len(bbox_preds)
          == len(centernesses)
          == len(beizer_preds)
      )
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      all_level_points = self.prior_generator.grid_priors(
          featmap_sizes,
          dtype=bbox_preds[0].dtype,
          device=bbox_preds[0].device,
      )
      labels, bbox_targets, bezier_targets = self.get_targets(
          all_level_points, data_samples
      )
  
      num_imgs = cls_scores[0].size(0)
      # flatten cls_scores, bbox_preds and centerness
      flatten_cls_scores = [
          cls_score.permute(0, 2, 3, 1).reshape(
              -1, self.cls_out_channels
          )
          for cls_score in cls_scores
      ]
      flatten_bbox_preds = [
          bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
          for bbox_pred in bbox_preds
      ]
      flatten_centerness = [
          centerness.permute(0, 2, 3, 1).reshape(-1)
          for centerness in centernesses
      ]
      flatten_bezier_preds = [
          bezier_pred.permute(0, 2, 3, 1).reshape(-1, 16)
          for bezier_pred in beizer_preds
      ]
      flatten_cls_scores = torch.cat(flatten_cls_scores)
      flatten_bbox_preds = torch.cat(flatten_bbox_preds)
      flatten_centerness = torch.cat(flatten_centerness)
      flatten_bezier_preds = torch.cat(flatten_bezier_preds)
      flatten_labels = torch.cat(labels)
      flatten_bbox_targets = torch.cat(bbox_targets)
      flatten_bezier_targets = torch.cat(bezier_targets)
      # repeat points to align with bbox_preds
      flatten_points = torch.cat(
          [points.repeat(num_imgs, 1) for points in all_level_points]
      )
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      bg_class_ind = self.num_classes
      pos_inds = (
          ((flatten_labels >= 0) & (flatten_labels < bg_class_ind))
          .nonzero()
          .reshape(-1)
      )
      num_pos = torch.tensor(
          len(pos_inds), dtype=torch.float, device=bbox_preds[0].device
      )
      num_pos = max(reduce_mean(num_pos), 1.0)
      loss_cls = self.loss_cls(
          flatten_cls_scores, flatten_labels, avg_factor=num_pos
      )
  
      pos_bbox_preds = flatten_bbox_preds[pos_inds]
      pos_centerness = flatten_centerness[pos_inds]
      pos_bezier_preds = flatten_bezier_preds[pos_inds]
      pos_bbox_targets = flatten_bbox_targets[pos_inds]
      pos_centerness_targets = self.centerness_target(pos_bbox_targets)
      pos_bezier_targets = flatten_bezier_targets[pos_inds]
      # centerness weighted iou loss
      centerness_denorm = max(
          reduce_mean(pos_centerness_targets.sum().detach()), 1e-6
      )
  
      if len(pos_inds) > 0:
          pos_points = flatten_points[pos_inds]
          pos_decoded_bbox_preds = self.bbox_coder.decode(
              pos_points, pos_bbox_preds
          )
          pos_decoded_target_preds = self.bbox_coder.decode(
              pos_points, pos_bbox_targets
          )
          loss_bbox = self.loss_bbox(
              pos_decoded_bbox_preds,
              pos_decoded_target_preds,
              weight=pos_centerness_targets,
              avg_factor=centerness_denorm,
          )
          loss_centerness = self.loss_centerness(
              pos_centerness, pos_centerness_targets, avg_factor=num_pos
          )
          loss_bezier = self.loss_bezier(
              pos_bezier_preds,
              pos_bezier_targets,
              weight=pos_centerness_targets[:, None],
              avg_factor=centerness_denorm,
          )
      else:
          loss_bbox = pos_bbox_preds.sum()
          loss_centerness = pos_centerness.sum()
          loss_bezier = pos_bezier_preds.sum()
  
      return dict(
          loss_cls=loss_cls,
          loss_bbox=loss_bbox,
          loss_centerness=loss_centerness,
          loss_bezier=loss_bezier,
      )
  
file_path: mmocr/projects/ABCNet/abcnet/model/abcnet_det_module_loss.py
imports: from typing import Dict, List, Tuple
  import torch
  from mmdet.models.task_modules.prior_generators import MlvlPointGenerator
  from mmdet.models.utils import multi_apply
  from mmdet.utils import reduce_mean
  from torch import Tensor
  from mmocr.models.textdet.module_losses.base import BaseTextDetModuleLoss
  from mmocr.registry import MODELS, TASK_UTILS
  from mmocr.structures import TextDetDataSample
  from mmocr.utils import ConfigType, DetSampleList, RangeType
  from ..utils import poly2bezier
label_desc: Use image node_id 2 for calling the ABCNetDetModuleLoss obj's underlying member method code with example usage: obj.forward(inputs, data_samples) and returns: dict