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

ImageFolder

ImageFolderDataset reads classification datasets laid out as one subdirectory per class. Each immediate child folder of root becomes a class name; supported image files inside are indexed as samples.

When absorb_folders=True, an extra top-level tier is allowed (root/<split>/<class>/...), which is useful when train/val folders are already separated in the dataset.

Example layout:

FundusDataset/
├── Glaucoma/
│   ├── img001.png
│   └── img002.png
└── Non Glaucoma/
    ├── img003.png
    └── img004.png
from hyppopipe.data import TrainVal
from hyppopipe.data.dataset import ImageFolderDataset

train_dataset = ImageFolderDataset("data/train")
val_dataset = ImageFolderDataset("data/val")
data = TrainVal(train=train_dataset, val=val_dataset)

Re-splitting an existing dataset

Set absorb_folders=True to ignore pre-defined train/val folders and call split_random_fractions(dataset, (0.7, 0.15, 0.15)) to create new splits without moving files on disk.

Documentation

Bases: ImageDataset, ClassificationConvertible

Classification dataset with one subdirectory per class under root.

Each immediate child directory of root is treated as a class name. Image files with supported extensions inside those directories become samples. When absorb_folders is True, an extra top-level directory tier is allowed (root/<top>/<class>/...).

__getitem__(index)

Load image and class index for index.

Parameters:

Name Type Description Default
index int

Sample index in [0, len(self)).

required

Returns:

Type Description
tuple[Image, int]

A pair of loaded Image and integer class index.

__init__(root, absorb_folders=False, *, strict=True)

Build the index of image paths and class indices.

Parameters:

Name Type Description Default
root str | Path

Dataset root directory.

required
absorb_folders bool

If True, expect root/<top>/<class>/ layout instead of root/<class>/.

False
strict bool

Passed to Image.from_path when loading samples.

True

__len__()

Number of indexed samples.

as_classification_dataset()

Return this dataset for classification training.