Exemplo n.º 1
0
 def test_value(self):
     shifter = RandShiftIntensity(offsets=1.0, prob=1.0)
     shifter.set_random_state(seed=0)
     result = shifter(self.imt)
     np.random.seed(0)
     expected = self.imt + np.random.uniform(low=-1.0, high=1.0)
     np.testing.assert_allclose(result, expected)
 def test_value(self):
     shifter = RandShiftIntensity(offsets=1.0, prob=1.0)
     shifter.set_random_state(seed=0)
     result = shifter(self.imt, factor=1.0)
     np.random.seed(0)
     # simulate the randomize() of transform
     np.random.random()
     expected = self.imt + np.random.uniform(low=-1.0, high=1.0)
     np.testing.assert_allclose(result, expected)
    def test_loading_array(self):
        set_determinism(seed=1234)
        # image dataset
        images = [
            np.arange(16, dtype=float).reshape(1, 4, 4),
            np.arange(16, dtype=float).reshape(1, 4, 4)
        ]
        # image patch sampler
        n_samples = 8
        sampler = RandSpatialCropSamples(roi_size=(3, 3),
                                         num_samples=n_samples,
                                         random_center=True,
                                         random_size=False)

        # image level
        patch_intensity = RandShiftIntensity(offsets=1.0, prob=1.0)
        image_ds = Dataset(images, transform=patch_intensity)
        # patch level
        ds = PatchDataset(dataset=image_ds,
                          patch_func=sampler,
                          samples_per_image=n_samples,
                          transform=patch_intensity)

        np.testing.assert_equal(len(ds), n_samples * len(images))
        # use the patch dataset, length: len(images) x samplers_per_image
        for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=0):
            np.testing.assert_equal(tuple(item.shape), (2, 1, 3, 3))
        np.testing.assert_allclose(
            item[0],
            np.array([[[1.338681, 2.338681, 3.338681],
                       [5.338681, 6.338681, 7.338681],
                       [9.338681, 10.338681, 11.338681]]]),
            rtol=1e-5,
        )
        if sys.platform != "win32":
            for item in DataLoader(ds,
                                   batch_size=2,
                                   shuffle=False,
                                   num_workers=2):
                np.testing.assert_equal(tuple(item.shape), (2, 1, 3, 3))
            np.testing.assert_allclose(
                item[0],
                np.array([[
                    [4.957847, 5.957847, 6.957847],
                    [8.957847, 9.957847, 10.957847],
                    [12.957847, 13.957847, 14.957847],
                ]]),
                rtol=1e-5,
            )
        set_determinism(seed=None)
Exemplo n.º 4
0
 def test_loading_array(self):
     set_determinism(seed=1234)
     # image dataset
     images = [
         np.arange(16, dtype=float).reshape(1, 4, 4),
         np.arange(16, dtype=float).reshape(1, 4, 4)
     ]
     # image level
     patch_intensity = RandShiftIntensity(offsets=1.0, prob=1.0)
     patch_iter = PatchIter(patch_size=(2, 2), start_pos=(0, 0))
     ds = GridPatchDataset(dataset=images,
                           patch_iter=patch_iter,
                           transform=patch_intensity)
     # use the grid patch dataset
     for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=0):
         np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
     np.testing.assert_allclose(
         item[0],
         np.array([[[[1.7413, 2.7413], [5.7413, 6.7413]]],
                   [[[9.1419, 10.1419], [13.1419, 14.1419]]]]),
         rtol=1e-5,
     )
     np.testing.assert_allclose(
         item[1],
         np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]),
         rtol=1e-5,
     )
     if sys.platform != "win32":
         for item in DataLoader(ds,
                                batch_size=2,
                                shuffle=False,
                                num_workers=2):
             np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
         np.testing.assert_allclose(
             item[0],
             np.array([[[[2.3944, 3.3944], [6.3944, 7.3944]]],
                       [[[10.6551, 11.6551], [14.6551, 15.6551]]]]),
             rtol=1e-3,
         )
         np.testing.assert_allclose(
             item[1],
             np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]),
             rtol=1e-5,
         )
Exemplo n.º 5
0
 def test_loading_array(self):
     set_determinism(seed=1234)
     # test sequence input data with images
     images = [
         np.arange(16, dtype=float).reshape(1, 4, 4),
         np.arange(16, dtype=float).reshape(1, 4, 4)
     ]
     # image level
     patch_intensity = RandShiftIntensity(offsets=1.0, prob=1.0)
     patch_iter = PatchIter(patch_size=(2, 2), start_pos=(0, 0))
     ds = GridPatchDataset(data=images,
                           patch_iter=patch_iter,
                           transform=patch_intensity)
     # use the grid patch dataset
     for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=0):
         np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
     np.testing.assert_allclose(
         item[0],
         np.array([[[[1.4965, 2.4965], [5.4965, 6.4965]]],
                   [[[11.3584, 12.3584], [15.3584, 16.3584]]]]),
         rtol=1e-4,
     )
     np.testing.assert_allclose(item[1],
                                np.array([[[0, 1], [0, 2], [2, 4]],
                                          [[0, 1], [2, 4], [2, 4]]]),
                                rtol=1e-5)
     if sys.platform != "win32":
         for item in DataLoader(ds,
                                batch_size=2,
                                shuffle=False,
                                num_workers=2):
             np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2))
         np.testing.assert_allclose(
             item[0],
             np.array([[[[1.2548, 2.2548], [5.2548, 6.2548]]],
                       [[[9.1106, 10.1106], [13.1106, 14.1106]]]]),
             rtol=1e-3,
         )
         np.testing.assert_allclose(item[1],
                                    np.array([[[0, 1], [0, 2], [2, 4]],
                                              [[0, 1], [2, 4], [2, 4]]]),
                                    rtol=1e-5)