def __init__(self, mix_gt_boxes: bool = True, num_sampled_rois: int = 512, foreground_fraction: float = 0.25, foreground_iou_threshold: float = 0.5, background_iou_high_threshold: float = 0.5, background_iou_low_threshold: float = 0, skip_subsampling: bool = False, **kwargs): """Initializes a ROI sampler. Args: mix_gt_boxes: A `bool` of whether to mix the groundtruth boxes with proposed ROIs. num_sampled_rois: An `int` of the number of sampled ROIs per image. foreground_fraction: A `float` in [0, 1], what percentage of proposed ROIs should be sampled from the foreground boxes. foreground_iou_threshold: A `float` that represents the IoU threshold for a box to be considered as positive (if >= `foreground_iou_threshold`). background_iou_high_threshold: A `float` that represents the IoU threshold for a box to be considered as negative (if overlap in [`background_iou_low_threshold`, `background_iou_high_threshold`]). background_iou_low_threshold: A `float` that represents the IoU threshold for a box to be considered as negative (if overlap in [`background_iou_low_threshold`, `background_iou_high_threshold`]) skip_subsampling: a bool that determines if we want to skip the sampling procedure than balances the fg/bg classes. Used for upper frcnn layers in cascade RCNN. **kwargs: Additional keyword arguments passed to Layer. """ self._config_dict = { 'mix_gt_boxes': mix_gt_boxes, 'num_sampled_rois': num_sampled_rois, 'foreground_fraction': foreground_fraction, 'foreground_iou_threshold': foreground_iou_threshold, 'background_iou_high_threshold': background_iou_high_threshold, 'background_iou_low_threshold': background_iou_low_threshold, 'skip_subsampling': skip_subsampling, } self._sim_calc = iou_similarity.IouSimilarity() self._box_matcher = box_matcher.BoxMatcher(thresholds=[ background_iou_low_threshold, background_iou_high_threshold, foreground_iou_threshold ], indicators=[-3, -1, -2, 1]) self._target_gather = target_gather.TargetGather() self._sampler = box_sampler.BoxSampler(num_sampled_rois, foreground_fraction) super(ROISampler, self).__init__(**kwargs)
def __init__(self, match_threshold=0.5, unmatched_threshold=0.5): """Constructs anchor labeler to assign labels to anchors. Args: match_threshold: a float number between 0 and 1 representing the lower-bound threshold to assign positive labels for anchors. An anchor with a score over the threshold is labeled positive. unmatched_threshold: a float number between 0 and 1 representing the upper-bound threshold to assign negative labels for anchors. An anchor with a score below the threshold is labeled negative. """ self.similarity_calc = iou_similarity.IouSimilarity() self.target_gather = target_gather.TargetGather() self.matcher = box_matcher.BoxMatcher( thresholds=[unmatched_threshold, match_threshold], indicators=[-1, -2, 1], force_match_for_each_col=True) self.box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()
def test_box_matcher_batched(self): sim_matrix = tf.constant([[[0.04, 0, 0, 0], [0, 0, 1., 0]]], dtype=tf.float32) fg_threshold = 0.5 bg_thresh_hi = 0.2 bg_thresh_lo = 0.0 matcher = box_matcher.BoxMatcher( thresholds=[bg_thresh_lo, bg_thresh_hi, fg_threshold], indicators=[-3, -2, -1, 1]) match_indices, match_indicators = matcher(sim_matrix) positive_matches = tf.greater_equal(match_indicators, 0) negative_matches = tf.equal(match_indicators, -2) self.assertAllEqual(positive_matches.numpy(), [[False, True]]) self.assertAllEqual(negative_matches.numpy(), [[True, False]]) self.assertAllEqual(match_indices.numpy(), [[0, 2]]) self.assertAllEqual(match_indicators.numpy(), [[-2, 1]])