PaDIS.pyΒΆ
Source: LION/losses/PaDIS.py
PaDIS patch-based denoising loss and training utilities.
- LION.losses.PaDIS.validate_patch_schedule(patch_sizes, patch_probabilities)[source]ΒΆ
Validate a discrete PaDIS patch-size distribution.
- Parameters:
patch_sizes (sequence of int) β Candidate square patch widths. Every size must be divisible by eight.
patch_probabilities (sequence of float) β Sampling probability corresponding to each patch size. Values must sum to one within numerical tolerance.
- Raises:
ValueError β If the schedule is empty, lengths differ, a size is incompatible with NCSN++ downsampling, or probabilities do not sum to one.
- Return type:
None
- LION.losses.PaDIS.build_position_grid(batch_size, height, width, *, device, dtype)[source]ΒΆ
Create normalised
(x, y)position channels for an image batch.- Parameters:
batch_size (int) β Number of grids to return.
height (int) β Spatial grid dimensions.
width (int) β Spatial grid dimensions.
device (torch.device) β Device on which to allocate the grid.
dtype (torch.dtype) β Floating-point type of the returned coordinates.
- Returns:
Tensor of shape
(batch_size, 2, height, width)spanning[-1, 1]in each spatial direction.- Return type:
torch.Tensor
- LION.losses.PaDIS.zero_pad_images(images, pad_width)[source]ΒΆ
Pad images with zeros on every spatial boundary.
- Parameters:
images (torch.Tensor) β Batched image tensor in
NCHWlayout.pad_width (int) β Number of pixels added to each boundary.
- Returns:
The original tensor when
pad_widthis zero, otherwise a padded tensor preserving batch and channel dimensions.- Return type:
torch.Tensor
- LION.losses.PaDIS.sample_patch_size(patch_sizes, patch_probabilities, *, device)[source]ΒΆ
Draw one patch size from a validated categorical schedule.
- Parameters:
patch_sizes (Sequence[int])
patch_probabilities (Sequence[float])
device (device)
- Return type:
int
- LION.losses.PaDIS.sample_patch_pair(images, positions, patch_size)[source]ΒΆ
Sample aligned image and position patches independently per batch item.
- Parameters:
images (torch.Tensor) β Aligned
NCHWtensors with identical spatial dimensions.positions (torch.Tensor) β Aligned
NCHWtensors with identical spatial dimensions.patch_size (int) β Width and height of each square crop.
- Returns:
image_patch, position_patch β Batched crops sharing the same random top-left coordinate for each input image.
- Return type:
tuple of torch.Tensor
- LION.losses.PaDIS.sample_image_patch_with_position_channels(images, patch_size)[source]ΒΆ
Sample image patches and construct their absolute position channels.
Unlike
sample_patch_pair(), this routine avoids materialising a full position grid and derives coordinates only for sampled pixels.- Parameters:
images (Tensor)
patch_size (int)
- Return type:
tuple[Tensor, Tensor]
- LION.losses.PaDIS.sample_image_patch(images, patch_size)[source]ΒΆ
Sample one independent square crop from every image in a batch.
- Parameters:
images (Tensor)
patch_size (int)
- Return type:
Tensor
- LION.losses.PaDIS.score_from_denoiser(noisy_image_patch, denoised_patch, sigma)[source]ΒΆ
Convert denoiser predictions into a variance-exploding score estimate.
The returned score is
(denoised - noisy) / sigma**2withsigmabroadcast across non-batch dimensions.- Parameters:
noisy_image_patch (Tensor)
denoised_patch (Tensor)
sigma (Tensor)
- Return type:
Tensor
- class LION.losses.PaDIS.PaDISDenoisingLoss(sigma_min=0.002, sigma_max=40.0, sigma_distribution='edm_lognormal_truncated', P_mean=-1.2, P_std=1.2, sigma_data=0.5, reduction='batch_mean_sum', augment_pipe=None)[source]ΒΆ
Bases:
ModuleEDM-style denoising objective used to train PaDIS priors.
- Parameters:
sigma_min (float) β Inclusive training-noise bounds.
sigma_max (float) β Inclusive training-noise bounds.
sigma_distribution ({"edm_lognormal", "edm_lognormal_truncated", "log_uniform"}) β Distribution used to sample per-image noise levels.
P_mean (float) β Location and scale of the EDM log-normal distribution.
P_std (float) β Location and scale of the EDM log-normal distribution.
sigma_data (float) β Assumed standard deviation of clean training data.
reduction ({"batch_mean_sum", "mean"}) β Reduction compatible with the released PaDIS objective or a global elementwise mean.
augment_pipe (torch.nn.Module, optional) β Optional augmentation callable applied before corrupting images.
- sample_sigma(batch_size, device)[source]ΒΆ
Sample a column vector of training noise levels.
- Parameters:
batch_size (int)
device (device)
- Return type:
Tensor
- forward(model, clean_patch, position_patch=None, augment_pipe=None)[source]ΒΆ
Evaluate the weighted PaDIS denoising loss.
- Parameters:
model (torch.nn.Module) β Denoiser accepting noisy images, noise levels, and optional position channels.
images (torch.Tensor) β Clean image or patch batch in normalised-intensity units.
position_channels (torch.Tensor, optional) β Position channels aligned with
images.clean_patch (Tensor)
position_patch (Tensor | None)
augment_pipe (Module | None)
- Returns:
Scalar reduced training loss.
- Return type:
torch.Tensor