def test_create_target_assigner(self): """Tests that named constructor gives working target assigners. TODO: Make this test more general. """ corners = [[0.0, 0.0, 1.0, 1.0]] groundtruth = box_list.BoxList(tf.constant(corners)) priors = box_list.BoxList(tf.constant(corners)) prior_stddevs = tf.constant([[1.0, 1.0, 1.0, 1.0]]) priors.add_field('stddev', prior_stddevs) multibox_ta = (targetassigner.create_target_assigner('Multibox', stage='proposal')) multibox_ta.assign(priors, groundtruth) # No tests on output, as that may vary arbitrarily as new target assigners # are added. As long as it is constructed correctly and runs without errors, # tests on the individual assigners cover correctness of the assignments. anchors = box_list.BoxList(tf.constant(corners)) faster_rcnn_proposals_ta = (targetassigner.create_target_assigner( 'FasterRCNN', stage='proposal')) faster_rcnn_proposals_ta.assign(anchors, groundtruth) fast_rcnn_ta = (targetassigner.create_target_assigner('FastRCNN')) fast_rcnn_ta.assign(anchors, groundtruth) faster_rcnn_detection_ta = (targetassigner.create_target_assigner( 'FasterRCNN', stage='detection')) faster_rcnn_detection_ta.assign(anchors, groundtruth) with self.assertRaises(ValueError): targetassigner.create_target_assigner('InvalidDetector', stage='invalid_stage')
def test_raises_error_on_invalid_groundtruth_labels(self): similarity_calc = region_similarity_calculator.NegSqDistSimilarity() matcher = bipartite_matcher.GreedyBipartiteMatcher() box_coder = mean_stddev_box_coder.MeanStddevBoxCoder() unmatched_cls_target = tf.constant([[0, 0], [0, 0], [0, 0]], tf.float32) target_assigner = targetassigner.TargetAssigner( similarity_calc, matcher, box_coder, unmatched_cls_target=unmatched_cls_target) prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5]]) prior_stddevs = tf.constant([[1.0, 1.0, 1.0, 1.0]]) priors = box_list.BoxList(prior_means) priors.add_field('stddev', prior_stddevs) box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9], [.75, 0, .95, .27]] boxes = box_list.BoxList(tf.constant(box_corners)) groundtruth_labels = tf.constant([[[0, 1], [1, 0]]], tf.float32) with self.assertRaises(ValueError): target_assigner.assign(priors, boxes, groundtruth_labels, num_valid_rows=3)
def testHardMiningNMS(self): location_losses = tf.constant([[100, 90, 80, 0], [0, 1, 2, 3]], tf.float32) cls_losses = tf.constant([[0, 10, 50, 110], [9, 6, 3, 0]], tf.float32) box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9], [0.9, 0.9, 0.99, 0.99], [0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9]], tf.float32) decoded_boxlist_list = [] decoded_boxlist_list.append(box_list.BoxList(box_corners)) decoded_boxlist_list.append(box_list.BoxList(box_corners)) loss_op = losses.HardExampleMiner(num_hard_examples=2, iou_threshold=0.5, loss_type='cls', cls_loss_weight=1, loc_loss_weight=1) (loc_loss, cls_loss) = loss_op(location_losses, cls_losses, decoded_boxlist_list) exp_loc_loss = 0 + 90 + 0 + 1 exp_cls_loss = 110 + 10 + 9 + 6 with self.test_session() as sess: loc_loss_output = sess.run(loc_loss) self.assertAllClose(loc_loss_output, exp_loc_loss) cls_loss_output = sess.run(cls_loss) self.assertAllClose(cls_loss_output, exp_cls_loss)
def test_raises_error_on_incompatible_groundtruth_boxes_and_labels(self): similarity_calc = region_similarity_calculator.NegSqDistSimilarity() matcher = bipartite_matcher.GreedyBipartiteMatcher() box_coder = mean_stddev_box_coder.MeanStddevBoxCoder() unmatched_cls_target = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32) target_assigner = targetassigner.TargetAssigner( similarity_calc, matcher, box_coder, unmatched_cls_target=unmatched_cls_target) prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8], [0, 0.5, .5, 1.0], [.75, 0, 1.0, .25]]) prior_stddevs = tf.constant(4 * [4 * [.1]]) priors = box_list.BoxList(prior_means) priors.add_field('stddev', prior_stddevs) box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.8], [0.5, 0.5, 0.9, 0.9], [.75, 0, .95, .27]] boxes = box_list.BoxList(tf.constant(box_corners)) groundtruth_labels = tf.constant( [[0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0]], tf.float32) result = target_assigner.assign(priors, boxes, groundtruth_labels, num_valid_rows=3) (cls_targets, cls_weights, reg_targets, reg_weights, _) = result with self.test_session() as sess: with self.assertRaisesWithPredicateMatch( tf.errors.InvalidArgumentError, 'Groundtruth boxes and labels have incompatible shapes!'): sess.run([cls_targets, cls_weights, reg_targets, reg_weights])
def testHardMiningWithSingleLossType(self): location_losses = tf.constant([[100, 90, 80, 0], [0, 1, 2, 3]], tf.float32) cls_losses = tf.constant([[0, 10, 50, 110], [9, 6, 3, 0]], tf.float32) box_corners = tf.constant([[0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9], [0.1, 0.1, 0.9, 0.9]], tf.float32) decoded_boxlist_list = [] decoded_boxlist_list.append(box_list.BoxList(box_corners)) decoded_boxlist_list.append(box_list.BoxList(box_corners)) # Uses only location loss to select hard examples loss_op = losses.HardExampleMiner(num_hard_examples=1, iou_threshold=0.0, loss_type='loc', cls_loss_weight=1, loc_loss_weight=1) (loc_loss, cls_loss) = loss_op(location_losses, cls_losses, decoded_boxlist_list) exp_loc_loss = 100 + 3 exp_cls_loss = 0 + 0 with self.test_session() as sess: loc_loss_output = sess.run(loc_loss) self.assertAllClose(loc_loss_output, exp_loc_loss) cls_loss_output = sess.run(cls_loss) self.assertAllClose(cls_loss_output, exp_cls_loss)
def test_batch_assign_multidimensional_targets(self): box_list1 = box_list.BoxList(tf.constant([[0., 0., 0.2, 0.2]])) box_list2 = box_list.BoxList( tf.constant([[0, 0.25123152, 1, 1], [0.015789, 0.0985, 0.55789, 0.3842]])) gt_box_batch = [box_list1, box_list2] class_targets1 = tf.constant([[[0, 1, 1], [1, 1, 0]]], tf.float32) class_targets2 = tf.constant( [[[0, 1, 1], [1, 1, 0]], [[0, 0, 1], [0, 0, 1]]], tf.float32) gt_class_targets = [class_targets1, class_targets2] prior_means = tf.constant([[0, 0, .25, .25], [0, .25, 1, 1], [0, .1, .5, .5], [.75, .75, 1, 1]]) prior_stddevs = tf.constant([[.1, .1, .1, .1], [.1, .1, .1, .1], [.1, .1, .1, .1], [.1, .1, .1, .1]]) priors = box_list.BoxList(prior_means) priors.add_field('stddev', prior_stddevs) exp_reg_targets = [[[0, 0, -0.5, -0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0.01231521, 0, 0], [0.15789001, -0.01500003, 0.57889998, -1.15799987], [0, 0, 0, 0]]] exp_cls_weights = [[1, 1, 1, 1], [1, 1, 1, 1]] exp_cls_targets = [[[[0., 1., 1.], [1., 1., 0.]], [[0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.]]], [[[0., 0., 0.], [0., 0., 0.]], [[0., 1., 1.], [1., 1., 0.]], [[0., 0., 1.], [0., 0., 1.]], [[0., 0., 0.], [0., 0., 0.]]]] exp_reg_weights = [[1, 0, 0, 0], [0, 1, 1, 0]] exp_match_0 = [0] exp_match_1 = [1, 2] multiclass_target_assigner = self._get_multi_dimensional_target_assigner( target_dimensions=(2, 3)) (cls_targets, cls_weights, reg_targets, reg_weights, match_list) = targetassigner.batch_assign_targets( multiclass_target_assigner, priors, gt_box_batch, gt_class_targets) self.assertTrue(isinstance(match_list, list) and len(match_list) == 2) with self.test_session() as sess: (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out, match_out_0, match_out_1) = sess.run( [cls_targets, cls_weights, reg_targets, reg_weights] + [match.matched_column_indices() for match in match_list]) self.assertAllClose(cls_targets_out, exp_cls_targets) self.assertAllClose(cls_weights_out, exp_cls_weights) self.assertAllClose(reg_targets_out, exp_reg_targets) self.assertAllClose(reg_weights_out, exp_reg_weights) self.assertAllClose(match_out_0, exp_match_0) self.assertAllClose(match_out_1, exp_match_1)
def test_concatenate_with_missing_fields(self): corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) scores1 = tf.constant([1.0, 2.1]) corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32) boxlist1 = box_list.BoxList(corners1) boxlist1.add_field('scores', scores1) boxlist2 = box_list.BoxList(corners2) with self.assertRaises(ValueError): box_list_ops.concatenate([boxlist1, boxlist2])
def test_assign_multidimensional_class_targets(self): similarity_calc = region_similarity_calculator.NegSqDistSimilarity() matcher = bipartite_matcher.GreedyBipartiteMatcher() box_coder = mean_stddev_box_coder.MeanStddevBoxCoder() unmatched_cls_target = tf.constant([[0, 0], [0, 0]], tf.float32) target_assigner = targetassigner.TargetAssigner( similarity_calc, matcher, box_coder, unmatched_cls_target=unmatched_cls_target) prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8], [0, 0.5, .5, 1.0], [.75, 0, 1.0, .25]]) prior_stddevs = tf.constant(4 * [4 * [.1]]) priors = box_list.BoxList(prior_means) priors.add_field('stddev', prior_stddevs) box_corners = [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.9, 0.9], [.75, 0, .95, .27]] boxes = box_list.BoxList(tf.constant(box_corners)) groundtruth_labels = tf.constant( [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 1], [1, .5]]], tf.float32) exp_cls_targets = [[[0, 1], [1, 0]], [[1, 0], [0, 1]], [[0, 0], [0, 0]], [[0, 1], [1, .5]]] exp_cls_weights = [1, 1, 1, 1] exp_reg_targets = [[0, 0, 0, 0], [0, 0, -1, 1], [0, 0, 0, 0], [0, 0, -.5, .2]] exp_reg_weights = [1, 1, 0, 1] exp_matching_anchors = [0, 1, 3] result = target_assigner.assign(priors, boxes, groundtruth_labels, num_valid_rows=3) (cls_targets, cls_weights, reg_targets, reg_weights, match) = result with self.test_session() as sess: (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out, matching_anchors_out) = sess.run([ cls_targets, cls_weights, reg_targets, reg_weights, match.matched_column_indices() ]) self.assertAllClose(cls_targets_out, exp_cls_targets) self.assertAllClose(cls_weights_out, exp_cls_weights) self.assertAllClose(reg_targets_out, exp_reg_targets) self.assertAllClose(reg_weights_out, exp_reg_weights) self.assertAllClose(matching_anchors_out, exp_matching_anchors) self.assertEquals(cls_targets_out.dtype, np.float32) self.assertEquals(cls_weights_out.dtype, np.float32) self.assertEquals(reg_targets_out.dtype, np.float32) self.assertEquals(reg_weights_out.dtype, np.float32) self.assertEquals(matching_anchors_out.dtype, np.int32)
def test_box_voting_fails_with_negative_scores(self): candidates = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool = box_list.BoxList(tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool.add_field('scores', tf.constant([-0.2])) averaged_boxes = box_list_ops.box_voting(candidates, pool) with self.test_session() as sess: with self.assertRaisesOpError('Scores must be non negative'): sess.run([averaged_boxes.get()])
def test_matched_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) exp_output = [2.0 / 16.0, 0] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.matched_iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output)
def test_box_voting_fails_when_unmatched(self): candidates = box_list.BoxList( tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) pool = box_list.BoxList(tf.constant([[0.6, 0.6, 0.8, 0.8]], tf.float32)) pool.add_field('scores', tf.constant([0.2])) averaged_boxes = box_list_ops.box_voting(candidates, pool) with self.test_session() as sess: with self.assertRaisesOpError('Each box in selected_boxes must match ' 'with at least one box in pool_boxes.'): sess.run([averaged_boxes.get()])
def test_very_small_Width_nan_after_encoding(self): boxes = [[10.0, 10.0, 10.0000001, 20.0]] anchors = [[15.0, 12.0, 30.0, 18.0]] expected_rel_codes = [[-0.833333, 0., -21.128731, 0.510826]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: rel_codes_out, = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_intersection(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_output = [[2.0, 0.0, 6.0], [1.0, 0.0, 5.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) intersect = box_list_ops.intersection(boxes1, boxes2) with self.test_session() as sess: intersect_output = sess.run(intersect) self.assertAllClose(intersect_output, exp_output)
def test_box_list_invalid_inputs(self): data0 = tf.constant([[[0, 0, 1, 1], [3, 4, 5, 5]]], tf.float32) data1 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.float32) data2 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.int32) with self.assertRaises(ValueError): _ = box_list.BoxList(data0) with self.assertRaises(ValueError): _ = box_list.BoxList(data1) with self.assertRaises(ValueError): _ = box_list.BoxList(data2)
def test_gather_with_invalid_inputs(self): corners = tf.constant( [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) indices_float32 = tf.constant([0, 2, 4], tf.float32) boxes = box_list.BoxList(corners) with self.assertRaises(ValueError): _ = box_list_ops.gather(boxes, indices_float32) indices_2d = tf.constant([[0, 2, 4]], tf.int32) boxes = box_list.BoxList(corners) with self.assertRaises(ValueError): _ = box_list_ops.gather(boxes, indices_2d)
def test_get_correct_pairwise_similarity_based_on_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou_similarity_calculator = region_similarity_calculator.IouSimilarity() iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou_similarity) self.assertAllClose(iou_output, exp_output)
def test_correct_relative_codes_with_small_width(self): boxes = [[10.0, 10.0, 10.0000001, 20.0]] anchors = [[15.0, 12.0, 30.0, 18.0]] scale_factors = None expected_rel_codes = [[-1.317616, 0., -20.670586]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: (rel_codes_out,) = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_get_correct_relative_codes_after_encoding(self): boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]] expected_rel_codes = [[-0.5, -0.416666, -0.405465, -0.182321], [-0.083333, -0.222222, -0.693147, -1.098612]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: rel_codes_out, = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_change_coordinate_frame(self): corners = tf.constant([[0.25, 0.5, 0.75, 0.75], [0.5, 0.0, 1.0, 1.0]]) window = tf.constant([0.25, 0.25, 0.75, 0.75]) boxes = box_list.BoxList(corners) expected_corners = tf.constant([[0, 0.5, 1.0, 1.0], [0.5, -0.5, 1.5, 1.5]]) expected_boxes = box_list.BoxList(expected_corners) output = box_list_ops.change_coordinate_frame(boxes, window) with self.test_session() as sess: output_, expected_boxes_ = sess.run([output.get(), expected_boxes.get()]) self.assertAllClose(output_, expected_boxes_)
def test_pairwise_distances(self): corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 2.0]]) corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], [-4.0, 0.0, 0.0, 3.0], [0.0, 0.0, 0.0, 0.0]]) exp_output = [[26, 25, 0], [18, 27, 6]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) dist_matrix = box_list_ops.sq_dist(boxes1, boxes2) with self.test_session() as sess: dist_output = sess.run(dist_matrix) self.assertAllClose(dist_output, exp_output)
def test_correct_relative_codes_with_non_default_scale(self): boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]] scale_factors = [2, 3, 4] expected_rel_codes = [[-1.581139, -0.790569, -1.175573], [-0.136083, -0.816497, -3.583519]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: (rel_codes_out,) = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_correct_relative_codes_with_default_scale(self): boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]] scale_factors = None expected_rel_codes = [[-0.790569, -0.263523, -0.293893], [-0.068041, -0.272166, -0.89588]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: (rel_codes_out,) = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_get_correct_pairwise_similarity_based_on_squared_distances(self): corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 2.0]]) corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], [-4.0, 0.0, 0.0, 3.0], [0.0, 0.0, 0.0, 0.0]]) exp_output = [[-26, -25, 0], [-18, -27, -6]] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) dist_similarity_calc = region_similarity_calculator.NegSqDistSimilarity() dist_similarity = dist_similarity_calc.compare(boxes1, boxes2) with self.test_session() as sess: dist_output = sess.run(dist_similarity) self.assertAllClose(dist_output, exp_output)
def test_copy_extra_fields(self): corners = tf.constant([[0, 0, 1, 1], [0, 0.1, 1, 1.1]], tf.float32) boxes = box_list.BoxList(corners) tensor1 = np.array([[1], [4]]) tensor2 = np.array([[1, 1], [2, 2]]) boxes.add_field('tensor1', tf.constant(tensor1)) boxes.add_field('tensor2', tf.constant(tensor2)) new_boxes = box_list.BoxList(tf.constant([[0, 0, 10, 10], [1, 3, 5, 5]], tf.float32)) new_boxes = box_list_ops._copy_extra_fields(new_boxes, boxes) with self.test_session() as sess: self.assertAllClose(tensor1, sess.run(new_boxes.get_field('tensor1'))) self.assertAllClose(tensor2, sess.run(new_boxes.get_field('tensor2')))
def test_very_small_width_nan_after_encoding(self): boxes = [[10., 10., 10.0000001, 20.]] keypoints = [[[10., 10.], [10.0000001, 20.]]] anchors = [[15., 12., 30., 18.]] expected_rel_codes = [[-0.833333, 0., -21.128731, 0.510826, -0.833333, -0.833333, -0.833333, 0.833333]] boxes = box_list.BoxList(tf.constant(boxes)) boxes.add_field(fields.BoxListFields.keypoints, tf.constant(keypoints)) anchors = box_list.BoxList(tf.constant(anchors)) coder = keypoint_box_coder.KeypointBoxCoder(2) rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: rel_codes_out, = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def testGetCorrectRelativeCodesAfterEncoding(self): box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]] boxes = box_list.BoxList(tf.constant(box_corners)) expected_rel_codes = [[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]] prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]]) prior_stddevs = tf.constant(2 * [4 * [.1]]) priors = box_list.BoxList(prior_means) priors.add_field('stddev', prior_stddevs) coder = mean_stddev_box_coder.MeanStddevBoxCoder() rel_codes = coder.encode(boxes, priors) with self.test_session() as sess: rel_codes_out = sess.run(rel_codes) self.assertAllClose(rel_codes_out, expected_rel_codes)
def test_get_correct_relative_codes_after_encoding_with_scaling(self): boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] anchors = [[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]] scale_factors = [2, 3, 4, 5] expected_rel_codes = [[-1., -1.25, -1.62186, -0.911608], [-0.166667, -0.666667, -2.772588, -5.493062]] boxes = box_list.BoxList(tf.constant(boxes)) anchors = box_list.BoxList(tf.constant(anchors)) coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( scale_factors=scale_factors) rel_codes = coder.encode(boxes, anchors) with self.test_session() as sess: rel_codes_out, = sess.run([rel_codes]) self.assertAllClose(rel_codes_out, expected_rel_codes)
def _decode(self, rel_codes, anchors): """Decode relative codes to boxes. Args: rel_codes: a tensor representing N anchor-encoded boxes. anchors: BoxList of anchors. Returns: boxes: BoxList holding N bounding boxes. """ ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() ty, tx, th, tw = tf.unstack(tf.transpose(rel_codes)) if self._scale_factors: ty /= self._scale_factors[0] tx /= self._scale_factors[1] th /= self._scale_factors[2] tw /= self._scale_factors[3] w = tf.exp(tw) * wa h = tf.exp(th) * ha ycenter = ty * ha + ycenter_a xcenter = tx * wa + xcenter_a ymin = ycenter - h / 2. xmin = xcenter - w / 2. ymax = ycenter + h / 2. xmax = xcenter + w / 2. return box_list.BoxList(tf.transpose(tf.stack([ymin, xmin, ymax, xmax])))
def test_ioaworks_on_empty_inputs(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], [0.0, 0.0, 20.0, 20.0]]) boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) boxes_empty = box_list.BoxList(tf.zeros((0, 4))) ioa_empty_1 = box_list_ops.ioa(boxes1, boxes_empty) ioa_empty_2 = box_list_ops.ioa(boxes_empty, boxes2) ioa_empty_3 = box_list_ops.ioa(boxes_empty, boxes_empty) with self.test_session() as sess: ioa_output_1, ioa_output_2, ioa_output_3 = sess.run( [ioa_empty_1, ioa_empty_2, ioa_empty_3]) self.assertAllEqual(ioa_output_1.shape, (2, 0)) self.assertAllEqual(ioa_output_2.shape, (0, 3)) self.assertAllEqual(ioa_output_3.shape, (0, 0))
def _decode(self, rel_codes, anchors): """Decodes relative codes to boxes. Args: rel_codes: a tensor representing N anchor-encoded boxes. anchors: BoxList of anchors. Returns: boxes: BoxList holding N bounding boxes. """ ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes( ) la = tf.sqrt(ha * wa) ty, tx, tl = tf.unstack(tf.transpose(rel_codes)) if self._scale_factors: ty /= self._scale_factors[0] tx /= self._scale_factors[1] tl /= self._scale_factors[2] l = tf.exp(tl) * la ycenter = ty * la + ycenter_a xcenter = tx * la + xcenter_a ymin = ycenter - l / 2. xmin = xcenter - l / 2. ymax = ycenter + l / 2. xmax = xcenter + l / 2. return box_list.BoxList( tf.transpose(tf.stack([ymin, xmin, ymax, xmax])))