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

Class GridPatchDataset

monai/data/grid_dataset.py:160–363  ·  view source on GitHub ↗

Yields patches from data read from an image dataset. Typically used with `PatchIter` or `PatchIterd` so that the patches are chosen in a contiguous grid sampling scheme. .. code-block:: python import numpy as np from monai.data import GridPatchDataset, DataLoader, Pa

Source from the content-addressed store, hash-verified

158
159
160class GridPatchDataset(IterableDataset):
161 """
162 Yields patches from data read from an image dataset.
163 Typically used with `PatchIter` or `PatchIterd` so that the patches are chosen in a contiguous grid sampling scheme.
164
165 .. code-block:: python
166
167 import numpy as np
168
169 from monai.data import GridPatchDataset, DataLoader, PatchIter, RandShiftIntensity
170
171 # image-level dataset
172 images = [np.arange(16, dtype=float).reshape(1, 4, 4),
173 np.arange(16, dtype=float).reshape(1, 4, 4)]
174 # image-level patch generator, "grid sampling"
175 patch_iter = PatchIter(patch_size=(2, 2), start_pos=(0, 0))
176 # patch-level intensity shifts
177 patch_intensity = RandShiftIntensity(offsets=1.0, prob=1.0)
178
179 # construct the dataset
180 ds = GridPatchDataset(data=images,
181 patch_iter=patch_iter,
182 transform=patch_intensity)
183 # use the grid patch dataset
184 for item in DataLoader(ds, batch_size=2, num_workers=2):
185 print("patch size:", item[0].shape)
186 print("coordinates:", item[1])
187
188 # >>> patch size: torch.Size([2, 1, 2, 2])
189 # coordinates: tensor([[[0, 1], [0, 2], [0, 2]],
190 # [[0, 1], [2, 4], [0, 2]]])
191
192 Args:
193 data: the data source to read image data from.
194 patch_iter: converts an input image (item from dataset) into a iterable of image patches.
195 `patch_iter(dataset[idx])` must yield a tuple: (patches, coordinates).
196 see also: :py:class:`monai.data.PatchIter` or :py:class:`monai.data.PatchIterd`.
197 transform: a callable data transform operates on the patches.
198 with_coordinates: whether to yield the coordinates of each patch, default to `True`.
199 cache: whether to use cache mache mechanism, default to `False`.
200 see also: :py:class:`monai.data.CacheDataset`.
201 cache_num: number of items to be cached. Default is `sys.maxsize`.
202 will take the minimum of (cache_num, data_length x cache_rate, data_length).
203 cache_rate: percentage of cached data in total, default is 1.0 (cache all).
204 will take the minimum of (cache_num, data_length x cache_rate, data_length).
205 num_workers: the number of worker threads if computing cache in the initialization.
206 If num_workers is None then the number returned by os.cpu_count() is used.
207 If a value less than 1 is specified, 1 will be used instead.
208 progress: whether to display a progress bar.
209 copy_cache: whether to `deepcopy` the cache content before applying the random transforms,
210 default to `True`. if the random transforms don't modify the cached content
211 (for example, randomly crop from the cached image and deepcopy the crop region)
212 or if every cache item is only used once in a `multi-processing` environment,
213 may set `copy=False` for better performance.
214 as_contiguous: whether to convert the cached NumPy array or PyTorch tensor to be contiguous.
215 it may help improve the performance of following logic.
216 hash_func: a callable to compute hash from data items to be cached.
217 defaults to `monai.data.utils.pickle_hashing`.

Callers 4

test_shapeMethod · 0.90
test_loading_arrayMethod · 0.90
test_loading_dictMethod · 0.90
test_set_dataMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_shapeMethod · 0.72
test_loading_arrayMethod · 0.72
test_loading_dictMethod · 0.72
test_set_dataMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…