albumentations.augmentations.mixing.domain_adaptation_functional


Functional implementations for domain adaptation image transformations. This module provides low-level functions and classes for performing domain adaptation between images. It includes implementations for histogram matching, Fourier domain adaptation, and pixel distribution matching with various normalization techniques.

BaseScalerclass

BaseScaler()

MinMaxScalerclass

MinMaxScaler(
    feature_range: tuple[float, float] = (0.0, 1.0)
)

Parameters

NameTypeDefaultDescription
feature_rangetuple[float, float](0.0, 1.0)-

StandardScalerclass

StandardScaler()

TransformerInterfaceclass

TransformerInterface()

DomainAdapterclass

DomainAdapter(
    transformer: TransformerInterface,
    ref_img: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    color_conversions: tuple[None, None] = (None, None)
)

Parameters

NameTypeDefaultDescription
transformerTransformerInterface--
ref_img
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
color_conversionstuple[None, None](None, None)-

adapt_pixel_distributionfunction

adapt_pixel_distribution(
    img: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    ref: np.ndarray,
    transform_type: Literal['pca', 'standard', 'minmax'],
    weight: float
)

Adapt input pixel distribution to match a reference using PCA, standard, or minmax. weight controls blend with original; same shape and dtype as input. This function adapts the pixel distribution of an image to match a reference image using a specified transformation type and weight. Args: img (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): The input image to be adapted. ref (np.ndarray): The reference image. transform_type (Literal['pca', 'standard', 'minmax']): The type of transformation to use. weight (float): The weight of the transformation. Returns: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]: The adapted image. Raises: ValueError: If the input image and reference image have different dtypes or numbers of channels.

Parameters

NameTypeDefaultDescription
img
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
refnp.ndarray--
transform_type
One of:
  • 'pca'
  • 'standard'
  • 'minmax'
--
weightfloat--

low_freq_mutatefunction

low_freq_mutate(
    amp_src: np.ndarray,
    amp_trg: np.ndarray,
    beta: float
)

Parameters

NameTypeDefaultDescription
amp_srcnp.ndarray--
amp_trgnp.ndarray--
betafloat--

fourier_domain_adaptationfunction

fourier_domain_adaptation(
    img: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    target_img: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    beta: float
)

Fourier Domain Adaptation: swap low-frequency amplitude with target, keep phase. beta controls strength. Same shape as input. This function performs domain adaptation in the frequency domain by modifying the amplitude spectrum of the source image based on the target image's amplitude spectrum. It preserves the phase information of the source image, which helps maintain its content while adapting its style to match the target image. Args: img (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): The source image to be adapted. Can be grayscale or RGB. target_img (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): The target image used as a reference for adaptation. Should have the same dimensions as the source image. beta (float): The adaptation strength, typically in the range [0, 1]. Higher values result in stronger adaptation towards the target image's style. Returns: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]: The adapted image with the same shape and type as the input image. Raises: ValueError: If the source and target images have different shapes. Note: - Both input images are converted to float32 for processing. - The function handles both grayscale (2D) and color (3D) images. - Grayscale inputs use an explicit singleton channel dimension. - The adaptation is performed channel-wise for color images. - The output is clipped to the valid range and preserves the original number of channels. The adaptation process involves the following steps for each channel: 1. Compute the 2D Fourier Transform of both source and target images. 2. Shift the zero frequency component to the center of the spectrum. 3. Extract amplitude and phase information from the source image's spectrum. 4. Mutate the source amplitude using the target amplitude and the beta parameter. 5. Combine the mutated amplitude with the original phase. 6. Perform the inverse Fourier Transform to obtain the adapted channel. The `low_freq_mutate` function (not shown here) is responsible for the actual amplitude mutation, focusing on low-frequency components which carry style information. Examples: >>> import numpy as np >>> import albumentations as A >>> source_img = np.random.rand(100, 100, 3).astype(np.float32) >>> target_img = np.random.rand(100, 100, 3).astype(np.float32) >>> adapted_img = A.fourier_domain_adaptation(source_img, target_img, beta=0.5) >>> assert adapted_img.shape == source_img.shape References: FDA: Fourier Domain Adaptation for Semantic Segmentation: Yang and Soatto, 2020, CVPR https://openaccess.thecvf.com/content_CVPR_2020/papers/Yang_FDA_Fourier_Domain_Adaptation_for_Semantic_Segmentation_CVPR_2020_paper.pdf

Parameters

NameTypeDefaultDescription
img
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
target_img
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
betafloat--

apply_histogramfunction

apply_histogram(
    img: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    reference_image: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    blend_ratio: float
)

Match input histogram to a reference, then blend with original. blend_ratio in [0,1] controls strength. Same shape and dtype as input. This function performs histogram matching between the input image and a reference image, then blends the result with the original input image based on the specified blend ratio. Args: img (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): The input image to be transformed. Can be either grayscale or RGB. Supported dtypes: uint8, float16, float32 (floating values are in [0, 1]). reference_image (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): The reference image used for histogram matching. Should have the same number of channels as the input image. Supported dtypes: uint8, float16, float32 (floating values are in [0, 1]). blend_ratio (float): The ratio for blending the matched image with the original image. Should be in the range [0, 1], where 0 means no change and 1 means full histogram matching. Returns: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]: The transformed image after histogram matching and blending. The output will have the same shape and dtype as the input image. Supported image types: - Grayscale images: `(H, W, 1)` arrays - RGB images: `(H, W, 3)` arrays - Multispectral images: `(H, W, C)` arrays with C greater than 3 Note: - If the input and reference images have different sizes, the reference image will be resized to match the input image's dimensions. - The function uses a custom implementation of histogram matching based on OpenCV and NumPy. - The function clips its output to the valid range and retains the input shape.

Parameters

NameTypeDefaultDescription
img
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
reference_image
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
blend_ratiofloat--

match_histogramsfunction

match_histograms(
    image: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32],
    reference: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]
)

Adjust image so its CDF matches the reference, per channel. Uses uint8 CDFs and restores the input dtype; the reference is quantized separately. Reference may be resized. The adjustment is applied separately for each channel. Args: image (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): Input image. Can be gray-scale or in color. reference (NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]): Image to match histogram of. Must have the same number of channels as image. channel_axis (int | None): If None, the image is assumed to be a grayscale (single channel) image. Otherwise, this indicates which axis of the array corresponds to channels. Returns: NDArray[np.uint8] | NDArray[np.float16] | NDArray[np.float32]: Transformed input image. Raises: ValueError: Thrown when the number of channels in the input image and the reference differ.

Parameters

NameTypeDefaultDescription
image
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--
reference
One of:
  • NDArray[np.uint8]
  • NDArray[np.float16]
  • NDArray[np.float32]
--