def test_label_invariance(self, label0: int, label1: int,
                              shuffle_inds: List[int]):
        """ Ensure that datum ordering doesn't matter for generate_targets. """
        # xyxy format
        prop = np.array([
            [-0.5, -0.5, 0.5,
             0.5],  # iou = 1 (truth 0) should be marked poitiive
            [0.0, -0.5, 0.49,
             0.5],  # iou = 0.5  (truth 0) should be marked ignore
            [0.0, -0.5, 0.39,
             0.5],  # iou = 0.39  (truth 0) should be marked negative
            [10.0, 10.0, 11, 11],
        ])  # iou = 1 (truth 1) should be marked positive

        # xyxy format
        truth = np.array([[-0.5, -0.5, 0.5, 0.5], [10.0, 10.0, 11, 11]])

        labels = np.array([label0, label1])

        out_labels = np.array(
            [label0, -1, 0,
             label1])  # truth 0 / ignore / background / truth 1 from above

        labels, reg = generate_targets(prop[shuffle_inds], truth, labels, 0.5,
                                       0.4)
        msg = "generate_targets is not invariant to datum-ordering"
        assert_allclose(actual=labels,
                        desired=out_labels[shuffle_inds],
                        err_msg=msg)
 def test_identical_proposed_and_truth(self, x: ndarray):
     """ Ensure that generate_targets produces regression targets that are zero for identical proposal and truth. """
     x = x.cumsum(axis=1)  # ensure (l, t, r , b)
     labels = np.array([0] * 5)
     _, reg = generate_targets(x, x, labels, 0.5, 0.4)
     msg = "generate_targets failed to produce the expected output when the proposed boxes are identical to ground truth"
     assert_allclose(actual=reg,
                     desired=np.zeros_like(x),
                     atol=1e-5,
                     rtol=1e-5,
                     err_msg=msg)
Esempio n. 3
0
    def val_dataloader(self) -> DataLoader:

        val_cls_targs, val_reg_targs = zip(
            *(generate_targets(self.anchor_boxes, bxs, lbls, 0.2, 0.1)
              for bxs, lbls in zip(self.val_boxes, self.val_labels)))

        val_reg_targs = tr.tensor(val_reg_targs).float()
        val_cls_targs = tr.tensor(val_cls_targs).long()
        return DataLoader(
            TensorDataset(self.val_images, val_cls_targs, val_reg_targs),
            batch_size=16,
            pin_memory=True,
            num_workers=4,
            shuffle=False,
            drop_last=True,
        )
    def test_shapes(self, boxes: ndarray, truth: ndarray,
                    data: st.SearchStrategy):
        """ Ensure the shape returned by generate_targets is correct, even in edge cases producing empty arrays. """
        boxes = boxes.cumsum(
            axis=1)  # to ensure we don't hit 0-width or -height boxes
        truth = truth.cumsum(
            axis=1)  # to ensure we don't hit 0-width or -height boxes
        N = boxes.shape[0]
        K = truth.shape[0]
        labels = data.draw(hnp.arrays(dtype=int, shape=(K, )))
        cls, reg = generate_targets(boxes, truth, labels, 0.5, 0.4)

        msg = "generate_targets failed to produce classification targets of the correct shape"
        assert cls.shape == (N, ), msg

        msg = "generate_targets failed to produce regression targets of the correct shape"
        assert reg.shape == (N, 4), msg
    def test_known_regression_values(self, shuffle_inds: List[int]):
        """ Ensure that generate_targets works for known values. Ensure that datum ordering does not matter. """
        prop = np.array([
            [-0.5, -0.5, 0.5, 0.5],  # neither axis matches truth
            [0, -0.5, np.exp(1), 0.5],  # x matches truth
            [-0.5, 0, 0.5, np.exp(1)],  # y matches truth
        ])
        truth = np.array([[0, 0, np.exp(1), np.exp(1)]])
        labels = np.array([1.0])

        out_reg = np.array([
            [np.exp(1) / 2, np.exp(1) / 2, 1, 1],
            [0, np.exp(1) / 2, 0, 1],
            [np.exp(1) / 2, 0, 1, 0],
        ])

        _, reg = generate_targets(prop[shuffle_inds], truth, labels, 0.5, 0.4)
        msg = "generate_targets failed to produce known-correct regression values"
        assert_allclose(actual=reg,
                        desired=out_reg[shuffle_inds],
                        atol=1e-5,
                        rtol=1e-5,
                        err_msg=msg)