Esempio n. 1
0
    def test_shape(self, input_param, input_data, expected_shape, expected_last_item):
        for p in TEST_NDARRAYS:
            xform = RandSpatialCropSamples(**input_param)
            xform.set_random_state(1234)
            result = xform(p(input_data))

            np.testing.assert_equal(len(result), input_param["num_samples"])
            for item, expected in zip(result, expected_shape):
                self.assertTupleEqual(item.shape, expected)
            assert_allclose(result[-1], expected_last_item, type_test=False)
Esempio n. 2
0
    def test_shape(self, input_param, input_shape, expected_shape,
                   expected_last_item):
        input_data = np.arange(192).reshape(*input_shape)

        for p in TEST_NDARRAYS_ALL:
            xform = RandSpatialCropSamples(**input_param)
            xform.set_random_state(1234)
            result = xform(p(input_data))

            np.testing.assert_equal(len(result), input_param["num_samples"])
            for i, (item, expected) in enumerate(zip(result, expected_shape)):
                self.assertTupleEqual(item.shape, expected)
                self.assertEqual(item.meta["patch_index"], i)
            assert_allclose(result[-1], expected_last_item, type_test="tensor")
    def __init__(
        self,
        inputZ01: Sequence,
        inputZ02: Sequence,
        inputZ03: Sequence,
        inputZ04: Sequence,
        inputZ05: Sequence,
        inputZ06: Sequence,
        inputZ07: Sequence,
        targetC01: Sequence,
        targetC02: Sequence,
        targetC03: Sequence,
        roi_size: int,
        n_samples: int,
        inputZ01_transform: Optional[Callable] = None,
        inputZ02_transform: Optional[Callable] = None,
        inputZ03_transform: Optional[Callable] = None,
        inputZ04_transform: Optional[Callable] = None,
        inputZ05_transform: Optional[Callable] = None,
        inputZ06_transform: Optional[Callable] = None,
        inputZ07_transform: Optional[Callable] = None,
        targetC01_transform: Optional[Callable] = None,
        targetC02_transform: Optional[Callable] = None,
        targetC03_transform: Optional[Callable] = None,

    ) -> None:
        """
        Initializes the dataset with the filename lists. The transform `img_transform` is applied
        to the images and `seg_transform` to the segmentations.
        """

        items = [(inputZ01, inputZ01_transform), (inputZ02, inputZ02_transform), (inputZ03, inputZ03_transform),
                 (inputZ04, inputZ04_transform), (inputZ05, inputZ05_transform), (inputZ06, inputZ06_transform),
                 (inputZ07, inputZ07_transform),
                 (targetC01, targetC01_transform), (targetC02, targetC02_transform), (targetC03, targetC03_transform)]
        self.roi_size = roi_size
        self.n_samples = n_samples
        
        self.set_random_state(seed=get_seed())
        sampler = RandSpatialCropSamples(roi_size=(self.roi_size, self.roi_size), 
                                         num_samples=self.n_samples,
                                         random_center=True, 
                                         random_size=False)
        datasets = [PatchDataset(dataset=x[0],
                                 patch_func=sampler,
                                 samples_per_image=self.n_samples,
                                 transform=x[1]) for x in items if x[0] is not None]
        self.dataset = datasets[0] if len(datasets) == 1 else ZipDataset(datasets)
        
        self._seed = 0  # transform synchronization seed
    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)
Esempio n. 5
0
 def __init__(self,
              data: Sequence,
              samples_per_image: int,
              roi_size: int,
              data_reader: Callable,
              transform: Optional[Callable] = None) -> None:
     """
     Args:
         data: input data to load and transform to generate dataset for model.
         transform: a callable data transform on input data.
     """
     self.samples_per_image = samples_per_image
     self.roi_size = (10, roi_size, roi_size)
     self.data = data
     self.image_reader = LoadImage(data_reader, image_only=True)
     self.transform = transform
     self.sampler = RandSpatialCropSamples(
         roi_size=self.roi_size,
         num_samples=self.samples_per_image,
         random_center=True,
         random_size=False)
Esempio n. 6
0
 def test_shape(self, input_param, input_data, expected_shape):
     result = RandSpatialCropSamples(**input_param)(input_data)
     for item in result:
         self.assertTupleEqual(item.shape, expected_shape)