Trace applied transforms

Sometimes we would like to see which transform was applied to a certain batch during training. This can be done in TorchIO using torchio.utils.history_collate() for the data loader. The transforms history can be saved during training to check what was applied.

  • ToCanonical, Gamma, Blur, Flip, RescaleIntensity, Sagittal, Coronal, Axial
  • ToCanonical, Blur, RescaleIntensity, Sagittal, Coronal, Axial
  • ToCanonical, Gamma, Flip, RescaleIntensity, Sagittal, Coronal, Axial
  • ToCanonical, Gamma, Blur, Flip, RescaleIntensity, Sagittal, Coronal, Axial

Out:

Downloading https://github.com/fepegar/torchio-data/raw/master/data/fernando/t1.nii.gz to /home/docs/.cache/torchio/fpg/t1.nii.gz

0it [00:00, ?it/s]
  0%|          | 0/10860389 [00:00<?, ?it/s]
 18%|#8        | 1966080/10860389 [00:00<00:00, 19657069.46it/s]
 51%|#####1    | 5586944/10860389 [00:00<00:00, 29366227.94it/s]Downloading https://github.com/fepegar/torchio-data/raw/master/data/fernando/t1_seg_gif.nii.gz to /home/docs/.cache/torchio/fpg/t1_seg_gif.nii.gz


0it [00:00, ?it/s]

  0%|          | 0/772405 [00:00<?, ?it/s]Downloading https://github.com/fepegar/torchio-data/raw/master/data/fernando/t1_to_mni.tfm to /home/docs/.cache/torchio/fpg/t1_to_mni.tfm



0it [00:00, ?it/s]


  0%|          | 0/329 [00:00<?, ?it/s]Downloading https://github.com/fepegar/torchio-data/raw/master/data/fernando/t1_to_mni_affine.h5 to /home/docs/.cache/torchio/fpg/t1_to_mni_affine.h5




0it [00:00, ?it/s]



  0%|          | 0/8392 [00:00<?, ?it/s]Applied transforms:
[ToCanonical(),
 Gamma(gamma={'t1': [0.8018916845321655]}),
 RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)]

Composed transform to reproduce history:
Compose([ToCanonical(), Gamma(gamma={'t1': [0.8018916845321655]}), RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)])

Composed transform to invert applied transforms when possible:
/home/docs/checkouts/readthedocs.org/user_builds/torchio/checkouts/latest/torchio/transforms/augmentation/composition.py:65: RuntimeWarning: Skipping ToCanonical as it is not invertible
  warnings.warn(message, RuntimeWarning)
/home/docs/checkouts/readthedocs.org/user_builds/torchio/checkouts/latest/torchio/transforms/augmentation/composition.py:65: RuntimeWarning: Skipping RescaleIntensity as it is not invertible
  warnings.warn(message, RuntimeWarning)
Compose([Gamma(gamma={'t1': [0.8018916845321655]}, invert=True)])

10862592it [00:06, 1709060.88it/s]

778240it [00:05, 139678.89it/s]

8192it [00:05, 1626.05it/s]

16384it [00:04, 3521.04it/s]

Transforms applied to subjects in batch:
[[ToCanonical(),
  Gamma(gamma={'t1': [1.125920057296753]}),
  Blur(std={'t1': [tensor([0.5645, 1.3632, 1.8304])]}),
  Flip(axes=(0,)),
  RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)],
 [ToCanonical(),
  Blur(std={'t1': [tensor([0.5397, 0.3014, 0.0634])]}),
  RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)],
 [ToCanonical(),
  Gamma(gamma={'t1': [0.8567072749137878]}),
  Flip(axes=(0,)),
  RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)],
 [ToCanonical(),
  Gamma(gamma={'t1': [0.7924771308898926]}),
  Blur(std={'t1': [tensor([1.4525, 1.4022, 0.4076])]}),
  Flip(axes=(0,)),
  RescaleIntensity(out_min_max=(-1, 1), percentiles=(0, 100), masking_method=None)]]

import pprint
import torch
import torchio as tio
import matplotlib.pyplot as plt

torch.manual_seed(0)

batch_size = 4
subject = tio.datasets.FPG()
subject.remove_image('seg')
subjects = 4 * [subject]

transform = tio.Compose((
    tio.ToCanonical(),
    tio.RandomGamma(p=0.75),
    tio.RandomBlur(p=0.5),
    tio.RandomFlip(),
    tio.RescaleIntensity(out_min_max=(-1, 1)),
))

dataset = tio.SubjectsDataset(subjects, transform=transform)

transformed = dataset[0]
print('Applied transforms:')  # noqa: T001
pprint.pprint(transformed.history)  # noqa: T003
print('\nComposed transform to reproduce history:')  # noqa: T001
print(transformed.get_composed_history())  # noqa: T001
print('\nComposed transform to invert applied transforms when possible:')  # noqa: T001, E501
print(transformed.get_inverse_transform(ignore_intensity=False))  # noqa: T001

loader = torch.utils.data.DataLoader(
    dataset,
    batch_size=batch_size,
    collate_fn=tio.utils.history_collate,
)

batch = tio.utils.get_first_item(loader)
print('\nTransforms applied to subjects in batch:')  # noqa: T001
pprint.pprint(batch[tio.HISTORY])  # noqa: T003

for i in range(batch_size):
    tensor = batch['t1'][tio.DATA][i]
    affine = batch['t1'][tio.AFFINE][i]
    image = tio.ScalarImage(tensor=tensor, affine=affine)
    image.plot(show=False)
    history = batch[tio.HISTORY][i]
    title = ', '.join(t.name for t in history)
    plt.suptitle(title)
    plt.tight_layout()

plt.show()

Total running time of the script: ( 0 minutes 8.440 seconds)

Gallery generated by Sphinx-Gallery