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()

DomainAdapterclass

DomainAdapter(
    transformer: TransformerInterface,
    ref_img: np.ndarray,
    color_conversions: tuple[None, None] = (None, None)
)

Parameters

NameTypeDefaultDescription
transformerTransformerInterface--
ref_imgnp.ndarray--
color_conversionstuple[None, None](None, None)-

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()

adapt_pixel_distributionfunction

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

Adapt the pixel distribution of an image to match a reference image. This function adapts the pixel distribution of an image to match a reference image using a specified transformation type and weight.

Parameters

NameTypeDefaultDescription
imgnp.ndarray-The input image to be adapted.
refnp.ndarray-The reference image.
transform_type
One of:
  • 'pca'
  • 'standard'
  • 'minmax'
-The type of transformation to use.
weightfloat-The weight of the transformation.

Returns

  • np.ndarray: The adapted image.

apply_histogramfunction

apply_histogram(
    img: np.ndarray,
    reference_image: np.ndarray,
    blend_ratio: float
)

Apply histogram matching to an input image using a reference image and blend the result. 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.

Parameters

NameTypeDefaultDescription
imgnp.ndarray-The input image to be transformed. Can be either grayscale or RGB. Supported dtypes: uint8, float32 (values should be in [0, 1] range).
reference_imagenp.ndarray-The reference image used for histogram matching. Should have the same number of channels as the input image. Supported dtypes: uint8, float32 (values should be in [0, 1] range).
blend_ratiofloat-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

  • np.ndarray: The transformed image after histogram matching and blending.

Notes

- 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 @clipped and @preserve_channel_dim decorators ensure the output is within the valid range and maintains the original number of dimensions.

fourier_domain_adaptationfunction

fourier_domain_adaptation(
    img: np.ndarray,
    target_img: np.ndarray,
    beta: float
)

Apply Fourier Domain Adaptation to the input image using a target image. 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.

Parameters

NameTypeDefaultDescription
imgnp.ndarray-The source image to be adapted. Can be grayscale or RGB.
target_imgnp.ndarray-The target image used as a reference for adaptation. Should have the same dimensions as the source image.
betafloat-The adaptation strength, typically in the range [0, 1]. Higher values result in stronger adaptation towards the target image's style.

Returns

  • np.ndarray: The adapted image with the same shape and type as the input image.

Example

>>> 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

Notes

- Both input images are converted to float32 for processing. - The function handles both grayscale (2D) and color (3D) images. - For grayscale images, an extra dimension is added to facilitate uniform processing. - The adaptation is performed channel-wise for color images. - The output is clipped to the valid range and preserves the original number of channels.

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

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--

match_histogramsfunction

match_histograms(
    image: np.ndarray,
    reference: np.ndarray
)

Adjust an image so that its cumulative histogram matches that of another. The adjustment is applied separately for each channel.

Parameters

NameTypeDefaultDescription
imagenp.ndarray-Input image. Can be gray-scale or in color.
referencenp.ndarray-Image to match histogram of. Must have the same number of channels as image.

Returns

  • np.ndarray: Transformed input image.