diffusion_models.diffusion_trainer ================================== .. py:module:: diffusion_models.diffusion_trainer Module Contents --------------- .. py:class:: DiffusionTrainer(model, dataset, optimizer, training_configuration, loss_function = F.l1_loss, log_configuration = LogConfiguration(), reverse_transforms = lambda x: x, device = 'cuda', output_directory = None, run_config = None, resume_from = None, evaluation_configuration = None, evaluation_dataset = None) A diffusion trainer framework. This is a simplified framework for training a diffusion models. :param model: A diffusion model. :param dataset: A dataset to train on. :param optimizer: The optimizer to use. :param training_configuration: The training configuration to use. :param loss_function: The loss function to use. :param log_configuration: The logging configuration to use. :param reverse_transforms: The reverse transforms to use. :param device: The device to use. :param output_directory: Where to write this run's artifacts. When provided, checkpoints go to ``/checkpoints`` and tensorboard logs to ``/tensorboard``. Otherwise the legacy ``../checkpoints/`` and ``../runs/`` locations are used. :param run_config: The resolved configuration (YAML) of the run. It is logged to tensorboard and stored in every checkpoint for reproducibility. :param resume_from: 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. :param evaluation_configuration: 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. :param evaluation_dataset: The real reference images for the evaluation, ideally a held-out split. .. py:attribute:: model The diffusion model to use. .. py:attribute:: optimizer The optimizer to use. .. py:attribute:: loss_function The loss function to use. .. py:attribute:: training_configuration The training configuration to use. .. py:attribute:: device :value: 'cuda' The device to use. .. py:attribute:: dataloader A torch dataloader. .. py:attribute:: scaler A torch GradScaler object (a no-op unless training in float16). .. py:attribute:: log_configuration A LogConfiguration object. .. py:attribute:: tensorboard_manager A tensorboard manager instance. .. py:attribute:: run_config :value: None The resolved run configuration (YAML), if any. .. py:attribute:: reverse_transforms A set of reverse transforms. .. py:attribute:: ema :type: Optional[diffusion_models.utils.ema.ExponentialMovingAverage] :value: None The EMA of the model weights, when ``ema_decay`` is configured. .. py:attribute:: evaluation_configuration :value: None The FID/KID evaluation settings. .. py:attribute:: evaluator :value: None The FID/KID evaluator, when ``fid_rate`` is configured. .. py:method:: load_checkpoint(file_path) Restore the training state from a checkpoint. :param file_path: The checkpoint to restore (e.g. ``latest.pt`` or ``interrupted.pt``). .. py:method:: save_checkpoint(epoch, checkpoint_name) Save a checkpoint (atomically). :param epoch: The current epoch. ``final.pt``, ``latest.pt`` and ``interrupted.pt`` store the number of completed epochs. :param checkpoint_name: The name of the checkpoint. .. py:method:: train() 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. .. py:method:: log_to_tensorboard(metrics, global_step) Log to tensorboard. This method logs some useful metrics and visualizations to tensorboard. :param metrics: A dictionary mapping metric names to values. :param global_step: The current global step.