image
Module for differentiable image-transform primitives.
These utilities are used as building blocks for image-similarity privacy losses
(see stainedglass_core.loss.image_similarity) but are kept
generic so callers can compose them for other image-domain transforms.
Functions:
| Name | Description |
|---|---|
gamma_correct |
Apply gamma correction |
gaussian_blur_2d |
Apply a 2D Gaussian blur via separable 1D convolutions. |
haar_dwt |
Single-level 2D Haar discrete wavelet transform. |
haar_idwt |
Inverse single-level 2D Haar DWT. |
minmax_normalize_per_image |
Min-max normalize each image in a batch into the [0, 1] range. |
rgb_to_grayscale_luma |
Convert an RGB image tensor to a single-channel luma tensor using BT.709 weights. |
sobel_magnitude |
Compute the Sobel gradient magnitude of a single-channel image. |
standardize_sigmoid |
Per-image z-score normalize each image then apply sigmoid to land in (0, 1). |
wavelet_denoise_haar |
Denoise via single-level Haar DWT soft-thresholding of detail subbands. |
gamma_correct
¶
Apply gamma correction x ** gamma with a floor that keeps the gradient finite at zero.
For gamma < 1 the gradient gamma * x ** (gamma - 1) diverges as x -> 0. The
input_floor clamp keeps that gradient bounded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Non-negative tensor. |
required |
|
float
|
Gamma exponent. |
required |
|
float
|
Floor applied to |
1e-06
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tensor with gamma applied elementwise. |
gaussian_blur_2d
¶
Apply a 2D Gaussian blur via separable 1D convolutions.
The kernel size is set to 2 * ceil(3 * sigma) + 1 so the truncation error is
negligible. The 1D kernel is normalized to sum to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor of shape |
required |
|
float
|
Standard deviation of the Gaussian kernel in pixels. Must be positive and finite. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Blurred tensor of the same shape as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
haar_dwt
¶
Single-level 2D Haar discrete wavelet transform.
The input's last two dimensions must be even. The output contains the approximation
subband LL plus three detail subbands LH, HL, HH at half resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]
|
Tuple |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the last two dimensions are not even. |
haar_idwt
¶
Inverse single-level 2D Haar DWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Approximation subband of shape |
required |
|
Tensor
|
Horizontal-detail subband of the same shape. |
required |
|
Tensor
|
Vertical-detail subband of the same shape. |
required |
|
Tensor
|
Diagonal-detail subband of the same shape. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Reconstructed tensor of shape |
minmax_normalize_per_image
¶
Min-max normalize each image in a batch into the [0, 1] range.
The per-image min and max are computed over all non-batch dimensions and are
detached from the graph so gradients flow only through the per-pixel rescaling, not
through the extrema. This keeps the gradient signal smooth instead of spiking on the
single pixels that happen to set the min/max.
Distinct from
batchwise_min_max_normalize,
which does not detach the extrema and emits NaN when the per-image range is zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor of shape |
required |
|
float
|
Floor applied to the |
0.01
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tensor of the same shape with each image rescaled to [0, 1]. Images that are |
torch.Tensor
|
constant across all pixels are rescaled using |
torch.Tensor
|
will be close to zero. |
rgb_to_grayscale_luma
¶
sobel_magnitude
¶
Compute the Sobel gradient magnitude of a single-channel image.
Apply horizontal and vertical 3x3 Sobel filters and return sqrt(Gx^2 + Gy^2 + eps).
The eps inside the square root keeps the gradient finite where the magnitude
approaches zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Single-channel image tensor of shape |
required |
|
float
|
Epsilon added inside the sqrt to keep the backward gradient finite at zero magnitude. |
1e-06
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Edge magnitude tensor of shape |
standardize_sigmoid
¶
Per-image z-score normalize each image then apply sigmoid to land in (0, 1).
Mean and std are computed per-image over all non-batch dimensions and detached from the graph so gradients flow only through the per-pixel rescaling. Compared to min-max normalization, standardization uses the first and second moments of the entire image instead of two extremal pixels, producing a smoother gradient signal that is less dominated by outliers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor of shape |
required |
|
float
|
Floor applied to the per-image standard deviation to keep the backward gradient finite. |
1e-05
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Tensor of the same shape with values in (0, 1). |
wavelet_denoise_haar
¶
Denoise via single-level Haar DWT soft-thresholding of detail subbands.
Apply the Haar DWT, soft-threshold each of the three detail subbands (LH,
HL, HH) at threshold, keep the approximation subband LL unchanged,
and invert the transform. This suppresses small high-frequency coefficients
(typically noise) while preserving coarse structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Tensor of shape |
required |
|
float
|
Soft-threshold value applied to the detail coefficients. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Denoised tensor of the same shape as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the last two dimensions are not even. |