Skip to content

simet.datasets.no_class_image_dataset

simet.datasets.no_class_image_dataset

NoClassImageDataset

NoClassImageDataset(root, transform=None, target_transform=None)

Bases: VisionDataset

Unlabeled image dataset loaded from a folder tree.

Recursively scans the root directory for image files and returns samples as (image, target) pairs where the target is always 0 (dummy label). Useful for feature extraction, self-supervised preprocessing, or inference pipelines that expect a Dataset interface without class folders.

Supported extensions (case-insensitive): .jpg, .jpeg, .png, .bmp, .tiff.

Parameters:

Name Type Description Default
root Path | str

Root directory to scan recursively for images.

required
transform Callable | None

Optional transform applied to each PIL image (after RGB conversion). Typical usage is a torchvision transform pipeline producing a torch.Tensor. If omitted, the raw PIL image is returned.

None
target_transform Callable | None

Accepted for API parity with VisionDataset but not applied in this implementation because the target is a constant 0.

None

Attributes:

Name Type Description
image_paths list[Path]

Absolute paths to all discovered image files under root.

Notes
  • Directory traversal is recursive via Path.rglob('*').
  • File extension matching is case-insensitive.
  • The returned target is the integer 0; downstream code should ignore or replace it if labels are not needed.
  • If you need a tensor target or to apply target_transform, consider wrapping this dataset or subclassing and overriding __getitem__.
Example

ds = NoClassImageDataset("data/images", transform=my_tfms) len(ds) > 0 True x, y = ds[0] int(y) 0

Initialize the dataset; see class docstring for parameter details.

Source code in simet/datasets/no_class_image_dataset.py
54
55
56
57
58
59
60
61
62
def __init__(
    self,
    root: Path,
    transform: Callable | None = None,
    target_transform: Callable | None = None,
):  # type: ignore
    """Initialize the dataset; see class docstring for parameter details."""
    super().__init__(root, transform=transform, target_transform=target_transform)  # type: ignore
    self.image_paths = self._get_image_paths()

__getitem__

__getitem__(idx)

Load and return the (image, target) pair at index idx.

The image is opened with PIL and converted to RGB, then transform is applied if provided. The target is the constant integer 0.

Parameters:

Name Type Description Default
idx int

Index of the sample to retrieve.

required

Returns:

Type Description
tuple[Image | Tensor, int]

tuple[PIL.Image.Image | torch.Tensor, int]: Transformed image (or raw PIL image if no transform) and the dummy target 0.

Source code in simet/datasets/no_class_image_dataset.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def __getitem__(self, idx: int) -> tuple[Image.Image | torch.Tensor, int]:
    """Load and return the (image, target) pair at index ``idx``.

    The image is opened with PIL and converted to RGB, then ``transform`` is
    applied if provided. The target is the constant integer ``0``.

    Args:
        idx (int): Index of the sample to retrieve.

    Returns:
        tuple[PIL.Image.Image | torch.Tensor, int]:
            Transformed image (or raw PIL image if no transform) and the dummy target ``0``.
    """
    image_path = self.image_paths[idx]
    image = Image.open(image_path).convert("RGB")

    if self.transform:  # type: ignore
        image = self.transform(image)  # type: ignore

    return image, 0  # type: ignore  # 0 as dummy target

__len__

__len__()

Return the number of images discovered under root.

Source code in simet/datasets/no_class_image_dataset.py
82
83
84
def __len__(self) -> int:
    """Return the number of images discovered under ``root``."""
    return len(self.image_paths)