clinicadl.callbacks.LoggerCallback

class clinicadl.callbacks.LoggerCallback(save_logs: bool = True, debug: bool = True, progress_bar: bool = True)[source]

To configure logging.

Logging is a convenient way to get insight of the current status of the Trainer, to record the important information that it raises, and to get clues on how to debug a failed execution.

Note

Some messages logged during the setup phase of Trainer.train (or validate, etc.) may not be handled by this callback, because this callback is activated after that setup phase.

Parameters:
  • save_logs (bool, default=True) –

    Whether log messages raised during the code execution should be saved in files. Otherwise, log messages will just be printed in your console.

    These files will be saved in your MAPS directory in <maps>/exec/run-<program_executed>_<datetime>. In this folder, you will find:

    • debug.log (if debug=True): log messages with level “DEBUG”. Useful to debug your execution when it failed.

    • info.log: log messages with level “INFO”. General information about the execution flow.

    • error.log: log messages with level “ERROR”. To have information on potential errors that stopped the execution.

    Note that a file named warning.log, which contains log messages with level “WARNING”, will also be saved, but its location depends on the program being executed. For example, if you execute Trainer.train, it will be saved under <maps>/training/split-<split_idx>/warning.log. This is because this file may contain methodological warnings that should be mentioned close to the results.

  • debug (bool, default=True) – Whether to print/save the log messages with level “DEBUG”. These messages are verbose but helpful for debugging.

  • progress_bar (bool, default=True) – Whether to display a progress bar every time an iteration on a dataloader is performed.

on_trainer_init(*, model: Model, maps: Maps, **kwargs) None[source]

Called once when the Trainer is created or restored with Trainer.from_maps.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • metrics (MetricsHandler) – The the metrics passed to the Trainer.

  • optimization (OptimizationConfig) – The optimization specifications of the training phase.

  • callbacks (CallbacksHandler) – The callbacks passed to the Trainer.

on_exception(*, state: TrainerState, **kwargs) None[source]

Called when an exception interrupts an execution of the Trainer.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • exception (Exception) – The exception that has been raised.

on_train_start(*, maps: Maps, state: TrainerState, split: Split, computational: ComputationalConfig, **kwargs) None[source]

Called once at the beginning of Trainer.train if resume=False.

If resuming a training, on_resume() will be called instead.

Parameters:
on_resume(*, maps: Maps, state: TrainerState, split: Split, computational: ComputationalConfig, **kwargs) None[source]

Called once when Trainer.train is resuming a training.

More precisely, this method will be called just before loading the checkpoints.

Parameters:
on_validate_start(*, maps: Maps, state: TrainerState, model_checkpoint: str, **kwargs) None[source]

Called when a model is validated in Trainer.validate.

Not to be confused with on_validation_start().

Important

If model_checkpoint=None was passed to Trainer.validate, all the models saved during training will be validated. Therefore, on_validate_start will be called for each model.

Parameters:
on_validation_start(*, state: TrainerState, **kwargs) None[source]

Called at the beginning of every validation loop in Trainer.train.

Not to be confused with on_validate_start().

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • dataloader (DataLoader) – The dataloader on which validation is performed.

  • metrics (MetricsHandler) – The validation metrics to compute.

on_test_start(*, maps: Maps, state: TrainerState, model_checkpoint: str, group_name: str, **kwargs) None[source]

Called once at the beginning of Trainer.test.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • dataloader (DataLoader) – The dataloader on which the test is performed.

  • model_checkpoint (str) – The model checkpoint currently being tested.

  • metrics (MetricsHandler) – The test metrics to compute.

  • group_name (str) – The name given to the test data.

  • callbacks (CallbacksHandler) – The callbacks passed to the Trainer.

  • computational (ComputationalConfig) – The clinicadl.train.ComputationalConfig defining the computational specifications of the validation phase.

on_train_end(*, state: TrainerState, **kwargs) None[source]

Called once at the end of Trainer.train.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

on_validate_end(**kwargs) None[source]

Called at the end of a model validation in Trainer.validate.

Not to be confused with on_validation_end().

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • metrics (MetricsHandler) – The validation metrics computed.

on_validation_end(**kwargs) None[source]

Called at the end of every validation loop in Trainer.train.

Not to be confused with on_validate_end().

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • metrics (MetricsHandler) – The validation metrics computed.

on_test_end(**kwargs) None[source]

Called once at the end of Trainer.test.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.``test metrics`.

on_epoch_start(*, state: TrainerState, **kwargs) None[source]

Called at the beginning of an epoch in Trainer.train.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

on_epoch_end(*, state: TrainerState, **kwargs) None[source]

Called at the end of an epoch in Trainer.train.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

on_batch_start(*, state: TrainerState, **kwargs) None[source]

Called every time a new batch has been loaded in training, validation or test phase.

Note

This event may be redundant with other events: e.g., in evaluation phases, it is equivalent to on_evaluation_start() (except if the batch is sent to another device).

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • batch (BatchType) – The input batch.

on_forward_step_start(*, model: Model, maps: Maps, batch: BatchType, **kwargs) None[source]

Called every time Model.forward_step will be called in Trainer.train.

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

  • batch (BatchType) – The batch input to Model.forward_step.

on_batch_end(*, state: TrainerState, **kwargs) None[source]

Called every time the processing of a batch is completed during training, validation, or test phase.

Note

This event may be redundant with other events: e.g., in evaluation phases, it is equivalent to on_evaluation_end().

Parameters:
  • model (Model) – The model associated to the Trainer.

  • maps (Maps) – The MAPS associated to the Trainer.

  • state (TrainerState) – The current state of the Trainer.

on_backward_step_start(*, model: Model, loss: Tensor | dict[str, Tensor], **kwargs) None[source]

Called every time Model.backward_step will be called in Trainer.train.

Note

This event is equivalent to on_forward_step_end.

Parameters: