Перейти к содержанию

PairedImageMaskFolder

PairedImageMaskFolderDataset reads semantic segmentation datasets where masks are stored as images. The constructor expects a root folder with two subdirectories — images and masks — paired by matching file stems.

Folder names default to "images" and "masks" but can be overridden.

Example layout:

NailsSegmentation/
├── images/
│   ├── 1eecab90-1a92-43a7-b952-0204384e1fae.jpg
│   └── ...
└── labels/
    ├── 1eecab90-1a92-43a7-b952-0204384e1fae.jpg
    └── ...
from hyppopipe.data.dataset import PairedImageMaskFolderDataset

nails_split = PairedImageMaskFolderDataset(
    "path/to/dataset/",
    image_folder="images",
    mask_folder="labels",
).as_split_data(fractions=(0.7, 0.15, 0.15))

By index, the dataset returns an image tensor and a per-pixel class map suitable for segmentation training.

Documentation

Bases: ImageDataset

Semantic segmentation dataset from parallel image and mask folders.

Pairs files by matching stem names under image_folder and mask_folder (both relative to root). Masks are converted to per-pixel class indices when loaded.

__getitem__(index)

Load image tensor and semantic class map for index.

Parameters:

Name Type Description Default
index int

Sample index.

required

Returns:

Type Description
tuple[Tensor, Tensor]

Image CHW tensor and a 2D long tensor of per-pixel class ids.

__init__(root, image_folder='images', mask_folder='masks', *, class_names=None, strict=True)

Index image–mask pairs and optional class metadata.

Parameters:

Name Type Description Default
root str | Path

Root directory containing image and mask subfolders.

required
image_folder str | Path

Subpath (under root) with input images.

'images'
mask_folder str | Path

Subpath (under root) with mask images.

'masks'
class_names list[str] | None

Optional human-readable class names for metadata.

None
strict bool

Passed to Image.from_path when loading files.

True

Raises:

Type Description
ValueError

If image or mask folder is missing or no pairs exist.

__len__()

Number of image–mask pairs.

as_segmentation_dataset(*, kind='semantic')

Return this dataset for semantic segmentation training.

Parameters:

Name Type Description Default
kind str

Segmentation task kind; only "semantic" is supported.

'semantic'

Returns:

Type Description
Self

This dataset instance.

Raises:

Type Description
ValueError

If kind is not "semantic".

as_split_data(fractions=(0.8, 0.2), *, seed=None)

Random train/val or train/val/test split for Trainer.

Unlike YAML-based datasets, a single paired folder is split randomly via split_random_fractions (default fractions (0.8, 0.2) for train/val).

Parameters:

Name Type Description Default
fractions tuple[float, float] | tuple[float, float, float]

Two or three non-negative fractions that sum to 1.

(0.8, 0.2)
seed int | None

Optional RNG seed for reproducible splits.

None

Returns:

Type Description
TrainVal | TrainValTest

TrainVal or TrainValTest wrapping subset views of this dataset.