Pipeline

This module provides the KfoldsTrainingPipeline class, which is responsible for managing the k-fold pipeline.

KfoldsTrainingPipeline Class

The KfoldsTrainingPipeline class is responsible for managing the interractions between Controller, Datasets, Trainer in the context of a k-fold training.

Note

You can train on multiple GPUs by setting the gpus parameter to the list of GPU ids you want to use. By default, the pipeline will use all available GPUs.

class lis2.pipeline.KfoldsTrainingPipeline(*args, **kwargs)[source]

Bases: Configurable

A pipeline for training models using K-Fold cross-validation.

This class handles the configuration and execution of training across multiple folds, including dataset parsing, training, and inference.

Configuration:
  • run_name (str): The name of the training run.

  • inference_source (Union[Path, str], optional): The path to the inference data source.

  • train_output_dir (Union[Path, str]): The directory where training outputs are saved.

  • nb_folds (int): The number of folds for cross-validation.

  • data (Config): The dataset configuration.

  • trainer (Config): The trainer configuration.

  • model (Config): The model configuration.

  • gpus (Union[int, list[int], str], optional): The GPU IDs to use for training. If ‘auto’, the pipeline will use all available GPUs. Default: ‘auto’.

Example Configuration:
run_name: "example_run"
inference_source: "data/test.fits"
train_output_dir: "output"
nb_folds: 5
data:
    dataset_path: "data/train.fits"
    type: "fits"
    controller:
        type: "kfold"
        nb_folds: 5
    trainset:
        type: "fits"
        transform: "default"
        batch_size: 32
    validset:
        type: "fits"
        transform: "default"
        batch_size: 32
    testset:
        type: "fits"
        transform: "default"
        batch_size: 32
trainer:
    type: "default"
    epochs: 10
    batch_size: 32
    learning_rate: 0.001
model:
    type: "default"
    name: "example_model"
aliases = ['kfold_pipeline']
config_schema: Dict = {'data': Schema(type=typing.Union[dict, str], aliases=[], optional=False, default=None), 'gpus': Schema(type=typing.Union[int, list[int], str], aliases=[], optional=True, default=auto), 'inference_source': Schema(type=<class 'pathlib.Path'>, aliases=[], optional=True, default=None), 'model': Schema(type=typing.Union[dict, str], aliases=[], optional=False, default=None), 'nb_folds': Schema(type=<class 'int'>, aliases=[], optional=True, default=1), 'run_name': Schema(type=<class 'str'>, aliases=[], optional=False, default=None), 'train_output_dir': Schema(type=<class 'pathlib.Path'>, aliases=[], optional=False, default=None), 'trainer': Schema(type=typing.Union[dict, str], aliases=[], optional=False, default=None)}
finish()[source]
preconditions() → None[source]

Checks preconditions for the inference source.

run_inference(model_snapshot: str, test_config: dict[str, Any]) → Any | None[source]

Runs inference using the trained model snapshot on the test dataset.

Parameters:
  • model_snapshot (str) – Path to the trained model snapshot.

  • test_config (dict[str, Any]) – Configuration for the test dataset.

Returns:

Inference results, if available.

Return type:

Optional[Any]

run_training() → None[source]

Executes training across multiple folds defined by the folds controller.

This method iterates through each fold, updating dataset configurations and trainer settings accordingly. It trains a model for each fold and logs the results. If inference is enabled, predictions from each fold are aggregated and saved as a FITS file.

Steps:
  1. Iterate through the predefined folds.

  2. Update dataset configurations for training, validation, and testing.

  3. Configure and initialize a trainer for the current fold.

  4. Train the model and log the results.

  5. If inference is enabled, generate and aggregate predictions across folds.

  6. Save the aggregated predictions to a FITS file.

Returns:

None

safe_train(trainer_config)[source]

Execute training in a safe environment with optional multi-GPU support. Only returns the result from the main GPU process.

Parameters:

trainer_config (dict) – Trainer configuration.

Returns:

Result from the main GPU training process.

Return type:

main_result

K-fold Training Pipeline

The run_training method orchestrates the process, including different phases Here’s a detailed explanation of its internal workings:

  1. K-folds handling:

    • Calls the Controller which contains the splits as well as the assignment of each area to a fold.

  2. Looping on each split:

    • For each split (i.e. each configuration of “which fold is in which set”), train, valid and test sets are created using the indices in fold assignments

    • A Trainer is loaded

    • A Models is trained using this trainer.

    • This loop is repeated k-times. See more details on the training in Trainer

  3. Results and metrics: - Results and metrics are logged for each split

  4. Inference:

    • Inference can be run using each trained model on each test set, in order to have an inference on the whole image.

Conclusion

The Pipeline class orchestrates the different block that can be involved in the training of a model in the context of k-folds.