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:
ConfigurableA 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)}
- 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:
Iterate through the predefined folds.
Update dataset configurations for training, validation, and testing.
Configure and initialize a trainer for the current fold.
Train the model and log the results.
If inference is enabled, generate and aggregate predictions across folds.
Save the aggregated predictions to a FITS file.
- Returns:
None
K-fold Training Pipeline
The run_training method orchestrates the process, including different phases Here’s a detailed explanation of its internal workings:
K-folds handling:
Calls the Controller which contains the splits as well as the assignment of each area to a fold.
Looping on each split:
Results and metrics: - Results and metrics are logged for each split
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.