diffusion_models.diffusion_trainer

Module Contents

class DiffusionTrainer(model, dataset, optimizer, training_configuration, loss_function=F.l1_loss, log_configuration=LogConfiguration(), reverse_transforms=lambda x: ..., device='cuda', output_directory=None, run_config=None, resume_from=None, evaluation_configuration=None, evaluation_dataset=None)[source]

A diffusion trainer framework.

This is a simplified framework for training a diffusion models.

Parameters:
  • model (BaseDiffusionModel) – A diffusion model.

  • dataset (Dataset) – A dataset to train on.

  • optimizer (Optimizer) – The optimizer to use.

  • training_configuration (TrainingConfiguration) – The training configuration to use.

  • loss_function (Callable) – The loss function to use.

  • log_configuration (LogConfiguration) – The logging configuration to use.

  • reverse_transforms (Callable) – The reverse transforms to use.

  • device (str) – The device to use.

  • output_directory (Optional[Union[str, Path]]) – Where to write this run’s artifacts. When provided, checkpoints go to <output_directory>/checkpoints and tensorboard logs to <output_directory>/tensorboard. Otherwise the legacy ../checkpoints/<name> and ../runs/<name> locations are used.

  • run_config (Optional[str]) – The resolved configuration (YAML) of the run. It is logged to tensorboard and stored in every checkpoint for reproducibility.

  • resume_from (Optional[Union[str, Path]]) – A checkpoint to resume training from. The model, optimizer, grad scaler and EMA are restored and training continues from the epoch after the last completed one. Resuming happens at an epoch boundary; progress within an interrupted epoch is lost.

  • evaluation_configuration (Optional[EvaluationConfiguration]) – When fid_rate is set, FID (and KID) of samples from the EMA model (or the raw model without EMA) is computed every that many epochs, and after the last epoch, then logged to tensorboard. Needs the evaluation extra.

  • evaluation_dataset (Optional[Dataset]) – The real reference images for the evaluation, ideally a held-out split.

model[source]

The diffusion model to use.

optimizer[source]

The optimizer to use.

loss_function[source]

The loss function to use.

training_configuration[source]

The training configuration to use.

device = 'cuda'[source]

The device to use.

dataloader[source]

A torch dataloader.

scaler[source]

A torch GradScaler object (a no-op unless training in float16).

log_configuration[source]

A LogConfiguration object.

tensorboard_manager[source]

A tensorboard manager instance.

run_config = None[source]

The resolved run configuration (YAML), if any.

reverse_transforms[source]

A set of reverse transforms.

ema: ExponentialMovingAverage | None = None[source]

The EMA of the model weights, when ema_decay is configured.

evaluation_configuration = None[source]

The FID/KID evaluation settings.

evaluator = None[source]

The FID/KID evaluator, when fid_rate is configured.

load_checkpoint(file_path)[source]

Restore the training state from a checkpoint.

Parameters:

file_path (Union[str, Path]) – The checkpoint to restore (e.g. latest.pt or interrupted.pt).

save_checkpoint(epoch, checkpoint_name)[source]

Save a checkpoint (atomically).

Parameters:
  • epoch (int) – The current epoch. final.pt, latest.pt and interrupted.pt store the number of completed epochs.

  • checkpoint_name (str) – The name of the checkpoint.

train()[source]

Start the diffusion training.

If the run is interrupted (KeyboardInterrupt or SIGTERM), an interrupted.pt checkpoint is saved before the interruption is re-raised. When latest_checkpoint_rate is set, latest.pt is overwritten every that many epochs.

log_to_tensorboard(metrics, global_step)[source]

Log to tensorboard.

This method logs some useful metrics and visualizations to tensorboard.

Parameters:
  • metrics (Dict[str, float]) – A dictionary mapping metric names to values.

  • global_step (int) – The current global step.