MIC-DKFZ / batchgenerators

A framework for data augmentation for 2D and 3D image classification and segmentation
Apache License 2.0
1.1k stars 221 forks source link

[Question] SpatialTransform_2 vs SpatialTransform? #65

Open neuronflow opened 4 years ago

neuronflow commented 4 years ago

Could you please elaborate a bit on the differences? When to choose which one? Does SpatialTransform exist to support legacy code while SpatialTransform_2 is the "up to date" version?

FabianIsensee commented 4 years ago

Hi, SpatialTransform_2 uses a different implementation of elastic deformation. You can use either of the SpatialTransform. Both will be supported in the future. I have yet to make up my mind which on the elastic deformations I prefer. SpatialTransform_2 is certainly easier to tune. Best, Fabian

neuronflow commented 4 years ago

Thanks for the quick response, I am trying to incorporate ST_2 in a MONAI pipeline for a U-net working on 256 cubes (no patches).

using the transform with these parameters:

_bg_st2 = SpatialTransform_2(patch_size=(256, 256, 256), random_crop=False, data_key="images", label_key="label", p_el_per_sample=1, p_scale_per_sample=1, p_rot_per_sample=1)

leads to following error message

Traceback (most recent call last):
  File "/home/florian/bratsunet/neuronflow/loading/brats_2020.py", line 80, in <module>
    check_data = monai.utils.misc.first(check_loader)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/utils/misc.py", line 41, in first
    for i in iterable:
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 363, in __next__
    data = self._next_data()
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 989, in _next_data
    return self._process_data(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1014, in _process_data
    data.reraise()
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/_utils.py", line 395, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/adaptors.py", line 175, in _inner
    ret = function(**dinputs)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/transforms/spatial_transforms.py", line 455, in __call__
    ret_val = augment_spatial_2(data, seg, patch_size=patch_size,
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/spatial_transformations.py", line 380, in augment_spatial_2
    coords = elastic_deform_coordinates_2(coords, sigmas, mag)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/utils.py", line 101, in elastic_deform_coordinates_2
    gaussian_filter((np.random.random(coordinates.shape[1:]) * 2 - 1), sigmas, mode="constant", cval=0))
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/filters.py", line 291, in gaussian_filter
    sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/_ni_support.py", line 62, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: sequence argument must have length equal to input rank

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/compose.py", line 229, in __call__
    input_ = apply_transform(_transform, input_)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/adaptors.py", line 175, in _inner
    ret = function(**dinputs)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/transforms/spatial_transforms.py", line 455, in __call__
    ret_val = augment_spatial_2(data, seg, patch_size=patch_size,
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/spatial_transformations.py", line 380, in augment_spatial_2
    coords = elastic_deform_coordinates_2(coords, sigmas, mag)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/utils.py", line 101, in elastic_deform_coordinates_2
    gaussian_filter((np.random.random(coordinates.shape[1:]) * 2 - 1), sigmas, mode="constant", cval=0))
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/filters.py", line 291, in gaussian_filter
    sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/_ni_support.py", line 62, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: applying transform <function adaptor.<locals>._inner at 0x7fd1db6640d0>.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/compose.py", line 229, in __call__
    input_ = apply_transform(_transform, input_)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/compose.py", line 229, in __call__
    input_ = apply_transform(_transform, input_)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/adaptors.py", line 175, in _inner
    ret = function(**dinputs)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/transforms/spatial_transforms.py", line 455, in __call__
    ret_val = augment_spatial_2(data, seg, patch_size=patch_size,
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/spatial_transformations.py", line 380, in augment_spatial_2
    coords = elastic_deform_coordinates_2(coords, sigmas, mag)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/utils.py", line 101, in elastic_deform_coordinates_2
    gaussian_filter((np.random.random(coordinates.shape[1:]) * 2 - 1), sigmas, mode="constant", cval=0))
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/filters.py", line 291, in gaussian_filter
    sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/_ni_support.py", line 62, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7fd1ec35ce20>.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/data/dataset.py", line 56, in __getitem__
    data = apply_transform(self.transform, data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/compose.py", line 229, in __call__
    input_ = apply_transform(_transform, input_)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/compose.py", line 229, in __call__
    input_ = apply_transform(_transform, input_)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 278, in apply_transform
    raise type(e)(f"applying transform {transform}.").with_traceback(e.__traceback__)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/utils.py", line 276, in apply_transform
    return transform(data)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/monai/transforms/adaptors.py", line 175, in _inner
    ret = function(**dinputs)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/transforms/spatial_transforms.py", line 455, in __call__
    ret_val = augment_spatial_2(data, seg, patch_size=patch_size,
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/spatial_transformations.py", line 380, in augment_spatial_2
    coords = elastic_deform_coordinates_2(coords, sigmas, mag)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/batchgenerators/augmentations/utils.py", line 101, in elastic_deform_coordinates_2
    gaussian_filter((np.random.random(coordinates.shape[1:]) * 2 - 1), sigmas, mode="constant", cval=0))
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/filters.py", line 291, in gaussian_filter
    sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
  File "/home/florian/miniconda3/envs/bun/lib/python3.8/site-packages/scipy/ndimage/_ni_support.py", line 62, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: applying transform <monai.transforms.compose.Compose object at 0x7fd1db61feb0>.
FabianIsensee commented 4 years ago

Hi, I know nothing about the MONAI pipelines and cannot help you with your issue. Have you tried running it separately or using the batchgenerator stuff? One thing you might look at is what the dimensionality of your data is. In batchgenerators, 'images' and 'label' need to be 5D tensors (B, C, X, Y, Z) for 3D images. Best, Fabian

neuronflow commented 4 years ago

Thanks for looking into this.

"Have you tried running it separately or using the batchgenerator stuff?" Not yet, but will try in the future.

"One thing you might look at is what the dimensionality of your data is. In batchgenerators, 'images' and 'label' need to be 5D tensors (B, C, X, Y, Z) for 3D images." That is the case for my data :)