MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / PersistentDataset

Class PersistentDataset

monai/data/dataset.py:162–438  ·  view source on GitHub ↗

Persistent storage of pre-computed values to efficiently manage larger than memory dictionary format data, it can operate transforms for specific fields. Results from the non-random transform components are computed when first used, and stored in the `cache_dir` for rapid retrieval on

Source from the content-addressed store, hash-verified

160
161
162class PersistentDataset(Dataset):
163 """
164 Persistent storage of pre-computed values to efficiently manage larger than memory dictionary format data,
165 it can operate transforms for specific fields. Results from the non-random transform components are computed
166 when first used, and stored in the `cache_dir` for rapid retrieval on subsequent uses.
167 If passing slicing indices, will return a PyTorch Subset, for example: `data: Subset = dataset[1:4]`,
168 for more details, please check: https://pytorch.org/docs/stable/data.html#torch.utils.data.Subset
169
170 The transforms which are supposed to be cached must implement the `monai.transforms.Transform`
171 interface and should not be `Randomizable`. This dataset will cache the outcomes before the first
172 `Randomizable` `Transform` within a `Compose` instance.
173
174 For example, typical input data can be a list of dictionaries::
175
176 [{ { {
177 'image': 'image1.nii.gz', 'image': 'image2.nii.gz', 'image': 'image3.nii.gz',
178 'label': 'label1.nii.gz', 'label': 'label2.nii.gz', 'label': 'label3.nii.gz',
179 'extra': 123 'extra': 456 'extra': 789
180 }, }, }]
181
182 For a composite transform like
183
184 .. code-block:: python
185
186 [ LoadImaged(keys=['image', 'label']),
187 Orientationd(keys=['image', 'label'], axcodes='RAS'),
188 ScaleIntensityRanged(keys=['image'], a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True),
189 RandCropByPosNegLabeld(keys=['image', 'label'], label_key='label', spatial_size=(96, 96, 96),
190 pos=1, neg=1, num_samples=4, image_key='image', image_threshold=0),
191 ToTensord(keys=['image', 'label'])]
192
193 Upon first use a filename based dataset will be processed by the transform for the
194 [LoadImaged, Orientationd, ScaleIntensityRanged] and the resulting tensor written to
195 the `cache_dir` before applying the remaining random dependant transforms
196 [RandCropByPosNegLabeld, ToTensord] elements for use in the analysis.
197
198 Subsequent uses of a dataset directly read pre-processed results from `cache_dir`
199 followed by applying the random dependant parts of transform processing.
200
201 During training call `set_data()` to update input data and recompute cache content.
202
203 Note:
204 The input data must be a list of file paths and will hash them as cache keys.
205
206 The filenames of the cached files also try to contain the hash of the transforms. In this
207 fashion, `PersistentDataset` should be robust to changes in transforms. This, however, is
208 not guaranteed, so caution should be used when modifying transforms to avoid unexpected
209 errors. If in doubt, it is advisable to clear the cache directory.
210
211 Cached data is expected to be tensors, primitives, or dictionaries keying to these values. Numpy arrays will
212 be converted to tensors, however any other object type returned by transforms will not be loadable since
213 `torch.load` will be used with `weights_only=True` to prevent loading of potentially malicious objects.
214 Legacy cache files may not be loadable and may need to be recomputed.
215
216 Lazy Resampling:
217 If you make use of the lazy resampling feature of `monai.transforms.Compose`, please refer to
218 its documentation to familiarize yourself with the interaction between `PersistentDataset` and
219 lazy resampling.

Callers 7

test_thread_safeMethod · 0.90
test_mp_datasetMethod · 0.90
test_mp_datasetMethod · 0.90
test_cacheMethod · 0.90
test_shapeMethod · 0.90

Calls

no outgoing calls

Tested by 7

test_thread_safeMethod · 0.72
test_mp_datasetMethod · 0.72
test_mp_datasetMethod · 0.72
test_cacheMethod · 0.72
test_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…