peal.data.datasets.ImageDataset¶
- class peal.data.datasets.ImageDataset[source]¶
Bases:
PealDatasetBase class of the image datasets with the explainer-facing callbacks.
Subclasses provide loading (
__getitem__,__len__,output_size) and the value-range helpersproject_to_pytorch_default/project_from_pytorch_defaultofPealDataset; this class adds collage rendering, serialization of counterfactual datasets and DINO-feature metrics. ADINOEvaluatoris created lazily indino_evaland fitted on this dataset the first time a metric is requested.Methods
Mahalanobis distance of images to the dataset's DINO feature Gaussian.
Mean DINO-FID of several image batches against the dataset.
Fraction of confidences above 0.5, averaged over samples.
Render factual/counterfactual/SSIM-difference collages to PNG files.
Mean DINO-LPIPS between corresponding batches of
x1andx2.Save images (and optional masks) as a class-folder dataset on disk.
Compute DINO-feature FID and LPIPS for generated images.
Mean per-sample pixel variance across attempts.
- generate_contrastive_collage(x_list, x_counterfactual_list, y_target_list, y_source_list, y_list, y_target_start_confidence_list, y_target_end_confidence_list, base_path, start_idx=0, y_original_teacher_list=None, y_counterfactual_teacher_list=None, feedback_list=None, hint_list=None, idx_to_info=None, tracking_level=1, history_list=None, additional_info=None, **kwargs)[source]¶
Render factual/counterfactual/SSIM-difference collages to PNG files.
Images are mapped to
[0, 1]withproject_to_pytorch_default, a high-contrast difference heatmap is computed for every pair and, iftracking_level >= 1, a three-panel figure titled with the labels, confidences, optional latent info, history and teacher feedback is saved as<base_path>/<zero-padded index>_collage.png. Titles longer than 800 characters are truncated to keep matplotlib from failing.- Parameters:
x_list (
listoftorch.Tensor) – Images of shape[C, H, W]in the dataset’s value range.x_counterfactual_list (
listoftorch.Tensor) – Images of shape[C, H, W]in the dataset’s value range.y_target_list (
list) – Target class, predicted source class and ground truth per pair.y_source_list (
list) – Target class, predicted source class and ground truth per pair.y_list (
list) – Target class, predicted source class and ground truth per pair.y_target_start_confidence_list (
list) – Target-class confidence before and after the edit.y_target_end_confidence_list (
list) – Target-class confidence before and after the edit.base_path (
str) – Output directory, created if missing; also stored inself.base_path.start_idx (
int, optional) – Offset added to the running index in the filenames.y_original_teacher_list – Teacher verdicts appended to the title when
feedback_listis given.y_counterfactual_teacher_list – Teacher verdicts appended to the title when
feedback_listis given.feedback_list – Teacher verdicts appended to the title when
feedback_listis given.hint_list (
list, optional) – Per-sample hints passed on toidx_to_info.idx_to_info (
callable, optional) –f(x, x_counterfactual, hint) -> strfor an extra title line.tracking_level (
int, optional) – Below 1 no figures are written (paths becomeNone).history_list (
list, optional) – Per-sample optimisation histories printed into the title.additional_info (
str, optional) – Free text drawn at the bottom of the figure.**kwargs – Ignored.
- Returns:
(heatmap_list, collage_paths): difference heatmaps as tensors and the PNG paths (Nonewhere nothing was written).- Return type:
- serialize_dataset(output_dir, x_list, y_list, hint_list=[], sample_names=None, classifier=None)[source]¶
Save images (and optional masks) as a class-folder dataset on disk.
Creates
<output_dir>/imgs/<class>/(andmasks/<class>/when hints are given) for every class, writes each image twice (inside its class folder and directly underimgs/) as 8-bit PNG afterproject_to_pytorch_defaultand writes anImgPath,ClassCSV to<output_dir>/<config.label_rel_path>so the result can be loaded again as a PEAL dataset.- Parameters:
output_dir (
str) – Root of the new dataset.x_list (
listoftorch.Tensor) – Images[C, H, W]in this dataset’s value range.y_list (
list) – Integer class labels.hint_list (
listoftorch.Tensor, optional) – Masks[C, H, W]in[0, 1]; saved when non-empty.sample_names (
listofstr) – File stem per sample (required).classifier – Unused.
- track_generator_performance(generator, batch_size=None, num_samples=None)[source]¶
Compute DINO-feature FID and LPIPS for generated images.
Draws
num_samplesimages from the generator (or uses the given tensor), fits theDINOEvaluatoron this dataset on first use (for CelebA a shared reference state under$PEAL_RUNS/sce_cfkd/results_for_paper/...is loaded if present) and compares against the first real samples of the dataset.- Parameters:
generator (
Generatorortorch.Tensor) – Generator withsample_xor a batch of images[B, C, H, W].batch_size (
int, optional) – Sampling batch size; defaults togenerator.config.batch_sizeor 1.num_samples (
int, optional) – Number of generated images evaluated. Defaults tobatch_size.
- Returns:
fid/dino_fid,lpips/dino_lpipsand, ifreference_fidis set on the dataset,quality_scorein(0, 1].- Return type:
- Raises:
NotImplementedError – For unsupported
generatortypes.
- calculate_outlier_score(x)[source]¶
Mahalanobis distance of images to the dataset’s DINO feature Gaussian.
- Parameters:
x (
torch.Tensor) – Batch[B, C, H, W]; its size is also used as the fitting batch size on first use.- Returns:
{"absolute": scores}plus"relative"(divided byreference_outlier_scores) when that attribute is set.- Return type:
- variance(x_list)[source]¶
Mean per-sample pixel variance across attempts.
- Parameters:
x_list (
listoflistoftorch.Tensor) –x_list[j][i]is attemptjfor samplei.- Returns:
Variance over attempts, averaged over pixels and samples.
- Return type: