Transforms¶
Generic Interfaces¶
Transform¶

class
monai.transforms.
Transform
[source]¶ An abstract class of a
Transform
. A transform is callable that processesdata
.It could be stateful and may modify
data
in place, the implementation should be aware of:thread safety when mutating its own states. When used from a multiprocess context, transform’s instance variables are readonly. threadunsafe transforms should inherit
monai.transforms.ThreadUnsafe
.data
content unused by this transform may still be used in the subsequent transforms in a composed transform.storing too much information in
data
may not scale.
See Also

abstract
__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
MapTransform¶

class
monai.transforms.
MapTransform
(keys, allow_missing_keys=False)[source]¶ A subclass of
monai.transforms.Transform
with an assumption that thedata
input ofself.__call__
is a MutableMapping such asdict
.The
keys
parameter will be used to get and set the actual data item to transform. That is, the callable of this transform should follow the pattern:def __call__(self, data): for key in self.keys: if key in data: # update output data with some_transform_function(data[key]). else: # raise exception unless allow_missing_keys==True. return data
 Raises
ValueError – When
keys
is an empty iterable.TypeError – When
keys
type is not inUnion[Hashable, Iterable[Hashable]]
.

abstract
__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.

key_iterator
(data, *extra_iterables)[source]¶ Iterate across keys and optionally extra iterables. If key is missing, exception is raised if allow_missing_keys==False (default). If allow_missing_keys==True, key is skipped.
 Parameters
data (
Dict
[Hashable
,Any
]) – data that the transform will be applied toextra_iterables (
Optional
[Iterable
]) – anything else to be iterated through
 Return type
Generator
Randomizable¶

class
monai.transforms.
Randomizable
[source]¶ An interface for handling random state locally, currently based on a class variable R, which is an instance of np.random.RandomState. This provides the flexibility of componentspecific determinism without affecting the global states. It is recommended to use this API with
monai.data.DataLoader
for deterministic behaviour of the preprocessing pipelines. This API is not threadsafe. Additionally, deepcopying instance of this class often causes insufficient randomness as the random states will be duplicated.
randomize
(data)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Randomizable
 Returns
a Randomizable instance.

RandomizableTransform¶

class
monai.transforms.
RandomizableTransform
(prob=1.0, do_transform=True)[source]¶ An interface for handling random state locally, currently based on a class variable R, which is an instance of np.random.RandomState. This class introduces a randomized flag _do_transform, is mainly for randomized data augmentation transforms. For example:
from monai.transforms import RandomizableTransform class RandShiftIntensity100(RandomizableTransform): def randomize(self): super().randomize(None) self._offset = self.R.uniform(low=0, high=100) def __call__(self, img): self.randomize() if not self._do_transform: return img return img + self._offset transform = RandShiftIntensity() transform.set_random_state(seed=0) print(transform(10))

randomize
(data)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None

Compose¶

class
monai.transforms.
Compose
(transforms=None, map_items=True)[source]¶ Compose
provides the ability to chain a series of callables together in a sequential manner. Each transform in the sequence must take a single argument and return a single value.Compose
can be used in two ways:With a series of transforms that accept and return a single ndarray / tensor / tensorlike parameter.
With a series of transforms that accept and return a dictionary that contains one or more parameters. Such transforms must have passthrough semantics; unused values in the dictionary must be copied to the return dictionary. It is required that the dictionary is copied between input and output of each transform.
If some transform takes a data item dictionary as input, and returns a sequence of data items in the transform chain, all following transforms will be applied to each item of this list if map_items is True (the default). If map_items is False, the returned sequence is passed whole to the next callable in the chain.
For example:
A Compose([transformA, transformB, transformC], map_items=True)(data_dict) could achieve the following patchbased transformation on the data_dict input:
transformA normalizes the intensity of ‘img’ field in the data_dict.
transformB crops out image patches from the ‘img’ and ‘seg’ of data_dict, and return a list of three patch samples:
{'img': 3x100x100 data, 'seg': 1x100x100 data, 'shape': (100, 100)} applying transformB > [{'img': 3x20x20 data, 'seg': 1x20x20 data, 'shape': (20, 20)}, {'img': 3x20x20 data, 'seg': 1x20x20 data, 'shape': (20, 20)}, {'img': 3x20x20 data, 'seg': 1x20x20 data, 'shape': (20, 20)},]
transformC then randomly rotates or flips ‘img’ and ‘seg’ of each dictionary item in the list returned by transformB.
The composed transforms will be set the same global random seed if user called set_determinism().
When using the passthrough dictionary operation, you can make use of
monai.transforms.adaptors.adaptor
to wrap transforms that don’t conform to the requirements. This approach allows you to use transforms from otherwise incompatible libraries with minimal additional work.Note
In many cases, Compose is not the best way to create preprocessing pipelines. Preprocessing is often not a strictly sequential series of operations, and much of the complexity arises when a notsequential set of functions must be called as if it were a sequence.
Example: images and labels Images typically require some kind of normalization that labels do not. Both are then typically augmented through the use of random rotations, flips, and deformations. Compose can be used with a series of transforms that take a dictionary that contains ‘image’ and ‘label’ entries. This might require wrapping torchvision transforms before passing them to compose. Alternatively, one can create a class with a __call__ function that calls your preprocessing functions taking into account that not all of them are called on the labels.

flatten
()[source]¶ Return a Composition with a simple list of transforms, as opposed to any nested Compositions.
e.g., t1 = Compose([x, x, x, x, Compose([Compose([x, x]), x, x])]).flatten() will result in the equivalent of t1 = Compose([x, x, x, x, x, x, x, x]).

inverse
(data)[source]¶ Inverse of
__call__
. Raises
NotImplementedError – When the subclass does not override this method.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Compose
 Returns
a Randomizable instance.
InvertibleTransform¶

class
monai.transforms.
InvertibleTransform
[source]¶ Classes for invertible transforms.
This class exists so that an
invert
method can be implemented. This allows, for example, images to be cropped, rotated, padded, etc., during training and inference, and after be returned to their original size before saving to file for comparison in an external viewer.When the
__call__
method is called, the transformation information for each key is stored. If the transforms were applied to keys “image” and “label”, there will be two extra keys in the dictionary: “image_transforms” and “label_transforms”. Each list contains a list of the transforms applied to that key. When theinverse
method is called, the inverse is called on each key individually, which allows for different parameters being passed to each label (e.g., different interpolation for image and label).When the
inverse
method is called, the inverse transforms are applied in a last infirstout order. As the inverse is applied, its entry is removed from the list detailing the applied transformations. That is to say that during the forward pass, the list of applied transforms grows, and then during the inverse it shrinks back down to an empty list.The information in
data[key_transform]
will be compatible with the default collate since it only stores strings, numbers and arrays.We currently check that the
id()
of the transform is the same in the forward and inverse directions. This is a useful check to ensure that the inverses are being processed in the correct order. However, this may cause issues if theid()
of the object changes (such as multiprocessing on Windows). If you feel this issue affects you, please raise a GitHub issue.Note to developers: When converting a transform to an invertible transform, you need to:
Inherit from this class.
In
__call__
, add a call topush_transform
.Any extra information that might be needed for the inverse can be included with the dictionary
extra_info
. This dictionary should have the same keys regardless of whetherdo_transform
was True or False and can only contain objects that are accepted in pytorch data loader’s collate function (e.g., None is not allowed).Implement an
inverse
method. Make sure that after performing the inverse,pop_transform
is called.
BatchInverseTransform¶

class
monai.transforms.
BatchInverseTransform
(transform, loader, collate_fn=<function no_collation>, num_workers=0, detach=True)[source]¶ Perform inverse on a batch of data. This is useful if you have inferred a batch of images and want to invert them all.
 Parameters
transform (
InvertibleTransform
) – a callable data transform on input data.loader (
DataLoader
) – data loader used to run transforms and generate the batch of data.collate_fn (
Optional
[Callable
]) – how to collate data after inverse transformations. default won’t do any collation, so the output will be a list of size batch size.num_workers (
Optional
[int
]) – number of workers when run data loader for inverse transforms, default to 0 as only run 1 iteration and multiprocessing may be even slower. if the transforms are really slow, set num_workers for multiprocessing. if set to None, use the num_workers of the transform data loader.detach (
bool
) – whether to detach the tensors. Scalars tensors will be detached into number types instead of torch tensors.
Vanilla Transforms¶
Crop and Pad¶
SpatialPad¶

class
monai.transforms.
SpatialPad
(spatial_size, method=<Method.SYMMETRIC: 'symmetric'>, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Performs padding to the data, symmetric for all sides or all on one side for each dimension. Uses np.pad so in practice, a mode needs to be provided. See numpy.lib.arraypad.pad for additional details.
 Parameters
c – the spatial size of output data after padding, if a dimension of the input data size is bigger than the pad size, will not pad that dimension. If its components have nonpositive values, the corresponding size of input image will be used (no padding). for example: if the spatial size of input data is [30, 30, 30] and spatial_size=[32, 25, 1], the spatial size of output data will be [32, 30, 30].
method (
Union
[Method
,str
]) – {"symmetric"
,"end"
} Pad image symmetric on every side or only pad at the end sides. Defaults to"symmetric"
.mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Return type
ndarray
BorderPad¶

class
monai.transforms.
BorderPad
(spatial_border, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data by adding specified borders to every dimension.
 Parameters
spatial_border (
Union
[Sequence
[int
],int
]) –specified size for every spatial border. Any ve values will be set to 0. It can be 3 shapes:
single int number, pad all the borders with the same size.
length equals the length of image shape, pad every spatial dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1], pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6].
length equals 2 x (length of image shape), pad every border of every dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1, pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4. the result shape is [1, 7, 11].
mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Raises
ValueError – When
self.spatial_border
does not contain ints.ValueError – When
self.spatial_border
length is not one of [1, len(spatial_shape), 2*len(spatial_shape)].
DivisiblePad¶

class
monai.transforms.
DivisiblePad
(k, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Pad the input data, so that the spatial sizes are divisible by k.
 Parameters
k (
Union
[Sequence
[int
],int
]) – the target k for each spatial dimension. if k is negative or 0, the original size is preserved. if k is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
See also
monai.transforms.SpatialPad

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to be transformed, assuming img is channelfirst and padding doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults toself.mode
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Return type
ndarray
SpatialCrop¶

class
monai.transforms.
SpatialCrop
(roi_center=None, roi_size=None, roi_start=None, roi_end=None, roi_slices=None)[source]¶ General purpose cropper to produce subvolume region of interest (ROI). If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape. It can support to crop ND spatial (channelfirst) data.
 The cropped region can be parameterised in various ways:
a list of slices for each spatial dimension (allows for use of ve indexing and None)
a spatial center and size
the start and end coordinates of the ROI
 Parameters
roi_center (
Union
[Sequence
[int
],ndarray
,None
]) – voxel coordinates for center of the crop ROI.roi_size (
Union
[Sequence
[int
],ndarray
,None
]) – size of the crop ROI, if a dimension of ROI size is bigger than image size, will not crop that dimension of the image.roi_start (
Union
[Sequence
[int
],ndarray
,None
]) – voxel coordinates for start of the crop ROI.roi_end (
Union
[Sequence
[int
],ndarray
,None
]) – voxel coordinates for end of the crop ROI, if a coordinate is out of image, use the end coordinate of image.roi_slices (
Optional
[Sequence
[slice
]]) – list of slices for each of the spatial dimensions.
CenterSpatialCrop¶

class
monai.transforms.
CenterSpatialCrop
(roi_size)[source]¶ Crop at the center of image with specified ROI size. If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224,224,128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].
RandSpatialCrop¶

class
monai.transforms.
RandSpatialCrop
(roi_size, max_roi_size=None, random_center=True, random_size=True)[source]¶ Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI.
Note: even random_size=False, if a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].max_roi_size (
Union
[int
,Sequence
[int
],None
]) – if random_size is True and roi_size specifies the min crop region size, max_roi_size can specify the max crop region size. if None, defaults to the input image size. if its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. if True, the actual size is sampled from randint(roi_size, max_roi_size + 1).

__call__
(img)[source]¶ Apply the transform to img, assuming img is channelfirst and slicing doesn’t apply to the channel dim.

randomize
(img_size)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandSpatialCropSamples¶

class
monai.transforms.
RandSpatialCropSamples
(roi_size, num_samples, max_roi_size=None, random_center=True, random_size=True)[source]¶ Crop image with random size or specific size ROI to generate a list of N samples. It can crop at a random position as center or at the image center. And allows to set the minimum size to limit the randomly generated ROI. It will return a list of cropped images.
Note: even random_size=False, if a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape.
 Parameters
roi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].num_samples (
int
) – number of samples (crop regions) to take in the returned list.max_roi_size (
Union
[int
,Sequence
[int
],None
]) – if random_size is True and roi_size specifies the min crop region size, max_roi_size can specify the max crop region size. if None, defaults to the input image size. if its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).
 Raises
ValueError – When
num_samples
is nonpositive.

__call__
(img)[source]¶ Apply the transform to img, assuming img is channelfirst and cropping doesn’t change the channel dim.
 Return type
List
[ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Randomizable
 Returns
a Randomizable instance.
CropForeground¶

class
monai.transforms.
CropForeground
(select_fn=<function is_positive>, channel_indices=None, margin=0, return_coords=False, k_divisible=1, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Crop an image using a bounding box. The bounding box is generated by selecting foreground using select_fn at channels channel_indices. margin is added in each spatial dimension of the bounding box. The typical usage is to help training and evaluation if the valid part is small in the whole medical image. Users can define arbitrary function to select expected foreground from the whole image or specified channels. And it can also add margin to every dim of the bounding box of foreground object. For example:
image = np.array( [[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [0, 1, 3, 2, 0], [0, 1, 2, 1, 0], [0, 0, 0, 0, 0]]]) # 1x5x5, single channel 5x5 image def threshold_at_one(x): # threshold at 1 return x > 1 cropper = CropForeground(select_fn=threshold_at_one, margin=0) print(cropper(image)) [[[2, 1], [3, 2], [2, 1]]]
 Parameters
select_fn (
Callable
) – function to select expected foreground, default is to select values > 0.channel_indices (
Union
[Iterable
[int
],int
,None
]) – if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image.margin (
Union
[Sequence
[int
],int
]) – add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.return_coords (
bool
) – whether return the coordinates of spatial bounding box for foreground.k_divisible (
Union
[Sequence
[int
],int
]) – make each spatial dimension to be divisible by k, default to 1. if k_divisible is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[NumpyPadMode
,str
]) – padding mode {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} one of the listed string values or a user supplied function. Defaults to"constant"
. see also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img)[source]¶ Apply the transform to img, assuming img is channelfirst and slicing doesn’t change the channel dim.
RandWeightedCrop¶

class
monai.transforms.
RandWeightedCrop
(spatial_size, num_samples=1, weight_map=None)[source]¶ Samples a list of num_samples image patches according to the provided weight_map.
 Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the image patch e.g. [224, 224, 128]. If its components have nonpositive values, the corresponding size of img will be used.num_samples (
int
) – number of samples (image patches) to take in the returned list.weight_map (
Optional
[ndarray
]) – weight map used to generate patch samples. The weights must be nonnegative. Each element denotes a sampling weight of the spatial location. 0 indicates no sampling. It should be a singlechannel array in shape, for example, (1, spatial_dim_0, spatial_dim_1, …).

__call__
(img, weight_map=None)[source]¶  Parameters
img (
ndarray
) – input image to sample patches from. assuming img is a channelfirst array.weight_map (
Optional
[ndarray
]) – weight map used to generate patch samples. The weights must be nonnegative. Each element denotes a sampling weight of the spatial location. 0 indicates no sampling. It should be a singlechannel array in shape, for example, (1, spatial_dim_0, spatial_dim_1, …)
 Return type
List
[ndarray
] Returns
A list of image patches

randomize
(weight_map)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandCropByPosNegLabel¶

class
monai.transforms.
RandCropByPosNegLabel
(spatial_size, label=None, pos=1.0, neg=1.0, num_samples=1, image=None, image_threshold=0.0, fg_indices=None, bg_indices=None)[source]¶ Crop random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. And will return a list of arrays for all the cropped images. For example, crop two (3 x 3) arrays from (5 x 5) array with pos/neg=1:
[[[0, 0, 0, 0, 0], [0, 1, 2, 1, 0], [[0, 1, 2], [[2, 1, 0], [0, 1, 3, 0, 0], > [0, 1, 3], [3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0]] [0, 0, 0]] [0, 0, 0, 0, 0]]]
If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than expected size, and the cropped results of several images may not have exactly same shape.
 Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224, 224, 128]. if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. if its components have nonpositive values, the corresponding size of label will be used. for example: if the spatial size of input data is [40, 40, 40] and spatial_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].label (
Optional
[ndarray
]) – the label image that is used for finding foreground/background, if None, must set at self.__call__. Nonzero indicates foreground, zero indicates background.pos (
float
) – used with neg together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.neg (
float
) – used with pos together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.num_samples (
int
) – number of samples (crop regions) to take in each list.image (
Optional
[ndarray
]) – optional image data to help select valid area, can be same as img or another image array. if not None, uselabel == 0 & image > image_threshold
to select the negative sample (background) center. So the crop center will only come from the valid image areas.image_threshold (
float
) – if enabled image, useimage > image_threshold
to determine the valid image content areas.fg_indices (
Optional
[ndarray
]) – if provided precomputed foreground indices of label, will ignore above image and image_threshold, and randomly select crop centers based on them, need to provide fg_indices and bg_indices together, expect to be 1 dim array of spatial indices after flattening. a typical usage is to call FgBgToIndices transform first and cache the results.bg_indices (
Optional
[ndarray
]) – if provided precomputed background indices of label, will ignore above image and image_threshold, and randomly select crop centers based on them, need to provide fg_indices and bg_indices together, expect to be 1 dim array of spatial indices after flattening. a typical usage is to call FgBgToIndices transform first and cache the results.
 Raises
ValueError – When
pos
orneg
are negative.ValueError – When
pos=0
andneg=0
. Incompatible values.

__call__
(img, label=None, image=None, fg_indices=None, bg_indices=None)[source]¶  Parameters
img (
ndarray
) – input data to crop samples from based on the pos/neg ratio of label and image. Assumes img is a channelfirst array.label (
Optional
[ndarray
]) – the label image that is used for finding foreground/background, if None, use self.label.image (
Optional
[ndarray
]) – optional image data to help select valid area, can be same as img or another image array. uselabel == 0 & image > image_threshold
to select the negative sample(background) center. so the crop center will only exist on valid image area. if None, use self.image.fg_indices (
Optional
[ndarray
]) – foreground indices to randomly select crop centers, need to provide fg_indices and bg_indices together.bg_indices (
Optional
[ndarray
]) – background indices to randomly select crop centers, need to provide fg_indices and bg_indices together.
 Return type
List
[ndarray
]

randomize
(label, fg_indices=None, bg_indices=None, image=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ResizeWithPadOrCrop¶

class
monai.transforms.
ResizeWithPadOrCrop
(spatial_size, mode=<NumpyPadMode.CONSTANT: 'constant'>)[source]¶ Resize an image to a target spatial size by either centrally cropping the image or padding it evenly with a userspecified mode. When the dimension is smaller than the target size, do symmetric padding along that dim. When the dimension is larger than the target size, do central cropping along that dim.
 Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of output data after padding or crop. If has nonpositive values, the corresponding size of input image will be used (no padding).mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function for padding. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html

__call__
(img, mode=None)[source]¶  Parameters
img (
ndarray
) – data to pad or crop, assuming img is channelfirst and padding or cropping doesn’t apply to the channel dim.mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function for padding. If None, defaults to themode
in construction. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
 Return type
ndarray
BoundingRect¶

class
monai.transforms.
BoundingRect
(select_fn=<function is_positive>)[source]¶ Compute coordinates of axisaligned bounding rectangles from input image img. The output format of the coordinates is (shape is [channel, 2 * spatial dims]):
 [[1st_spatial_dim_start, 1st_spatial_dim_end,
2nd_spatial_dim_start, 2nd_spatial_dim_end, …, Nth_spatial_dim_start, Nth_spatial_dim_end],
…
[1st_spatial_dim_start, 1st_spatial_dim_end, 2nd_spatial_dim_start, 2nd_spatial_dim_end, …, Nth_spatial_dim_start, Nth_spatial_dim_end]]
The bounding boxes edges are aligned with the input image edges. This function returns [1, 1, …] if there’s no positive intensity.
 Parameters
select_fn (
Callable
) – function to select expected foreground, default is to select values > 0.

__call__
(img)[source]¶ See also:
monai.transforms.utils.generate_spatial_bounding_box
. Return type
ndarray
RandScaleCrop¶

class
monai.transforms.
RandScaleCrop
(roi_scale, max_roi_scale=None, random_center=True, random_size=True)[source]¶ Subclass of
monai.transforms.RandSpatialCrop
. Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum and maximum scale of image size to limit the randomly generated ROI. Parameters
roi_scale (
Union
[Sequence
[float
],float
]) – if random_size is True, it specifies the minimum crop size: roi_scale * image spatial size. if random_size is False, it specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5]. If its components have nonpositive values, will use 1.0 instead, which means the input image size.max_roi_scale (
Union
[Sequence
[float
],float
,None
]) – if random_size is True and roi_scale specifies the min crop region size, max_roi_scale can specify the max crop region size: max_roi_scale * image spatial size. if None, defaults to the input image size. if its components have nonpositive values, will use 1.0 instead, which means the input image size.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specified size ROI by roi_scale * image spatial size. if True, the actual size is sampled from randint(roi_scale * image spatial size, max_roi_scale * image spatial size + 1).
CenterScaleCrop¶

class
monai.transforms.
CenterScaleCrop
(roi_scale)[source]¶ Crop at the center of image with specified scale of ROI size.
 Parameters
roi_scale (
Union
[Sequence
[float
],float
]) – specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5] or a number for all dims. If its components have nonpositive values, will use 1.0 instead, which means the input image size.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
Intensity¶
RandGaussianNoise¶

class
monai.transforms.
RandGaussianNoise
(prob=0.1, mean=0.0, std=0.1)[source]¶ Add Gaussian noise to image.
 Parameters
prob (
float
) – Probability to add Gaussian noise.mean (
Union
[Sequence
[float
],float
]) – Mean or “centre” of the distribution.std (
float
) – Standard deviation (spread) of distribution.

randomize
(im_shape)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
ShiftIntensity¶
RandShiftIntensity¶

class
monai.transforms.
RandShiftIntensity
(offsets, prob=0.1)[source]¶ Randomly shift intensity with randomly picked offset.
 Parameters
offsets (
Union
[Tuple
[float
,float
],float
]) – offset range to randomly shift. if single number, offset value is picked from (offsets, offsets).prob (
float
) – probability of shift.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
StdShiftIntensity¶

class
monai.transforms.
StdShiftIntensity
(factor, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>)[source]¶ Shift intensity for the image with a factor and the standard deviation of the image by:
v = v + factor * std(v)
. This transform can focus on only nonzero values or the entire image, and can also calculate the std on each channel separately. Parameters
factor (
float
) – factor shift byv = v + factor * std(v)
.nonzero (
bool
) – whether only count nonzero values.channel_wise (
bool
) – if True, calculate on each channel separately. Please ensure that the first dimension represents the channel of the image if True.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.
RandStdShiftIntensity¶

class
monai.transforms.
RandStdShiftIntensity
(factors, prob=0.1, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>)[source]¶ Shift intensity for the image with a factor and the standard deviation of the image by:
v = v + factor * std(v)
where the factor is randomly picked. Parameters
factors (
Union
[Tuple
[float
,float
],float
]) – if tuple, the randomly picked range is (min(factors), max(factors)). If single number, the range is (factors, factors).prob (
float
) – probability of std shift.nonzero (
bool
) – whether only count nonzero values.channel_wise (
bool
) – if True, calculate on each channel separately.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandBiasField¶

class
monai.transforms.
RandBiasField
(degree=3, coeff_range=(0.0, 0.1), dtype=<class 'numpy.float32'>, prob=1.0)[source]¶ Random bias field augmentation for MR images. The bias field is considered as a linear combination of smoothly varying basis (polynomial) functions, as described in Automated ModelBased Tissue Classification of MR Images of the Brain. This implementation adapted from NiftyNet. Referred to Longitudinal segmentation of agerelated white matter hyperintensities.
 Parameters
degree (
int
) – degree of freedom of the polynomials. The value should be no less than 1. Defaults to 3.coeff_range (
Tuple
[float
,float
]) – range of the random coefficients. Defaults to (0.0, 0.1).dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.prob (
float
) – probability to do random bias field.

randomize
(data)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
ScaleIntensity¶

class
monai.transforms.
ScaleIntensity
(minv=0.0, maxv=1.0, factor=None)[source]¶ Scale the intensity of input image to the given value range (minv, maxv). If minv and maxv not provided, use factor to scale image by
v = v * (1 + factor)
. Parameters
minv (
Optional
[float
]) – minimum value of output data.maxv (
Optional
[float
]) – maximum value of output data.factor (
Optional
[float
]) – factor scale byv = v * (1 + factor)
. In order to use this parameter, please set minv and maxv into None.
RandScaleIntensity¶

class
monai.transforms.
RandScaleIntensity
(factors, prob=0.1)[source]¶ Randomly scale the intensity of input image by
v = v * (1 + factor)
where the factor is randomly picked. Parameters
factors (
Union
[Tuple
[float
,float
],float
]) – factor range to randomly scale byv = v * (1 + factor)
. if single number, factor value is picked from (factors, factors).prob (
float
) – probability of scale.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
NormalizeIntensity¶

class
monai.transforms.
NormalizeIntensity
(subtrahend=None, divisor=None, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>)[source]¶ Normalize input based on provided args, using calculated mean and std if not provided. This transform can normalize only nonzero values or entire image, and can also calculate mean and std on each channel separately. When channel_wise is True, the first dimension of subtrahend and divisor should be the number of image channels if they are not None.
 Parameters
subtrahend (
Union
[Sequence
,ndarray
,None
]) – the amount to subtract by (usually the mean).divisor (
Union
[Sequence
,ndarray
,None
]) – the amount to divide by (usually the standard deviation).nonzero (
bool
) – whether only normalize nonzero values.channel_wise (
bool
) – if using calculated mean and std, calculate on each channel separately or calculate on the entire image directly.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.
ThresholdIntensity¶

class
monai.transforms.
ThresholdIntensity
(threshold, above=True, cval=0.0)[source]¶ Filter the intensity values of whole image to below threshold or above threshold. And fill the remaining parts of the image to the cval value.
 Parameters
threshold (
float
) – the threshold to filter intensity values.above (
bool
) – filter values above the threshold or below the threshold, default is True.cval (
float
) – value to fill the remaining parts of the image, default is 0.
ScaleIntensityRange¶

class
monai.transforms.
ScaleIntensityRange
(a_min, a_max, b_min, b_max, clip=False)[source]¶ Apply specific intensity scaling to the whole numpy array. Scaling from [a_min, a_max] to [b_min, b_max] with clip option.
 Parameters
a_min (
float
) – intensity original range min.a_max (
float
) – intensity original range max.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.
ScaleIntensityRangePercentiles¶

class
monai.transforms.
ScaleIntensityRangePercentiles
(lower, upper, b_min, b_max, clip=False, relative=False)[source]¶ Apply range scaling to a numpy array based on the intensity distribution of the input.
By default this transform will scale from [lower_intensity_percentile, upper_intensity_percentile] to [b_min, b_max], where {lower,upper}_intensity_percentile are the intensity values at the corresponding percentiles of
img
.The
relative
parameter can also be set to scale from [lower_intensity_percentile, upper_intensity_percentile] to the lower and upper percentiles of the output range [b_min, b_max]For example:
image = np.array( [[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]) # Scale from lower and upper image intensity percentiles # to output range [b_min, b_max] scaler = ScaleIntensityRangePercentiles(10, 90, 0, 200, False, False) print(scaler(image)) [[[0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.], [0., 50., 100., 150., 200.]]] # Scale from lower and upper image intensity percentiles # to lower and upper percentiles of the output range [b_min, b_max] rel_scaler = ScaleIntensityRangePercentiles(10, 90, 0, 200, False, True) print(rel_scaler(image)) [[[20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.], [20., 60., 100., 140., 180.]]]
 Parameters
lower (
float
) – lower intensity percentile.upper (
float
) – upper intensity percentile.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.relative (
bool
) – whether to scale to the corresponding percentiles of [b_min, b_max].
AdjustContrast¶
RandAdjustContrast¶

class
monai.transforms.
RandAdjustContrast
(prob=0.1, gamma=(0.5, 4.5))[source]¶ Randomly changes image intensity by gamma. Each pixel/voxel intensity is updated as:
x = ((x  min) / intensity_range) ^ gamma * intensity_range + min
 Parameters
prob (
float
) – Probability of adjustment.gamma (
Union
[Sequence
[float
],float
]) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
MaskIntensity¶

class
monai.transforms.
MaskIntensity
(mask_data)[source]¶ Mask the intensity values of input image with the specified mask data. Mask data must have the same spatial size as the input image, and all the intensity values of input image corresponding to 0 in the mask data will be set to 0, others will keep the original value.
 Parameters
mask_data (
Optional
[ndarray
]) – if mask_data is single channel, apply to every channel of input image. if multiple channels, the number of channels must match the input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image.

__call__
(img, mask_data=None)[source]¶  Parameters
mask_data (
Optional
[ndarray
]) – if mask data is single channel, apply to every channel of input image. if multiple channels, the channel number must match input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image. Raises
 ValueError – When both
mask_data
andself.mask_data
are None. ValueError – When
mask_data
andimg
channels differ andmask_data
is not single channel.
 Return type
ndarray
SavitzkyGolaySmooth¶

class
monai.transforms.
SavitzkyGolaySmooth
(window_length, order, axis=1, mode='zeros')[source]¶ Smooth the input data along the given axis using a SavitzkyGolay filter.
 Parameters
window_length (
int
) – Length of the filter window, must be a positive odd integer.order (
int
) – Order of the polynomial to fit to each window, must be less thanwindow_length
.axis (
int
) – Optional axis along which to apply the filter kernel. Default 1 (first spatial dimension).mode (
str
) – Optional padding mode, passed to convolution class.'zeros'
,'reflect'
,'replicate'
or'circular'
. Default:'zeros'
. Seetorch.nn.Conv1d()
for more information.
GaussianSmooth¶

class
monai.transforms.
GaussianSmooth
(sigma=1.0, approx='erf')[source]¶ Apply Gaussian smooth to the input data based on specified sigma parameter. A default value sigma=1.0 is provided for reference.
 Parameters
sigma (
Union
[Sequence
[float
],float
]) – if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
RandGaussianSmooth¶

class
monai.transforms.
RandGaussianSmooth
(sigma_x=(0.25, 1.5), sigma_y=(0.25, 1.5), sigma_z=(0.25, 1.5), prob=0.1, approx='erf')[source]¶ Apply Gaussian smooth to the input data based on randomly selected sigma parameters.
 Parameters
sigma_x (
Tuple
[float
,float
]) – randomly select sigma value for the first spatial dimension.sigma_y (
Tuple
[float
,float
]) – randomly select sigma value for the second spatial dimension if have.sigma_z (
Tuple
[float
,float
]) – randomly select sigma value for the third spatial dimension if have.prob (
float
) – probability of Gaussian smooth.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
GaussianSharpen¶

class
monai.transforms.
GaussianSharpen
(sigma1=3.0, sigma2=1.0, alpha=30.0, approx='erf')[source]¶ Sharpen images using the Gaussian Blur filter. Referring to: http://scipylectures.org/advanced/image_processing/auto_examples/plot_sharpen.html. The algorithm is shown as below
blurred_f = gaussian_filter(img, sigma1) filter_blurred_f = gaussian_filter(blurred_f, sigma2) img = blurred_f + alpha * (blurred_f  filter_blurred_f)
A set of default values sigma1=3.0, sigma2=1.0 and alpha=30.0 is provide for reference.
 Parameters
sigma1 (
Union
[Sequence
[float
],float
]) – sigma parameter for the first gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.sigma2 (
Union
[Sequence
[float
],float
]) – sigma parameter for the second gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.alpha (
float
) – weight parameter to compute the final result.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
RandGaussianSharpen¶

class
monai.transforms.
RandGaussianSharpen
(sigma1_x=(0.5, 1.0), sigma1_y=(0.5, 1.0), sigma1_z=(0.5, 1.0), sigma2_x=0.5, sigma2_y=0.5, sigma2_z=0.5, alpha=(10.0, 30.0), approx='erf', prob=0.1)[source]¶ Sharpen images using the Gaussian Blur filter based on randomly selected sigma1, sigma2 and alpha. The algorithm is
monai.transforms.GaussianSharpen
. Parameters
sigma1_x (
Tuple
[float
,float
]) – randomly select sigma value for the first spatial dimension of first gaussian kernel.sigma1_y (
Tuple
[float
,float
]) – randomly select sigma value for the second spatial dimension(if have) of first gaussian kernel.sigma1_z (
Tuple
[float
,float
]) – randomly select sigma value for the third spatial dimension(if have) of first gaussian kernel.sigma2_x (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the first spatial dimension of second gaussian kernel. if only 1 value X provided, it must be smaller than sigma1_x and randomly select from [X, sigma1_x].sigma2_y (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the second spatial dimension(if have) of second gaussian kernel. if only 1 value Y provided, it must be smaller than sigma1_y and randomly select from [Y, sigma1_y].sigma2_z (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the third spatial dimension(if have) of second gaussian kernel. if only 1 value Z provided, it must be smaller than sigma1_z and randomly select from [Z, sigma1_z].alpha (
Tuple
[float
,float
]) – randomly select weight parameter to compute the final result.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.prob (
float
) – probability of Gaussian sharpen.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandHistogramShift¶

class
monai.transforms.
RandHistogramShift
(num_control_points=10, prob=0.1)[source]¶ Apply random nonlinear transform to the image’s intensity histogram.
 Parameters
num_control_points (
Union
[Tuple
[int
,int
],int
]) – number of control points governing the nonlinear intensity mapping. a smaller number of control points allows for larger intensity shifts. if two values provided, number of control points selecting from range (min_value, max_value).prob (
float
) – probability of histogram shift.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
ndarray

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
DetectEnvelope¶

class
monai.transforms.
DetectEnvelope
(axis=1, n=None)[source]¶ Find the envelope of the input data along the requested axis using a Hilbert transform. Requires PyTorch 1.7.0+ and the PyTorch FFT module (which is not included in NVIDIA PyTorch Release 20.10).
 Parameters
axis (
int
) – Axis along which to detect the envelope. Default 1, i.e. the first spatial dimension.N – FFT size. Default img.shape[axis]. Input will be zeropadded or truncated to this size along dimension
axis. –
GibbsNoise¶

class
monai.transforms.
GibbsNoise
(alpha=0.5, as_tensor_output=True)[source]¶ The transform applies Gibbs noise to 2D/3D MRI images. Gibbs artifacts are one of the common type of type artifacts appearing in MRI scans.
The transform is applied to all the channels in the data.
For general information on Gibbs artifacts, please refer to: https://pubs.rsna.org/doi/full/10.1148/rg.313105115 https://pubs.rsna.org/doi/full/10.1148/radiographics.22.4.g02jl14949
 Parameters
alpha (float) – Parametrizes the intensity of the Gibbs noise filter applied. Takes values in the interval [0,1] with alpha = 0 acting as the identity mapping.
as_tensor_output (
bool
) – if true return torch.Tensor, else return np.array. default: True.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Union
[Tensor
,ndarray
]
RandGibbsNoise¶

class
monai.transforms.
RandGibbsNoise
(prob=0.1, alpha=(0.0, 1.0), as_tensor_output=True)[source]¶ Naturalistic image augmentation via Gibbs artifacts. The transform randomly applies Gibbs noise to 2D/3D MRI images. Gibbs artifacts are one of the common type of type artifacts appearing in MRI scans.
The transform is applied to all the channels in the data.
For general information on Gibbs artifacts, please refer to: https://pubs.rsna.org/doi/full/10.1148/rg.313105115 https://pubs.rsna.org/doi/full/10.1148/radiographics.22.4.g02jl14949
 Parameters
prob (float) – probability of applying the transform.
alpha (float, Sequence(float)) – Parametrizes the intensity of the Gibbs noise filter applied. Takes values in the interval [0,1] with alpha = 0 acting as the identity mapping. If a length2 list is given as [a,b] then the value of alpha will be sampled uniformly from the interval [a,b]. 0 <= a <= b <= 1.
as_tensor_output (
bool
) – if true return torch.Tensor, else return np.array. default: True.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Union
[Tensor
,ndarray
]
KSpaceSpikeNoise¶

class
monai.transforms.
KSpaceSpikeNoise
(loc, k_intensity=None, as_tensor_output=True)[source]¶ Apply localized spikes in kspace at the given locations and intensities. Spike (Herringbone) artifact is a type of data acquisition artifact which may occur during MRI scans.
For general information on spike artifacts, please refer to:
AAPM/RSNA physics tutorial for residents: fundamental physics of MR imaging.
Body MRI artifacts in clinical practice: A physicist’s and radiologist’s perspective.
 Parameters
loc (
Union
[Tuple
,Sequence
[Tuple
]]) – spatial location for the spikes. For images with 3D spatial dimensions, the user can provide (C, X, Y, Z) to fix which channel C is affected, or (X, Y, Z) to place the same spike in all channels. For 2D cases, the user can provide (C, X, Y) or (X, Y).k_intensity (
Union
[Sequence
[float
],float
,None
]) – value for the logintensity of the kspace version of the image. If one location is passed toloc
or the channel is not specified, then this argument should receive a float. Ifloc
is given a sequence of locations, then this argument should receive a sequence of intensities. This value should be tested as it is datadependent. The default values are the 2.5 the mean of the logintensity for each channel.as_tensor_output (
bool
) – ifTrue
return torch.Tensor, else return np.array. Default:True
.
Example
When working with 4D data,
KSpaceSpikeNoise(loc = ((3,60,64,32), (64,60,32)), k_intensity = (13,14))
will place a spike at [3, 60, 64, 32] with logintensity = 13, and one spike per channel located respectively at [: , 64, 60, 32] with logintensity = 14.
RandKSpaceSpikeNoise¶
 class
monai.transforms.
RandKSpaceSpikeNoise
(prob=0.1, intensity_range=None, channel_wise=True, as_tensor_output=True)[source]¶Naturalistic data augmentation via spike artifacts. The transform applies localized spikes in kspace, and it is the random version of
monai.transforms.KSpaceSpikeNoise
.Spike (Herringbone) artifact is a type of data acquisition artifact which may occur during MRI scans. For general information on spike artifacts, please refer to:
AAPM/RSNA physics tutorial for residents: fundamental physics of MR imaging.
Body MRI artifacts in clinical practice: A physicist’s and radiologist’s perspective.
 Parameters
prob (
float
) – probability of applying the transform, either on all channels at once, or channelwise ifchannel_wise = True
.intensity_range (
Optional
[Sequence
[Union
[Sequence
[float
],float
]]]) – pass a tuple (a, b) to sample the logintensity from the interval (a, b) uniformly for all channels. Or pass sequence of intevals ((a0, b0), (a1, b1), …) to sample for each respective channel. In the second case, the number of 2tuples must match the number of channels. Default ranges is (0.95x, 1.10x) where x is the mean logintensity for each channel.channel_wise – treat each channel independently. True by default.
as_tensor_output (
bool
) – if True return torch.Tensor, else return np.array. default: True.Example
To apply kspace spikes randomly with probability 0.5, and logintensity sampled from the interval [11, 12] for each channel independently, one uses
RandKSpaceSpikeNoise(prob=0.5, intensity_range=(11, 12), channel_wise=True)
IO¶
LoadImage¶

class
monai.transforms.
LoadImage
(reader=None, image_only=False, dtype=<class 'numpy.float32'>, *args, **kwargs)[source]¶ Load image file or files from provided path based on reader. Automatically choose readers based on the supported suffixes and in below order:  User specified reader at runtime when call this loader.  Registered readers from the latest to the first in list.  Default readers: (nii, nii.gz > NibabelReader), (png, jpg, bmp > PILReader), (npz, npy > NumpyReader), (others > ITKReader).
 Parameters
reader (
Union
[ImageReader
,str
,None
]) – register reader to load image file and meta data, if None, still can register readers at runtime or use the default readers. If a string of reader name provided, will construct a reader object with the *args and **kwargs parameters, supported reader name: “NibabelReader”, “PILReader”, “ITKReader”, “NumpyReader”.image_only (
bool
) – if True return only the image volume, otherwise return image data array and header dict.dtype (
Union
[dtype
,type
,None
]) – if not None convert the loaded image to this data type.args – additional parameters for reader if providing a reader name.
kwargs – additional parameters for reader if providing a reader name.
Note
The transform returns image data array if image_only is True, or a tuple of two elements containing the data array, and the meta data in a dict format otherwise.

__call__
(filename, reader=None)[source]¶  Parameters
filename (
Union
[Sequence
[str
],str
]) – path file or filelike object or a list of files. will save the filename to meta_data with key filename_or_obj. if provided a list of files, use the filename of first file.reader (
Optional
[ImageReader
]) – runtime reader to load image file and meta data.

register
(reader)[source]¶ Register image reader to load image file and meta data, latest registered reader has higher priority. Return all the registered image readers.
 Parameters
reader (
ImageReader
) – registered reader to load image file and meta data based on suffix, if all registered readers can’t match suffix at runtime, use the default readers. Return type
List
[ImageReader
]
SaveImage¶

class
monai.transforms.
SaveImage
(output_dir='./', output_postfix='trans', output_ext='.nii.gz', resample=True, mode='nearest', padding_mode=<GridSamplePadMode.BORDER: 'border'>, scale=None, dtype=<class 'numpy.float64'>, output_dtype=<class 'numpy.float32'>, save_batch=False, squeeze_end_dims=True, data_root_dir='', print_log=True)[source]¶ Save transformed data into files, support NIfTI and PNG formats. It can work for both numpy array and PyTorch Tensor in both pretransform chain and post transform chain. The name of saved file will be {input_image_name}_{output_postfix}{output_ext}, where the input image name is extracted from the provided meta data dictionary. If no meta data provided, use index from 0 as the filename prefix. It can also save a list of PyTorch Tensor or numpy array without batch dim.
Note: image should include channel dimension: [B],C,H,W,[D].
 Parameters
output_dir (
str
) – output image directory.output_postfix (
str
) – a string appended to all output file names, default to trans.output_ext (
str
) – output file extension name, available extensions: .nii.gz, .nii, .png.resample (
bool
) – whether to resample before saving the data array. if saving PNG format image, based on the spatial_shape from metadata. if saving NIfTI format image, based on the original_affine from metadata.mode (
Union
[GridSampleMode
,InterpolateMode
,str
]) –This option is used when
resample = True
. Defaults to"nearest"
. NIfTI files {
"bilinear"
,"nearest"
} Interpolation mode to calculate output values. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 NIfTI files {
 PNG files {
"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 PNG files {
padding_mode (
Union
[GridSamplePadMode
,str
]) –This option is used when
resample = True
. Defaults to"border"
. NIfTI files {
"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 NIfTI files {
 PNG files
This option is ignored.
scale (
Optional
[int
]) – {255
,65535
} postprocess data by clipping to [0, 1] and scaling [0, 255] (uint8) or [0, 65535] (uint16). Default is None to disable scaling. it’s used for PNG format only.dtype (
Union
[dtype
,type
,None
]) – data type during resampling computation. Defaults tonp.float64
for best precision. if None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
. it’s used for NIfTI format only.output_dtype (
Union
[dtype
,type
,None
]) – data type for saving data. Defaults tonp.float32
. it’s used for NIfTI format only.save_batch (
bool
) – whether the import image is a batch data, default to False. usually pretransforms run for channel first data, while posttransforms run for batch data.squeeze_end_dims (
bool
) – if True, any trailing singleton dimensions will be removed (after the channel has been moved to the end). So if input is (C,H,W,D), this will be altered to (H,W,D,C), and then if C==1, it will be saved as (H,W,D). If D also ==1, it will be saved as (H,W). If false, image will always be saved as (H,W,D,C). it’s used for NIfTI format only.data_root_dir (
str
) – if not empty, it specifies the beginning parts of the input file’s absolute path. it’s used to compute input_file_rel_path, the relative path to the file from data_root_dir to preserve folder structure when saving in case there are files in different folders with the same file names. for example: input_file_name: /foo/bar/test1/image.nii, output_postfix: seg output_ext: nii.gz output_dir: /output, data_root_dir: /foo/bar, output will be: /output/test1/image/image_seg.nii.gzprint_log (
bool
) – whether to print log about the saved file path, etc. default to True.
Postprocessing¶
Activations¶

class
monai.transforms.
Activations
(sigmoid=False, softmax=False, other=None)[source]¶ Add activation operations to the model output, typically Sigmoid or Softmax.
 Parameters
sigmoid (
bool
) – whether to execute sigmoid function on model output before transform. Defaults toFalse
.softmax (
bool
) – whether to execute softmax function on model output before transform. Defaults toFalse
.other (
Optional
[Callable
]) – callable function to execute other activation layers, for example: other = lambda x: torch.tanh(x). Defaults toNone
.
 Raises
TypeError – When
other
is not anOptional[Callable]
.

__call__
(img, sigmoid=None, softmax=None, other=None)[source]¶  Parameters
sigmoid (
Optional
[bool
]) – whether to execute sigmoid function on model output before transform. Defaults toself.sigmoid
.softmax (
Optional
[bool
]) – whether to execute softmax function on model output before transform. Defaults toself.softmax
.other (
Optional
[Callable
]) – callable function to execute other activation layers, for example: other = torch.tanh. Defaults toself.other
.
 Raises
ValueError – When
sigmoid=True
andsoftmax=True
. Incompatible values.TypeError – When
other
is not anOptional[Callable]
.ValueError – When
self.other=None
andother=None
. Incompatible values.
 Return type
Tensor
AsDiscrete¶

class
monai.transforms.
AsDiscrete
(argmax=False, to_onehot=False, n_classes=None, threshold_values=False, logit_thresh=0.5)[source]¶ Execute after model forward to transform model output to discrete values. It can complete below operations:
execute argmax for input logits values.
threshold input value to 0.0 or 1.0.
convert input value to OneHot format
 Parameters
argmax (
bool
) – whether to execute argmax function on input data before transform. Defaults toFalse
.to_onehot (
bool
) – whether to convert input data into the onehot format. Defaults toFalse
.n_classes (
Optional
[int
]) – the number of classes to convert to OneHot format. Defaults toNone
.threshold_values (
bool
) – whether threshold the float value to int number 0 or 1. Defaults toFalse
.logit_thresh (
float
) – the threshold value for thresholding operation.. Defaults to0.5
.

__call__
(img, argmax=None, to_onehot=None, n_classes=None, threshold_values=None, logit_thresh=None)[source]¶  Parameters
argmax (
Optional
[bool
]) – whether to execute argmax function on input data before transform. Defaults toself.argmax
.to_onehot (
Optional
[bool
]) – whether to convert input data into the onehot format. Defaults toself.to_onehot
.n_classes (
Optional
[int
]) – the number of classes to convert to OneHot format. Defaults toself.n_classes
.threshold_values (
Optional
[bool
]) – whether threshold the float value to int number 0 or 1. Defaults toself.threshold_values
.logit_thresh (
Optional
[float
]) – the threshold value for thresholding operation.. Defaults toself.logit_thresh
.
 Return type
Tensor
KeepLargestConnectedComponent¶

class
monai.transforms.
KeepLargestConnectedComponent
(applied_labels, independent=True, connectivity=None)[source]¶ Keeps only the largest connected component in the image. This transform can be used as a postprocessing step to clean up oversegment areas in model output.
 The input is assumed to be a PyTorch Tensor:
With shape (batch_size, 1, spatial_dim1[, spatial_dim2, …]) and the values correspond to expected labels.
With shape (batch_size, C, spatial_dim1[, spatial_dim2, …]) and the values should be 0, 1 on each labels.
Note
For single channel data, 0 will be treated as background and the oversegment pixels will be set to 0. For onehot data, the oversegment pixels will be set to 0 in its channel.
For example: Use KeepLargestConnectedComponent with applied_labels=[1], connectivity=1:
[1, 0, 0] [0, 0, 0] [0, 1, 1] => [0, 1 ,1] [0, 1, 1] [0, 1, 1]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=False, connectivity=1:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [1, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [1, 2, 0, 0 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,0]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=True, connectivity=1:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [0, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [0, 2, 0, 0 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,0]
Use KeepLargestConnectedComponent with applied_labels[1, 2], independent=False, connectivity=2:
[0, 0, 1, 0 ,0] [0, 0, 1, 0 ,0] [0, 2, 1, 1 ,1] [0, 2, 1, 1 ,1] [1, 2, 1, 0 ,0] => [1, 2, 1, 0 ,0] [1, 2, 0, 1 ,0] [1, 2, 0, 1 ,0] [2, 2, 0, 0 ,2] [2, 2, 0, 0 ,2]
 Parameters
applied_labels (
Union
[Sequence
[int
],int
]) – Labels for applying the connected component on. If only one channel. The pixel whose value is not in this list will remain unchanged. If the data is in onehot format, this is used to determine what channels to apply.independent (
bool
) – consider several labels as a whole or independent, default is True. Example use case would be segment label 1 is liver and label 2 is liver tumor, in that case you want this “independent” to be specified as False.connectivity (
Optional
[int
]) – Maximum number of orthogonal hops to consider a pixel/voxel as a neighbor. Accepted values are ranging from 1 to input.ndim. IfNone
, a full connectivity ofinput.ndim
is used.
LabelToContour¶

class
monai.transforms.
LabelToContour
(kernel_type='Laplace')[source]¶ Return the contour of binary input images that only compose of 0 and 1, with Laplace kernel set as default for edge detection. Typical usage is to plot the edge of label or segmentation output.
 Parameters
kernel_type (
str
) – the method applied to do edge detection, default is “Laplace”. Raises
NotImplementedError – When
kernel_type
is not “Laplace”.

__call__
(img)[source]¶  Parameters
img (
Tensor
) – torch tensor data to extract the contour, with shape: [batch_size, channels, height, width[, depth]] Raises
ValueError – When
image
ndim is not one of [4, 5]. Returns
it’s the binary classification result of whether a pixel is edge or not.
in order to keep the original shape of mask image, we use padding as default.
the edge detection is just approximate because it defects inherent to Laplace kernel, ideally the edge should be thin enough, but now it has a thickness.
 Return type
A torch tensor with the same shape as img, note
MeanEnsemble¶

class
monai.transforms.
MeanEnsemble
(weights=None)[source]¶ Execute mean ensemble on the input data. The input data can be a list or tuple of PyTorch Tensor with shape: [B, C[, H, W, D]], Or a single PyTorch Tensor with shape: [E, B, C[, H, W, D]], the E dimension represents the output data from different models. Typically, the input data is model output of segmentation task or classification task. And it also can support to add weights for the input data.
 Parameters
weights (
Union
[Sequence
[float
],Tensor
,ndarray
,None
]) – can be a list or tuple of numbers for input data with shape: [E, B, C, H, W[, D]]. or a Numpy ndarray or a PyTorch Tensor data. the weights will be added to input data from highest dimension, for example: 1. if the weights only has 1 dimension, it will be added to the E dimension of input data. 2. if the weights has 3 dimensions, it will be added to E, B and C dimensions. it’s a typical practice to add weights for different classes: to ensemble 3 segmentation model outputs, every output has 4 channels(classes), so the input data shape can be: [3, B, 4, H, W, D]. and add different weights for different classes, so the weights shape can be: [3, 1, 4]. for example: weights = [[[1, 2, 3, 4]], [[4, 3, 2, 1]], [[1, 1, 1, 1]]].

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Tensor
Prob NMS¶

class
monai.transforms.
ProbNMS
(spatial_dims=2, sigma=0.0, prob_threshold=0.5, box_size=48)[source]¶ Performs probability based nonmaximum suppression (NMS) on the probabilities map via iteratively selecting the coordinate with highest probability and then move it as well as its surrounding values. The remove range is determined by the parameter box_size. If multiple coordinates have the same highest probability, only one of them will be selected.
 Parameters
spatial_dims (
int
) – number of spatial dimensions of the input probabilities map. Defaults to 2.sigma (
Union
[Sequence
[float
],float
,Sequence
[Tensor
],Tensor
]) – the standard deviation for gaussian filter. It could be a single value, or spatial_dims number of values. Defaults to 0.0.prob_threshold (
float
) – the probability threshold, the function will stop searching if the highest probability is no larger than the threshold. The value should be no less than 0.0. Defaults to 0.5.box_size (
Union
[int
,Sequence
[int
]]) – the box size (in pixel) to be removed around the the pixel with the maximum probability. It can be an integer that defines the size of a square or cube, or a list containing different values for each dimensions. Defaults to 48.
 Returns
a list of selected lists, where inner lists contain probability and coordinates. For example, for 3D input, the inner lists are in the form of [probability, x, y, z].
 Raises
ValueError – When
prob_threshold
is less than 0.0.ValueError – When
box_size
is a list or tuple, and its length is not equal to spatial_dims.ValueError – When
box_size
has a less than 1 value.
VoteEnsemble¶

class
monai.transforms.
VoteEnsemble
(num_classes=None)[source]¶ Execute vote ensemble on the input data. The input data can be a list or tuple of PyTorch Tensor with shape: [B[, C, H, W, D]], Or a single PyTorch Tensor with shape: [E, B[, C, H, W, D]], the E dimension represents the output data from different models. Typically, the input data is model output of segmentation task or classification task.
Note
This vote transform expects the input data is discrete values. It can be multiple channels data in OneHot format or single channel data. It will vote to select the most common data between items. The output data has the same shape as every item of the input data.
 Parameters
num_classes (
Optional
[int
]) – if the input is single channel data instead of OneHot, we can’t get class number from channel, need to explicitly specify the number of classes to vote.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Tensor
Spatial¶
Spacing¶

class
monai.transforms.
Spacing
(pixdim, diagonal=False, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Resample input image into the specified pixdim.
 Parameters
pixdim (
Union
[Sequence
[float
],float
]) – output voxel spacing. if providing a single number, will use it for the first dimension. items of the pixdim sequence map to the spatial dimensions of input image, if length of pixdim sequence is longer than image spatial dimensions, will ignore the longer part, if shorter, will pad with 1.0. if the components of the pixdim are nonpositive values, the transform will use the corresponding components of the origial pixdim, which is computed from the affine matrix of input image.diagonal (
bool
) –whether to resample the input to have a diagonal affine matrix. If True, the input data is resampled to the following affine:
np.diag((pixdim_0, pixdim_1, ..., pixdim_n, 1))
This effectively resets the volume to the world coordinate system (RAS+ in nibabel). The original orientation, rotation, shearing are not preserved.
If False, this transform preserves the axes orientation, orthogonal rotation and translation components from the original affine. This option will not flip/swap axes of the original data.
mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Geometrically, we consider the pixels of the input as squares rather than points. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(data_array, affine=None, mode=None, padding_mode=None, align_corners=None, dtype=None, output_spatial_shape=None)[source]¶  Parameters
data_array (
ndarray
) – in shape (num_channels, H[, W, …]).affine (matrix) – (N+1)x(N+1) original affine matrix for spatially ND data_array. Defaults to identity.
mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Geometrically, we consider the pixels of the input as squares rather than points. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.output_spatial_shape (
Optional
[ndarray
]) – specify the shape of the output data_array. This is typically useful for the inverse of Spacingd where sometimes we could not compute the exact shape due to the quantization error with the affines.
 Raises
ValueError – When
data_array
has no spatial dimensions.ValueError – When
pixdim
is nonpositive.
 Return type
Tuple
[ndarray
,ndarray
,ndarray
] Returns
data_array (resampled into self.pixdim), original affine, current affine.
Orientation¶

class
monai.transforms.
Orientation
(axcodes=None, as_closest_canonical=False, labels=(('L', 'R'), ('P', 'A'), ('I', 'S')))[source]¶ Change the input image’s orientation into the specified based on axcodes.
 Parameters
axcodes (
Optional
[str
]) – N elements sequence for spatial ND input’s orientation. e.g. axcodes=’RAS’ represents 3D orientation: (Left, Right), (Posterior, Anterior), (Inferior, Superior). default orientation labels options are: ‘L’ and ‘R’ for the first dimension, ‘P’ and ‘A’ for the second, ‘I’ and ‘S’ for the third.as_closest_canonical (
bool
) – if True, load the image as closest to canonical axis format.labels (
Optional
[Sequence
[Tuple
[str
,str
]]]) – optional, None or sequence of (2,) sequences (2,) sequences are labels for (beginning, end) of output axis. Defaults to(('L', 'R'), ('P', 'A'), ('I', 'S'))
.
 Raises
ValueError – When
axcodes=None
andas_closest_canonical=True
. Incompatible values.
See Also: nibabel.orientations.ornt2axcodes.

__call__
(data_array, affine=None)[source]¶ original orientation of data_array is defined by affine.
 Parameters
data_array (
ndarray
) – in shape (num_channels, H[, W, …]).affine (matrix) – (N+1)x(N+1) original affine matrix for spatially ND data_array. Defaults to identity.
 Raises
ValueError – When
data_array
has no spatial dimensions.ValueError – When
axcodes
spatiality differs fromdata_array
.
 Return type
Tuple
[ndarray
,ndarray
,ndarray
] Returns
data_array (reoriented in self.axcodes), original axcodes, current axcodes.
RandRotate¶

class
monai.transforms.
RandRotate
(range_x=0.0, range_y=0.0, range_z=0.0, prob=0.1, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Randomly rotate the input arrays.
 Parameters
range_x (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in radians in the plane defined by the first and second axes. If single number, angle is uniformly sampled from (range_x, range_x).range_y (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in radians in the plane defined by the first and third axes. If single number, angle is uniformly sampled from (range_y, range_y).range_z (
Union
[Tuple
[float
,float
],float
]) – Range of rotation angle in radians in the plane defined by the second and third axes. If single number, angle is uniformly sampled from (range_z, range_z).prob (
float
) – Probability of rotation.keep_size (
bool
) – If it is False, the output shape is adapted so that the input array is contained completely in the output. If it is True, the output shape is the same as the input. Default is True.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(img, mode=None, padding_mode=None, align_corners=None, dtype=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape 2D: (nchannels, H, W), or 3D: (nchannels, H, W, D).mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.
 Return type
ndarray

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandFlip¶

class
monai.transforms.
RandFlip
(prob=0.1, spatial_axis=None)[source]¶ Randomly flips the image along axes. Preserves shape. See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
 Parameters
prob (
float
) – Probability of flipping.spatial_axis (
Union
[int
,Sequence
[int
],None
]) – Spatial axes along which to flip over. Default is None.
RandAxisFlip¶

class
monai.transforms.
RandAxisFlip
(prob=0.1)[source]¶ Randomly select a spatial axis and flip along it. See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
 Parameters
prob (
float
) – Probability of flipping.

__call__
(img)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]), Return type
ndarray

randomize
(data)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandZoom¶

class
monai.transforms.
RandZoom
(prob=0.1, min_zoom=0.9, max_zoom=1.1, mode=<InterpolateMode.AREA: 'area'>, padding_mode=<NumpyPadMode.EDGE: 'edge'>, align_corners=None, keep_size=True)[source]¶ Randomly zooms input arrays with given probability within given zoom range.
 Parameters
prob (
float
) – Probability of zooming.min_zoom (
Union
[Sequence
[float
],float
]) – Min zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, min_zoom should contain one value for each spatial axis. If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio.max_zoom (
Union
[Sequence
[float
],float
]) – Max zoom factor. Can be float or sequence same size as image. If a float, select a random factor from [min_zoom, max_zoom] then apply to all spatial dims to keep the original spatial shape ratio. If a sequence, max_zoom should contain one value for each spatial axis. If 2 values provided for 3D data, use the first value for both H & W dims to keep the same zoom ratio.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatepadding_mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge
”,"linear_ramp
”,"maximum
”,"mean
”, “median`”,"minimum
”, “reflect`”,"symmetric
”,"wrap
”,"empty
”,"<function>
”} The mode to pad data after zooming. See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.htmlalign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatekeep_size (
bool
) – Should keep original size (pad if needed), default is True.

__call__
(img, mode=None, padding_mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape 2D: (nchannels, H, W), or 3D: (nchannels, H, W, D).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatepadding_mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge
”,"linear_ramp
”,"maximum
”,"mean
”, “median`”,"minimum
”, “reflect`”,"symmetric
”,"wrap
”,"empty
”,"<function>
”} The mode to pad data after zooming, default toself.padding_mode
. See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.htmlalign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 Return type
ndarray

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
Affine¶

class
monai.transforms.
Affine
(rotate_params=None, shear_params=None, translate_params=None, scale_params=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None, image_only=False)[source]¶ Transform
img
given the affine parameters.The affine transformations are applied in rotate, shear, translate, scale order.
 Parameters
rotate_params (
Union
[Sequence
[float
],float
,None
]) – a rotation angle in radians, a scalar for 2D image, a tuple of 3 floats for 3D. Defaults to no rotation.shear_params (
Union
[Sequence
[float
],float
,None
]) – a tuple of 2 floats for 2D, a tuple of 6 floats for 3D. Defaults to no shearing.translate_params (
Union
[Sequence
[float
],float
,None
]) – a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Translation is in pixel/voxel relative to the center of the input image. Defaults to no translation.scale_params (
Union
[Sequence
[float
],float
,None
]) – a tuple of 2 floats for 2D, a tuple of 3 floats for 3D. Defaults to no scaling.spatial_size (
Union
[int
,Sequence
[int
],None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.image_only (
bool
) – if True return only the image volume, otherwise return (image, affine).

__call__
(img, spatial_size=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]),spatial_size (
Union
[int
,Sequence
[int
],None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
Resample¶

class
monai.transforms.
Resample
(mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, as_tensor_output=False, device=None)[source]¶ computes output image using values from img, locations from grid using pytorch. supports spatially 2D or 3D (num_channels, H, W[, D]).
 Parameters
mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – whether to return a torch tensor. Defaults to False.device (
Optional
[device
]) – device on which the tensor will be allocated.

__call__
(img, grid=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]).grid (
Union
[ndarray
,Tensor
,None
]) – shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]
RandAffine¶

class
monai.transforms.
RandAffine
(prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, cache_grid=False, as_tensor_output=True, device=None)[source]¶ Random affine transform.
 Parameters
prob (
float
) – probability of returning a randomized affine grid. defaults to 0.1, with 10% chance returns a randomized grid.rotate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – angle range in radians. If element i is iterable, then uniform[rotate_range[i][0], rotate_range[i][1]) will be used to generate the rotation parameter for the ith dimension. If not, uniform[rotate_range[i], rotate_range[i]) will be used. This can be altered on a perdimension basis. E.g., ((0,3), 1, …): for dim0, rotation will be in range [0, 3], and for dim1 [1, 1] will be used. Setting a single value will use [x, x] for dim0 and nothing for the remaining dimensions.shear_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – shear_range with format matching rotate_range.translate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – translate_range with format matching rotate_range.scale_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – scaling_range with format matching rotate_range. A value of 1.0 is added to the result. This allows 0 to correspond to no change (i.e., a scaling of 1).spatial_size (
Union
[int
,Sequence
[int
],None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplecache_grid (
bool
) – whether to cache the identity sampling grid. If the spatial size is not dynamically defined by input image, enabling this option could accelerate the transform.as_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.
See also
RandAffineGrid
for the random affine parameters configurations.Affine
for the affine transformation parameters configurations.

__call__
(img, spatial_size=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W[, D]),spatial_size (
Union
[int
,Sequence
[int
],None
]) – output image spatial size. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if img has two spatial dimensions, spatial_size should have 2 elements [h, w]. if img has three spatial dimensions, spatial_size should have 3 elements [h, w, d].mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

get_identity_grid
(spatial_size)[source]¶ Return a cached or new identity grid depends on the availability.
 Parameters
spatial_size (
Sequence
[int
]) – nondynamic spatial size

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
RandAffine
 Returns
a Randomizable instance.
RandDeformGrid¶

class
monai.transforms.
RandDeformGrid
(spacing, magnitude_range, as_tensor_output=True, device=None)[source]¶ Generate random deformation grid.
 Parameters
spacing (
Union
[Sequence
[float
],float
]) – spacing of the grid in 2D or 3D. e.g., spacing=(1, 1) indicates pixelwise deformation in 2D, spacing=(1, 1, 1) indicates voxelwise deformation in 3D, spacing=(2, 2) indicates deformation field defined on every other pixel in 2D.magnitude_range (
Tuple
[float
,float
]) – the random offsets will be generated from uniform[magnitude[0], magnitude[1]).as_tensor_output (
bool
) – whether to output tensor instead of numpy array. defaults to True.device (
Optional
[device
]) – device to store the output grid data.

randomize
(grid_size)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
AffineGrid¶

class
monai.transforms.
AffineGrid
(rotate_params=None, shear_params=None, translate_params=None, scale_params=None, as_tensor_output=True, device=None, affine=None)[source]¶ Affine transforms on the coordinates.
 Parameters
rotate_params (
Union
[Sequence
[float
],float
,None
]) – angle range in radians. rotate_params[0] with be used to generate the 1st rotation parameter from uniform[rotate_params[0], rotate_params[0]). Similarly, rotate_params[1] and rotate_params[2] are used in 3D affine for the range of 2nd and 3rd axes.shear_params (
Union
[Sequence
[float
],float
,None
]) – shear_params[0] with be used to generate the 1st shearing parameter from uniform[shear_params[0], shear_params[0]). Similarly, shear_params[1] to shear_params[N] controls the range of the uniform distribution used to generate the 2nd to Nth parameter.translate_params (
Union
[Sequence
[float
],float
,None
]) – translate_params[0] with be used to generate the 1st shift parameter from uniform[translate_params[0], translate_params[0]). Similarly, translate_params[1] to translate_params[N] controls the range of the uniform distribution used to generate the 2nd to Nth parameter.scale_params (
Union
[Sequence
[float
],float
,None
]) – scale_params[0] with be used to generate the 1st scaling factor from uniform[scale_params[0], scale_params[0]) + 1.0. Similarly, scale_params[1] to scale_params[N] controls the range of the uniform distribution used to generate the 2nd to Nth parameter.as_tensor_output (
bool
) – whether to output tensor instead of numpy array. defaults to True.device (
Optional
[device
]) – device to store the output grid data.affine (
Union
[ndarray
,Tensor
,None
]) – If applied, ignore the params (rotate_params, etc.) and use the supplied matrix. Should be square with each side = num of image spatial dimensions + 1.

__call__
(spatial_size=None, grid=None)[source]¶  Parameters
spatial_size (
Optional
[Sequence
[int
]]) – output grid size.grid (
Union
[ndarray
,Tensor
,None
]) – grid to be transformed. Shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.
 Raises
ValueError – When
grid=None
andspatial_size=None
. Incompatible values. Return type
Tuple
[Union
[ndarray
,Tensor
],Union
[ndarray
,Tensor
]]
RandAffineGrid¶

class
monai.transforms.
RandAffineGrid
(rotate_range=None, shear_range=None, translate_range=None, scale_range=None, as_tensor_output=True, device=None)[source]¶ Generate randomised affine grid.
 Parameters
rotate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – angle range in radians. If element i is iterable, then uniform[rotate_range[i][0], rotate_range[i][1]) will be used to generate the rotation parameter for the ith dimension. If not, uniform[rotate_range[i], rotate_range[i]) will be used. This can be altered on a perdimension basis. E.g., ((0,3), 1, …): for dim0, rotation will be in range [0, 3], and for dim1 [1, 1] will be used. Setting a single value will use [x, x] for dim0 and nothing for the remaining dimensions.shear_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – shear_range with format matching rotate_range.translate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – translate_range with format matching rotate_range.scale_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – scaling_range with format matching rotate_range. A value of 1.0 is added to the result. This allows 0 to correspond to no change (i.e., a scaling of 1).as_tensor_output (
bool
) – whether to output tensor instead of numpy array. defaults to True.device (
Optional
[device
]) – device to store the output grid data.
See also

__call__
(spatial_size=None, grid=None)[source]¶  Parameters
spatial_size (
Optional
[Sequence
[int
]]) – output grid size.grid (
Union
[ndarray
,Tensor
,None
]) – grid to be transformed. Shape must be (3, H, W) for 2D or (4, H, W, D) for 3D.
 Return type
Union
[ndarray
,Tensor
] Returns
a 2D (3xHxW) or 3D (4xHxWxD) grid.

get_transformation_matrix
()[source]¶ Get the most recently applied transformation matrix
 Return type
Union
[ndarray
,Tensor
,None
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
Rand2DElastic¶

class
monai.transforms.
Rand2DElastic
(spacing, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Random elastic deformation and affine in 2D
 Parameters
spacing (
Union
[Tuple
[float
,float
],float
]) – distance in between the control points.magnitude_range (
Tuple
[float
,float
]) – the random offsets will be generated fromuniform[magnitude[0], magnitude[1])
.prob (
float
) – probability of returning a randomized elastic transform. defaults to 0.1, with 10% chance returns a randomized elastic transform, otherwise returns aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – angle range in radians. If element i is iterable, then uniform[rotate_range[i][0], rotate_range[i][1]) will be used to generate the rotation parameter for the ith dimension. If not, uniform[rotate_range[i], rotate_range[i]) will be used. This can be altered on a perdimension basis. E.g., ((0,3), 1, …): for dim0, rotation will be in range [0, 3], and for dim1 [1, 1] will be used. Setting a single value will use [x, x] for dim0 and nothing for the remaining dimensions.shear_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – shear_range with format matching rotate_range.translate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – translate_range with format matching rotate_range.scale_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – scaling_range with format matching rotate_range. A value of 1.0 is added to the result. This allows 0 to correspond to no change (i.e., a scaling of 1).spatial_size (
Union
[int
,Tuple
[int
,int
],None
]) – specifying output image spatial size [h, w]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.
See also
RandAffineGrid
for the random affine parameters configurations.Affine
for the affine transformation parameters configurations.

__call__
(img, spatial_size=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W),spatial_size (
Union
[int
,Tuple
[int
,int
],None
]) – specifying output image spatial size [h, w]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

randomize
(spatial_size)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Rand2DElastic
 Returns
a Randomizable instance.
Rand3DElastic¶

class
monai.transforms.
Rand3DElastic
(sigma_range, magnitude_range, prob=0.1, rotate_range=None, shear_range=None, translate_range=None, scale_range=None, spatial_size=None, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.REFLECTION: 'reflection'>, as_tensor_output=False, device=None)[source]¶ Random elastic deformation and affine in 3D
 Parameters
sigma_range (
Tuple
[float
,float
]) – a Gaussian kernel with standard deviation sampled fromuniform[sigma_range[0], sigma_range[1])
will be used to smooth the random offset grid.magnitude_range (
Tuple
[float
,float
]) – the random offsets on the grid will be generated fromuniform[magnitude[0], magnitude[1])
.prob (
float
) – probability of returning a randomized elastic transform. defaults to 0.1, with 10% chance returns a randomized elastic transform, otherwise returns aspatial_size
centered area extracted from the input image.rotate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – angle range in radians. If element i is iterable, then uniform[rotate_range[i][0], rotate_range[i][1]) will be used to generate the rotation parameter for the ith dimension. If not, uniform[rotate_range[i], rotate_range[i]) will be used. This can be altered on a perdimension basis. E.g., ((0,3), 1, …): for dim0, rotation will be in range [0, 3], and for dim1 [1, 1] will be used. Setting a single value will use [x, x] for dim0 and nothing for the remaining dimensions.shear_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – shear_range with format matching rotate_range.translate_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – translate_range with format matching rotate_range.scale_range (
Union
[Sequence
[Union
[Tuple
[float
,float
],float
]],float
,None
]) – scaling_range with format matching rotate_range. A value of 1.0 is added to the result. This allows 0 to correspond to no change (i.e., a scaling of 1).spatial_size (
Union
[Tuple
[int
,int
,int
],int
,None
]) – specifying output image spatial size [h, w, d]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 32, 1) will be adapted to (32, 32, 64) if the third spatial dimension size of img is 64.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"reflection"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampleas_tensor_output (
bool
) – the computation is implemented using pytorch tensors, this option specifies whether to convert it back to numpy arrays.device (
Optional
[device
]) – device on which the tensor will be allocated.
See also
RandAffineGrid
for the random affine parameters configurations.Affine
for the affine transformation parameters configurations.

__call__
(img, spatial_size=None, mode=None, padding_mode=None)[source]¶  Parameters
img (
Union
[ndarray
,Tensor
]) – shape must be (num_channels, H, W, D),spatial_size (
Union
[Tuple
[int
,int
,int
],int
,None
]) – specifying spatial 3D output image spatial size [h, w, d]. if spatial_size and self.spatial_size are not defined, or smaller than 1, the transform will use the spatial size of img.mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 Return type
Union
[ndarray
,Tensor
]

randomize
(grid_size)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Rand3DElastic
 Returns
a Randomizable instance.
Rotate90¶

class
monai.transforms.
Rotate90
(k=1, spatial_axes=(0, 1))[source]¶ Rotate an array by 90 degrees in the plane specified by axes. See np.rot90 for additional details: https://numpy.org/doc/stable/reference/generated/numpy.rot90.html.
 Parameters
k (
int
) – number of times to rotate by 90 degrees.spatial_axes (
Tuple
[int
,int
]) – 2 int numbers, defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions. If axis is negative it counts from the last to the first axis.
RandRotate90¶

class
monai.transforms.
RandRotate90
(prob=0.1, max_k=3, spatial_axes=(0, 1))[source]¶ With probability prob, input arrays are rotated by 90 degrees in the plane specified by spatial_axes.
 Parameters
prob (
float
) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array)max_k (
int
) – number of rotations will be sampled from np.random.randint(max_k) + 1, (Default 3).spatial_axes (
Tuple
[int
,int
]) – 2 int numbers, defines the plane to rotate with 2 spatial axes. Default: (0, 1), this is the first two axis in spatial dimensions.

__call__
(img)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]), Return type
ndarray

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
Flip¶

class
monai.transforms.
Flip
(spatial_axis=None)[source]¶ Reverses the order of elements along the given spatial axis. Preserves shape. Uses
np.flip
in practice. See numpy.flip for additional details: https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html. Parameters
spatial_axis (
Union
[int
,Sequence
[int
],None
]) – spatial axes along which to flip over. Default is None. The default axis=None will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of ints, flipping is performed on all of the axes specified in the tuple.
Resize¶

class
monai.transforms.
Resize
(spatial_size, mode=<InterpolateMode.AREA: 'area'>, align_corners=None)[source]¶ Resize the input image to given spatial size (with scaling, not cropping/padding). Implemented using
torch.nn.functional.interpolate
. Parameters
spatial_size (
Union
[Sequence
[int
],int
]) – expected shape of spatial dimensions after resize operation. if the components of the spatial_size are nonpositive values, the transform will use the corresponding components of img size. For example, spatial_size=(32, 1) will be adapted to (32, 64) if the second spatial dimension size of img is 64.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate

__call__
(img, mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatealign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 Raises
ValueError – When
self.spatial_size
length is less thanimg
spatial dimensions. Return type
ndarray
Rotate¶

class
monai.transforms.
Rotate
(angle, keep_size=True, mode=<GridSampleMode.BILINEAR: 'bilinear'>, padding_mode=<GridSamplePadMode.BORDER: 'border'>, align_corners=False, dtype=<class 'numpy.float64'>)[source]¶ Rotates an input image by given angle using
monai.networks.layers.AffineTransform
. Parameters
angle (
Union
[Sequence
[float
],float
]) – Rotation angle(s) in radians. should a float for 2D, three floats for 3D.keep_size (
bool
) – If it is True, the output shape is kept the same as the input. If it is False, the output shape is adapted so that the input array is contained completely in the output. Default is True.mode (
Union
[GridSampleMode
,str
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults to"bilinear"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults to"border"
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
bool
) – Defaults to False. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults tonp.float64
for best precision. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.

__call__
(img, mode=None, padding_mode=None, align_corners=None, dtype=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: [chns, H, W] or [chns, H, W, D].mode (
Union
[GridSampleMode
,str
,None
]) – {"bilinear"
,"nearest"
} Interpolation mode to calculate output values. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplepadding_mode (
Union
[GridSamplePadMode
,str
,None
]) – {"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. Defaults toself.padding_mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample align_corners: Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsamplealign_corners (
Optional
[bool
]) – Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsampledtype (
Union
[dtype
,type
,None
]) – data type for resampling computation. Defaults toself.dtype
. If None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
.
 Raises
ValueError – When
img
spatially is not one of [2D, 3D]. Return type
ndarray
Zoom¶

class
monai.transforms.
Zoom
(zoom, mode=<InterpolateMode.AREA: 'area'>, padding_mode=<NumpyPadMode.EDGE: 'edge'>, align_corners=None, keep_size=True)[source]¶ Zooms an ND image using
torch.nn.functional.interpolate
. For details, please see https://pytorch.org/docs/stable/nn.functional.html#interpolate.Different from
monai.transforms.resize
, this transform takes scaling factors as input, and provides an option of preserving the input spatial size. Parameters
zoom (
Union
[Sequence
[float
],float
]) – The zoom factor along the spatial axes. If a float, zoom is the same for each spatial axis. If a sequence, zoom should contain one value for each spatial axis.mode (
Union
[InterpolateMode
,str
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults to"area"
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatepadding_mode (
Union
[NumpyPadMode
,str
]) – {"constant"
,"edge
”,"linear_ramp
”,"maximum
”,"mean
”, “median`”,"minimum
”, “reflect`”,"symmetric
”,"wrap
”,"empty
”,"<function>
”} The mode to pad data after zooming. See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.htmlalign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: None. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatekeep_size (
bool
) – Should keep original size (padding/slicing if needed), default is True.

__call__
(img, mode=None, padding_mode=None, align_corners=None)[source]¶  Parameters
img (
ndarray
) – channel first array, must have shape: (num_channels, H[, W, …, ]).mode (
Union
[InterpolateMode
,str
,None
]) – {"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. Defaults toself.mode
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolatepadding_mode (
Union
[NumpyPadMode
,str
,None
]) – {"constant"
,"edge
”,"linear_ramp
”,"maximum
”,"mean
”, “median`”,"minimum
”, “reflect`”,"symmetric
”,"wrap
”,"empty
”,"<function>
”} The mode to pad data after zooming, default toself.padding_mode
. See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.htmlalign_corners (
Optional
[bool
]) – This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Defaults toself.align_corners
. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
AddCoordinateChannels¶

class
monai.transforms.
AddCoordinateChannels
(spatial_channels)[source]¶ Appends additional channels encoding coordinates of the input. Useful when e.g. training using patchbased sampling, to allow feeding of the patch’s location into the network.
This can be seen as a inputonly version of CoordConv:
Liu, R. et al. An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution, NeurIPS 2018.
 Parameters
spatial_channels (
Sequence
[int
]) – the spatial dimensions that are to have their coordinates encoded in a channel and appended to the input. E.g., (1,2,3) will append three channels to the input, encoding the coordinates of the input’s three spatial dimensions (0 is reserved for the channel dimension).
Utility¶
Identity¶
AsChannelFirst¶

class
monai.transforms.
AsChannelFirst
(channel_dim= 1)[source]¶ Change the channel dimension of the image to the first dimension.
Most of the image transformations in
monai.transforms
assume the input image is in the channelfirst format, which has the shape (num_channels, spatial_dim_1[, spatial_dim_2, …]).This transform could be used to convert, for example, a channellast image array in shape (spatial_dim_1[, spatial_dim_2, …], num_channels) into the channelfirst format, so that the multidimensional image array can be correctly interpreted by the other transforms.
 Parameters
channel_dim (
int
) – which dimension of input image is the channel, default is the last dimension.
AsChannelLast¶

class
monai.transforms.
AsChannelLast
(channel_dim=0)[source]¶ Change the channel dimension of the image to the last dimension.
Some of other 3rd party transforms assume the input image is in the channellast format with shape (spatial_dim_1[, spatial_dim_2, …], num_channels).
This transform could be used to convert, for example, a channelfirst image array in shape (num_channels, spatial_dim_1[, spatial_dim_2, …]) into the channellast format, so that MONAI transforms can construct a chain with other 3rd party transforms together.
 Parameters
channel_dim (
int
) – which dimension of input image is the channel, default is the first dimension.
AddChannel¶

class
monai.transforms.
AddChannel
[source]¶ Adds a 1length channel dimension to the input image.
Most of the image transformations in
monai.transforms
assumes the input image is in the channelfirst format, which has the shape (num_channels, spatial_dim_1[, spatial_dim_2, …]).This transform could be used, for example, to convert a (spatial_dim_1[, spatial_dim_2, …]) spatial image into the channelfirst format so that the multidimensional image array can be correctly interpreted by the other transforms.
EnsureChannelFirst¶

class
monai.transforms.
EnsureChannelFirst
(strict_check=True)[source]¶ Automatically adjust or add the channel dimension of input data to ensure channel_first shape. It extracts the original_channel_dim info from provided meta_data dictionary. Typical values of original_channel_dim can be: “no_channel”, 0, 1. Convert the data to channel_first based on the original_channel_dim information.
 Parameters
strict_check (
bool
) – whether to raise an error when the meta information is insufficient.
RepeatChannel¶

class
monai.transforms.
RepeatChannel
(repeats)[source]¶ Repeat channel data to construct expected input shape for models. The repeats count includes the origin data, for example:
RepeatChannel(repeats=2)([[1, 2], [3, 4]])
generates:[[1, 2], [1, 2], [3, 4], [3, 4]]
 Parameters
repeats (
int
) – the number of repetitions for each element.
SplitChannel¶

class
monai.transforms.
SplitChannel
(channel_dim=None)[source]¶ Split Numpy array or PyTorch Tensor data according to the channel dim. It can help applying different following transforms to different channels. Channel number must be greater than 1.
 Parameters
channel_dim (
Optional
[int
]) – which dimension of input image is the channel, default to None to automatically select: if data is numpy array, channel_dim is 0 as numpy array is used in the pre transforms, if PyTorch Tensor, channel_dim is 1 as in most of the cases Tensor is uses in the post transforms.

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
List
[Union
[ndarray
,Tensor
]]
CastToType¶

class
monai.transforms.
CastToType
(dtype=<class 'numpy.float32'>)[source]¶ Cast the Numpy data to specified numpy data type, or cast the PyTorch Tensor to specified PyTorch data type.
 Parameters
dtype – convert image to this data type, default is np.float32.

__call__
(img, dtype=None)[source]¶ Apply the transform to img, assuming img is a numpy array or PyTorch Tensor.
 Parameters
dtype (
Union
[dtype
,type
,None
,dtype
]) – convert image to this data type, default is self.dtype. Raises
TypeError – When
img
type is not inUnion[numpy.ndarray, torch.Tensor]
. Return type
Union
[ndarray
,Tensor
]
ToTensor¶
ToNumpy¶
ToCupy¶
Transpose¶
SqueezeDim¶
DataStats¶

class
monai.transforms.
DataStats
(prefix='Data', data_type=True, data_shape=True, value_range=True, data_value=False, additional_info=None, logger_handler=None)[source]¶ Utility transform to show the statistics of data for debug or analysis. It can be inserted into any place of a transform chain and check results of previous transforms. It support both numpy.ndarray and torch.tensor as input data, so it can be used in preprocessing and postprocessing.
 Parameters
prefix (
str
) – will be printed in format: “{prefix} statistics”.data_type (
bool
) – whether to show the type of input data.data_shape (
bool
) – whether to show the shape of input data.value_range (
bool
) – whether to show the value range of input data.data_value (
bool
) – whether to show the raw value of input data. a typical example is to print some properties of Nifti image: affine, pixdim, etc.additional_info (
Optional
[Callable
]) – user can define callable function to extract additional info from input data.logger_handler (
Optional
[Handler
]) – add additional handler to output data: save to file, etc. add existing python logging handlers: https://docs.python.org/3/library/logging.handlers.html the handler should have a logging level of at least INFO.
 Raises
TypeError – When
additional_info
is not anOptional[Callable]
.
SimulateDelay¶

class
monai.transforms.
SimulateDelay
(delay_time=0.0)[source]¶ This is a pass through transform to be used for testing purposes. It allows adding fake behaviors that are useful for testing purposes to simulate how large datasets behave without needing to test on large data sets.
For example, simulating slow NFS data transfers, or slow network transfers in testing by adding explicit timing delays. Testing of small test data can lead to incomplete understanding of real world issues, and may lead to suboptimal design choices.
 Parameters
delay_time (
float
) – The minimum amount of time, in fractions of seconds, to accomplish this delay task.
Lambda¶

class
monai.transforms.
Lambda
(func=None)[source]¶ Apply a userdefined lambda as a transform.
For example:
image = np.ones((10, 2, 2)) lambd = Lambda(func=lambda x: x[:4, :, :]) print(lambd(image).shape) (4, 2, 2)
 Parameters
func (
Optional
[Callable
]) – Lambda/function to be applied. Raises
TypeError – When
func
is not anOptional[Callable]
.
LabelToMask¶

class
monai.transforms.
LabelToMask
(select_labels, merge_channels=False)[source]¶ Convert labels to mask for other tasks. A typical usage is to convert segmentation labels to mask data to preprocess images and then feed the images into classification network. It can support single channel labels or OneHot labels with specified select_labels. For example, users can select label value = [2, 3] to construct mask data, or select the second and the third channels of labels to construct mask data. The output mask data can be a multiple channels binary data or a single channel binary data that merges all the channels.
 Parameters
select_labels (
Union
[Sequence
[int
],int
]) – labels to generate mask from. for 1 channel label, the select_labels is the expected label values, like: [1, 2, 3]. for OneHot format label, the select_labels is the expected channel indices.merge_channels (
bool
) – whether to use np.any() to merge the result on channel dim. if yes, will return a single channel mask with binary data.

__call__
(img, select_labels=None, merge_channels=False)[source]¶  Parameters
select_labels (
Union
[int
,Sequence
[int
],None
]) – labels to generate mask from. for 1 channel label, the select_labels is the expected label values, like: [1, 2, 3]. for OneHot format label, the select_labels is the expected channel indices.merge_channels (
bool
) – whether to use np.any() to merge the result on channel dim. if yes, will return a single channel mask with binary data.
FgBgToIndices¶

class
monai.transforms.
FgBgToIndices
(image_threshold=0.0, output_shape=None)[source]¶ Compute foreground and background of the input label data, return the indices. If no output_shape specified, output data will be 1 dim indices after flattening. This transform can help precompute foreground and background regions for other transforms. A typical usage is to randomly select foreground and background to crop. The main logic is based on
monai.transforms.utils.map_binary_to_indices
. Parameters
image_threshold (
float
) – if enabled image at runtime, useimage > image_threshold
to determine the valid image content area and select background only in this area.output_shape (
Optional
[Sequence
[int
]]) – expected shape of output indices. if not None, unravel indices to specified shape.

__call__
(label, image=None, output_shape=None)[source]¶  Parameters
label (
ndarray
) – input data to compute foreground and background indices.image (
Optional
[ndarray
]) – if image is not None, uselabel = 0 & image > image_threshold
to define background. so the output items will not map to all the voxels in the label.output_shape (
Optional
[Sequence
[int
]]) – expected shape of output indices. if None, use self.output_shape instead.
 Return type
Tuple
[ndarray
,ndarray
]
ConvertToMultiChannelBasedOnBratsClasses¶

class
monai.transforms.
ConvertToMultiChannelBasedOnBratsClasses
[source]¶ Convert labels to multi channels based on brats18 classes: label 1 is the necrotic and nonenhancing tumor core label 2 is the the peritumoral edema label 4 is the GDenhancing tumor The possible classes are TC (Tumor core), WT (Whole tumor) and ET (Enhancing tumor).

__call__
(img)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
ndarray

AddExtremePointsChannel¶

class
monai.transforms.
AddExtremePointsChannel
(background=0, pert=0.0)[source]¶ Add extreme points of label to the image as a new channel. This transform generates extreme point from label and applies a gaussian filter. The pixel values in points image are rescaled to range [rescale_min, rescale_max] and added as a new channel to input image. The algorithm is described in Roth et al., Going to Extremes: Weakly Supervised Medical Image Segmentation https://arxiv.org/abs/2009.11988.
This transform only supports single channel labels (1, spatial_dim1, [spatial_dim2, …]). The background
index
is ignored when calculating extreme points. Parameters
background (
int
) – Class index of background label, defaults to 0.pert (
float
) – Random perturbation amount to add to the points, defaults to 0.0.
 Raises
ValueError – When no label image provided.
ValueError – When label image is not single channel.

__call__
(img, label=None, sigma=3.0, rescale_min= 1.0, rescale_max=1.0)[source]¶  Parameters
img (
ndarray
) – the image that we want to add new channel to.label (
Optional
[ndarray
]) – label image to get extreme points from. Shape must be (1, spatial_dim1, [, spatial_dim2, …]). Doesn’t support onehot labels.sigma (
Union
[Sequence
[float
],float
,Sequence
[Tensor
],Tensor
]) – if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.rescale_min (
float
) – minimum value of output data.rescale_max (
float
) – maximum value of output data.

randomize
(label)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
TorchVision¶

class
monai.transforms.
TorchVision
(name, *args, **kwargs)[source]¶ This is a wrapper transform for PyTorch TorchVision transform based on the specified transform name and args. As most of the TorchVision transforms only work for PIL image and PyTorch Tensor, this transform expects input data to be PyTorch Tensor, users can easily call ToTensor transform to convert a Numpy array to Tensor.
 Parameters
name (
str
) – The transform name in TorchVision package.args – parameters for the TorchVision transform.
kwargs – parameters for the TorchVision transform.
MapLabelValue¶

class
monai.transforms.
MapLabelValue
(orig_labels, target_labels, dtype=<class 'numpy.float32'>)[source]¶ Utility to map label values to another set of values. For example, map [3, 2, 1] to [0, 1, 2], [1, 2, 3] > [0.5, 1.5, 2.5], [“label3”, “label2”, “label1”] > [0, 1, 2], [3.5, 2.5, 1.5] > [“label0”, “label1”, “label2”], etc. The label data must be numpy array or arraylike data and the output data will be numpy array.
 Parameters
orig_labels (
Sequence
) – original labels that map to others.target_labels (
Sequence
) – expected label values, 1: 1 map to the orig_labels.dtype (
Union
[dtype
,type
,None
]) – convert the output data to dtype, default to float32.
Dictionary Transforms¶
Crop and Pad (Dict)¶
SpatialPadd¶

class
monai.transforms.
SpatialPadd
(keys, spatial_size, method=<Method.SYMMETRIC: 'symmetric'>, mode=<NumpyPadMode.CONSTANT: 'constant'>, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.SpatialPad
. Performs padding to the data, symmetric for all sides or all on one side for each dimension. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of output data after padding, if a dimension of the input data size is bigger than the pad size, will not pad that dimension. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [30, 30, 30] and spatial_size=[32, 25, 1], the spatial size of output data will be [32, 30, 30].method (
Union
[Method
,str
]) – {"symmetric"
,"end"
} Pad image symmetric on every side or only pad at the end sides. Defaults to"symmetric"
.mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
BorderPadd¶

class
monai.transforms.
BorderPadd
(keys, spatial_border, mode=<NumpyPadMode.CONSTANT: 'constant'>, allow_missing_keys=False)[source]¶ Pad the input data by adding specified borders to every dimension. Dictionarybased wrapper of
monai.transforms.BorderPad
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
spatial_border (
Union
[Sequence
[int
],int
]) –specified size for every spatial border. it can be 3 shapes:
single int number, pad all the borders with the same size.
length equals the length of image shape, pad every spatial dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [2, 1], pad every border of H dim with 2, pad every border of W dim with 1, result shape is [1, 8, 6].
length equals 2 x (length of image shape), pad every border of every dimension separately. for example, image shape(CHW) is [1, 4, 4], spatial_border is [1, 2, 3, 4], pad top of H dim with 1, pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4. the result shape is [1, 7, 11].
mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
DivisiblePadd¶

class
monai.transforms.
DivisiblePadd
(keys, k, mode=<NumpyPadMode.CONSTANT: 'constant'>, allow_missing_keys=False)[source]¶ Pad the input data, so that the spatial sizes are divisible by k. Dictionarybased wrapper of
monai.transforms.DivisiblePad
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
k (
Union
[Sequence
[int
],int
]) – the target k for each spatial dimension. if k is negative or 0, the original size is preserved. if k is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.
See also
monai.transforms.SpatialPad

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
SpatialCropd¶

class
monai.transforms.
SpatialCropd
(keys, roi_center=None, roi_size=None, roi_start=None, roi_end=None, roi_slices=None, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.SpatialCrop
. General purpose cropper to produce subvolume region of interest (ROI). If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape. It can support to crop ND spatial (channelfirst) data. The cropped region can be parameterised in various ways:
a list of slices for each spatial dimension (allows for use of ve indexing and None)
a spatial center and size
the start and end coordinates of the ROI
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
roi_center (
Optional
[Sequence
[int
]]) – voxel coordinates for center of the crop ROI.roi_size (
Optional
[Sequence
[int
]]) – size of the crop ROI, if a dimension of ROI size is bigger than image size, will not crop that dimension of the image.roi_start (
Optional
[Sequence
[int
]]) – voxel coordinates for start of the crop ROI.roi_end (
Optional
[Sequence
[int
]]) – voxel coordinates for end of the crop ROI, if a coordinate is out of image, use the end coordinate of image.roi_slices (
Optional
[Sequence
[slice
]]) – list of slices for each of the spatial dimensions.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
CenterSpatialCropd¶

class
monai.transforms.
CenterSpatialCropd
(keys, roi_size, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.CenterSpatialCrop
. If a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – the size of the crop region e.g. [224,224,128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandSpatialCropd¶

class
monai.transforms.
RandSpatialCropd
(keys, roi_size, max_roi_size=None, random_center=True, random_size=True, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandSpatialCrop
. Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum and maximum size to limit the randomly generated ROI. Suppose all the expected fields specified by keys have same shape.Note: even random_size=False, if a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].max_roi_size (
Union
[int
,Sequence
[int
],None
]) – if random_size is True and roi_size specifies the min crop region size, max_roi_size can specify the max crop region size. if None, defaults to the input image size. if its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. if True, the actual size is sampled from: randint(roi_scale * image spatial size, max_roi_scale * image spatial size + 1).allow_missing_keys (
bool
) – don’t raise exception if key is missing.

inverse
(data)[source]¶ Inverse of
__call__
. Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(img_size)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandSpatialCropSamplesd¶

class
monai.transforms.
RandSpatialCropSamplesd
(keys, roi_size, num_samples, max_roi_size=None, random_center=True, random_size=True, meta_keys=None, meta_key_postfix='meta_dict', allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandSpatialCropSamples
. Crop image with random size or specific size ROI to generate a list of N samples. It can crop at a random position as center or at the image center. And allows to set the minimum size to limit the randomly generated ROI. Suppose all the expected fields specified by keys have same shape, and add patch_index to the corresponding meta data. It will return a list of dictionaries for all the cropped images.Note: even random_size=False, if a dimension of the expected ROI size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected ROI, and the cropped results of several images may not have exactly the same shape.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_size (
Union
[Sequence
[int
],int
]) – if random_size is True, it specifies the minimum crop region. if random_size is False, it specifies the expected ROI size to crop. e.g. [224, 224, 128] if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. If its components have nonpositive values, the corresponding size of input image will be used. for example: if the spatial size of input data is [40, 40, 40] and roi_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].num_samples (
int
) – number of samples (crop regions) to take in the returned list.max_roi_size (
Union
[int
,Sequence
[int
],None
]) – if random_size is True and roi_size specifies the min crop region size, max_roi_size can specify the max crop region size. if None, defaults to the input image size. if its components have nonpositive values, the corresponding size of input image will be used.random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specific size ROI. The actual size is sampled from randint(roi_size, img_size).meta_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – explicitly indicate the key of the corresponding meta data dictionary. used to add patch_index to the meta dict. for example, for data with key image, the metadata by default is in image_meta_dict. the meta data is a dictionary object which contains: filename, original_shape, etc. it can be a sequence of string, map to the keys. if None, will try to construct meta_keys by key_{meta_key_postfix}.meta_key_postfix (
str
) – if meta_keys is None, use key_{postfix} to to fetch the meta data according to the key data, default is meta_dict, the meta data is a dictionary object. used to add patch_index to the meta dict.allow_missing_keys (
bool
) – don’t raise exception if key is missing.
 Raises
ValueError – When
num_samples
is nonpositive.

inverse
(data)[source]¶ Inverse of
__call__
. Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None

set_random_state
(seed=None, state=None)[source]¶ Set the random state locally, to control the randomness, the derived classes should use
self.R
instead of np.random to introduce random factors. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Raises
TypeError – When
state
is not anOptional[np.random.RandomState]
. Return type
Randomizable
 Returns
a Randomizable instance.
CropForegroundd¶

class
monai.transforms.
CropForegroundd
(keys, source_key, select_fn=<function is_positive>, channel_indices=None, margin=0, k_divisible=1, mode=<NumpyPadMode.CONSTANT: 'constant'>, start_coord_key='foreground_start_coord', end_coord_key='foreground_end_coord', allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.CropForeground
. Crop only the foreground object of the expected images. The typical usage is to help training and evaluation if the valid part is small in the whole medical image. The valid part can be determined by any field in the data with source_key, for example:  Select values > 0 in image field as the foreground and crop on all fields specified by keys.  Select label = 3 in label field as the foreground to crop on all fields specified by keys.  Select label > 0 in the third channel of a OneHot label field as the foreground to crop all keys fields. Users can define arbitrary function to select expected foreground from the whole source image or specified channels. And it can also add margin to every dim of the bounding box of foreground object. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
source_key (
str
) – data source to generate the bounding box of foreground, can be image or label, etc.select_fn (
Callable
) – function to select expected foreground, default is to select values > 0.channel_indices (
Union
[Iterable
[int
],int
,None
]) – if defined, select foreground only on the specified channels of image. if None, select foreground on the whole image.margin (
Union
[Sequence
[int
],int
]) – add margin value to spatial dims of the bounding box, if only 1 value provided, use it for all dims.k_divisible (
Union
[Sequence
[int
],int
]) – make each spatial dimension to be divisible by k, default to 1. if k_divisible is an int, the same k be applied to all the input spatial dimensions.mode (
Union
[NumpyPadMode
,str
]) – padding mode {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} one of the listed string values or a user supplied function. Defaults to"constant"
. see also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.htmlstart_coord_key (
str
) – key to record the start coordinate of spatial bounding box for foreground.end_coord_key (
str
) – key to record the end coordinate of spatial bounding box for foreground.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandWeightedCropd¶

class
monai.transforms.
RandWeightedCropd
(keys, w_key, spatial_size, num_samples=1, center_coord_key=None, meta_keys=None, meta_key_postfix='meta_dict', allow_missing_keys=False)[source]¶ Samples a list of num_samples image patches according to the provided weight_map.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
w_key (
str
) – key for the weight map. The corresponding value will be used as the sampling weights, it should be a singlechannel array in size, for example, (1, spatial_dim_0, spatial_dim_1, …)spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the image patch e.g. [224, 224, 128]. If its components have nonpositive values, the corresponding size of img will be used.num_samples (
int
) – number of samples (image patches) to take in the returned list.center_coord_key (
Optional
[str
]) – if specified, the actual sampling location will be stored with the corresponding key.meta_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – explicitly indicate the key of the corresponding meta data dictionary. used to add patch_index to the meta dict. for example, for data with key image, the metadata by default is in image_meta_dict. the meta data is a dictionary object which contains: filename, original_shape, etc. it can be a sequence of string, map to the keys. if None, will try to construct meta_keys by key_{meta_key_postfix}.meta_key_postfix (
str
) – if meta_keys is None, use key_{postfix} to to fetch the meta data according to the key data, default is meta_dict, the meta data is a dictionary object. used to add patch_index to the meta dict.allow_missing_keys (
bool
) – don’t raise exception if key is missing.
See also

inverse
(data)[source]¶ Inverse of
__call__
. Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(weight_map)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
RandCropByPosNegLabeld¶

class
monai.transforms.
RandCropByPosNegLabeld
(keys, label_key, spatial_size, pos=1.0, neg=1.0, num_samples=1, image_key=None, image_threshold=0.0, fg_indices_key=None, bg_indices_key=None, meta_keys=None, meta_key_postfix='meta_dict', allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandCropByPosNegLabel
. Crop random fixed sized regions with the center being a foreground or background voxel based on the Pos Neg Ratio. Suppose all the expected fields specified by keys have same shape, and add patch_index to the corresponding meta data. And will return a list of dictionaries for all the cropped images.If a dimension of the expected spatial size is bigger than the input image size, will not crop that dimension. So the cropped result may be smaller than the expected size, and the cropped results of several images may not have exactly the same shape.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
label_key (
str
) – name of key for label image, this will be used for finding foreground/background.spatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of the crop region e.g. [224, 224, 128]. if a dimension of ROI size is bigger than image size, will not crop that dimension of the image. if its components have nonpositive values, the corresponding size of data[label_key] will be used. for example: if the spatial size of input data is [40, 40, 40] and spatial_size=[32, 64, 1], the spatial size of output data will be [32, 40, 40].pos (
float
) – used with neg together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.neg (
float
) – used with pos together to calculate the ratiopos / (pos + neg)
for the probability to pick a foreground voxel as a center rather than a background voxel.num_samples (
int
) – number of samples (crop regions) to take in each list.image_key (
Optional
[str
]) – if image_key is not None, uselabel == 0 & image > image_threshold
to select the negative sample(background) center. so the crop center will only exist on valid image area.image_threshold (
float
) – if enabled image_key, useimage > image_threshold
to determine the valid image content area.fg_indices_key (
Optional
[str
]) – if provided precomputed foreground indices of label, will ignore above image_key and image_threshold, and randomly select crop centers based on them, need to provide fg_indices_key and bg_indices_key together, expect to be 1 dim array of spatial indices after flattening. a typical usage is to call FgBgToIndicesd transform first and cache the results.bg_indices_key (
Optional
[str
]) – if provided precomputed background indices of label, will ignore above image_key and image_threshold, and randomly select crop centers based on them, need to provide fg_indices_key and bg_indices_key together, expect to be 1 dim array of spatial indices after flattening. a typical usage is to call FgBgToIndicesd transform first and cache the results.meta_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – explicitly indicate the key of the corresponding meta data dictionary. used to add patch_index to the meta dict. for example, for data with key image, the metadata by default is in image_meta_dict. the meta data is a dictionary object which contains: filename, original_shape, etc. it can be a sequence of string, map to the keys. if None, will try to construct meta_keys by key_{meta_key_postfix}.meta_key_postfix (
str
) – if meta_keys is None, use key_{postfix} to to fetch the meta data according to the key data, default is meta_dict, the meta data is a dictionary object. used to add patch_index to the meta dict.allow_missing_keys (
bool
) – don’t raise exception if key is missing.
 Raises
ValueError – When
pos
orneg
are negative.ValueError – When
pos=0
andneg=0
. Incompatible values.

inverse
(data)[source]¶ Inverse of
__call__
. Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(label, fg_indices=None, bg_indices=None, image=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
None
ResizeWithPadOrCropd¶

class
monai.transforms.
ResizeWithPadOrCropd
(keys, spatial_size, mode=<NumpyPadMode.CONSTANT: 'constant'>, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ResizeWithPadOrCrop
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformspatial_size (
Union
[Sequence
[int
],int
]) – the spatial size of output data after padding or crop. If has nonpositive values, the corresponding size of input image will be used (no padding).mode (
Union
[Sequence
[Union
[NumpyPadMode
,str
]],NumpyPadMode
,str
]) – {"constant"
,"edge"
,"linear_ramp"
,"maximum"
,"mean"
,"median"
,"minimum"
,"reflect"
,"symmetric"
,"wrap"
,"empty"
} One of the listed string values or a user supplied function for padding. Defaults to"constant"
. See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html It also can be a sequence of string, each element corresponds to a key inkeys
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
BoundingRectd¶

class
monai.transforms.
BoundingRectd
(keys, bbox_key_postfix='bbox', select_fn=<function is_positive>, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.BoundingRect
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformbbox_key_postfix (
str
) – the output bounding box coordinates will be written to the value of {key}_{bbox_key_postfix}.select_fn (
Callable
) – function to select expected foreground, default is to select values > 0.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ See also:
monai.transforms.utils.generate_spatial_bounding_box
. Return type
Dict
[Hashable
,ndarray
]
RandScaleCropd¶

class
monai.transforms.
RandScaleCropd
(keys, roi_scale, max_roi_scale=None, random_center=True, random_size=True, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandScaleCrop
. Crop image with random size or specific size ROI. It can crop at a random position as center or at the image center. And allows to set the minimum and maximum scale of image size to limit the randomly generated ROI. Suppose all the expected fields specified by keys have same shape. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_scale (
Union
[Sequence
[float
],float
]) – if random_size is True, it specifies the minimum crop size: roi_scale * image spatial size. if random_size is False, it specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5]. If its components have nonpositive values, will use 1.0 instead, which means the input image size.max_roi_size – if random_size is True and roi_scale specifies the min crop region size, max_roi_scale can specify the max crop region size: max_roi_scale * image spatial size. if None, defaults to the input image size. if its components have nonpositive values, will use 1.0 instead, which means the input image size.
random_center (
bool
) – crop at random position as center or the image center.random_size (
bool
) – crop with random size or specified size ROI by roi_scale * image spatial size. if True, the actual size is sampled from: randint(roi_scale * image spatial size, max_roi_scale * image spatial size + 1).allow_missing_keys (
bool
) – don’t raise exception if key is missing.
CenterScaleCropd¶

class
monai.transforms.
CenterScaleCropd
(keys, roi_scale, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.CenterScaleCrop
. Note: as using the same scaled ROI to crop, all the input data specified by keys should have the same spatial shape. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformroi_scale (
Union
[Sequence
[float
],float
]) – specifies the expected scale of image size to crop. e.g. [0.3, 0.4, 0.5] or a number for all dims. If its components have nonpositive values, will use 1.0 instead, which means the input image size.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
Instensity (Dict)¶
RandGaussianNoised¶

class
monai.transforms.
RandGaussianNoised
(keys, prob=0.1, mean=0.0, std=0.1, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandGaussianNoise
. Add Gaussian noise to image. This transform assumes all the expected fields have same shape. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
prob (
float
) – Probability to add Gaussian noise.mean (
Union
[Sequence
[float
],float
]) – Mean or “centre” of the distribution.std (
float
) – Standard deviation (spread) of distribution.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(im_shape)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
ShiftIntensityd¶

class
monai.transforms.
ShiftIntensityd
(keys, offset, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
offset (
float
) – offset value to shift the intensity of image.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandShiftIntensityd¶

class
monai.transforms.
RandShiftIntensityd
(keys, offsets, prob=0.1, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
offsets (
Union
[Tuple
[float
,float
],float
]) – offset range to randomly shift. if single number, offset value is picked from (offsets, offsets).prob (
float
) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array.)allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
StdShiftIntensityd¶

class
monai.transforms.
StdShiftIntensityd
(keys, factor, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.StdShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
factor (
float
) – factor shift byv = v + factor * std(v)
.nonzero (
bool
) – whether only count nonzero values.channel_wise (
bool
) – if True, calculate on each channel separately. Please ensure that the first dimension represents the channel of the image if True.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandStdShiftIntensityd¶

class
monai.transforms.
RandStdShiftIntensityd
(keys, factors, prob=0.1, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandStdShiftIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
factors (
Union
[Tuple
[float
,float
],float
]) – if tuple, the randomly picked range is (min(factors), max(factors)). If single number, the range is (factors, factors).prob (
float
) – probability of std shift.nonzero (
bool
) – whether only count nonzero values.channel_wise (
bool
) – if True, calculate on each channel separately.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandBiasFieldd¶

class
monai.transforms.
RandBiasFieldd
(keys, degree=3, coeff_range=(0.0, 0.1), dtype=<class 'numpy.float32'>, prob=1.0, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandBiasField
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
degree (
int
) – degree of freedom of the polynomials. The value should be no less than 1. Defaults to 3.coeff_range (
Tuple
[float
,float
]) – range of the random coefficients. Defaults to (0.0, 0.1).dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.prob (
float
) – probability to do random bias field.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
ScaleIntensityd¶

class
monai.transforms.
ScaleIntensityd
(keys, minv=0.0, maxv=1.0, factor=None, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ScaleIntensity
. Scale the intensity of input image to the given value range (minv, maxv). If minv and maxv not provided, use factor to scale image byv = v * (1 + factor)
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
minv (
Optional
[float
]) – minimum value of output data.maxv (
Optional
[float
]) – maximum value of output data.factor (
Optional
[float
]) – factor scale byv = v * (1 + factor)
. In order to use this parameter, please set minv and maxv into None.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandScaleIntensityd¶

class
monai.transforms.
RandScaleIntensityd
(keys, factors, prob=0.1, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandScaleIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
factors (
Union
[Tuple
[float
,float
],float
]) – factor range to randomly scale byv = v * (1 + factor)
. if single number, factor value is picked from (factors, factors).prob (
float
) – probability of rotating. (Default 0.1, with 10% probability it returns a rotated array.)allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
NormalizeIntensityd¶

class
monai.transforms.
NormalizeIntensityd
(keys, subtrahend=None, divisor=None, nonzero=False, channel_wise=False, dtype=<class 'numpy.float32'>, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.NormalizeIntensity
. This transform can normalize only nonzero values or entire image, and can also calculate mean and std on each channel separately. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformsubtrahend (
Optional
[ndarray
]) – the amount to subtract by (usually the mean)divisor (
Optional
[ndarray
]) – the amount to divide by (usually the standard deviation)nonzero (
bool
) – whether only normalize nonzero values.channel_wise (
bool
) – if using calculated mean and std, calculate on each channel separately or calculate on the entire image directly.dtype (
Union
[dtype
,type
,None
]) – output data type, defaults to float32.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
ThresholdIntensityd¶

class
monai.transforms.
ThresholdIntensityd
(keys, threshold, above=True, cval=0.0, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ThresholdIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformthreshold (
float
) – the threshold to filter intensity values.above (
bool
) – filter values above the threshold or below the threshold, default is True.cval (
float
) – value to fill the remaining parts of the image, default is 0.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
ScaleIntensityRanged¶

class
monai.transforms.
ScaleIntensityRanged
(keys, a_min, a_max, b_min, b_max, clip=False, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ScaleIntensityRange
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransforma_min (
float
) – intensity original range min.a_max (
float
) – intensity original range max.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
GibbsNoised¶

class
monai.transforms.
GibbsNoised
(keys, alpha=0.5, as_tensor_output=True, allow_missing_keys=False)[source]¶ Dictionarybased version of GibbsNoise.
The transform applies Gibbs noise to 2D/3D MRI images. Gibbs artifacts are one of the common type of type artifacts appearing in MRI scans.
For general information on Gibbs artifacts, please refer to: https://pubs.rsna.org/doi/full/10.1148/rg.313105115 https://pubs.rsna.org/doi/full/10.1148/radiographics.22.4.g02jl14949
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – ‘image’, ‘label’, or [‘image’, ‘label’] depending on which data you need to transform.alpha (float) – Parametrizes the intensity of the Gibbs noise filter applied. Takes values in the interval [0,1] with alpha = 0 acting as the identity mapping.
as_tensor_output (
bool
) – if true return torch.Tensor, else return np.array. default: True.allow_missing_keys (
bool
) – do not raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,Union
[Tensor
,ndarray
]] Returns
An updated dictionary version of
data
by applying the transform.
RandGibbsNoised¶

class
monai.transforms.
RandGibbsNoised
(keys, prob=0.1, alpha=(0.0, 1.0), as_tensor_output=True, allow_missing_keys=False)[source]¶ Dictionarybased version of RandGibbsNoise.
Naturalistic image augmentation via Gibbs artifacts. The transform randomly applies Gibbs noise to 2D/3D MRI images. Gibbs artifacts are one of the common type of type artifacts appearing in MRI scans.
The transform is applied to all the channels in the data.
For general information on Gibbs artifacts, please refer to: https://pubs.rsna.org/doi/full/10.1148/rg.313105115 https://pubs.rsna.org/doi/full/10.1148/radiographics.22.4.g02jl14949
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – ‘image’, ‘label’, or [‘image’, ‘label’] depending on which data you need to transform.prob (float) – probability of applying the transform.
alpha (float, List[float]) – Parametrizes the intensity of the Gibbs noise filter applied. Takes values in the interval [0,1] with alpha = 0 acting as the identity mapping. If a length2 list is given as [a,b] then the value of alpha will be sampled uniformly from the interval [a,b].
as_tensor_output (
bool
) – if true return torch.Tensor, else return np.array. default: True.allow_missing_keys (
bool
) – do not raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,Union
[Tensor
,ndarray
]]
KSpaceSpikeNoised¶

class
monai.transforms.
KSpaceSpikeNoised
(keys, loc, k_intensity=None, as_tensor_output=True, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.KSpaceSpikeNoise
.Applies localized spikes in kspace at the given locations and intensities. Spike (Herringbone) artifact is a type of data acquisition artifact which may occur during MRI scans.
For general information on spike artifacts, please refer to:
AAPM/RSNA physics tutorial for residents: fundamental physics of MR imaging.
Body MRI artifacts in clinical practice: A physicist’s and radiologist’s perspective.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – “image”, “label”, or [“image”, “label”] depending on which data you need to transform.loc (
Union
[Tuple
,Sequence
[Tuple
]]) – spatial location for the spikes. For images with 3D spatial dimensions, the user can provide (C, X, Y, Z) to fix which channel C is affected, or (X, Y, Z) to place the same spike in all channels. For 2D cases, the user can provide (C, X, Y) or (X, Y).k_intensity (
Union
[Sequence
[float
],float
,None
]) – value for the logintensity of the kspace version of the image. If one location is passed toloc
or the channel is not specified, then this argument should receive a float. Ifloc
is given a sequence of locations, then this argument should receive a sequence of intensities. This value should be tested as it is datadependent. The default values are the 2.5 the mean of the logintensity for each channel.as_tensor_output (
bool
) – ifTrue
return torch.Tensor, else return np.array. Default:True
.allow_missing_keys (
bool
) – do not raise exception if key is missing.
Example
When working with 4D data,
KSpaceSpikeNoised("image", loc = ((3,60,64,32), (64,60,32)), k_intensity = (13,14))
will place a spike at [3, 60, 64, 32] with logintensity = 13, and one spike per channel located respectively at [: , 64, 60, 32] with logintensity = 14.
RandKSpaceSpikeNoised¶

class
monai.transforms.
RandKSpaceSpikeNoised
(keys, global_prob=1.0, prob=0.1, img_intensity_range=None, label_intensity_range=None, channel_wise=True, common_sampling=False, common_seed=42, as_tensor_output=True, allow_missing_keys=False)[source]¶ Dictionarybased version of
monai.transforms.RandKSpaceSpikeNoise
.Naturalistic data augmentation via spike artifacts. The transform applies localized spikes in kspace.
For general information on spike artifacts, please refer to:
AAPM/RSNA physics tutorial for residents: fundamental physics of MR imaging.
Body MRI artifacts in clinical practice: A physicist’s and radiologist’s perspective.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – “image”, “label”, or [“image”, “label”] depending on which data you need to transform.global_prob (
float
) – probability of applying transform to the dictionary.prob (
float
) – probability to add spike artifact to each item in the dictionary provided it is realized that the noise will be applied to the dictionary.img_intensity_range (
Optional
[Sequence
[Union
[Sequence
[float
],float
]]]) – Intensity range to sample for"image"
key. Pass a tuple (a, b) to sample the logintensity from the interval (a, b) uniformly for all channels. Or pass sequence of intevals ((a0, b0), (a1, b1), …) to sample for each respective channel. In the second case, the number of 2tuples must match the number of channels. Default ranges is (0.95x, 1.10x) where x is the mean logintensity for each channel.label_intensity_range (
Optional
[Sequence
[Union
[Sequence
[float
],float
]]]) – Intensity range to sample for"label"
key. Same as behavior asimg_intensity_range
but"label"
key.channel_wise (
bool
) – treat each channel independently. True by default.common_sampling (
bool
) – IfTrue
same values for location and logintensity will be sampled for the image and label.common_seed (
int
) – Seed to be used in casecommon_sampling = True
.as_tensor_output (
bool
) – ifTrue
return torch.Tensor, else return np.array. Default:True
.allow_missing_keys (
bool
) – do not raise exception if key is missing.
Example
To apply kspace spikes randomly on the image only, with probability 0.5, and logintensity sampled from the interval [13, 15] for each channel independently, one uses
RandKSpaceSpikeNoised("image", prob=0.5, img_intensity_range=(13,15), channel_wise=True)
.
__call__
(data)[source]¶  Parameters
data (
Mapping
[Hashable
,Union
[Tensor
,ndarray
]]) – Expects image/label to have dimensions (C, H, W) or (C, H, W, D), where C is the channel. Return type
Dict
[Hashable
,Union
[Tensor
,ndarray
]]

set_rand_state
(seed=None, state=None)[source]¶ Set the random state locally to control the randomness. User should use this method instead of
set_random_state
. Parameters
seed (
Optional
[int
]) – set the random state with an integer seed.state (
Optional
[RandomState
]) – set the random state with a np.random.RandomState object.
 Return type
None
ScaleIntensityRangePercentilesd¶

class
monai.transforms.
ScaleIntensityRangePercentilesd
(keys, lower, upper, b_min, b_max, clip=False, relative=False, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.ScaleIntensityRangePercentiles
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformlower (
float
) – lower percentile.upper (
float
) – upper percentile.b_min (
float
) – intensity target range min.b_max (
float
) – intensity target range max.clip (
bool
) – whether to perform clip after scaling.relative (
bool
) – whether to scale to the corresponding percentiles of [b_min, b_max]allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
AdjustContrastd¶

class
monai.transforms.
AdjustContrastd
(keys, gamma, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.AdjustContrast
. Changes image intensity by gamma. Each pixel/voxel intensity is updated as:x = ((x  min) / intensity_range) ^ gamma * intensity_range + min
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformgamma (
float
) – gamma value to adjust the contrast as function.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandAdjustContrastd¶

class
monai.transforms.
RandAdjustContrastd
(keys, prob=0.1, gamma=(0.5, 4.5), allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandAdjustContrast
. Randomly changes image intensity by gamma. Each pixel/voxel intensity is updated as:x = ((x  min) / intensity_range) ^ gamma * intensity_range + min
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformprob (
float
) – Probability of adjustment.gamma (
Union
[Tuple
[float
,float
],float
]) – Range of gamma values. If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
MaskIntensityd¶

class
monai.transforms.
MaskIntensityd
(keys, mask_data=None, mask_key=None, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.MaskIntensity
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
mask_data (
Optional
[ndarray
]) – if mask data is single channel, apply to every channel of input image. if multiple channels, the channel number must match input data. mask_data will be converted to bool values by mask_data > 0 before applying transform to input image. if None, will extract the mask data from input data based on mask_key.mask_key (
Optional
[str
]) – the key to extract mask data from input dictionary, only works when mask_data is None.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
GaussianSmoothd¶

class
monai.transforms.
GaussianSmoothd
(keys, sigma, approx='erf', allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSmooth
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma (
Union
[Sequence
[float
],float
]) – if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandGaussianSmoothd¶

class
monai.transforms.
RandGaussianSmoothd
(keys, sigma_x=(0.25, 1.5), sigma_y=(0.25, 1.5), sigma_z=(0.25, 1.5), approx='erf', prob=0.1, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSmooth
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma_x (
Tuple
[float
,float
]) – randomly select sigma value for the first spatial dimension.sigma_y (
Tuple
[float
,float
]) – randomly select sigma value for the second spatial dimension if have.sigma_z (
Tuple
[float
,float
]) – randomly select sigma value for the third spatial dimension if have.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.prob (
float
) – probability of Gaussian smooth.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
GaussianSharpend¶

class
monai.transforms.
GaussianSharpend
(keys, sigma1=3.0, sigma2=1.0, alpha=30.0, approx='erf', allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSharpen
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma1 (
Union
[Sequence
[float
],float
]) – sigma parameter for the first gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.sigma2 (
Union
[Sequence
[float
],float
]) – sigma parameter for the second gaussian kernel. if a list of values, must match the count of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension. if only 1 value provided, use it for all spatial dimensions.alpha (
float
) – weight parameter to compute the final result.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
] Returns
An updated dictionary version of
data
by applying the transform.
RandGaussianSharpend¶

class
monai.transforms.
RandGaussianSharpend
(keys, sigma1_x=(0.5, 1.0), sigma1_y=(0.5, 1.0), sigma1_z=(0.5, 1.0), sigma2_x=0.5, sigma2_y=0.5, sigma2_z=0.5, alpha=(10.0, 30.0), approx='erf', prob=0.1, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.GaussianSharpen
. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
sigma1_x (
Tuple
[float
,float
]) – randomly select sigma value for the first spatial dimension of first gaussian kernel.sigma1_y (
Tuple
[float
,float
]) – randomly select sigma value for the second spatial dimension(if have) of first gaussian kernel.sigma1_z (
Tuple
[float
,float
]) – randomly select sigma value for the third spatial dimension(if have) of first gaussian kernel.sigma2_x (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the first spatial dimension of second gaussian kernel. if only 1 value X provided, it must be smaller than sigma1_x and randomly select from [X, sigma1_x].sigma2_y (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the second spatial dimension(if have) of second gaussian kernel. if only 1 value Y provided, it must be smaller than sigma1_y and randomly select from [Y, sigma1_y].sigma2_z (
Union
[Tuple
[float
,float
],float
]) – randomly select sigma value for the third spatial dimension(if have) of second gaussian kernel. if only 1 value Z provided, it must be smaller than sigma1_z and randomly select from [Z, sigma1_z].alpha (
Tuple
[float
,float
]) – randomly select weight parameter to compute the final result.approx (
str
) – discrete Gaussian kernel type, available options are “erf”, “sampled”, and “scalespace”. see alsomonai.networks.layers.GaussianFilter()
.prob (
float
) – probability of Gaussian sharpen.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
RandHistogramShiftd¶

class
monai.transforms.
RandHistogramShiftd
(keys, num_control_points=10, prob=0.1, allow_missing_keys=False)[source]¶ Dictionarybased version
monai.transforms.RandHistogramShift
. Apply random nonlinear transform the the image’s intensity histogram. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also: monai.transforms.MapTransformnum_control_points (
Union
[Tuple
[int
,int
],int
]) – number of control points governing the nonlinear intensity mapping. a smaller number of control points allows for larger intensity shifts. if two values provided, number of control points selecting from range (min_value, max_value).prob (
float
) – probability of histogram shift.allow_missing_keys (
bool
) – don’t raise exception if key is missing.

__call__
(data)[source]¶ data
is an element which often comes from an iteration over an iterable, such astorch.utils.data.Dataset
. This method should return an updated version ofdata
. To simplify the input validations, most of the transforms assume thatdata
is a Numpy ndarray, PyTorch Tensor or stringthe data shape can be:
string data without shape, LoadImage transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChannel expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirst expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
This method can optionally take additional arguments to help execute transformation operation.
 Raises
NotImplementedError – When the subclass does not override this method.
 Return type
Dict
[Hashable
,ndarray
]

randomize
(data=None)[source]¶ Within this method,
self.R
should be used, instead of np.random, to introduce random factors.all
self.R
calls happen here so that we have a better chance to identify errors of sync the random state.This method can generate the random factors based on properties of the input data.
 Return type
None
IO (Dict)¶
LoadImaged¶

class
monai.transforms.
LoadImaged
(keys, reader=None, dtype=<class 'numpy.float32'>, meta_keys=None, meta_key_postfix='meta_dict', overwriting=False, image_only=False, allow_missing_keys=False, *args, **kwargs)[source]¶ Dictionarybased wrapper of
monai.transforms.LoadImage
, must load image and metadata together. If loading a list of files in one key, stack them together and add a new dimension as the first dimension, and use the meta data of the first image to represent the stacked result. Note that the affine transform of all the stacked images should be same. The output metadata field will be created asmeta_keys
orkey_{meta_key_postfix}
.It can automatically choose readers based on the supported suffixes and in below order:  User specified reader at runtime when call this loader.  Registered readers from the latest to the first in list.  Default readers: (nii, nii.gz > NibabelReader), (png, jpg, bmp > PILReader), (npz, npy > NumpyReader), (others > ITKReader).
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
reader (
Union
[ImageReader
,str
,None
]) – register reader to load image file and meta data, if None, still can register readers at runtime or use the default readers. If a string of reader name provided, will construct a reader object with the *args and **kwargs parameters, supported reader name: “NibabelReader”, “PILReader”, “ITKReader”, “NumpyReader”.dtype (
Union
[dtype
,type
,None
]) – if not None convert the loaded image data to this data type.meta_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – explicitly indicate the key to store the corresponding meta data dictionary. the meta data is a dictionary object which contains: filename, original_shape, etc. it can be a sequence of string, map to the keys. if None, will try to construct meta_keys by key_{meta_key_postfix}.meta_key_postfix (
str
) – if meta_keys is None, use key_{postfix} to store the metadata of the nifti image, default is meta_dict. The meta data is a dictionary object. For example, load nifti file for image, store the metadata into image_meta_dict.overwriting (
bool
) – whether allow to overwrite existing meta data of same key. default is False, which will raise exception if encountering existing key.image_only (
bool
) – if True return dictionary containing just only the image volumes, otherwise return dictionary containing image data array and header dict per input key.allow_missing_keys (
bool
) – don’t raise exception if key is missing.args – additional parameters for reader if providing a reader name.
kwargs – additional parameters for reader if providing a reader name.
SaveImaged¶

class
monai.transforms.
SaveImaged
(keys, meta_keys=None, meta_key_postfix='meta_dict', output_dir='./', output_postfix='trans', output_ext='.nii.gz', resample=True, mode='nearest', padding_mode=<GridSamplePadMode.BORDER: 'border'>, scale=None, dtype=<class 'numpy.float64'>, output_dtype=<class 'numpy.float32'>, save_batch=False, allow_missing_keys=False, squeeze_end_dims=True, data_root_dir='', print_log=True)[source]¶ Dictionarybased wrapper of
monai.transforms.SaveImage
.Note
Image should include channel dimension: [B],C,H,W,[D]. If the data is a patch of big image, will append the patch index to filename.
 Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to be transformed. See also:monai.transforms.compose.MapTransform
meta_keys (
Union
[Collection
[Hashable
],Hashable
,None
]) – explicitly indicate the key of the corresponding meta data dictionary. for example, for data with key image, the metadata by default is in image_meta_dict. the meta data is a dictionary object which contains: filename, original_shape, etc. it can be a sequence of string, map to the keys. if None, will try to construct meta_keys by key_{meta_key_postfix}.meta_key_postfix (
str
) – if meta_keys is None and key_{postfix} was used to store the metadata in LoadImaged. need the key to extract metadata to save images, default is meta_dict. for example, for data with key image, the metadata by default is in image_meta_dict. the meta data is a dictionary object which contains: filename, affine, original_shape, etc. if no corresponding metadata, set to None.output_dir (
str
) – output image directory.output_postfix (
str
) – a string appended to all output file names, default to trans.output_ext (
str
) – output file extension name, available extensions: .nii.gz, .nii, .png.resample (
bool
) – whether to resample before saving the data array. if saving PNG format image, based on the spatial_shape from metadata. if saving NIfTI format image, based on the original_affine from metadata.mode (
Union
[GridSampleMode
,InterpolateMode
,str
]) –This option is used when
resample = True
. Defaults to"nearest"
. NIfTI files {
"bilinear"
,"nearest"
} Interpolation mode to calculate output values. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 NIfTI files {
 PNG files {
"nearest"
,"linear"
,"bilinear"
,"bicubic"
,"trilinear"
,"area"
} The interpolation mode. See also: https://pytorch.org/docs/stable/nn.functional.html#interpolate
 PNG files {
padding_mode (
Union
[GridSamplePadMode
,str
]) –This option is used when
resample = True
. Defaults to"border"
. NIfTI files {
"zeros"
,"border"
,"reflection"
} Padding mode for outside grid values. See also: https://pytorch.org/docs/stable/nn.functional.html#gridsample
 NIfTI files {
 PNG files
This option is ignored.
scale (
Optional
[int
]) – {255
,65535
} postprocess data by clipping to [0, 1] and scaling [0, 255] (uint8) or [0, 65535] (uint16). Default is None to disable scaling. it’s used for PNG format only.dtype (
Union
[dtype
,type
,None
]) – data type during resampling computation. Defaults tonp.float64
for best precision. if None, use the data type of input data. To be compatible with other modules, the output data type is alwaysnp.float32
. it’s used for NIfTI format only.output_dtype (
Union
[dtype
,type
,None
]) – data type for saving data. Defaults tonp.float32
. it’s used for NIfTI format only.save_batch (
bool
) – whether the import image is a batch data, default to False. usually pretransforms run for channel first data, while posttransforms run for batch data.allow_missing_keys (
bool
) – don’t raise exception if key is missing.squeeze_end_dims (
bool
) – if True, any trailing singleton dimensions will be removed (after the channel has been moved to the end). So if input is (C,H,W,D), this will be altered to (H,W,D,C), and then if C==1, it will be saved as (H,W,D). If D also ==1, it will be saved as (H,W). If false, image will always be saved as (H,W,D,C). it’s used for NIfTI format only.data_root_dir (
str
) – if not empty, it specifies the beginning parts of the input file’s absolute path. it’s used to compute input_file_rel_path, the relative path to the file from data_root_dir to preserve folder structure when saving in case there are files in different folders with the same file names. for example: input_file_name: /foo/bar/test1/image.nii, output_postfix: seg output_ext: nii.gz output_dir: /output, data_root_dir: /foo/bar, output will be: /output/test1/image/image_seg.nii.gzprint_log (
bool
) – whether to print log about the saved file path, etc. default to True.

__call__
(data)[source]¶ data
often comes from an iteration over an iterable, such astorch.utils.data.Dataset
.To simplify the input validations, this method assumes:
data
is a Python dictionarydata[key]
is a Numpy ndarray, PyTorch Tensor or string, wherekey
is an element ofself.keys
, the data shape can be:string data without shape, LoadImaged transform expects file paths
most of the preprocessing transforms expect:
(num_channels, spatial_dim_1[, spatial_dim_2, ...])
, except that AddChanneld expects (spatial_dim_1[, spatial_dim_2, …]) and AsChannelFirstd expects (spatial_dim_1[, spatial_dim_2, …], num_channels)most of the postprocessing transforms expect
(batch_size, num_channels, spatial_dim_1[, spatial_dim_2, ...])
the channel dimension is not omitted even if number of channels is one
 Raises
NotImplementedError – When the subclass does not override this method.
 Returns
An updated dictionary version of
data
by applying the transform.
Postprocessing (Dict)¶
Activationsd¶

class
monai.transforms.
Activationsd
(keys, sigmoid=False, softmax=False, other=None, allow_missing_keys=False)[source]¶ Dictionarybased wrapper of
monai.transforms.AddActivations
. Add activation layers to the input data specified by keys. Parameters
keys (
Union
[Collection
[Hashable
],Hashable
]) – keys of the corresponding items to model output and label. See also:monai.transforms.compose.MapTransform
sigmoid (
Union
[Sequence
[bool
],bool
]) – whether to execute sigmoid function on model output before transform. it also can be a sequence of bool, each element corresponds to a key inkeys
.softmax (
Union
[Sequence
[bool
],bool
]) – whether to execute softmax function on model output before transform. it also can be a sequence of bool, each element corresponds to a key inkeys
.other (
Union
[Sequence
[Callable
],Callable
,None
]) – callable function to execute other activation layers, for example: other = torch.tanh. it also can be a sequence of Callable, each element corresponds to a key inkeys
.allow_missing_keys (
bool
) – don’t raise exception if key is missing.