parameters: (all_rois, gt_boxes, rois_per_image, num_classes, gt_masks, im_scale, mask_info)
return_statement: return blobs, fg_inds, bg_inds, keep_inds
docstring: Generate a random sample of RoIs comprising
      foreground and background examples.
docstring_summary: Generate a random sample of RoIs comprising
      foreground and background examples.
docstring_tokens: Generate a random sample of RoIs comprising foreground and background examples .
function: def _sample_rois(all_rois, gt_boxes, rois_per_image, num_classes, gt_masks, im_scale, mask_info):
      """
      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]
  
      # Sample foreground indexes
      fg_inds = []
      for i in xrange(len(cfg.TRAIN.FG_FRACTION)):
          cur_inds = np.where((max_overlaps >= cfg.TRAIN.FG_THRESH_LO[i]) &
                              (max_overlaps <= cfg.TRAIN.FG_THRESH_HI[i]))[0]
          cur_rois_this_image = min(cur_inds.size, np.round(rois_per_image *
                                                            cfg.TRAIN.FG_FRACTION[i]))
          if cur_inds.size > 0:
              cur_inds = npr.choice(cur_inds, size=cur_rois_this_image, replace=False)
          fg_inds = np.hstack((fg_inds, cur_inds))
          fg_inds = np.unique(fg_inds)
      fg_rois_per_image = fg_inds.size
      # Sample background indexes according to number of foreground
      bg_rois_per_this_image = rois_per_image - fg_rois_per_image
      bg_inds = []
      for i in xrange(len(cfg.TRAIN.BG_FRACTION)):
          cur_inds = np.where((max_overlaps >= cfg.TRAIN.BG_THRESH_LO[i]) &
                              (max_overlaps <= cfg.TRAIN.BG_THRESH_HI[i]))[0]
          cur_rois_this_image = min(cur_inds.size, np.round(bg_rois_per_this_image *
                                                            cfg.TRAIN.BG_FRACTION[i]))
          if cur_inds.size > 0:
              cur_inds = npr.choice(cur_inds, size=cur_rois_this_image, replace=False)
          bg_inds = np.hstack((bg_inds, cur_inds))
          bg_inds = np.unique(bg_inds)
  
      # The indices that we're selecting (both fg and bg)
      keep_inds = np.append(fg_inds, bg_inds).astype(int)
      # Select sampled values from various arrays:
      labels = labels[keep_inds]
      # Clamp labels for the background RoIs to 0
      labels[fg_rois_per_image:] = 0
      rois = all_rois[keep_inds]
  
      bbox_target_data = bbox_compute_targets(
          rois[:, 1:5], gt_boxes[gt_assignment[keep_inds], :4], normalize=True)
      bbox_target_data = np.hstack((labels[:, np.newaxis], bbox_target_data))\
          .astype(np.float32, copy=False)
      bbox_targets, bbox_inside_weights = get_bbox_regression_label(
          bbox_target_data, num_classes)
      bbox_outside_weights = np.array(bbox_inside_weights > 0).astype(np.float32)
  
      blobs = {'rois': rois}
  
      if cfg.MNC_MODE:
          scaled_rois = rois[:, 1:5] / float(im_scale)
  
          # map to original image space
          scaled_gt_boxes = gt_boxes[:, :4] / float(im_scale)
          pos_masks = np.zeros((len(keep_inds), 1, cfg.MASK_SIZE,  cfg.MASK_SIZE))
          top_mask_info = np.zeros((len(keep_inds), 12))
          top_mask_info[len(fg_inds):, :] = -1
  
          for i, val in enumerate(fg_inds):
              gt_box = scaled_gt_boxes[gt_assignment[val]]
              gt_box = np.around(gt_box).astype(int)
              ex_box = np.around(scaled_rois[i]).astype(int)
              gt_mask = gt_masks[gt_assignment[val]]
              gt_mask_info = mask_info[gt_assignment[val]]
              gt_mask = gt_mask[0:gt_mask_info[0], 0:gt_mask_info[1]]
              # calculate mask regression targets
              # (intersection of bounding box and gt mask)
              ex_mask = intersect_mask(ex_box, gt_box, gt_mask)
  
              pos_masks[i, ...] = ex_mask
              top_mask_info[i, 0] = gt_assignment[val]
              top_mask_info[i, 1] = gt_mask_info[0]
              top_mask_info[i, 2] = gt_mask_info[1]
              top_mask_info[i, 3] = labels[i]
  
              top_mask_info[i, 4:8] = ex_box
              top_mask_info[i, 8:12] = gt_box
  
          mask_weight = np.zeros((rois.shape[0], 1, cfg.MASK_SIZE, cfg.MASK_SIZE))
          # only assign box-level foreground as positive mask regression
          mask_weight[0:len(fg_inds), :, :, :] = 1
          blobs['mask_targets'] = pos_masks
          blobs['mask_weight'] = mask_weight
          blobs['gt_masks_info'] = top_mask_info
  
      return blobs, fg_inds, bg_inds, keep_inds
function_tokens: def _sample_rois ( all_rois , gt_boxes , rois_per_image , num_classes , gt_masks , im_scale , mask_info ) : # 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 ] # Sample foreground indexes fg_inds = [ ] for i in xrange ( len ( cfg . TRAIN . FG_FRACTION ) ) : cur_inds = np . where ( ( max_overlaps >= cfg . TRAIN . FG_THRESH_LO [ i ] ) & ( max_overlaps <= cfg . TRAIN . FG_THRESH_HI [ i ] ) ) [ 0 ] cur_rois_this_image = min ( cur_inds . size , np . round ( rois_per_image * cfg . TRAIN . FG_FRACTION [ i ] ) ) if cur_inds . size > 0 : cur_inds = npr . choice ( cur_inds , size = cur_rois_this_image , replace = False ) fg_inds = np . hstack ( ( fg_inds , cur_inds ) ) fg_inds = np . unique ( fg_inds ) fg_rois_per_image = fg_inds . size # Sample background indexes according to number of foreground bg_rois_per_this_image = rois_per_image - fg_rois_per_image bg_inds = [ ] for i in xrange ( len ( cfg . TRAIN . BG_FRACTION ) ) : cur_inds = np . where ( ( max_overlaps >= cfg . TRAIN . BG_THRESH_LO [ i ] ) & ( max_overlaps <= cfg . TRAIN . BG_THRESH_HI [ i ] ) ) [ 0 ] cur_rois_this_image = min ( cur_inds . size , np . round ( bg_rois_per_this_image * cfg . TRAIN . BG_FRACTION [ i ] ) ) if cur_inds . size > 0 : cur_inds = npr . choice ( cur_inds , size = cur_rois_this_image , replace = False ) bg_inds = np . hstack ( ( bg_inds , cur_inds ) ) bg_inds = np . unique ( bg_inds ) # The indices that we're selecting (both fg and bg) keep_inds = np . append ( fg_inds , bg_inds ) . astype ( int ) # Select sampled values from various arrays: labels = labels [ keep_inds ] # Clamp labels for the background RoIs to 0 labels [ fg_rois_per_image : ] = 0 rois = all_rois [ keep_inds ] bbox_target_data = bbox_compute_targets ( rois [ : , 1 : 5 ] , gt_boxes [ gt_assignment [ keep_inds ] , : 4 ] , normalize = True ) bbox_target_data = np . hstack ( ( labels [ : , np . newaxis ] , bbox_target_data ) ) . astype ( np . float32 , copy = False ) bbox_targets , bbox_inside_weights = get_bbox_regression_label ( bbox_target_data , num_classes ) bbox_outside_weights = np . array ( bbox_inside_weights > 0 ) . astype ( np . float32 ) blobs = { 'rois' : rois } if cfg . MNC_MODE : scaled_rois = rois [ : , 1 : 5 ] / float ( im_scale ) # map to original image space scaled_gt_boxes = gt_boxes [ : , : 4 ] / float ( im_scale ) pos_masks = np . zeros ( ( len ( keep_inds ) , 1 , cfg . MASK_SIZE , cfg . MASK_SIZE ) ) top_mask_info = np . zeros ( ( len ( keep_inds ) , 12 ) ) top_mask_info [ len ( fg_inds ) : , : ] = - 1 for i , val in enumerate ( fg_inds ) : gt_box = scaled_gt_boxes [ gt_assignment [ val ] ] gt_box = np . around ( gt_box ) . astype ( int ) ex_box = np . around ( scaled_rois [ i ] ) . astype ( int ) gt_mask = gt_masks [ gt_assignment [ val ] ] gt_mask_info = mask_info [ gt_assignment [ val ] ] gt_mask = gt_mask [ 0 : gt_mask_info [ 0 ] , 0 : gt_mask_info [ 1 ] ] # calculate mask regression targets # (intersection of bounding box and gt mask) ex_mask = intersect_mask ( ex_box , gt_box , gt_mask ) pos_masks [ i , ... ] = ex_mask top_mask_info [ i , 0 ] = gt_assignment [ val ] top_mask_info [ i , 1 ] = gt_mask_info [ 0 ] top_mask_info [ i , 2 ] = gt_mask_info [ 1 ] top_mask_info [ i , 3 ] = labels [ i ] top_mask_info [ i , 4 : 8 ] = ex_box top_mask_info [ i , 8 : 12 ] = gt_box mask_weight = np . zeros ( ( rois . shape [ 0 ] , 1 , cfg . MASK_SIZE , cfg . MASK_SIZE ) ) # only assign box-level foreground as positive mask regression mask_weight [ 0 : len ( fg_inds ) , : , : , : ] = 1 blobs [ 'mask_targets' ] = pos_masks blobs [ 'mask_weight' ] = mask_weight blobs [ 'gt_masks_info' ] = top_mask_info return blobs , fg_inds , bg_inds , keep_inds