Dataset#

Training with volumes

SubjectsDataset#

class torchio.data.SubjectsDataset(subjects: Sequence[Subject], transform: Callable | None = None, load_getitem: bool = True)[source]#

Bases: Dataset

Base TorchIO dataset.

Reader of 3D medical images that directly inherits from the PyTorch Dataset. It can be used with a PyTorch DataLoader for efficient loading and augmentation. It receives a list of instances of Subject and an optional transform applied to the volumes after loading.

Parameters:
  • subjects – List of instances of Subject.

  • transform – An instance of Transform that will be applied to each subject.

  • load_getitem – Load all subject images before returning it in __getitem__(). Set it to False if some of the images will not be needed during training.

Example

>>> import torchio as tio
>>> subject_a = tio.Subject(
...     t1=tio.ScalarImage('t1.nrrd',),
...     t2=tio.ScalarImage('t2.mha',),
...     label=tio.LabelMap('t1_seg.nii.gz'),
...     age=31,
...     name='Fernando Perez',
... )
>>> subject_b = tio.Subject(
...     t1=tio.ScalarImage('colin27_t1_tal_lin.minc',),
...     t2=tio.ScalarImage('colin27_t2_tal_lin_dicom',),
...     label=tio.LabelMap('colin27_seg1.nii.gz'),
...     age=56,
...     name='Colin Holmes',
... )
>>> subjects_list = [subject_a, subject_b]
>>> transforms = [
...     tio.RescaleIntensity(out_min_max=(0, 1)),
...     tio.RandomAffine(),
... ]
>>> transform = tio.Compose(transforms)
>>> subjects_dataset = tio.SubjectsDataset(subjects_list, transform=transform)
>>> subject = subjects_dataset[0]

Tip

To quickly iterate over the subjects without loading the images, use dry_iter().

dry_iter()[source]#

Return the internal list of subjects.

This can be used to iterate over the subjects without loading the data and applying any transforms:

>>> names = [subject.name for subject in dataset.dry_iter()]
classmethod from_batch(batch: dict) SubjectsDataset[source]#

Instantiate a dataset from a batch generated by a data loader.

Parameters:

batch – Dictionary generated by a data loader, containing data that can be converted to instances of Subject.

set_transform(transform: Callable | None) None[source]#

Set the transform attribute.

Parameters:

transform – Callable object, typically an subclass of torchio.transforms.Transform.