Return a patch generator with predefined properties such as `patch_size`. Typically used with :py:class:`monai.data.GridPatchDataset`.
| 40 | |
| 41 | |
| 42 | class PatchIter: |
| 43 | """ |
| 44 | Return a patch generator with predefined properties such as `patch_size`. |
| 45 | Typically used with :py:class:`monai.data.GridPatchDataset`. |
| 46 | |
| 47 | """ |
| 48 | |
| 49 | def __init__( |
| 50 | self, |
| 51 | patch_size: Sequence[int], |
| 52 | start_pos: Sequence[int] = (), |
| 53 | mode: str | None = NumpyPadMode.WRAP, |
| 54 | **pad_opts: dict, |
| 55 | ): |
| 56 | """ |
| 57 | |
| 58 | Args: |
| 59 | patch_size: size of patches to generate slices for, 0/None selects whole dimension |
| 60 | start_pos: starting position in the array, default is 0 for each dimension |
| 61 | mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, |
| 62 | ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``} |
| 63 | (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}. |
| 64 | One of the listed string values or a user supplied function. |
| 65 | If None, no wrapping is performed. Defaults to ``"wrap"``. |
| 66 | See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html |
| 67 | https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html |
| 68 | requires pytorch >= 1.10 for best compatibility. |
| 69 | pad_opts: other arguments for the `np.pad` function. |
| 70 | note that `np.pad` treats channel dimension as the first dimension. |
| 71 | |
| 72 | Note: |
| 73 | The `patch_size` is the size of the |
| 74 | patch to sample from the input arrays. It is assumed the arrays first dimension is the channel dimension which |
| 75 | will be yielded in its entirety so this should not be specified in `patch_size`. For example, for an input 3D |
| 76 | array with 1 channel of size (1, 20, 20, 20) a regular grid sampling of eight patches (1, 10, 10, 10) would be |
| 77 | specified by a `patch_size` of (10, 10, 10). |
| 78 | |
| 79 | """ |
| 80 | self.patch_size = (None,) + tuple(patch_size) # expand to have the channel dim |
| 81 | self.start_pos = ensure_tuple(start_pos) |
| 82 | self.mode = mode |
| 83 | self.pad_opts = pad_opts |
| 84 | |
| 85 | def __call__(self, array: NdarrayTensor) -> Generator[tuple[NdarrayTensor, np.ndarray], None, None]: |
| 86 | """ |
| 87 | Args: |
| 88 | array: the image to generate patches from. |
| 89 | |
| 90 | """ |
| 91 | yield from iter_patch( |
| 92 | array, |
| 93 | patch_size=self.patch_size, # type: ignore |
| 94 | start_pos=self.start_pos, |
| 95 | overlap=0.0, |
| 96 | copy_back=False, |
| 97 | mode=self.mode, |
| 98 | **self.pad_opts, |
| 99 | ) |
no outgoing calls
searching dependent graphs…