MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / CrossValidation

Class CrossValidation

monai/apps/datasets.py:678–745  ·  view source on GitHub ↗

Cross validation dataset based on the general dataset which must have `_split_datalist` API. Args: dataset_cls: dataset class to be used to create the cross validation partitions. It must have `_split_datalist` API. nfolds: number of folds to split the data for

Source from the content-addressed store, hash-verified

676
677
678class CrossValidation:
679 """
680 Cross validation dataset based on the general dataset which must have `_split_datalist` API.
681
682 Args:
683 dataset_cls: dataset class to be used to create the cross validation partitions.
684 It must have `_split_datalist` API.
685 nfolds: number of folds to split the data for cross validation.
686 seed: random seed to randomly shuffle the datalist before splitting into N folds, default is 0.
687 dataset_params: other additional parameters for the dataset_cls base class.
688
689 Example of 5 folds cross validation training::
690
691 cvdataset = CrossValidation(
692 dataset_cls=DecathlonDataset,
693 nfolds=5,
694 seed=12345,
695 root_dir="./",
696 task="Task09_Spleen",
697 section="training",
698 transform=train_transform,
699 download=True,
700 )
701 dataset_fold0_train = cvdataset.get_dataset(folds=[1, 2, 3, 4])
702 dataset_fold0_val = cvdataset.get_dataset(folds=0, transform=val_transform, download=False)
703 # execute training for fold 0 ...
704
705 dataset_fold1_train = cvdataset.get_dataset(folds=[0, 2, 3, 4])
706 dataset_fold1_val = cvdataset.get_dataset(folds=1, transform=val_transform, download=False)
707 # execute training for fold 1 ...
708
709 ...
710
711 dataset_fold4_train = ...
712 # execute training for fold 4 ...
713
714 """
715
716 def __init__(self, dataset_cls: object, nfolds: int = 5, seed: int = 0, **dataset_params: Any) -> None:
717 if not hasattr(dataset_cls, "_split_datalist"):
718 raise ValueError("dataset class must have _split_datalist API.")
719 self.dataset_cls = dataset_cls
720 self.nfolds = nfolds
721 self.seed = seed
722 self.dataset_params = dataset_params
723
724 def get_dataset(self, folds: Sequence[int] | int, **dataset_params: Any) -> object:
725 """
726 Generate dataset based on the specified fold indices in the cross validation group.
727
728 Args:
729 folds: index of folds for training or validation, if a list of values, concatenate the data.
730 dataset_params: other additional parameters for the dataset_cls base class, will override
731 the same parameters in `self.dataset_params`.
732
733 """
734 nfolds = self.nfolds
735 seed = self.seed

Callers 1

test_valuesMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_valuesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…