AlbumentationsExplore
DocumentationExploreAdoptionPricingBlog
GitHub

StochasticConvolution

Targets:
image
volume
Image Types:uint8, float32

Apply a stochastic identity-centered convolution kernel with configurable spectral strength and channel sharing for images and volumes.

The kernel is a discrete impulse plus a zero-mean Gaussian field. kernel_range controls the odd side length K (the spectral resolution in PRIME), while strength_range controls the perturbation energy. The random field is scaled by strength / K so the expected perturbation energy remains comparable across kernel sizes.

Arguments
kernel_range
tuple[int, int]
[3, 7]

Inclusive odd range for the square kernel side length. Values must be greater than or equal to 3. Default: (3, 7).

strength_range
tuple[float, float]
[0, 1]

Non-negative range for the random field strength. Zero is an exact identity. Default: (0.0, 1.0).

per_channel
bool
false

If True, sample an independent kernel for each channel. If False, share one kernel across all channels. Default: False.

border_mode
0 | 1 | 2 | 4
4

OpenCV border policy. Supported values are constant, replicate, reflect, and reflect-101; wrap is rejected because it is not supported by the convolution backend. Default: cv2.BORDER_REFLECT_101.

p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>> image = np.random.default_rng(137).random((128, 128, 3), dtype=np.float32)
>>> transform = A.Compose(
...     [
...         A.StochasticConvolution(
...             kernel_range=(3, 7),
...             strength_range=(0.05, 0.25),
...             border_mode=cv2.BORDER_REFLECT_101,
...             p=1.0,
...         ),
...     ],
...     seed=137,
... )
>>> transformed = transform(image=image)["image"]

Use `per_channel=True` for independent spectral perturbations, or `strength_range=(0.0, 0.0)` for an
exact identity while keeping the transform in a pipeline.
Notes
  • The random weights are not normalized or mean-subtracted. They may be signed, and the realized DC gain is the sampled kernel sum (with expected gain 1).
  • cv2.BORDER_CONSTANT uses zero padding, matching the PRIME reference implementation. The default reflect-101 border is the project-wide image-friendly choice.
  • One kernel realization is sampled per transform invocation and reused for every image in a batch and every depth slice in a volume.
  • Applied configuration records the sampled scalar values for the range fields and remains runnable after JSON transport. The in-memory replay path retains the realized kernel through transform parameters.
References
  • PRIME issuehttps://github.com/albumentations-team/AlbumentationsX/issues/330
  • PRIME randomized-filter constructionhttps://github.com/amodas/PRIME-augmentations/blob/main/utils/rand_filter.py