peal.generators.ddpm_generator.DDPM¶
- class peal.generators.ddpm_generator.DDPM[source]¶
Bases:
EditCapableGenerator,InvertibleGeneratorPixel-space denoising diffusion model used as a PEAL generator.
Wraps the guided-diffusion UNet and gaussian diffusion created by
create_model_and_diffusionfrom the vendored ACE code. Besides plain sampling it provides the invertible interface (encode/decode), arepaintstep that keeps everything outside a smooth change mask, andedit, which runs the ACE or FastDiME counterfactual attack.- Parameters:
config (
strorDDPMConfig) – Config path or object; loaded withload_yaml_config. The image size is overwritten with the last entry ofconfig.data.input_size.model_dir (
str, optional) – Run directory holding the weights, logs and outputs. Defaults toconfig.base_path.device (
str, optional) – Device the UNet is moved to.predictor_dataset (optional) – Accepted for interface compatibility; unused.
- Variables:
diffusion (model,) – The UNet and the gaussian diffusion process.
dataset – First dataset built from
config.data; used for the normalisation between the model range and the pytorch default range.model_path (
str) –final.cplin the run directory if it exists, elsefinal.pt(downloaded fromconfig.download_weightswhen missing). If no weights are found, an existing run directory is moved aside with a timestamp suffix and a fresh one is created.noise_fn (
callable) –torch.randn_likefor stochastic sampling,torch.zeros_likeotherwise.
Methods
Denoise a latent back to an image with the reverse diffusion loop.
Create counterfactuals for
x_inwith ACE or FastDiME.Map images to the noisy latent at a fraction
tof the schedule.Re-diffuse an edited image while pinning the unchanged region.
Draw images from the prior with the full ancestral sampling loop.
Train the UNet with the guided-diffusion
TrainLoop.- __init__(config, model_dir=None, device='cpu', predictor_dataset=None)[source]¶
Load the config, build the UNet and diffusion and restore weights.
- sample_x(batch_size=None, renormalize=True)[source]¶
Draw images from the prior with the full ancestral sampling loop.
- Parameters:
- Returns:
Samples of shape
(batch_size, *config.data.input_size).- Return type:
- encode(x, t=1.0, stochastic=None, num_steps=None)[source]¶
Map images to the noisy latent at a fraction
tof the schedule.With
stochastic == "fully"the latent is a singleq_sampledraw at the respaced timestep, clamped to[-1, 1]. Otherwise the image is inverted deterministically by iteratingddim_reverse_sampleover the firstt * timestep_respacingsteps.- Parameters:
x (
torch.Tensor) – Batch of images in the model range, shape(B, C, H, W).t (
float, optional) – Fraction ofconfig.timestep_respacingto noise up to.stochastic (
boolorstr, optional) –"fully"for one-shot noising, anything else for DDIM inversion. Defaults toconfig.stochastic.num_steps (optional) – Accepted for interface compatibility; unused.
- Returns:
Latent with the same shape as
x.- Return type:
- decode(z, t=1.0, stochastic=None, num_steps=None)[source]¶
Denoise a latent back to an image with the reverse diffusion loop.
Iterates
p_mean_variancefrom stept * timestep_respacing - 1down to 0, taking the posterior mean and adding the posterior noise in every step but the last whenconfig.stochasticis set.- Parameters:
z (
torch.Tensororlist) – Latent of shape(B, C, H, W); a one-element list is unwrapped.t (
float, optional) – Fraction of the respaced schedule the latent was noised to; must match the value used inencode().stochastic (
bool, optional) – Defaults toconfig.stochastic. Note that the per-step noise is gated byconfig.stochasticrather than by this argument.num_steps (optional) – Accepted for interface compatibility; unused.
- Returns:
Reconstructed images in the model range.
- Return type:
- repaint(x, pe, inpaint, dilation, t, stochastic, boolmask_in=None, max_avg_combination=0.5, exceptions=None)[source]¶
Re-diffuse an edited image while pinning the unchanged region.
A smooth change mask between
xand the edited imagepeis built withgenerate_smooth_mask; everything whose dilated mask value stays belowinpaintis considered background and is, in every reverse step, replaced by the correspondingly noised original. The final image also takes the background pixels straight fromx.- Parameters:
x (
torch.Tensor) – Original images, shape(B, C, H, W).pe (
torch.Tensor) – Edited (“pre-explanation”) images of the same shape.inpaint (
float) – Mask threshold;0disables the per-step re-injection of the original but not the final composition.dilation (
float) – Dilation of the change mask, passed togenerate_smooth_mask.t (
float) – Fraction ofconfig.timestep_respacingto restart from.stochastic (
bool) – Add posterior noise during the reverse loop and noise when re-injecting the original.boolmask_in (
torch.Tensor, optional) – Background mask of a previous attempt; its complement is unioned into the current background mask, so already-edited regions stay editable.max_avg_combination (
float, optional) – Mixing weight between the max and the mean channel difference ingenerate_smooth_mask.exceptions (
torch.Tensor, optional) – Per-sample flags; where the entry is 1 the background mask is zeroed, i.e. that sample is not repainted at all.
- Returns:
ce (
torch.Tensor) – Repainted images.boolmask (
torch.Tensor) – The background mask used, on the CPU.
- train_model()[source]¶
Train the UNet with the guided-diffusion
TrainLoop.Configures the guided-diffusion logger and the schedule sampler named by
config.schedule_sampler, builds a training dataloader whose epoch length isconfig.max_steps, logs a batch of training images to<model_dir>/logsand runs the loop, which writes checkpoints intomodel_direveryconfig.save_intervalsteps. If the model was not trained before, an existing run directory is moved aside with a timestamp suffix.Notes
config.x_selectionoverrides the dataset’stask_configso that the generator can be trained on a different input column than the one the dataset was built for.
- edit(x_in, target_confidence_goal, source_classes, target_classes, predictor, explainer_config, predictor_datasets, pbar=None, base_path='', mode='', boolmask_in=None, attempt_number=None)[source]¶
Create counterfactuals for
x_inwith ACE or FastDiME.Trains the diffusion model first if needed, optionally distils the predictor into a surrogate that supplies the guidance gradients (cached at
<base_path>/explainer/distilled_predictor/model.cpl), and then calls the vendoredace_mainorfastdime_maindepending onexplainer_config.subtype.The attack is repeated
explainer_config.num_attemptstimes. Any ofattack_iterations,sampling_time_fractionandsampling_inpaintgiven as a two-element list is interpolated linearly between the attempts,dist_l1/dist_l2geometrically, and the seed is incremented per attempt. All attempts are concatenated into the returned batch rather than being filtered by success.- Parameters:
x_in (
torch.Tensor) – Images to explain, shape(B, C, H, W).target_confidence_goal (
float) – Accepted for interface compatibility; the stopping criterion is taken fromexplainer_config.source_classes (
torch.Tensor) – Current and desired class index per sample, shape(B,).target_classes (
torch.Tensor) – Current and desired class index per sample, shape(B,).predictor (
nn.Module) – Classifier whose decision the counterfactual has to flip; also used to score the results.explainer_config (
ACEConfig) – Attack hyper-parameters;subtypeselects"ACE"or"FastDiME",l_perc/l_perc_layerenable the VGG perceptual loss,distilled_predictorthe surrogate.predictor_datasets (
list) –[train, val, test]-style datasets; entry 1 is passed to the attack as the predictor’s dataset and entry set is used for distillation.pbar (optional) – Progress bar, unused here.
base_path (
str, optional) – Run directory used to cache the distilled predictor.mode (
str, optional) – Unused; kept for interface compatibility.boolmask_in (optional) – Unused; kept for interface compatibility with other generators.
attempt_number (optional) – Unused; attempts are looped over internally.
- Returns:
(counterfactuals, differences, target confidences, originals, histories, zeros), each withB * num_attemptsentries. The differences arex_in - counterfactualand the trailing zeros stand in for the per-sample masks other generators return.- Return type:
- Raises:
Exception – If
explainer_config.subtypeis neither ACE nor FastDiME.