clinicadl.data.dataloader.collate.MergeBatchesCollate¶
- class clinicadl.data.dataloader.collate.MergeBatchesCollate(ignore: Sequence[str] | None = None)[source]¶
To merge several batches into a single batch.
This collating mode is typically to get a single batch from the outputs of a
Datasetreturning a sequence of samples.MergeBatchesCollatewill try to merge this sequence of samples by merging each field of theSample, except those inignore.- More precisely:
torchio.Imageswill be concatenated along the channel dimension;numpy.ndarraysandtorch.Tensorswill be stacked along a new dimension;otherwise, the values will be merged in a tuple. If this tuple contains only one unique element (i.e. the value is the same in all the samples), a single value will be returned.
- Parameters:
ignore (Optional[Sequence[str]], default=None) –
To ignore some fields in the samples to merge. Thus, the output sample will not have these fields.
Important
The mandatory arguments of
Samplecannot be ignored.
Examples
from clinicadl.data.dataloader import MergeBatchesCollate from clinicadl.data.structures.examples import Colin27Sample import numpy as np sample = Colin27Sample(participant_id="sub-001", label=np.array([0, 1]), age=55, sex="M") sample_bis = Colin27Sample(participant_id="sub-001", label=np.array([1, 2]), age=56, to_ignore="abc") batch = MergeBatchesCollate(ignore=["to_ignore"])([(sample, sample_bis)])
>>> batch [Colin27Sample(Keys: ('head', 'sex', 'age', 'label', 'file_type', 'image_path', 'sample_type', 'sample_position', 'image', 'participant_id', 'session_id'); images: 2)] >>> batch[0].participant_id # same value in the two samples 'sub-001' >>> batch[0].age (55, 56) >>> batch[0].sex # only in one sample, so kept as it is 'M' >>> batch[0].image.shape # 2 channels now! (2, 181, 217, 181) >>> batch[0].label # arrays are stacked array([[0, 1], [1, 2]])
See also
ToBatchesCollateTo return a sequence of batches.
- __call__(samples: Sequence[Sequence[T]]) Batch[T][source]¶
Merges a sequence of sequences of
Samplein a singleBatch.- Parameters:
samples (Sequence[Sequence[T]]) – A sequence of sequences of
Sample, e.g. a sequence of outputs of aPairedDataset.- Returns:
Batch[T] – A
Batch, whose samples are the results of the merger of the inner input sequences.