peal.data.datasets.Image2ClassDataset¶
- class peal.data.datasets.Image2ClassDataset[source]¶
Bases:
ImageDatasetImageFolder-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 byconfig.splitinto train/val/test. Masks, ifconfig.has_hints, are expected under<root_dir>/masks/[<class>/]and must exist for every image.config.class_ratiossubsamples the split to fixed class proportions.- Parameters:
mode (
str) – Split:"train","val"or"test"; anything else keeps all.config (
DataConfig) – Usesdataset_path,x_selection,split,has_hints,input_size,output_size,class_ratiosandin_memory.root_dir (
str, optional) – Dataset root; falls back todata_dirand thenconfig.dataset_path.config.dataset_pathis rewritten to it.data_dir (
str, optional) – Alternative spelling ofroot_dir.transform (
callable, optional) – Applied to image and mask (same RNG state). Defaults toToTensor().task_config (
TaskConfig, optional) – Only its presence matters (forclass_ratiosbalancing).return_dict (
bool, optional) – Return a dict from__getitem__instead of(x, y...).
- Variables:
Methods
Return the directory name of class
class_idx.Restore
backup_urlsand clear the restriction flag.Stop returning
has_confounder.Stop returning masks and restore the full
urlslist.Stop returning the sample index.
Stop returning the description.
Drop the tokenizer and restore the previous description setting.
Stop returning the filename.
Keep only samples of the given class(es); the full list goes to
backup_urls.Also return
has_confounder(always 0 unless a subclass overrides it).Return masks as
maskand restricturlsto samples that have one.Also return the sample index.
Also return the class directory name as
description.Also return
tokens: the description tokenized withtokenizer.Also return the filename as
url.Confounder presence for a file; placeholder returning 0.
Cache all images (and masks, if
has_hints) of the split as arrays.Subsample
urlsso the classes followconfig.class_ratios.Attributes
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.
- enable_tokens(tokenizer)[source]¶
Also return
tokens: the description tokenized withtokenizer.- Parameters:
tokenizer (
callable) – HuggingFace-style tokenizer withmodel_max_length.
- property output_size¶
Number of classes from
config.output_size(int or one-element list).
- set_task_specific_urls()[source]¶
Subsample
urlsso the classes followconfig.class_ratios.Same scheme as
Image2MixedDataset.set_task_specific_keys; the original list is saved inurls_backup.