class_docstr: Proposal layer takes as input:
  (1) output of RPN cls_score
  (2) output of RPN bbox regression
  
  Converts that to an output ROIs
  (5, num_ROIs) <- [image_idx, x_min, y_min, x_max, y_max]
  
  Steps:
  1. For each anchor, generate anchor boxes and apply bbox deltas to get
     bbox proposals
  2. Clip bboxes to image
  3. remove bbox with H < threshold or W < threshold
  4. set the scores to be -1 for the padded area
  5. Take top pre_nms_N scores
  6. apply NMS with a threshold
  7. return the top num_ROIs proposals
  8. provide ROIs
  9. compute bbox targets and store in target_buffers
class_docstr_tok: ["Proposal","layer","takes","as","input",":","(","1",")","output","of","RPN","cls_score","(","2",")","output","of","RPN","bbox","regression","Converts","that","to","an","output","ROIs","(","5",",","num_ROIs",")","<","-","[","image_idx",",","x_min",",","y_min",",","x_max",",","y_max","]","Steps",":","1",".","For","each","anchor",",","generate","anchor","boxes","and","apply","bbox","deltas","to","get","bbox","proposals","2",".","Clip","bboxes","to","image","3.","remove","bbox","with","H","<","threshold","or","W","<","threshold","4.","set","the","scores","to","be","-1","for","the","padded","area","5",".","Take","top","pre_nms_N","scores","6.","apply","NMS","with","a","threshold","7.","return","the","top","num_ROIs","proposals","8.","provide","ROIs","9.","compute","bbox","targets","and","store","in","target_buffers"]
docstr: Return sample of at most fg_fraction * num_rois foreground indicies, padding
  the remaining num_rois with background indicies. Foreground and background labels
  are determined based on max_overlaps and the thresholds fg_thresh, bg_thresh_hi,
  bg_thresh_lo.
  Returns:
      keep_inds (array): (num_rois,) sampled indicies of bboxes.
      fg_rois_per_this_image (int): number of fg rois sampled from the image.
docstr_tok: ["Return","sample","of","at","most","fg_fraction","*","num_rois","foreground","indicies",",","padding","the","remaining","num_rois","with","background","indicies",".","Foreground","and","background","labels","are","determined","based","on","max_overlaps","and","the","thresholds","fg_thresh",",","bg_thresh_hi",",","bg_thresh_lo",".","Returns",":","keep_inds","(","array",")",":","(","num_rois",",",")","sampled","indicies","of","bboxes",".","fg_rois_per_this_image","(","int",")",":","number","of","fg","rois","sampled","from","the","image","."]
code: def _sample_fg_bg(self, max_overlaps):
      """Return sample of at most fg_fraction * num_rois foreground indicies, padding
      the remaining num_rois with background indicies. Foreground and background labels
      are determined based on max_overlaps and the thresholds fg_thresh, bg_thresh_hi,
      bg_thresh_lo.
      Returns:
          keep_inds (array): (num_rois,) sampled indicies of bboxes.
          fg_rois_per_this_image (int): number of fg rois sampled from the image.
      """
      # Split proposals into foreground and background based on overlap
      fg_inds = np.where(max_overlaps >= self.fg_thresh)[0]
      fg_rois_per_image = np.round(self.fg_fraction * self.num_rois)
  
      # 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 self.deterministic:
          fg_inds = self.be.rng.choice(
              fg_inds, size=int(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 < 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 = self.num_rois - 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 self.deterministic:
          bg_inds = self.be.rng.choice(
              bg_inds, size=int(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)
      return keep_inds, int(fg_rois_per_this_image)
  
imports: from __future__ import division
  import numpy
  from neon.layers.layer import Layer
  from generate_anchors import generate_all_anchors
  from util import compute_targets, calculate_bb_overlap
label_desc: Use image node_id 14 for calling the ProposalLayer obj's underlying member method code with example usage: obj._sample_fg_bg(max_overlaps) and returns: keep_inds,  int