code: def _sample_rois(
      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 = calculate_bb_overlap(all_rois[:, 1:], gt_boxes[:, :4])
      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 >= 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 and not deterministic:
          fg_inds = npr.choice(
              fg_inds, size=fg_rois_per_this_image, replace=False
          )
      elif fg_inds.size > 0:
          fg_inds = fg_inds[:fg_rois_per_this_image]
  
      # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
      bg_inds = np.where(
          (max_overlaps < BG_THRESH_HI) & (max_overlaps >= 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 and not deterministic:
          bg_inds = npr.choice(
              bg_inds, size=bg_rois_per_this_image, replace=False
          )
      elif bg_inds.size > 0:
          bg_inds = bg_inds[:bg_rois_per_this_image]
  
      # 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 = _compute_targets(
          rois[:, 1:5], gt_boxes[gt_assignment[keep_inds], :4], labels
      )
  
      bbox_targets, bbox_inside_weights = _get_bbox_regression_labels(
          bbox_target_data, num_classes
      )
  
      return labels, rois, bbox_targets, bbox_inside_weights
  
file_path: neon/examples/faster-rcnn/tests/proposal_target_layer_ref.py
imports: from __future__ import division
  from __future__ import print_function
  from builtins import object
  import numpy
  import numpy.random
  from util import bbox_transform, calculate_bb_overlap
label_desc: Use image node_id 3 for calling a global function with example usage: _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