Data augmentation
Image Transformations
- class InnerEye.ML.augmentations.image_transforms.AddGaussianNoise(p_apply: float, std: float)[source]
- class InnerEye.ML.augmentations.image_transforms.ElasticTransform(sigma: float, alpha: float, p_apply: float)[source]
Elastic deformation of images as described in [Simard2003].
[Simard2003]Simard, Steinkraus and Platt, “Best Practices for Convolutional Neural Networks applied to Visual Document Analysis”, in Proc. of the International Conference on Document Analysis and Recognition, 2003. https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.160.8494&rep=rep1&type=pdf
- class InnerEye.ML.augmentations.image_transforms.ExpandChannels[source]
Transforms an image with 1 channel to an image with 3 channels by copying pixel intensities of the image along the 1st dimension.
- class InnerEye.ML.augmentations.image_transforms.RandomGamma(scale: Tuple[float, float])[source]
Custom function to apply a random gamma transform within a specified range of possible gamma value. See documentation of [adjust_gamma](https://pytorch.org/vision/0.8/transforms.html#torchvision.transforms.functional.adjust_gamma) for more details.
Transformation Pipeline
- class InnerEye.ML.augmentations.transform_pipeline.ImageTransformationPipeline(transforms: Union[Callable, List[Callable]], use_different_transformation_per_channel: bool = False)[source]
This class is the base class to classes built to define data augmentation transformations for 3D or 2D image inputs (tensor or PIL.Image). In the case of 3D images, the transformations are applied slice by slices along the Z dimension (same transformation applied for each slice). The transformations are applied channel by channel, the user can specify whether to apply the same transformation to each channel (no random shuffling) or whether each channel should use a different transformation (random parameters of transforms shuffled for each channel).
- transform_image(image: Union[Image, Tensor]) Tensor[source]
Main function to apply the transformation pipeline to either slice by slice on one 3D-image or on the 2D image.
Note for 3D images: Assumes the same transformations have to be applied on each 2D-slice along the Z-axis. Assumes the Z axis is the first dimension.
- Parameters:
image – batch of tensor images of size [C, Z, Y, X] or batch of 2D images as PIL Image
- InnerEye.ML.augmentations.transform_pipeline.create_transforms_from_config(config: CfgNode, apply_augmentations: bool, expand_channels: bool = True) ImageTransformationPipeline[source]
Defines the image transformations pipeline from a config file. It has been designed for Chest X-Ray images but it can be used for other types of images data, type of augmentations to use and strength are expected to be defined in the config. The channel expansion is needed for gray images.
- Parameters:
config – config yaml file fixing strength and type of augmentation to apply
apply_augmentations – if True return transformation pipeline with augmentations. Else, disable augmentations i.e. only resize and center crop the image.
expand_channels – if True the expand channel transformation from InnerEye.ML.augmentations.image_transforms will be added to the transformation passed through the config. This is needed for single channel images as CXR.
Utils
- InnerEye.ML.augmentations.augmentation_for_segmentation_utils.random_crop(sample: Sample, crop_size: Tuple[int, int, int], class_weights: Optional[List[float]] = None) Tuple[Sample, ndarray][source]
Randomly crops images, mask, and labels arrays according to the crop_size argument. The selection of the center is dependant on background probability. By default it does not center on background.
- Parameters:
sample – A set of Image channels, ground truth labels and mask to randomly crop.
crop_size – The size of the crop expressed as a list of 3 ints, one per spatial dimension.
class_weights – A weighting vector with values [0, 1] to influence the class the center crop voxel belongs to (must sum to 1), uniform distribution assumed if none provided.
- Returns:
Tuple item 1: The cropped images, labels, and mask. Tuple item 2: The center that was chosen for the crop, before shifting to be inside of the image. Tuple item 3: The slicers that convert the input image to the chosen crop.
- Raises:
ValueError – If there are shape mismatches among the arguments or if the crop size is larger than the image.
- InnerEye.ML.augmentations.augmentation_for_segmentation_utils.random_select_patch_center(sample: Sample, class_weights: Optional[List[float]] = None) ndarray[source]
Samples a point to use as the coordinates of the patch center. First samples one class among the available classes then samples a center point among the pixels of the sampled class.
- Parameters:
sample – A set of Image channels, ground truth labels and mask to randomly crop.
class_weights – A weighting vector with values [0, 1] to influence the class the center crop voxel belongs to (must sum to 1), uniform distribution assumed if none provided.
- Returns:
numpy int array (3x1) containing patch center spatial coordinates
- InnerEye.ML.augmentations.augmentation_for_segmentation_utils.slicers_for_random_crop(sample: Sample, crop_size: Tuple[int, int, int], class_weights: Optional[List[float]] = None) Tuple[List[slice], ndarray][source]
Computes array slicers that produce random crops of the given crop_size. The selection of the center is dependant on background probability. By default it does not center on background.
- Parameters:
sample – A set of Image channels, ground truth labels and mask to randomly crop.
crop_size – The size of the crop expressed as a list of 3 ints, one per spatial dimension.
class_weights – A weighting vector with values [0, 1] to influence the class the center crop voxel belongs to (must sum to 1), uniform distribution assumed if none provided.
- Returns:
Tuple element 1: The slicers that convert the input image to the chosen crop. Tuple element 2: The indices of the center point of the crop.
- Raises:
ValueError – If there are shape mismatches among the arguments or if the crop size is larger than the image.