Skip to content

segmentation

SegmentationDataModule(data_path, name='segmentation_datamodule', test_size=0.3, val_size=0.3, seed=42, dataset=SegmentationDataset, batch_size=32, num_workers=6, train_transform=None, test_transform=None, val_transform=None, train_split_file=None, test_split_file=None, val_split_file=None, num_data_class=None, exclude_good=False, **kwargs)

Bases: BaseDataModule

Base class for segmentation datasets.

Parameters:

  • data_path (str) –

    Path to the data main folder.

  • name (str) –

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

  • val_size (float) –

    The validation split. Defaults to 0.2.

  • test_size (float) –

    The test split. Defaults to 0.2.

  • seed (int) –

    Random generator seed. Defaults to 42.

  • dataset (Type[SegmentationDataset]) –

    Dataset class.

  • batch_size (int) –

    Batch size. Defaults to 32.

  • num_workers (int) –

    Number of workers for dataloaders. Defaults to 16.

  • train_transform (Optional[albumentations.Compose]) –

    Transformations for train dataset. Defaults to None.

  • val_transform (Optional[albumentations.Compose]) –

    Transformations for validation dataset. Defaults to None.

  • test_transform (Optional[albumentations.Compose]) –

    Transformations for test dataset. Defaults to None.

  • num_data_class (Optional[int]) –

    The number of samples per class. Defaults to None.

  • exclude_good (bool) –

    If True, exclude good samples from the dataset. Defaults to False.

Source code in quadra/datamodules/segmentation.py
44
45
46
47
48
49
50
51
52
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
def __init__(
    self,
    data_path: str,
    name: str = "segmentation_datamodule",
    test_size: float = 0.3,
    val_size: float = 0.3,
    seed: int = 42,
    dataset: Type[SegmentationDataset] = SegmentationDataset,
    batch_size: int = 32,
    num_workers: int = 6,
    train_transform: Optional[albumentations.Compose] = None,
    test_transform: Optional[albumentations.Compose] = None,
    val_transform: Optional[albumentations.Compose] = None,
    train_split_file: Optional[str] = None,
    test_split_file: Optional[str] = None,
    val_split_file: Optional[str] = None,
    num_data_class: Optional[int] = None,
    exclude_good: bool = False,
    **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.test_size = test_size
    self.val_size = val_size
    self.num_data_class = num_data_class
    self.exclude_good = exclude_good
    self.train_split_file = train_split_file
    self.test_split_file = test_split_file
    self.val_split_file = val_split_file
    self.dataset = dataset
    self.train_dataset: SegmentationDataset
    self.val_dataset: SegmentationDataset
    self.test_dataset: SegmentationDataset

predict_dataloader()

Returns a dataloader used for predictions.

Source code in quadra/datamodules/segmentation.py
363
364
365
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/segmentation.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def setup(self, stage=None):
    """Setup data module based on stages of training."""
    if stage in ["fit", "train"]:
        self.train_dataset = self.dataset(
            image_paths=self.data[self.data["split"] == "train"]["samples"].tolist(),
            mask_paths=self.data[self.data["split"] == "train"]["masks"].tolist(),
            mask_preprocess=self._preprocess_mask,
            labels=self.data[self.data["split"] == "train"]["targets"].tolist(),
            object_masks=None,
            transform=self.train_transform,
            batch_size=None,
            defect_transform=None,
            resize=None,
        )
        self.val_dataset = self.dataset(
            image_paths=self.data[self.data["split"] == "val"]["samples"].tolist(),
            mask_paths=self.data[self.data["split"] == "val"]["masks"].tolist(),
            defect_transform=None,
            labels=self.data[self.data["split"] == "val"]["targets"].tolist(),
            object_masks=None,
            batch_size=None,
            mask_preprocess=self._preprocess_mask,
            transform=self.test_transform,
            resize=None,
        )
    elif stage == "test":
        self.test_dataset = self.dataset(
            image_paths=self.data[self.data["split"] == "test"]["samples"].tolist(),
            mask_paths=self.data[self.data["split"] == "test"]["masks"].tolist(),
            labels=self.data[self.data["split"] == "test"]["targets"].tolist(),
            object_masks=None,
            batch_size=None,
            mask_preprocess=self._preprocess_mask,
            transform=self.test_transform,
            resize=None,
        )
    elif stage == "predict":
        pass
    else:
        raise ValueError(f"Unknown stage {stage}")

test_dataloader()

Returns the test dataloader.

Raises:

  • ValueError

    If test dataset is not initialized.

Returns:

Source code in quadra/datamodules/segmentation.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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/segmentation.py
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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/segmentation.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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.val_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,
    )

SegmentationMulticlassDataModule(data_path, idx_to_class, name='multiclass_segmentation_datamodule', dataset=SegmentationDatasetMulticlass, batch_size=32, test_size=0.3, val_size=0.3, seed=42, num_workers=6, train_transform=None, test_transform=None, val_transform=None, train_split_file=None, test_split_file=None, val_split_file=None, exclude_good=False, num_data_train=None, one_hot_encoding=False, **kwargs)

Bases: BaseDataModule

Base class for segmentation datasets with multiple classes.

Parameters:

  • data_path

    Path to the data main folder.

  • idx_to_class (Dict) –

    dict with corrispondence btw mask index and classes: {1: class_1, 2: class_2, ..., N: class_N} except background class which is 0.

  • name

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

  • dataset (Type[SegmentationDatasetMulticlass]) –

    Dataset class.

  • batch_size

    Batch size. Defaults to 32.

  • val_size

    The validation split. Defaults to 0.3.

  • test_size

    The test split. Defaults to 0.3.

  • seed

    Random generator seed. Defaults to 42.

  • num_workers (int) –

    Number of workers for dataloaders. Defaults to 6.

  • train_transform (Optional[albumentations.Compose]) –

    Transformations for train dataset. Defaults to None.

  • val_transform

    Transformations for validation dataset. Defaults to None.

  • test_transform

    Transformations for test dataset. Defaults to None.

  • train_split_file (Optional[str]) –

    path to txt file with training samples list

  • val_split_file (Optional[str]) –

    path to txt file with validation samples list

  • test_split_file (Optional[str]) –

    path to txt file with test samples list

  • exclude_good

    If True, exclude good samples from the dataset. Defaults to False.

  • num_data_train (Optional[int]) –

    number of samples to use in the train split (shuffle the samples and pick the first num_data_train)

  • one_hot_encoding (bool) –

    if True, the labels are one-hot encoded to N channels, where N is the number of classes. If False, masks are single channel that contains values as class indexes. Defaults to True.

Source code in quadra/datamodules/segmentation.py
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def __init__(
    self,
    data_path: str,
    idx_to_class: Dict,
    name: str = "multiclass_segmentation_datamodule",
    dataset: Type[SegmentationDatasetMulticlass] = SegmentationDatasetMulticlass,
    batch_size: int = 32,
    test_size: float = 0.3,
    val_size: float = 0.3,
    seed: int = 42,
    num_workers: int = 6,
    train_transform: Optional[albumentations.Compose] = None,
    test_transform: Optional[albumentations.Compose] = None,
    val_transform: Optional[albumentations.Compose] = None,
    train_split_file: Optional[str] = None,
    test_split_file: Optional[str] = None,
    val_split_file: Optional[str] = None,
    exclude_good: bool = False,
    num_data_train: Optional[int] = None,
    one_hot_encoding: bool = False,
    **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.test_size = test_size
    self.val_size = val_size
    self.exclude_good = exclude_good
    self.train_split_file = train_split_file
    self.test_split_file = test_split_file
    self.val_split_file = val_split_file
    self.dataset = dataset
    self.idx_to_class = idx_to_class
    self.num_data_train = num_data_train
    self.one_hot_encoding = one_hot_encoding
    self.train_dataset: SegmentationDataset
    self.val_dataset: SegmentationDataset
    self.test_dataset: SegmentationDataset

predict_dataloader()

Returns a dataloader used for predictions.

Source code in quadra/datamodules/segmentation.py
718
719
720
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/segmentation.py
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
def setup(self, stage=None):
    """Setup data module based on stages of training."""
    if stage in ["fit", "train"]:
        train_data = self.data[self.data["split"] == "train"]
        val_data = self.data[self.data["split"] == "val"]

        self.train_dataset = self.dataset(
            image_paths=train_data["samples"].tolist(),
            mask_paths=train_data["masks"].tolist(),
            idx_to_class=self.idx_to_class,
            transform=self.train_transform,
            one_hot=self.one_hot_encoding,
        )
        self.val_dataset = self.dataset(
            image_paths=val_data["samples"].tolist(),
            mask_paths=val_data["masks"].tolist(),
            transform=self.val_transform,
            idx_to_class=self.idx_to_class,
            one_hot=self.one_hot_encoding,
        )
    elif stage == "test":
        self.test_dataset = self.dataset(
            image_paths=self.data[self.data["split"] == "test"]["samples"].tolist(),
            mask_paths=self.data[self.data["split"] == "test"]["masks"].tolist(),
            transform=self.test_transform,
            idx_to_class=self.idx_to_class,
            one_hot=self.one_hot_encoding,
        )
    elif stage == "predict":
        pass
    else:
        raise ValueError(f"Unknown stage {stage}")

test_dataloader()

Returns the test dataloader.

Raises:

  • ValueError

    If test dataset is not initialized.

Returns:

Source code in quadra/datamodules/segmentation.py
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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/segmentation.py
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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/segmentation.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
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.val_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,
    )