Beispiel #1
0
    def test_correct_results(self, degrees, keep_size, mode, padding_mode,
                             align_corners):
        rotate_fn = RandRotated(
            "img",
            range_x=degrees,
            prob=1.0,
            keep_size=keep_size,
            mode=mode,
            padding_mode=padding_mode,
            align_corners=align_corners,
        )
        rotate_fn.set_random_state(243)
        rotated = rotate_fn({"img": self.imt[0], "seg": self.segn[0]})

        _order = 0 if mode == "nearest" else 1
        if padding_mode == "border":
            _mode = "nearest"
        elif padding_mode == "reflection":
            _mode = "reflect"
        else:
            _mode = "constant"
        angle = rotate_fn.x
        expected = scipy.ndimage.rotate(self.imt[0, 0],
                                        -np.rad2deg(angle), (0, 1),
                                        not keep_size,
                                        order=_order,
                                        mode=_mode,
                                        prefilter=False)
        expected = np.stack(expected).astype(np.float32)
        self.assertTrue(np.allclose(expected, rotated["img"][0]))
Beispiel #2
0
    def test_correct_results(self, im_type, degrees, keep_size, mode, padding_mode, align_corners):
        rotate_fn = RandRotated(
            "img",
            range_x=degrees,
            prob=1.0,
            keep_size=keep_size,
            mode=mode,
            padding_mode=padding_mode,
            align_corners=align_corners,
            dtype=np.float64,
        )
        im = im_type(self.imt[0])
        rotate_fn.set_random_state(243)
        rotated = rotate_fn({"img": im, "seg": im_type(self.segn[0])})

        _order = 0 if mode == "nearest" else 1
        if padding_mode == "border":
            _mode = "nearest"
        elif padding_mode == "reflection":
            _mode = "reflect"
        else:
            _mode = "constant"
        angle = rotate_fn.rand_rotate.x
        expected = scipy.ndimage.rotate(
            self.imt[0, 0], -np.rad2deg(angle), (0, 1), not keep_size, order=_order, mode=_mode, prefilter=False
        )
        test_local_inversion(rotate_fn, rotated, {"img": im}, "img")
        for k, v in rotated.items():
            rotated[k] = v.cpu() if isinstance(v, torch.Tensor) else v
        expected = np.stack(expected).astype(np.float32)
        good = np.sum(np.isclose(expected, rotated["img"][0], atol=1e-3))
        self.assertLessEqual(np.abs(good - expected.size), 5, "diff at most 5 pixels")
Beispiel #3
0
    def test_correct_results(self, degrees, spatial_axes, reshape, order, mode,
                             cval, prefilter):
        rotate_fn = RandRotated('img',
                                degrees,
                                prob=1.0,
                                spatial_axes=spatial_axes,
                                reshape=reshape,
                                order=order,
                                mode=mode,
                                cval=cval,
                                prefilter=prefilter)
        rotate_fn.set_random_state(243)
        rotated = rotate_fn({'img': self.imt[0]})

        angle = rotate_fn.angle
        expected = list()
        for channel in self.imt[0]:
            expected.append(
                scipy.ndimage.rotate(channel,
                                     angle,
                                     spatial_axes,
                                     reshape,
                                     order=order,
                                     mode=mode,
                                     cval=cval,
                                     prefilter=prefilter))
        expected = np.stack(expected).astype(np.float32)
        self.assertTrue(np.allclose(expected, rotated['img']))
Beispiel #4
0
 def test_correct_shapes(self, im_type, x, y, z, keep_size, mode, padding_mode, align_corners, expected):
     rotate_fn = RandRotated(
         "img",
         range_x=x,
         range_y=y,
         range_z=z,
         prob=1.0,
         keep_size=keep_size,
         mode=mode,
         padding_mode=padding_mode,
         align_corners=align_corners,
         dtype=np.float64,
     )
     rotate_fn.set_random_state(243)
     rotated = rotate_fn({"img": im_type(self.imt[0]), "seg": im_type(self.segn[0])})
     np.testing.assert_allclose(rotated["img"].shape, expected)