class_docstr: Assign object detection proposals to ground-truth targets. Produces proposal
  classification labels and bounding-box regression targets.
class_docstr_tok: ["Assign","object","detection","proposals","to","ground-truth","targets",".","Produces","proposal","classification","labels","and","bounding-box","regression","targets","."]
docstr: Generate a random sample of RoIs comprising foreground and background
  examples.
docstr_tok: ["Generate","a","random","sample","of","RoIs","comprising","foreground","and","background","examples","."]
code: def _sample_rois(
      self,
      all_rois,
      gt_boxes,
      fg_rois_per_image,
      rois_per_image,
      num_classes,
      deterministic=False,
  ):
      """Generate a random sample of RoIs comprising foreground and background
      examples.
      """
      # overlaps: (rois x gt_boxes)
      overlaps = bbox_overlaps(
          np.ascontiguousarray(all_rois[:, 1:5], dtype=np.float),
          np.ascontiguousarray(gt_boxes[:, :4], dtype=np.float),
      )
      gt_assignment = overlaps.argmax(axis=1)
      max_overlaps = overlaps.max(axis=1)
      labels = gt_boxes[gt_assignment, 4]
  
      # Select foreground RoIs as those with >= FG_THRESH overlap
      fg_inds = np.where(max_overlaps >= self._fg_thresh)[0]
      # Guard against the case when an image has fewer than fg_rois_per_image
      # foreground RoIs
      fg_rois_per_this_image = min(fg_rois_per_image, fg_inds.size)
  
      # Sample foreground regions without replacement
      if fg_inds.size > 0:
          if deterministic:
              fg_inds = fg_inds[:fg_rois_per_this_image]
          else:
              fg_inds = npr.choice(
                  fg_inds, size=fg_rois_per_this_image, replace=False
              )
  
      # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
      bg_inds = np.where(
          (max_overlaps < self._bg_thresh_hi)
          & (max_overlaps >= self._bg_thresh_lo)
      )[0]
      # Compute number of background RoIs to take from this image (guarding
      # against there being fewer than desired)
      bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
      bg_rois_per_this_image = min(bg_rois_per_this_image, bg_inds.size)
      # Sample background regions without replacement
      if bg_inds.size > 0:
          if deterministic:
              bg_inds = bg_inds[:bg_rois_per_this_image]
          else:
              bg_inds = npr.choice(
                  bg_inds, size=bg_rois_per_this_image, replace=False
              )
  
      # The indices that we're selecting (both fg and bg)
      keep_inds = np.append(fg_inds, bg_inds)
      # Select sampled values from various arrays:
      labels = labels[keep_inds]
      # Clamp labels for the background RoIs to 0
      labels[fg_rois_per_this_image:] = 0
      rois = all_rois[keep_inds]
  
      bbox_target_data = self._compute_targets(
          rois[:, 1:5], gt_boxes[gt_assignment[keep_inds], :4], labels
      )
  
      (
          bbox_targets,
          bbox_inside_weights,
      ) = self._get_bbox_regression_labels(
          bbox_target_data, num_classes
      )
  
      return labels, rois, bbox_targets, bbox_inside_weights
  
file_path: CNTK/Examples/Image/Detection/utils/rpn/proposal_target_layer.py
imports: from cntk import output_variable, FreeDimension
  from cntk.ops.functions import UserFunction
  import yaml
  import numpy
  import numpy.random
  from utils.rpn.bbox_transform import bbox_transform
  from utils.cython_modules.cython_bbox import bbox_overlaps
label_desc: Use image node_id 10 for calling the ProposalTargetLayer obj's underlying member method code with example usage: obj._sample_rois(all_rois, gt_boxes, fg_rois_per_image, rois_per_image, num_classes, deterministic) and returns: labels,  rois,  bbox_targets,  bbox_inside_weights