docstr: Compute regression and classification targets for a single image.
docstr_tok: ["Compute","regression","and","classification","targets","for","a","single","image","."]
code: def _get_targets_single(
      self,
      data_sample: TextDetDataSample,
      points: Tensor,
      regress_ranges: Tensor,
      num_points_per_lvl: List[int],
  ) -> Tuple[Tensor, Tensor, Tensor]:
      """Compute regression and classification targets for a single image."""
      num_points = points.size(0)
      gt_instances = data_sample.gt_instances
      gt_instances = gt_instances[~gt_instances.ignored]
      num_gts = len(gt_instances)
      gt_bboxes = gt_instances.bboxes
      gt_labels = gt_instances.labels
      data_sample.gt_instances = gt_instances
      polygons = gt_instances.polygons
      beziers = gt_bboxes.new([poly2bezier(poly) for poly in polygons])
      gt_instances.beziers = beziers
      if num_gts == 0:
          return (
              gt_labels.new_full((num_points,), self.num_classes),
              gt_bboxes.new_zeros((num_points, 4)),
              gt_bboxes.new_zeros((num_points, 16)),
          )
  
      areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0]) * (
          gt_bboxes[:, 3] - gt_bboxes[:, 1]
      )
      # TODO: figure out why these two are different
      # areas = areas[None].expand(num_points, num_gts)
      areas = areas[None].repeat(num_points, 1)
      regress_ranges = regress_ranges[:, None, :].expand(
          num_points, num_gts, 2
      )
      gt_bboxes = gt_bboxes[None].expand(num_points, num_gts, 4)
      xs, ys = points[:, 0], points[:, 1]
      xs = xs[:, None].expand(num_points, num_gts)
      ys = ys[:, None].expand(num_points, num_gts)
  
      left = xs - gt_bboxes[..., 0]
      right = gt_bboxes[..., 2] - xs
      top = ys - gt_bboxes[..., 1]
      bottom = gt_bboxes[..., 3] - ys
      bbox_targets = torch.stack((left, top, right, bottom), -1)
  
      beziers = beziers.reshape(-1, 8, 2)[None].expand(
          num_points, num_gts, 8, 2
      )
      beziers_left = beziers[..., 0] - xs[..., None]
      beziers_right = beziers[..., 1] - ys[..., None]
      bezier_targets = torch.stack(
          (beziers_left, beziers_right), dim=-1
      )
      bezier_targets = bezier_targets.view(num_points, num_gts, 16)
      if self.center_sampling:
          # condition1: inside a `center bbox`
          radius = self.center_sample_radius
          center_xs = (gt_bboxes[..., 0] + gt_bboxes[..., 2]) / 2
          center_ys = (gt_bboxes[..., 1] + gt_bboxes[..., 3]) / 2
          center_gts = torch.zeros_like(gt_bboxes)
          stride = center_xs.new_zeros(center_xs.shape)
  
          # project the points on current lvl back to the `original` sizes
          lvl_begin = 0
          for lvl_idx, num_points_lvl in enumerate(num_points_per_lvl):
              lvl_end = lvl_begin + num_points_lvl
              stride[lvl_begin:lvl_end] = self.strides[lvl_idx] * radius
              lvl_begin = lvl_end
  
          x_mins = center_xs - stride
          y_mins = center_ys - stride
          x_maxs = center_xs + stride
          y_maxs = center_ys + stride
          center_gts[..., 0] = torch.where(
              x_mins > gt_bboxes[..., 0], x_mins, gt_bboxes[..., 0]
          )
          center_gts[..., 1] = torch.where(
              y_mins > gt_bboxes[..., 1], y_mins, gt_bboxes[..., 1]
          )
          center_gts[..., 2] = torch.where(
              x_maxs > gt_bboxes[..., 2], gt_bboxes[..., 2], x_maxs
          )
          center_gts[..., 3] = torch.where(
              y_maxs > gt_bboxes[..., 3], gt_bboxes[..., 3], y_maxs
          )
  
          cb_dist_left = xs - center_gts[..., 0]
          cb_dist_right = center_gts[..., 2] - xs
          cb_dist_top = ys - center_gts[..., 1]
          cb_dist_bottom = center_gts[..., 3] - ys
          center_bbox = torch.stack(
              (
                  cb_dist_left,
                  cb_dist_top,
                  cb_dist_right,
                  cb_dist_bottom,
              ),
              -1,
          )
          inside_gt_bbox_mask = center_bbox.min(-1)[0] > 0
      else:
          # condition1: inside a gt bbox
          inside_gt_bbox_mask = bbox_targets.min(-1)[0] > 0
  
      # condition2: limit the regression range for each location
      max_regress_distance = bbox_targets.max(-1)[0]
      inside_regress_range = (
          max_regress_distance >= regress_ranges[..., 0]
      ) & (max_regress_distance <= regress_ranges[..., 1])
  
      # if there are still more than one objects for a location,
      # we choose the one with minimal area
      areas[inside_gt_bbox_mask == 0] = INF
      areas[inside_regress_range == 0] = INF
      min_area, min_area_inds = areas.min(dim=1)
  
      labels = gt_labels[min_area_inds]
      labels[min_area == INF] = self.num_classes  # set as BG
      bbox_targets = bbox_targets[range(num_points), min_area_inds]
      bezier_targets = bezier_targets[range(num_points), min_area_inds]
  
      return labels, bbox_targets, bezier_targets
  
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 4 for calling the ABCNetDetModuleLoss obj's underlying member method code with example usage: obj._get_targets_single(data_sample, points, regress_ranges, num_points_per_lvl) and returns: labels,  bbox_targets,  bezier_targets, gt_labels,  gt_bboxes,  gt_bboxes