peal.data.datasets.Image2ClassDataset

class peal.data.datasets.Image2ClassDataset[source]

Bases: ImageDataset

ImageFolder-style dataset: one sub-directory per class.

Class names are the sorted directory names under <root_dir>/<config.x_selection> and the class index is the position in that list. All (class, file) pairs are shuffled with seed 0 and split by config.split into train/val/test. Masks, if config.has_hints, are expected under <root_dir>/masks/[<class>/] and must exist for every image. config.class_ratios subsamples the split to fixed class proportions.

Parameters:
  • mode (str) – Split: "train", "val" or "test"; anything else keeps all.

  • config (DataConfig) – Uses dataset_path, x_selection, split, has_hints, input_size, output_size, class_ratios and in_memory.

  • root_dir (str, optional) – Dataset root; falls back to data_dir and then config.dataset_path. config.dataset_path is rewritten to it.

  • data_dir (str, optional) – Alternative spelling of root_dir.

  • transform (callable, optional) – Applied to image and mask (same RNG state). Defaults to ToTensor().

  • task_config (TaskConfig, optional) – Only its presence matters (for class_ratios balancing).

  • return_dict (bool, optional) – Return a dict from __getitem__ instead of (x, y...).

Variables:
  • idx_to_name (list of str) – Class index to directory name.

  • urls (list of tuple) – (class_name, filename) of the current split/restriction.

Methods

class_idx_to_name

Return the directory name of class class_idx.

disable_class_restriction

Restore backup_urls and clear the restriction flag.

disable_groups

Stop returning has_confounder.

disable_hints

Stop returning masks and restore the full urls list.

disable_idx

Stop returning the sample index.

disable_string_description

Stop returning the description.

disable_tokens

Drop the tokenizer and restore the previous description setting.

disable_url

Stop returning the filename.

enable_class_restriction

Keep only samples of the given class(es); the full list goes to backup_urls.

enable_groups

Also return has_confounder (always 0 unless a subclass overrides it).

enable_hints

Return masks as mask and restrict urls to samples that have one.

enable_idx

Also return the sample index.

enable_string_description

Also return the class directory name as description.

enable_tokens

Also return tokens: the description tokenized with tokenizer.

enable_url

Also return the filename as url.

has_confounder

Confounder presence for a file; placeholder returning 0.

load_in_memory

Cache all images (and masks, if has_hints) of the split as arrays.

set_task_specific_urls

Subsample urls so the classes follow config.class_ratios.

Attributes

output_size

Number of classes from config.output_size (int or one-element list).

__init__(mode, config, root_dir=None, data_dir=None, transform=torchvision.transforms.ToTensor, task_config=None, return_dict=False)[source]

Scan the class directories, split them and optionally cache images.

load_in_memory()[source]

Cache all images (and masks, if has_hints) of the split as arrays.

class_idx_to_name(class_idx)[source]

Return the directory name of class class_idx.

enable_hints()[source]

Return masks as mask and restrict urls to samples that have one.

disable_hints()[source]

Stop returning masks and restore the full urls list.

enable_string_description()[source]

Also return the class directory name as description.

disable_string_description()[source]

Stop returning the description.

enable_tokens(tokenizer)[source]

Also return tokens: the description tokenized with tokenizer.

Parameters:

tokenizer (callable) – HuggingFace-style tokenizer with model_max_length.

disable_tokens()[source]

Drop the tokenizer and restore the previous description setting.

enable_idx()[source]

Also return the sample index.

disable_idx()[source]

Stop returning the sample index.

enable_url()[source]

Also return the filename as url.

disable_url()[source]

Stop returning the filename.

property output_size

Number of classes from config.output_size (int or one-element list).

set_task_specific_urls()[source]

Subsample urls so the classes follow config.class_ratios.

Same scheme as Image2MixedDataset.set_task_specific_keys; the original list is saved in urls_backup.

enable_class_restriction(class_idx)[source]

Keep only samples of the given class(es); the full list goes to backup_urls.

Parameters:

class_idx (int or list of int) – Class index or indices to keep.

disable_class_restriction()[source]

Restore backup_urls and clear the restriction flag.

enable_groups()[source]

Also return has_confounder (always 0 unless a subclass overrides it).

disable_groups()[source]

Stop returning has_confounder.

has_confounder(filename)[source]

Confounder presence for a file; placeholder returning 0.

Subclasses that know their confounder override this. The base implementation prints a warning.

Parameters:

filename (str) – Image filename.

Returns:

Always 0.

Return type:

int