class_docstr: Importance-based Sample Reweighting (ISR_N), described in `Prime Sample
  Attention in Object Detection <https://arxiv.org/abs/1904.04821>`_.
  
  Score hierarchical local rank (HLR) differentiates with RandomSampler in
  negative part. It firstly computes Score-HLR in a two-step way,
  then linearly maps score hlr to the loss weights.
  
  Args:
      num (int): Total number of sampled RoIs.
      pos_fraction (float): Fraction of positive samples.
      context (:obj:`BaseRoIHead`): RoI head that the sampler belongs to.
      neg_pos_ub (int): Upper bound of the ratio of num negative to num
          positive, -1 means no upper bound. Defaults to -1.
      add_gt_as_proposals (bool): Whether to add ground truth as proposals.
          Defaults to True.
      k (float): Power of the non-linear mapping. Defaults to 0.5
      bias (float): Shift of the non-linear mapping. Defaults to 0.
      score_thr (float): Minimum score that a negative sample is to be
          considered as valid bbox. Defaults to 0.05.
      iou_thr (float): IoU threshold for NMS match. Defaults to 0.5.
class_docstr_tok: ["Importance-based","Sample","Reweighting","(","ISR_N",")",",","described","in","`","Prime","Sample","Attention","in","Object","Detection","<","https",":","\/\/arxiv.org\/abs\/1904.04821",">","`","_",".","Score","hierarchical","local","rank","(","HLR",")","differentiates","with","RandomSampler","in","negative","part",".","It","firstly","computes","Score-HLR","in","a","two-step","way",",","then","linearly","maps","score","hlr","to","the","loss","weights",".","Args",":","num","(","int",")",":","Total","number","of","sampled","RoIs",".","pos_fraction","(","float",")",":","Fraction","of","positive","samples",".","context","(",":","obj",":","`","BaseRoIHead","`",")",":","RoI","head","that","the","sampler","belongs","to",".","neg_pos_ub","(","int",")",":","Upper","bound","of","the","ratio","of","num","negative","to","num","positive",",","-1","means","no","upper","bound",".","Defaults","to","-1.","add_gt_as_proposals","(","bool",")",":","Whether","to","add","ground","truth","as","proposals",".","Defaults","to","True",".","k","(","float",")",":","Power","of","the","non-linear","mapping",".","Defaults","to","0.5","bias","(","float",")",":","Shift","of","the","non-linear","mapping",".","Defaults","to","0.","score_thr","(","float",")",":","Minimum","score","that","a","negative","sample","is","to","be","considered","as","valid","bbox",".","Defaults","to","0.05.","iou_thr","(","float",")",":","IoU","threshold","for","NMS","match",".","Defaults","to","0.5","."]
docstr: Sample positive and negative bboxes.
  
  This is a simple implementation of bbox sampling given candidates,
  assigning results and ground truth bboxes.
  
  Args:
      assign_result (:obj:`AssignResult`): Assigning results.
      pred_instances (:obj:`InstanceData`): Instances of model
          predictions. It includes ``priors``, and the priors can
          be anchors or points, or the bboxes predicted by the
          previous stage, has shape (n, 4). The bboxes predicted by
          the current model or stage will be named ``bboxes``,
          ``labels``, and ``scores``, the same as the ``InstanceData``
          in other places.
      gt_instances (:obj:`InstanceData`): Ground truth of instance
          annotations. It usually includes ``bboxes``, with shape (k, 4),
          and ``labels``, with shape (k, ).
  
  Returns:
      :obj:`SamplingResult`: Sampling result.
docstr_tok: ["Sample","positive","and","negative","bboxes",".","This","is","a","simple","implementation","of","bbox","sampling","given","candidates",",","assigning","results","and","ground","truth","bboxes",".","Args",":","assign_result","(",":","obj",":","`","AssignResult","`",")",":","Assigning","results",".","pred_instances","(",":","obj",":","`","InstanceData","`",")",":","Instances","of","model","predictions",".","It","includes","``","priors","``",",","and","the","priors","can","be","anchors","or","points",",","or","the","bboxes","predicted","by","the","previous","stage",",","has","shape","(","n",",","4",")",".","The","bboxes","predicted","by","the","current","model","or","stage","will","be","named","``","bboxes","``",",","``","labels","``",",","and","``","scores","``",",","the","same","as","the","``","InstanceData","``","in","other","places",".","gt_instances","(",":","obj",":","`","InstanceData","`",")",":","Ground","truth","of","instance","annotations",".","It","usually","includes","``","bboxes","``",",","with","shape","(","k",",","4",")",",","and","``","labels","``",",","with","shape","(","k",",",")",".","Returns",":",":","obj",":","`","SamplingResult","`",":","Sampling","result","."]
code: def sample(
      self,
      assign_result: AssignResult,
      pred_instances: InstanceData,
      gt_instances: InstanceData,
      **kwargs,
  ) -> SamplingResult:
      """Sample positive and negative bboxes.
  
      This is a simple implementation of bbox sampling given candidates,
      assigning results and ground truth bboxes.
  
      Args:
          assign_result (:obj:`AssignResult`): Assigning results.
          pred_instances (:obj:`InstanceData`): Instances of model
              predictions. It includes ``priors``, and the priors can
              be anchors or points, or the bboxes predicted by the
              previous stage, has shape (n, 4). The bboxes predicted by
              the current model or stage will be named ``bboxes``,
              ``labels``, and ``scores``, the same as the ``InstanceData``
              in other places.
          gt_instances (:obj:`InstanceData`): Ground truth of instance
              annotations. It usually includes ``bboxes``, with shape (k, 4),
              and ``labels``, with shape (k, ).
  
      Returns:
          :obj:`SamplingResult`: Sampling result.
      """
      gt_bboxes = gt_instances.bboxes
      priors = pred_instances.priors
      gt_labels = gt_instances.labels
  
      gt_flags = priors.new_zeros((priors.shape[0],), dtype=torch.uint8)
      if self.add_gt_as_proposals and len(gt_bboxes) > 0:
          priors = torch.cat([gt_bboxes, priors], dim=0)
          assign_result.add_gt_(gt_labels)
          gt_ones = priors.new_ones(
              gt_bboxes.shape[0], dtype=torch.uint8
          )
          gt_flags = torch.cat([gt_ones, gt_flags])
  
      num_expected_pos = int(self.num * self.pos_fraction)
      pos_inds = self.pos_sampler._sample_pos(
          assign_result, num_expected_pos, bboxes=priors, **kwargs
      )
      num_sampled_pos = pos_inds.numel()
      num_expected_neg = self.num - num_sampled_pos
      if self.neg_pos_ub >= 0:
          _pos = max(1, num_sampled_pos)
          neg_upper_bound = int(self.neg_pos_ub * _pos)
          if num_expected_neg > neg_upper_bound:
              num_expected_neg = neg_upper_bound
      neg_inds, neg_label_weights = self.neg_sampler._sample_neg(
          assign_result, num_expected_neg, bboxes=priors, **kwargs
      )
  
      sampling_result = SamplingResult(
          pos_inds=pos_inds,
          neg_inds=neg_inds,
          priors=priors,
          gt_bboxes=gt_bboxes,
          assign_result=assign_result,
          gt_flags=gt_flags,
      )
      return sampling_result, neg_label_weights
  
file_path: mmdetection/mmdet/models/task_modules/samplers/score_hlr_sampler.py
imports: from typing import Union
  import torch
  from mmcv.ops import nms_match
  from mmengine.structures import InstanceData
  from numpy import ndarray
  from torch import Tensor
  from mmdet.registry import TASK_UTILS
  from mmdet.structures.bbox import bbox2roi
  from ..assigners import AssignResult
  from .base_sampler import BaseSampler
  from .sampling_result import SamplingResult
label_desc: Use image node_id 5 for calling the ScoreHLRSampler obj's underlying member method code with example usage: obj.sample(assign_result, pred_instances, gt_instances) and returns: sampling_result,  neg_label_weights