Your ad could be here - Reach CV/ML engineers
Contact for advertisingContactInterested in advertising?
Contact usStay updated
News & Insightsalbumentations.augmentations.transforms3d.transforms
Module containing 3D transformation classes for volumetric data augmentation. This module provides a collection of transformation classes designed specifically for 3D volumetric data (such as medical CT/MRI scans). These transforms can manipulate properties such as spatial dimensions, apply dropout effects, and perform symmetry operations on 3D volumes, masks, and keypoints. Each transformation inherits from a base transform interface and implements specific 3D augmentation logic.
Members
- classBaseCropAndPad3D
- classBasePad3D
- classCenterCrop3D
- classCoarseDropout3D
- classCubicSymmetry
- classPad3D
- classPadIfNeeded3D
- classRandomCrop3D
BaseCropAndPad3Dclass
Base class for 3D transforms that need both cropping and padding. This class serves as a foundation for transforms that combine cropping and padding operations on 3D volumetric data. It provides functionality for calculating padding parameters, applying crop and pad operations to volumes, masks, and handling keypoint coordinate shifts.
Parameters
Name | Type | Default | Description |
---|---|---|---|
pad_if_needed | bool | - | Whether to pad if the volume is smaller than target dimensions |
fill | One of:
| - | Value to fill the padded voxels for volume |
fill_mask | One of:
| - | Value to fill the padded voxels for mask |
pad_position | One of:
| - | How to distribute padding when needed "center" - equal amount on both sides, "random" - random distribution |
p | float | 1.0 | Probability of applying the transform. Default: 1.0 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Example of a custom crop transform inheriting from BaseCropAndPad3D
>>> class CustomFixedCrop3D(A.BaseCropAndPad3D):
... def __init__(self, crop_size: tuple[int, int, int] = (8, 64, 64), *args, **kwargs):
... super().__init__(
... pad_if_needed=True,
... fill=0,
... fill_mask=0,
... pad_position="center",
... *args,
... **kwargs
... )
... self.crop_size = crop_size
...
... def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, Any]) -> dict[str, Any]:
... # Get the volume shape
... volume = data["volume"]
... z, h, w = volume.shape[:3]
... target_z, target_h, target_w = self.crop_size
...
... # Check if padding is needed and calculate parameters
... pad_params = self._get_pad_params(
... image_shape=(z, h, w),
... target_shape=self.crop_size,
... )
...
... # Update dimensions if padding is applied
... if pad_params is not None:
... z = z + pad_params["pad_front"] + pad_params["pad_back"]
... h = h + pad_params["pad_top"] + pad_params["pad_bottom"]
... w = w + pad_params["pad_left"] + pad_params["pad_right"]
...
... # Calculate fixed crop coordinates - always start at position (0,0,0)
... crop_coords = (0, target_z, 0, target_h, 0, target_w)
...
... return {
... "crop_coords": crop_coords,
... "pad_params": pad_params,
... }
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[20, 30, 5], [60, 70, 8]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Use the custom transform in a pipeline
>>> transform = A.Compose([
... CustomFixedCrop3D(
... crop_size=(8, 64, 64), # Crop first 8x64x64 voxels (with padding if needed)
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> cropped_volume = transformed["volume"] # Shape: (8, 64, 64)
>>> cropped_mask3d = transformed["mask3d"] # Shape: (8, 64, 64)
>>> cropped_keypoints = transformed["keypoints"] # Keypoints shifted relative to crop
>>> cropped_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
Notes
This is a base class and not intended to be used directly. Use its derivatives like CenterCrop3D or RandomCrop3D instead, or create a custom transform by inheriting from this class.
BasePad3Dclass
Base class for 3D padding transforms. This class serves as a foundation for all 3D transforms that perform padding operations on volumetric data. It provides common functionality for padding 3D volumes, masks, and processing 3D keypoints during padding operations. The class handles different types of padding values (scalar or per-channel) and provides separate fill values for volumes and masks.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fill | One of:
| 0 | Value to fill the padded voxels for volumes. Can be a single value for all channels or a tuple of values per channel. |
fill_mask | One of:
| 0 | Value to fill the padded voxels for 3D masks. Can be a single value for all channels or a tuple of values per channel. |
p | float | 1.0 | Probability of applying the transform. Default: 1.0. |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Example of a custom padding transform inheriting from BasePad3D
>>> class CustomPad3D(A.BasePad3D):
... def __init__(self, padding_size: tuple[int, int, int] = (5, 5, 5), *args, **kwargs):
... super().__init__(*args, **kwargs)
... self.padding_size = padding_size
...
... def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, Any]) -> dict[str, Any]:
... # Create symmetric padding: same amount on all sides of each dimension
... pad_d, pad_h, pad_w = self.padding_size
... padding = (pad_d, pad_d, pad_h, pad_h, pad_w, pad_w)
... return {"padding": padding}
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[20, 30, 5], [60, 70, 8]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Use the custom transform in a pipeline
>>> transform = A.Compose([
... CustomPad3D(
... padding_size=(2, 10, 10),
... fill=0,
... fill_mask=1,
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_volume = transformed["volume"] # Shape: (14, 120, 120)
>>> transformed_mask3d = transformed["mask3d"] # Shape: (14, 120, 120)
>>> transformed_keypoints = transformed["keypoints"] # Keypoints shifted by padding offsets
>>> transformed_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
Notes
This is a base class and not intended to be used directly. Use its derivatives like Pad3D or PadIfNeeded3D instead, or create a custom padding transform by inheriting from this class.
CenterCrop3Dclass
Crop the center of 3D volume.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, int, int] | - | Desired output size of the crop in format (depth, height, width) |
pad_if_needed | bool | False | Whether to pad if the volume is smaller than desired crop size. Default: False |
fill | One of:
| 0 | Padding value for image if pad_if_needed is True. Default: 0 |
fill_mask | One of:
| 0 | Padding value for mask if pad_if_needed is True. Default: 0 |
p | float | 1.0 | probability of applying the transform. Default: 1.0 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[100, 100, 10], [150, 150, 15]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Create the transform - crop to 16x128x128 from center
>>> transform = A.Compose([
... A.CenterCrop3D(
... size=(16, 128, 128), # Output size (depth, height, width)
... pad_if_needed=True, # Pad if input is smaller than crop size
... fill=0, # Fill value for volume padding
... fill_mask=1, # Fill value for mask padding
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> cropped_volume = transformed["volume"] # Shape: (16, 128, 128)
>>> cropped_mask3d = transformed["mask3d"] # Shape: (16, 128, 128)
>>> cropped_keypoints = transformed["keypoints"] # Keypoints shifted relative to center crop
>>> cropped_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
>>>
>>> # Example with a small volume that requires padding
>>> small_volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8)
>>> small_transform = A.Compose([
... A.CenterCrop3D(
... size=(16, 128, 128),
... pad_if_needed=True, # Will pad since the input is smaller
... fill=0,
... p=1.0
... )
... ])
>>> small_result = small_transform(volume=small_volume)
>>> padded_and_cropped = small_result["volume"] # Shape: (16, 128, 128), padded to size
Notes
If you want to perform cropping only in the XY plane while preserving all slices along the Z axis, consider using CenterCrop instead. CenterCrop will apply the same XY crop to each slice independently, maintaining the full depth of the volume.
CoarseDropout3Dclass
CoarseDropout3D randomly drops out cuboid regions from a 3D volume and optionally, the corresponding regions in an associated 3D mask, to simulate occlusion and varied object sizes found in real-world volumetric data.
Parameters
Name | Type | Default | Description |
---|---|---|---|
num_holes_range | tuple[int, int] | (1, 1) | Range (min, max) for the number of cuboid regions to drop out. Default: (1, 1) |
hole_depth_range | tuple[float, float] | (0.1, 0.2) | Range (min, max) for the depth of dropout regions as a fraction of the volume depth (between 0 and 1). Default: (0.1, 0.2) |
hole_height_range | tuple[float, float] | (0.1, 0.2) | Range (min, max) for the height of dropout regions as a fraction of the volume height (between 0 and 1). Default: (0.1, 0.2) |
hole_width_range | tuple[float, float] | (0.1, 0.2) | Range (min, max) for the width of dropout regions as a fraction of the volume width (between 0 and 1). Default: (0.1, 0.2) |
fill | One of:
| 0 | Value for the dropped voxels. Can be: - int or float: all channels are filled with this value - tuple: tuple of values for each channel Default: 0 |
fill_mask | One of:
| None | Fill value for dropout regions in the 3D mask. If None, mask regions corresponding to volume dropouts are unchanged. Default: None |
p | float | 0.5 | Probability of applying the transform. Default: 0.5 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> aug = A.CoarseDropout3D(
... num_holes_range=(3, 6),
... hole_depth_range=(0.1, 0.2),
... hole_height_range=(0.1, 0.2),
... hole_width_range=(0.1, 0.2),
... fill=0,
... p=1.0
... )
>>> transformed = aug(volume=volume, mask3d=mask3d)
>>> transformed_volume, transformed_mask3d = transformed["volume"], transformed["mask3d"]
Notes
- The actual number and size of dropout regions are randomly chosen within the specified ranges. - All values in hole_depth_range, hole_height_range and hole_width_range must be between 0 and 1. - If you want to apply dropout only in the XY plane while preserving the full depth dimension, consider using CoarseDropout instead. CoarseDropout will apply the same rectangular dropout to each slice independently, effectively creating cylindrical dropout regions that extend through the entire depth of the volume.
CubicSymmetryclass
Applies a random cubic symmetry transformation to a 3D volume. This transform is a 3D extension of D4. While D4 handles the 8 symmetries of a square (4 rotations x 2 reflections), CubicSymmetry handles all 48 symmetries of a cube. Like D4, this transform does not create any interpolation artifacts as it only remaps voxels from one position to another without any interpolation. The 48 transformations consist of: - 24 rotations (orientation-preserving): * 4 rotations around each face diagonal (6 face diagonals x 4 rotations = 24) - 24 rotoreflections (orientation-reversing): * Reflection through a plane followed by any of the 24 rotations For a cube, these transformations preserve: - All face centers (6) - All vertex positions (8) - All edge centers (12) works with 3D volumes and masks of the shape (D, H, W) or (D, H, W, C)
Parameters
Name | Type | Default | Description |
---|---|---|---|
p | float | 1.0 | Probability of applying the transform. Default: 1.0 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> transform = A.CubicSymmetry(p=1.0)
>>> transformed = transform(volume=volume, mask3d=mask3d)
>>> transformed_volume = transformed["volume"]
>>> transformed_mask3d = transformed["mask3d"]
Notes
- This transform is particularly useful for data augmentation in 3D medical imaging, crystallography, and voxel-based 3D modeling where the object's orientation is arbitrary. - All transformations preserve the object's chirality (handedness) when using pure rotations (indices 0-23) and invert it when using rotoreflections (indices 24-47).
Pad3Dclass
Pad the sides of a 3D volume by specified number of voxels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
padding | One of:
| - | Padding values. Can be: * int - pad all sides by this value * tuple[int, int, int] - symmetric padding (depth, height, width) where each value is applied to both sides of the corresponding dimension * tuple[int, int, int, int, int, int] - explicit padding per side in order: (depth_front, depth_back, height_top, height_bottom, width_left, width_right) |
fill | One of:
| 0 | Padding value for image |
fill_mask | One of:
| 0 | Padding value for mask |
p | float | 1.0 | probability of applying the transform. Default: 1.0. |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[20, 30, 5], [60, 70, 8]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Create the transform with symmetric padding
>>> transform = A.Compose([
... A.Pad3D(
... padding=(2, 5, 10), # (depth, height, width) applied symmetrically
... fill=0,
... fill_mask=1,
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> padded_volume = transformed["volume"] # Shape: (14, 110, 120)
>>> padded_mask3d = transformed["mask3d"] # Shape: (14, 110, 120)
>>> padded_keypoints = transformed["keypoints"] # Keypoints shifted by padding
>>> padded_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
Notes
Input volume should be a numpy array with dimensions ordered as (z, y, x) or (depth, height, width), with optional channel dimension as the last axis.
PadIfNeeded3Dclass
Pads the sides of a 3D volume if its dimensions are less than specified minimum dimensions. If the pad_divisor_zyx is specified, the function additionally ensures that the volume dimensions are divisible by these values.
Parameters
Name | Type | Default | Description |
---|---|---|---|
min_zyx | One of:
| None | Minimum desired size as (depth, height, width). Ensures volume dimensions are at least these values. If not specified, pad_divisor_zyx must be provided. |
pad_divisor_zyx | One of:
| None | If set, pads each dimension to make it divisible by corresponding value in format (depth_div, height_div, width_div). If not specified, min_zyx must be provided. |
position | One of:
| center | Position where the volume is to be placed after padding. Default is 'center'. |
fill | One of:
| 0 | Value to fill the border voxels for volume. Default: 0 |
fill_mask | One of:
| 0 | Value to fill the border voxels for masks. Default: 0 |
p | float | 1.0 | Probability of applying the transform. Default: 1.0 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (10, 100, 100), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[20, 30, 5], [60, 70, 8]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Create a transform with both min_zyx and pad_divisor_zyx
>>> transform = A.Compose([
... A.PadIfNeeded3D(
... min_zyx=(16, 128, 128), # Minimum size (depth, height, width)
... pad_divisor_zyx=(8, 16, 16), # Make dimensions divisible by these values
... position="center", # Center the volume in the padded space
... fill=0, # Fill value for volume
... fill_mask=1, # Fill value for mask
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> padded_volume = transformed["volume"] # Shape: (16, 128, 128)
>>> padded_mask3d = transformed["mask3d"] # Shape: (16, 128, 128)
>>> padded_keypoints = transformed["keypoints"] # Keypoints shifted by padding
>>> padded_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
Notes
Input volume should be a numpy array with dimensions ordered as (z, y, x) or (depth, height, width), with optional channel dimension as the last axis.
RandomCrop3Dclass
Crop random part of 3D volume.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, int, int] | - | Desired output size of the crop in format (depth, height, width) |
pad_if_needed | bool | False | Whether to pad if the volume is smaller than desired crop size. Default: False |
fill | One of:
| 0 | Padding value for image if pad_if_needed is True. Default: 0 |
fill_mask | One of:
| 0 | Padding value for mask if pad_if_needed is True. Default: 0 |
p | float | 1.0 | probability of applying the transform. Default: 1.0 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[100, 100, 10], [150, 150, 15]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Create the transform with random crop and padding if needed
>>> transform = A.Compose([
... A.RandomCrop3D(
... size=(16, 128, 128), # Output size (depth, height, width)
... pad_if_needed=True, # Pad if input is smaller than crop size
... fill=0, # Fill value for volume padding
... fill_mask=1, # Fill value for mask padding
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> cropped_volume = transformed["volume"] # Shape: (16, 128, 128)
>>> cropped_mask3d = transformed["mask3d"] # Shape: (16, 128, 128)
>>> cropped_keypoints = transformed["keypoints"] # Keypoints shifted relative to random crop
>>> cropped_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged
Notes
If you want to perform random cropping only in the XY plane while preserving all slices along the Z axis, consider using RandomCrop instead. RandomCrop will apply the same XY crop to each slice independently, maintaining the full depth of the volume.