Principle:PeterL1n BackgroundMattingV2 Image dataset loading
| Knowledge Sources | |
|---|---|
| Domains | Data_Loading, Computer_Vision |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
A dataset abstraction that loads all images from a directory tree into a PyTorch Dataset, supporting recursive glob discovery and configurable color modes.
Description
Image dataset loading provides a standard interface for reading image files from a directory hierarchy. The loader recursively discovers all .jpg and .png files under a root directory, sorts them alphabetically for deterministic ordering, and returns individual images through PyTorch's Dataset interface. Each image is opened on demand using PIL and converted to the specified color mode (RGB, grayscale, etc.).
This design supports the matting pipeline's need for separate foreground, alpha, and background image directories that must maintain consistent ordering across paired datasets.
Usage
Use this principle when loading image data for training or inference. Create one ImagesDataset per directory (foreground, alpha, background) and combine them via ZipDataset to form paired training samples.
Theoretical Basis
The image dataset follows the PyTorch Dataset protocol:
# Abstract dataset protocol
class ImageDataset:
def __init__(self, root_dir, color_mode, transforms):
self.files = sorted(glob(root_dir + '/**/*.{jpg,png}'))
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
image = load_and_convert(self.files[idx], color_mode)
return apply_transforms(image, transforms)
Sorting ensures that paired datasets (foreground + alpha) maintain consistent alignment by index.