Ejemplo n.º 1
0
def test_single_gt_single_match_zero_neutral():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.5],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    ground_truth_boxes = tf.constant([[0.24, 0.5, 0.74, 1.0]])
    ground_truth_labels = tf.constant([[8]])
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_func(
        ground_truth_boxes,
        ground_truth_labels,
        anchors,
        negative_iou_threshold=1 / 3)
    expected_matched_gt_boxes = np.asarray([
        anchors[0, :], ground_truth_boxes[0, :], anchors[2, :], anchors[3, :]
    ])
    np.testing.assert_allclose(expected_matched_gt_boxes, matched_gt_boxes)
    expected_matched_gt_labels = np.zeros((4, 1))
    expected_matched_gt_labels[1] = ground_truth_labels[0]
    np.testing.assert_allclose(expected_matched_gt_labels, matched_gt_labels)
    expected_positive_mask = np.asarray([0, 1, 0, 0]).astype(np.int)
    expected_negative_mask = np.asarray([1, 0, 1, 1]).astype(np.int)
    np.testing.assert_equal(expected_positive_mask, positive_mask)
    np.testing.assert_equal(expected_negative_mask, negative_mask)
Ejemplo n.º 2
0
def test_single_gt_no_intersect():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.2],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    ground_truth_boxes = tf.constant([[0.4, 0.65, 0.6, 0.85]])
    ground_truth_labels = tf.constant([[8]])
    # Since it does not intersect with any anchor, it will be matched with the first gt.
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_func(
        ground_truth_boxes, ground_truth_labels, anchors)
    expected_matched_gt_boxes = np.asarray([
        ground_truth_boxes[0, :], anchors[1, :], anchors[2, :], anchors[3, :]
    ])
    np.testing.assert_allclose(expected_matched_gt_boxes, matched_gt_boxes)
    expected_matched_gt_labels = np.zeros((4, 1))
    expected_matched_gt_labels[0] = ground_truth_labels[0]
    np.testing.assert_allclose(expected_matched_gt_labels, matched_gt_labels)
    expected_positive_mask = np.asarray([1, 0, 0, 0]).astype(np.int)
    expected_negative_mask = np.asarray([0, 1, 1, 1]).astype(np.int)
    np.testing.assert_equal(expected_positive_mask, positive_mask)
    np.testing.assert_equal(expected_negative_mask, negative_mask)
Ejemplo n.º 3
0
def test_two_gt_two_matches():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.2],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    # The first box will be matched to the second anchor
    # The second box will be matched to the first anchor
    ground_truth_boxes = tf.constant([
        [0.15, 0.65, 0.35, 0.85],
        [0.14, 0.64, 0.34, 0.84],
    ])
    ground_truth_labels = tf.constant([[8], [6]])
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_func(
        ground_truth_boxes, ground_truth_labels, anchors)
    expected_matched_gt_boxes = np.asarray([
        ground_truth_boxes[1, :], ground_truth_boxes[0, :], anchors[2, :],
        anchors[3, :]
    ])
    np.testing.assert_allclose(expected_matched_gt_boxes, matched_gt_boxes)
    expected_matched_gt_labels = np.zeros((4, 1))
    expected_matched_gt_labels[1] = ground_truth_labels[0]
    expected_matched_gt_labels[0] = ground_truth_labels[1]
    np.testing.assert_allclose(expected_matched_gt_labels, matched_gt_labels)
    expected_positive_mask = np.asarray([1, 1, 0, 0]).astype(np.int)
    expected_negative_mask = np.asarray([0, 0, 1, 1]).astype(np.int)
    np.testing.assert_equal(expected_positive_mask, positive_mask)
    np.testing.assert_equal(expected_negative_mask, negative_mask)
Ejemplo n.º 4
0
def test_single_gt_four_match():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.5],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    ground_truth_boxes = tf.constant([[0.25, 0.25, 0.75, 0.75]])
    ground_truth_labels = tf.constant([[8]])
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_func(
        ground_truth_boxes,
        ground_truth_labels,
        anchors,
        positive_iou_threshold=1 / 7,
        negative_iou_threshold=1 / 8,
    )
    expected_matched_gt_boxes = np.tile(ground_truth_boxes, (4, 1))
    np.testing.assert_allclose(expected_matched_gt_boxes, matched_gt_boxes)
    expected_matched_gt_labels = np.tile(ground_truth_labels, (4, 1))
    np.testing.assert_allclose(expected_matched_gt_labels, matched_gt_labels)
    expected_positive_mask = np.asarray([1, 1, 1, 1]).astype(np.int)
    expected_negative_mask = np.asarray([0, 0, 0, 0]).astype(np.int)
    np.testing.assert_equal(expected_positive_mask, positive_mask)
    np.testing.assert_equal(expected_negative_mask, negative_mask)
Ejemplo n.º 5
0
def test_single_gt_best_match():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.2],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    ground_truth_boxes = tf.constant([[0.14, 0.64, 0.34, 0.84]])
    ground_truth_labels = tf.constant([[2]], dtype=tf.int64)
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_tf_func(
        ground_truth_boxes, ground_truth_labels, anchors)
    matched_gt_boxes = tf.expand_dims(matched_gt_boxes, axis=0)
    matched_gt_labels = tf.expand_dims(tf.squeeze(matched_gt_labels, axis=-1),
                                       axis=0)
    anchors = tf.expand_dims(anchors, axis=0)
    positive_mask = tf.expand_dims(positive_mask, axis=0)
    negative_mask = tf.expand_dims(negative_mask, axis=0)
    loss_layer = SSDLossLayer()
    anchor_labels = tf.constant(
        [[[.2, .8, 0.], [.2, .2, .2], [.1, .1, .1], [.1, .1, .1]]],
        dtype=tf.float32)
    _ = loss_layer(matched_gt_boxes, anchors, matched_gt_labels, anchor_labels,
                   positive_mask, negative_mask)
    reg_losses, cls_losses = loss_layer.losses
    # reg loss is 0.5 * (0.01^2+0.01^2+0.01^2+0.01^2) = 0.0002
    expected_reg_losses = 0.0002
    np.testing.assert_allclose(expected_reg_losses, reg_losses, atol=1e-7)
    # cls loss is log(0.2) + log(1/3) + log(1/3) + log(1/3)
    expected_cls_losses = 4.905275
    np.testing.assert_allclose(expected_cls_losses, cls_losses)
Ejemplo n.º 6
0
def test_two_gt_two_matches():
    anchor_gen = AnchorGenerator(
        image_size=(300, 300),
        scales=[0.2],
        aspect_ratios=[1.0],
        clip_boxes=False,
        normalize_coordinates=True,
    )
    anchors = anchor_gen((2, 2))
    print(anchors)
    # The first box will be matched to the second anchor
    # The second box will be matched to the first anchor
    ground_truth_boxes = tf.constant([
        [0.15, 0.65, 0.35, 0.85],
        [0.14, 0.64, 0.34, 0.84],
    ])
    ground_truth_labels = tf.constant([[1], [2]], dtype=tf.int64)
    matched_gt_boxes, matched_gt_labels, positive_mask, negative_mask = target_assign_tf_func(
        ground_truth_boxes, ground_truth_labels, anchors)
    loss_layer = SSDLossLayer(from_logits=True)
    anchor_labels = tf.constant(
        [[[.2, .8, .0], [.2, .2, .2], [.1, .1, .1], [.1, .1, .1]]],
        dtype=tf.float32)
    matched_gt_boxes = tf.expand_dims(matched_gt_boxes, axis=0)
    matched_gt_labels = tf.expand_dims(tf.squeeze(matched_gt_labels, axis=-1),
                                       axis=0)
    anchors = tf.expand_dims(anchors, axis=0)
    positive_mask = tf.expand_dims(positive_mask, axis=0)
    negative_mask = tf.expand_dims(negative_mask, axis=0)
    _ = loss_layer(matched_gt_boxes, anchors, matched_gt_labels, anchor_labels,
                   positive_mask, negative_mask)
    reg_losses, cls_losses = loss_layer.losses
    # reg loss is 0.5 * (0.01^2+0.49^2+0.01^2+0.49^2) / 2, since n_positives = 2
    np.testing.assert_allclose(0.12009999, reg_losses)
    # cls loss is (log(0.22487354) + log(1/3) + log(1/3) + log(1/3)) / 2, since n_positives = 2
    np.testing.assert_allclose(2.394027, cls_losses)