Dataset with cache mechanism that can load data and cache deterministic transforms' result during training. By caching the results of non-random preprocessing transforms, it accelerates the training data pipeline. If the requested data is not in the cache, all transforms will run norma
| 731 | |
| 732 | |
| 733 | class CacheDataset(Dataset): |
| 734 | """ |
| 735 | Dataset with cache mechanism that can load data and cache deterministic transforms' result during training. |
| 736 | |
| 737 | By caching the results of non-random preprocessing transforms, it accelerates the training data pipeline. |
| 738 | If the requested data is not in the cache, all transforms will run normally |
| 739 | (see also :py:class:`monai.data.dataset.Dataset`). |
| 740 | |
| 741 | Users can set the cache rate or number of items to cache. |
| 742 | It is recommended to experiment with different `cache_num` or `cache_rate` to identify the best training speed. |
| 743 | |
| 744 | The transforms which are supposed to be cached must implement the `monai.transforms.Transform` |
| 745 | interface and should not be `Randomizable`. This dataset will cache the outcomes before the first |
| 746 | `Randomizable` `Transform` within a `Compose` instance. |
| 747 | So to improve the caching efficiency, please always put as many as possible non-random transforms |
| 748 | before the randomized ones when composing the chain of transforms. |
| 749 | If passing slicing indices, will return a PyTorch Subset, for example: `data: Subset = dataset[1:4]`, |
| 750 | for more details, please check: https://pytorch.org/docs/stable/data.html#torch.utils.data.Subset |
| 751 | |
| 752 | For example, if the transform is a `Compose` of:: |
| 753 | |
| 754 | transforms = Compose([ |
| 755 | LoadImaged(), |
| 756 | EnsureChannelFirstd(), |
| 757 | Spacingd(), |
| 758 | Orientationd(), |
| 759 | ScaleIntensityRanged(), |
| 760 | RandCropByPosNegLabeld(), |
| 761 | ToTensord() |
| 762 | ]) |
| 763 | |
| 764 | when `transforms` is used in a multi-epoch training pipeline, before the first training epoch, |
| 765 | this dataset will cache the results up to ``ScaleIntensityRanged``, as |
| 766 | all non-random transforms `LoadImaged`, `EnsureChannelFirstd`, `Spacingd`, `Orientationd`, `ScaleIntensityRanged` |
| 767 | can be cached. During training, the dataset will load the cached results and run |
| 768 | ``RandCropByPosNegLabeld`` and ``ToTensord``, as ``RandCropByPosNegLabeld`` is a randomized transform |
| 769 | and the outcome not cached. |
| 770 | |
| 771 | During training call `set_data()` to update input data and recompute cache content, note that it requires |
| 772 | `persistent_workers=False` in the PyTorch DataLoader. |
| 773 | |
| 774 | Note: |
| 775 | `CacheDataset` executes non-random transforms and prepares cache content in the main process before |
| 776 | the first epoch, then all the subprocesses of DataLoader will read the same cache content in the main process |
| 777 | during training. it may take a long time to prepare cache content according to the size of expected cache data. |
| 778 | So to debug or verify the program before real training, users can set `cache_rate=0.0` or `cache_num=0` to |
| 779 | temporarily skip caching. |
| 780 | |
| 781 | Lazy Resampling: |
| 782 | If you make use of the lazy resampling feature of `monai.transforms.Compose`, please refer to |
| 783 | its documentation to familiarize yourself with the interaction between `CacheDataset` and |
| 784 | lazy resampling. |
| 785 | |
| 786 | """ |
| 787 | |
| 788 | def __init__( |
| 789 | self, |
| 790 | data: Sequence, |
no outgoing calls
searching dependent graphs…