Skip to content

classification

ClassificationDataModule(data_path, dataset=ImageClassificationListDataset, name='classification_datamodule', num_workers=8, batch_size=32, seed=42, val_size=0.2, test_size=0.2, num_data_class=None, exclude_filter=None, include_filter=None, label_map=None, load_aug_images=False, aug_name=None, n_aug_to_take=4, replace_str_from=None, replace_str_to=None, train_transform=None, val_transform=None, test_transform=None, train_split_file=None, test_split_file=None, val_split_file=None, class_to_idx=None, **kwargs)

Bases: BaseDataModule

Base class single folder based classification datamodules. If there is no nested folders, use this class.

Parameters:

  • data_path (str) –

    Path to the data main folder.

  • name (str, default: 'classification_datamodule' ) –

    The name for the data module. Defaults to "classification_datamodule".

  • num_workers (int, default: 8 ) –

    Number of workers for dataloaders. Defaults to 16.

  • batch_size (int, default: 32 ) –

    Batch size. Defaults to 32.

  • seed (int, default: 42 ) –

    Random generator seed. Defaults to 42.

  • dataset (Type[ImageClassificationListDataset], default: ImageClassificationListDataset ) –

    Dataset class.

  • val_size (Optional[float], default: 0.2 ) –

    The validation split. Defaults to 0.2.

  • test_size (float, default: 0.2 ) –

    The test split. Defaults to 0.2.

  • exclude_filter (Optional[List[str]], default: None ) –

    The filter for excluding folders. Defaults to None.

  • include_filter (Optional[List[str]], default: None ) –

    The filter for including folders. Defaults to None.

  • label_map (Optional[Dict[str, Any]], default: None ) –

    The mapping for labels. Defaults to None.

  • num_data_class (Optional[int], default: None ) –

    The number of samples per class. Defaults to None.

  • train_transform (Optional[Compose], default: None ) –

    Transformations for train dataset. Defaults to None.

  • val_transform (Optional[Compose], default: None ) –

    Transformations for validation dataset. Defaults to None.

  • test_transform (Optional[Compose], default: None ) –

    Transformations for test dataset. Defaults to None.

  • train_split_file (Optional[str], default: None ) –

    The file with train split. Defaults to None.

  • val_split_file (Optional[str], default: None ) –

    The file with validation split. Defaults to None.

  • test_split_file (Optional[str], default: None ) –

    The file with test split. Defaults to None.

  • class_to_idx (Optional[Dict[str, int]], default: None ) –

    The mapping from class name to index. Defaults to None.

  • **kwargs (Any, default: {} ) –

    Additional arguments for BaseDataModule.

Source code in quadra/datamodules/classification.py
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def __init__(
    self,
    data_path: str,
    dataset: Type[ImageClassificationListDataset] = ImageClassificationListDataset,
    name: str = "classification_datamodule",
    num_workers: int = 8,
    batch_size: int = 32,
    seed: int = 42,
    val_size: Optional[float] = 0.2,
    test_size: float = 0.2,
    num_data_class: Optional[int] = None,
    exclude_filter: Optional[List[str]] = None,
    include_filter: Optional[List[str]] = None,
    label_map: Optional[Dict[str, Any]] = None,
    load_aug_images: bool = False,
    aug_name: Optional[str] = None,
    n_aug_to_take: Optional[int] = 4,
    replace_str_from: Optional[str] = None,
    replace_str_to: Optional[str] = None,
    train_transform: Optional[albumentations.Compose] = None,
    val_transform: Optional[albumentations.Compose] = None,
    test_transform: Optional[albumentations.Compose] = None,
    train_split_file: Optional[str] = None,
    test_split_file: Optional[str] = None,
    val_split_file: Optional[str] = None,
    class_to_idx: Optional[Dict[str, int]] = None,
    **kwargs: Any,
):
    super().__init__(
        data_path=data_path,
        name=name,
        seed=seed,
        batch_size=batch_size,
        num_workers=num_workers,
        train_transform=train_transform,
        val_transform=val_transform,
        test_transform=test_transform,
        load_aug_images=load_aug_images,
        aug_name=aug_name,
        n_aug_to_take=n_aug_to_take,
        replace_str_from=replace_str_from,
        replace_str_to=replace_str_to,
        **kwargs,
    )
    self.replace_str = None
    self.exclude_filter = exclude_filter
    self.include_filter = include_filter
    self.val_size = val_size
    self.test_size = test_size
    self.label_map = label_map
    self.num_data_class = num_data_class
    self.dataset = dataset
    self.train_split_file = train_split_file
    self.test_split_file = test_split_file
    self.val_split_file = val_split_file
    self.class_to_idx: Optional[Dict[str, int]]

    if class_to_idx is not None:
        self.class_to_idx = class_to_idx
        self.num_classes = len(self.class_to_idx)
    else:
        self.class_to_idx = self._find_classes_from_data_path(self.data_path)
        if self.class_to_idx is None:
            log.warning("Could not build a class_to_idx from the data_path subdirectories")
            self.num_classes = 0
        else:
            self.num_classes = len(self.class_to_idx)

predict_dataloader()

Returns a dataloader used for predictions.

Source code in quadra/datamodules/classification.py
410
411
412
def predict_dataloader(self) -> DataLoader:
    """Returns a dataloader used for predictions."""
    return self.test_dataloader()

setup(stage=None)

Setup data module based on stages of training.

Source code in quadra/datamodules/classification.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
def setup(self, stage: Optional[str] = None) -> None:
    """Setup data module based on stages of training."""
    if stage in ["train", "fit"]:
        self.train_dataset = self.dataset(
            samples=self.data[self.data["split"] == "train"]["samples"].tolist(),
            targets=self.data[self.data["split"] == "train"]["targets"].tolist(),
            transform=self.train_transform,
            class_to_idx=self.class_to_idx,
        )
        self.val_dataset = self.dataset(
            samples=self.data[self.data["split"] == "val"]["samples"].tolist(),
            targets=self.data[self.data["split"] == "val"]["targets"].tolist(),
            transform=self.val_transform,
            class_to_idx=self.class_to_idx,
        )
    if stage in ["test", "predict"]:
        self.test_dataset = self.dataset(
            samples=self.data[self.data["split"] == "test"]["samples"].tolist(),
            targets=self.data[self.data["split"] == "test"]["targets"].tolist(),
            transform=self.test_transform,
            class_to_idx=self.class_to_idx,
        )

test_dataloader()

Returns the test dataloader.

Raises:

  • ValueError

    If test dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
def test_dataloader(self) -> DataLoader:
    """Returns the test dataloader.

    Raises:
        ValueError: If test dataset is not initialized.


    Returns:
        test dataloader.
    """
    if not self.test_dataset_available:
        raise ValueError("Test dataset is not initialized")

    loader = DataLoader(
        self.test_dataset,
        batch_size=self.batch_size,
        shuffle=False,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )
    return loader

train_dataloader()

Returns the train dataloader.

Raises:

  • ValueError

    If train dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
def train_dataloader(self) -> DataLoader:
    """Returns the train dataloader.

    Raises:
        ValueError: If train dataset is not initialized.

    Returns:
        Train dataloader.
    """
    if not self.train_dataset_available:
        raise ValueError("Train dataset is not initialized")
    if not isinstance(self.train_dataset, torch.utils.data.Dataset):
        raise ValueError("Train dataset has to be single `torch.utils.data.Dataset` instance.")
    return DataLoader(
        self.train_dataset,
        batch_size=self.batch_size,
        shuffle=True,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )

val_dataloader()

Returns the validation dataloader.

Raises:

  • ValueError

    If validation dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def val_dataloader(self) -> DataLoader:
    """Returns the validation dataloader.

    Raises:
        ValueError: If validation dataset is not initialized.

    Returns:
        val dataloader.
    """
    if not self.val_dataset_available:
        raise ValueError("Validation dataset is not initialized")
    if not isinstance(self.val_dataset, torch.utils.data.Dataset):
        raise ValueError("Validation dataset has to be single `torch.utils.data.Dataset` instance.")
    return DataLoader(
        self.val_dataset,
        batch_size=self.batch_size,
        shuffle=True,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )

MultilabelClassificationDataModule(data_path, images_and_labels_file=None, train_split_file=None, test_split_file=None, val_split_file=None, name='multilabel_datamodule', dataset=MultilabelClassificationDataset, num_classes=None, num_workers=16, batch_size=64, test_batch_size=64, seed=42, val_size=0.2, test_size=0.2, train_transform=None, val_transform=None, test_transform=None, class_to_idx=None, **kwargs)

Bases: BaseDataModule

Base class for all multi-label modules.

Parameters:

  • data_path (str) –

    Path to the data main folder.

  • images_and_labels_file (Optional[str], default: None ) –

    a path to a txt file containing the relative (to data_path) path of images with their relative labels, in a comma-separated way. E.g.:

    • path1,l1,l2,l3
    • path2,l4,l5
    • ...

    One of images_and_label and both train_split_file and test_split_file must be set. Defaults to None.

  • name (str, default: 'multilabel_datamodule' ) –

    The name for the data module. Defaults to "multilabel_datamodule".

  • dataset (Callable, default: MultilabelClassificationDataset ) –

    a callable returning a torch.utils.data.Dataset class.

  • num_classes (Optional[int], default: None ) –

    the number of classes in the dataset. This is used to create one-hot encoded targets. Defaults to None.

  • num_workers (int, default: 16 ) –

    Number of workers for dataloaders. Defaults to 16.

  • batch_size (int, default: 64 ) –

    Training batch size. Defaults to 64.

  • test_batch_size (int, default: 64 ) –

    Testing batch size. Defaults to 64.

  • seed (int, default: 42 ) –

    Random generator seed. Defaults to SegmentationEvalua2.

  • val_size (Optional[float], default: 0.2 ) –

    The validation split. Defaults to 0.2.

  • test_size (Optional[float], default: 0.2 ) –

    The test split. Defaults to 0.2.

  • train_transform (Optional[Compose], default: None ) –

    Transformations for train dataset. Defaults to None.

  • val_transform (Optional[Compose], default: None ) –

    Transformations for validation dataset. Defaults to None.

  • test_transform (Optional[Compose], default: None ) –

    Transformations for test dataset. Defaults to None.

  • train_split_file (Optional[str], default: None ) –

    The file with train split. Defaults to None.

  • val_split_file (Optional[str], default: None ) –

    The file with validation split. Defaults to None.

  • test_split_file (Optional[str], default: None ) –

    The file with test split. Defaults to None.

  • class_to_idx (Optional[Dict[str, int]], default: None ) –

    a clss to idx dictionary. Defaults to None.

Source code in quadra/datamodules/classification.py
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
def __init__(
    self,
    data_path: str,
    images_and_labels_file: Optional[str] = None,
    train_split_file: Optional[str] = None,
    test_split_file: Optional[str] = None,
    val_split_file: Optional[str] = None,
    name: str = "multilabel_datamodule",
    dataset: Callable = MultilabelClassificationDataset,
    num_classes: Optional[int] = None,
    num_workers: int = 16,
    batch_size: int = 64,
    test_batch_size: int = 64,
    seed: int = 42,
    val_size: Optional[float] = 0.2,
    test_size: Optional[float] = 0.2,
    train_transform: Optional[albumentations.Compose] = None,
    val_transform: Optional[albumentations.Compose] = None,
    test_transform: Optional[albumentations.Compose] = None,
    class_to_idx: Optional[Dict[str, int]] = None,
    **kwargs,
):
    super().__init__(
        data_path=data_path,
        name=name,
        num_workers=num_workers,
        batch_size=batch_size,
        seed=seed,
        train_transform=train_transform,
        val_transform=val_transform,
        test_transform=test_transform,
        **kwargs,
    )
    if not (images_and_labels_file is not None or (train_split_file is not None and test_split_file is not None)):
        raise ValueError(
            "Either `images_and_labels_file` or both `train_split_file` and `test_split_file` must be set"
        )
    self.images_and_labels_file = images_and_labels_file
    self.dataset = dataset
    self.num_classes = num_classes
    self.train_batch_size = batch_size
    self.test_batch_size = test_batch_size
    self.val_size = val_size
    self.test_size = test_size
    self.train_split_file = train_split_file
    self.test_split_file = test_split_file
    self.val_split_file = val_split_file
    self.class_to_idx = class_to_idx
    self.train_dataset: MultilabelClassificationDataset
    self.val_dataset: MultilabelClassificationDataset
    self.test_dataset: MultilabelClassificationDataset

predict_dataloader()

Returns a dataloader used for predictions.

Source code in quadra/datamodules/classification.py
993
994
995
def predict_dataloader(self) -> DataLoader:
    """Returns a dataloader used for predictions."""
    return self.test_dataloader()

setup(stage=None)

Setup data module based on stages of training.

Source code in quadra/datamodules/classification.py
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
def setup(self, stage: Optional[str] = None) -> None:
    """Setup data module based on stages of training."""
    if stage in ["train", "fit"]:
        train_samples = self.data[self.data["split"] == "train"]["samples"].tolist()
        train_targets = self.data[self.data["split"] == "train"]["targets"].tolist()
        val_samples = self.data[self.data["split"] == "val"]["samples"].tolist()
        val_targets = self.data[self.data["split"] == "val"]["targets"].tolist()
        self.train_dataset = self.dataset(
            samples=train_samples,
            targets=train_targets,
            transform=self.train_transform,
            class_to_idx=self.class_to_idx,
        )
        self.val_dataset = self.dataset(
            samples=val_samples,
            targets=val_targets,
            transform=self.val_transform,
            class_to_idx=self.class_to_idx,
        )
    if stage == "test":
        test_samples = self.data[self.data["split"] == "test"]["samples"].tolist()
        test_targets = self.data[self.data["split"] == "test"]["targets"].tolist()
        self.test_dataset = self.dataset(
            samples=test_samples,
            targets=test_targets,
            transform=self.test_transform,
            class_to_idx=self.class_to_idx,
        )

test_dataloader()

Returns the test dataloader.

Raises:

  • ValueError

    If test dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
def test_dataloader(self) -> DataLoader:
    """Returns the test dataloader.

    Raises:
        ValueError: If test dataset is not initialized.


    Returns:
        test dataloader.
    """
    if not self.test_dataset_available:
        raise ValueError("Test dataset is not initialized")

    loader = DataLoader(
        self.test_dataset,
        batch_size=self.batch_size,
        shuffle=False,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )
    return loader

train_dataloader()

Returns the train dataloader.

Raises:

  • ValueError

    If train dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
def train_dataloader(self) -> DataLoader:
    """Returns the train dataloader.

    Raises:
        ValueError: If train dataset is not initialized.

    Returns:
        Train dataloader.
    """
    if not self.train_dataset_available:
        raise ValueError("Train dataset is not initialized")
    return DataLoader(
        self.train_dataset,
        batch_size=self.batch_size,
        shuffle=True,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )

val_dataloader()

Returns the validation dataloader.

Raises:

  • ValueError

    If validation dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
def val_dataloader(self) -> DataLoader:
    """Returns the validation dataloader.

    Raises:
        ValueError: If validation dataset is not initialized.

    Returns:
        val dataloader.
    """
    if not self.val_dataset_available:
        raise ValueError("Validation dataset is not initialized")
    return DataLoader(
        self.train_dataset,
        batch_size=self.batch_size,
        shuffle=True,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )

SklearnClassificationDataModule(data_path, exclude_filter=None, include_filter=None, val_size=0.2, class_to_idx=None, label_map=None, seed=42, batch_size=32, num_workers=6, train_transform=None, val_transform=None, test_transform=None, roi=None, n_splits=1, phase='train', cache=False, limit_training_data=None, train_split_file=None, test_split_file=None, name='sklearn_classification_datamodule', dataset=ImageClassificationListDataset, **kwargs)

Bases: BaseDataModule

A generic Data Module for classification with frozen torch backbone and sklearn classifier.

It can also handle k-fold cross validation.

Parameters:

  • name (str, default: 'sklearn_classification_datamodule' ) –

    The name for the data module. Defaults to "sklearn_classification_datamodule".

  • data_path (str) –

    Path to images main folder

  • exclude_filter (Optional[List[str]], default: None ) –

    List of string filter to be used to exclude images. If None no filter will be applied.

  • include_filter (Optional[List[str]], default: None ) –

    List of string filter to be used to include images. Only images that satisfied at list one of the filter will be included.

  • val_size (float, default: 0.2 ) –

    The validation split. Defaults to 0.2.

  • class_to_idx (Optional[Dict[str, int]], default: None ) –

    Dictionary of conversion btw folder name and index. Only file whose label is in dictionary key list will be considered. If None all files will be considered and a custom conversion is created.

  • seed (int, default: 42 ) –

    Fixed seed for random operations

  • batch_size (int, default: 32 ) –

    Dimension of batches for dataloader

  • num_workers (int, default: 6 ) –

    Number of workers for dataloader

  • train_transform (Optional[Compose], default: None ) –

    Albumentation transformations for training set

  • val_transform (Optional[Compose], default: None ) –

    Albumentation transformations for validation set

  • test_transform (Optional[Compose], default: None ) –

    Albumentation transformations for test set

  • roi (Optional[Tuple[int, int, int, int]], default: None ) –

    Optional cropping region

  • n_splits (int, default: 1 ) –

    Number of dataset subdivision (default 1 -> train/test). Use a value >= 2 for cross validation.

  • phase (str, default: 'train' ) –

    Either train or test

  • cache (bool, default: False ) –

    If true disable shuffling in all dataloader to enable feature caching

  • limit_training_data (Optional[int], default: None ) –

    if defined, each class will be donwsampled to this number. It must be >= 2 to allow splitting

  • label_map (Optional[Dict[str, Any]], default: None ) –

    Dictionary of conversion btw folder name and label.

  • train_split_file (Optional[str], default: None ) –

    Optional path to a csv file containing the train split samples.

  • test_split_file (Optional[str], default: None ) –

    Optional path to a csv file containing the test split samples.

  • **kwargs (Any, default: {} ) –

    Additional arguments for BaseDataModule

Source code in quadra/datamodules/classification.py
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
def __init__(
    self,
    data_path: str,
    exclude_filter: Optional[List[str]] = None,
    include_filter: Optional[List[str]] = None,
    val_size: float = 0.2,
    class_to_idx: Optional[Dict[str, int]] = None,
    label_map: Optional[Dict[str, Any]] = None,
    seed: int = 42,
    batch_size: int = 32,
    num_workers: int = 6,
    train_transform: Optional[albumentations.Compose] = None,
    val_transform: Optional[albumentations.Compose] = None,
    test_transform: Optional[albumentations.Compose] = None,
    roi: Optional[Tuple[int, int, int, int]] = None,
    n_splits: int = 1,
    phase: str = "train",
    cache: bool = False,
    limit_training_data: Optional[int] = None,
    train_split_file: Optional[str] = None,
    test_split_file: Optional[str] = None,
    name: str = "sklearn_classification_datamodule",
    dataset: Type[ImageClassificationListDataset] = ImageClassificationListDataset,
    **kwargs: Any,
):
    super().__init__(
        data_path=data_path,
        name=name,
        seed=seed,
        batch_size=batch_size,
        num_workers=num_workers,
        train_transform=train_transform,
        val_transform=val_transform,
        test_transform=test_transform,
        **kwargs,
    )

    self.class_to_idx = class_to_idx
    self.roi = roi
    self.cache = cache
    self.limit_training_data = limit_training_data

    self.dataset = dataset
    self.phase = phase
    self.n_splits = n_splits
    self.train_split_file = train_split_file
    self.test_split_file = test_split_file
    self.exclude_filter = exclude_filter
    self.include_filter = include_filter
    self.val_size = val_size
    self.label_map = label_map
    self.full_dataset: ImageClassificationListDataset
    self.train_dataset: List[ImageClassificationListDataset]
    self.val_dataset: List[ImageClassificationListDataset]

full_dataloader()

Return a dataloader to perform training on the entire dataset.

Returns:

  • DataLoader

    dataloader to perform training on the entire dataset after evaluation. This is useful

  • DataLoader

    to perform a final training on the entire dataset after the evaluation phase.

Source code in quadra/datamodules/classification.py
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
def full_dataloader(self) -> DataLoader:
    """Return a dataloader to perform training on the entire dataset.

    Returns:
        dataloader to perform training on the entire dataset after evaluation. This is useful
        to perform a final training on the entire dataset after the evaluation phase.

    """
    if self.full_dataset is None:
        raise ValueError("Full dataset is not initialized")

    return DataLoader(
        self.full_dataset,
        batch_size=self.batch_size,
        shuffle=not self.cache,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
    )

predict_dataloader()

Returns a dataloader used for predictions.

Source code in quadra/datamodules/classification.py
597
598
599
def predict_dataloader(self) -> DataLoader:
    """Returns a dataloader used for predictions."""
    return self.test_dataloader()

setup(stage)

Setup data module based on stages of training.

Source code in quadra/datamodules/classification.py
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
def setup(self, stage: str) -> None:
    """Setup data module based on stages of training."""
    if stage == "fit":
        self.train_dataset = []
        self.val_dataset = []

        for cv_idx in range(self.n_splits):
            cv_df = self.data[self.data["cv"] == cv_idx]
            train_samples = cv_df[cv_df["split"] == "train"]["samples"].tolist()
            train_targets = cv_df[cv_df["split"] == "train"]["targets"].tolist()
            val_samples = cv_df[cv_df["split"] == "val"]["samples"].tolist()
            val_targets = cv_df[cv_df["split"] == "val"]["targets"].tolist()
            self.train_dataset.append(
                self.dataset(
                    class_to_idx=self.class_to_idx,
                    samples=train_samples,
                    targets=train_targets,
                    transform=self.train_transform,
                    roi=self.roi,
                )
            )
            self.val_dataset.append(
                self.dataset(
                    class_to_idx=self.class_to_idx,
                    samples=val_samples,
                    targets=val_targets,
                    transform=self.val_transform,
                    roi=self.roi,
                )
            )
        all_samples = self.data[self.data["cv"] == 0]["samples"].tolist()
        all_targets = self.data[self.data["cv"] == 0]["targets"].tolist()
        self.full_dataset = self.dataset(
            class_to_idx=self.class_to_idx,
            samples=all_samples,
            targets=all_targets,
            transform=self.train_transform,
            roi=self.roi,
        )
    if stage == "test":
        test_samples = self.data[self.data["split"] == "test"]["samples"].tolist()
        test_targets = self.data[self.data["split"] == "test"]["targets"]
        self.test_dataset = self.dataset(
            class_to_idx=self.class_to_idx,
            samples=test_samples,
            targets=test_targets.tolist(),
            transform=self.test_transform,
            roi=self.roi,
            allow_missing_label=True,
        )

test_dataloader()

Returns the test dataloader.

Raises:

  • ValueError

    If test dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
def test_dataloader(self) -> DataLoader:
    """Returns the test dataloader.

    Raises:
        ValueError: If test dataset is not initialized.


    Returns:
        test dataloader.
    """
    if not self.test_dataset_available:
        raise ValueError("Test dataset is not initialized")

    loader = DataLoader(
        self.test_dataset,
        batch_size=self.batch_size,
        shuffle=False,
        num_workers=self.num_workers,
        drop_last=False,
        pin_memory=True,
        persistent_workers=self.num_workers > 0,
    )
    return loader

train_dataloader()

Returns a list of train dataloader.

Raises:

  • ValueError

    If train dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
def train_dataloader(self) -> List[DataLoader]:
    """Returns a list of train dataloader.

    Raises:
        ValueError: If train dataset is not initialized.

    Returns:
        list of train dataloader.
    """
    if not self.train_dataset_available:
        raise ValueError("Train dataset is not initialized")

    loader = []
    for dataset in self.train_dataset:
        loader.append(
            DataLoader(
                dataset,
                batch_size=self.batch_size,
                shuffle=not self.cache,
                num_workers=self.num_workers,
                drop_last=False,
                pin_memory=True,
            )
        )
    return loader

val_dataloader()

Returns a list of validation dataloader.

Raises:

  • ValueError

    If validation dataset is not initialized.

Returns:

Source code in quadra/datamodules/classification.py
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
def val_dataloader(self) -> List[DataLoader]:
    """Returns a list of validation dataloader.

    Raises:
        ValueError: If validation dataset is not initialized.

    Returns:
        List of validation dataloader.
    """
    if not self.val_dataset_available:
        raise ValueError("Validation dataset is not initialized")

    loader = []
    for dataset in self.val_dataset:
        loader.append(
            DataLoader(
                dataset,
                batch_size=self.batch_size,
                shuffle=False,
                num_workers=self.num_workers,
                drop_last=False,
                pin_memory=True,
            )
        )

    return loader