class_docstr: This class samples batches, ensuring that they contain a fixed proportion of positives
class_docstr_tok: ["This","class","samples","batches",",","ensuring","that","they","contain","a","fixed","proportion","of","positives"]
docstr: Args:
      matched_idxs: list of tensors containing -1, 0 or positive values.
          Each tensor corresponds to a specific image.
          -1 values are ignored, 0 are considered as negatives and > 0 as
          positives.
  
  Returns:
      pos_idx (list[tensor])
      neg_idx (list[tensor])
  
  Returns two lists of binary masks for each image.
  The first list contains the positive elements that were selected,
  and the second list the negative example.
docstr_tok: ["Args",":","matched_idxs",":","list","of","tensors","containing","-1",",","0","or","positive","values",".","Each","tensor","corresponds","to","a","specific","image",".","-1","values","are","ignored",",","0","are","considered","as","negatives","and",">","0","as","positives",".","Returns",":","pos_idx","(","list","[","tensor","]",")","neg_idx","(","list","[","tensor","]",")","Returns","two","lists","of","binary","masks","for","each","image",".","The","first","list","contains","the","positive","elements","that","were","selected",",","and","the","second","list","the","negative","example","."]
code: def __call__(
      self, matched_idxs: List[Tensor]
  ) -> Tuple[List[Tensor], List[Tensor]]:
      """
      Args:
          matched_idxs: list of tensors containing -1, 0 or positive values.
              Each tensor corresponds to a specific image.
              -1 values are ignored, 0 are considered as negatives and > 0 as
              positives.
  
      Returns:
          pos_idx (list[tensor])
          neg_idx (list[tensor])
  
      Returns two lists of binary masks for each image.
      The first list contains the positive elements that were selected,
      and the second list the negative example.
      """
      pos_idx = []
      neg_idx = []
      for matched_idxs_per_image in matched_idxs:
          positive = torch.where(matched_idxs_per_image >= 1)[0]
          negative = torch.where(matched_idxs_per_image == 0)[0]
  
          num_pos = int(
              self.batch_size_per_image * self.positive_fraction
          )
          # protect against not enough positive examples
          num_pos = min(positive.numel(), num_pos)
          num_neg = self.batch_size_per_image - num_pos
          # protect against not enough negative examples
          num_neg = min(negative.numel(), num_neg)
  
          # randomly select positive and negative examples
          perm1 = torch.randperm(
              positive.numel(), device=positive.device
          )[:num_pos]
          perm2 = torch.randperm(
              negative.numel(), device=negative.device
          )[:num_neg]
  
          pos_idx_per_image = positive[perm1]
          neg_idx_per_image = negative[perm2]
  
          # create binary mask from indices
          pos_idx_per_image_mask = torch.zeros_like(
              matched_idxs_per_image, dtype=torch.uint8
          )
          neg_idx_per_image_mask = torch.zeros_like(
              matched_idxs_per_image, dtype=torch.uint8
          )
  
          pos_idx_per_image_mask[pos_idx_per_image] = 1
          neg_idx_per_image_mask[neg_idx_per_image] = 1
  
          pos_idx.append(pos_idx_per_image_mask)
          neg_idx.append(neg_idx_per_image_mask)
  
      return pos_idx, neg_idx
  
imports: import math
  from collections import OrderedDict
  from typing import Dict, List, Optional, Tuple
  import torch
  from torch import nn, Tensor
  from torch.nn import functional
  from torchvision.ops import complete_box_iou_loss, distance_box_iou_loss, FrozenBatchNorm2d, generalized_box_iou_loss
label_desc: Use image node_id 2 for calling the BalancedPositiveNegativeSampler obj's underlying member method code with example usage: obj.__call__(matched_idxs) and returns: pos_idx,  neg_idx