Provides an iterable over the given `dataset`. It inherits the PyTorch DataLoader and adds enhanced `collate_fn` and `worker_fn` by default. Although this class could be configured to be the same as `torch.utils.data.DataLoader`, its default configuration is recommended, mainl
| 24 | |
| 25 | |
| 26 | class DataLoader(_TorchDataLoader): |
| 27 | """ |
| 28 | Provides an iterable over the given `dataset`. It inherits the PyTorch |
| 29 | DataLoader and adds enhanced `collate_fn` and `worker_fn` by default. |
| 30 | |
| 31 | Although this class could be configured to be the same as |
| 32 | `torch.utils.data.DataLoader`, its default configuration is |
| 33 | recommended, mainly for the following extra features: |
| 34 | |
| 35 | - It handles MONAI randomizable objects with appropriate random state |
| 36 | managements for deterministic behaviour. |
| 37 | - It is aware of the patch-based transform (such as |
| 38 | :py:class:`monai.transforms.RandSpatialCropSamplesDict`) samples for |
| 39 | preprocessing with enhanced data collating behaviour. |
| 40 | See: :py:class:`monai.transforms.Compose`. |
| 41 | |
| 42 | For more details about :py:class:`torch.utils.data.DataLoader`, please see: |
| 43 | https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader. |
| 44 | |
| 45 | For example, to construct a randomized dataset and iterate with the data loader: |
| 46 | |
| 47 | .. code-block:: python |
| 48 | |
| 49 | import torch |
| 50 | |
| 51 | from monai.data import DataLoader |
| 52 | from monai.transforms import Randomizable |
| 53 | |
| 54 | |
| 55 | class RandomDataset(torch.utils.data.Dataset, Randomizable): |
| 56 | def __getitem__(self, index): |
| 57 | return self.R.randint(0, 1000, (1,)) |
| 58 | |
| 59 | def __len__(self): |
| 60 | return 16 |
| 61 | |
| 62 | |
| 63 | dataset = RandomDataset() |
| 64 | dataloader = DataLoader(dataset, batch_size=2, num_workers=4) |
| 65 | for epoch in range(2): |
| 66 | for i, batch in enumerate(dataloader): |
| 67 | print(epoch, i, batch.data.numpy().flatten().tolist()) |
| 68 | |
| 69 | Args: |
| 70 | dataset: dataset from which to load the data. |
| 71 | num_workers: how many subprocesses to use for data |
| 72 | loading. ``0`` means that the data will be loaded in the main process. |
| 73 | (default: ``0``) |
| 74 | collate_fn: default to :py:func:`monai.data.utils.list_data_collate`. |
| 75 | worker_init_fn: default to :py:func:`monai.data.utils.worker_init_fn`. |
| 76 | kwargs: other parameters for PyTorch DataLoader. |
| 77 | """ |
| 78 | |
| 79 | def __init__(self, dataset: Dataset, num_workers: int = 0, **kwargs) -> None: |
| 80 | if num_workers == 0: |
| 81 | # when num_workers > 0, random states are determined by worker_init_fn |
| 82 | # this is to make the behavior consistent when num_workers == 0 |
| 83 | # torch.int64 doesn't work well on some versions of windows |
no outgoing calls
searching dependent graphs…