peal.data.custom_datasets.SquareDataset¶
- class peal.data.custom_datasets.SquareDataset[source]¶
Bases:
Image2MixedDatasetSynthetic 64x64 squares whose colour is confounded by the background.
An image shows a bordered square with red interior intensity
ColorA(the true feature) on a grey background of intensityColorB(the confounder), at one of nine positions.SquareDatasetGeneratorwrites the dataset on first use and only supportsconfig.confounder_probability == 0.5; confounded variants are derived from that pool elsewhere. Since both generative factors are known, this class provides an exact 2D latent readout for the decision boundary and counterfactual plots, either from the hint mask or from an oracle model.- Parameters:
config (
DataConfig) – Usesdataset_path,confounder_probabilityandconfounding_factors.**kwargs – Forwarded to
Image2MixedDataset.
- Variables:
oracle (
torch.nn.Module, optional) –$PEAL_RUNS/square/colora_confounding_colorb/ classifier_all_attributes_resnet18/model.cplin eval mode, if present. Used bysample_to_2d_latentwhen no mask is given.- Raises:
NotImplementedError – If the dataset has to be generated but
confounder_probabilityis not 0.5.
Methods
Mean intensity outside the hint mask, i.e. the background level.
Mean red intensity inside the hint mask, i.e. the square's interior.
Contrastive collage annotated with the oracle and hint latents.
Render the counterfactual arrow plot twice: from the oracle and the hints.
Map a sample to its two generative factors.
Draw both the generic oracle plot and the precise hint-based plot.
Render the decision boundary over the true generative factors.
Attributes
- __init__(config, **kwargs)[source]¶
Generate the dataset if missing and load the latent oracle if present.
- Parameters:
config (DataConfig)
- visualize_decision_boundary(*args, **kwargs)[source]¶
Draw both the generic oracle plot and the precise hint-based plot.
The base implementation is run with hints disabled (so the predictor sees plain images), the previous hint setting is restored, and
visualize_decision_boundary_preciseis called with the same arguments.
- visualize_decision_boundary_precise(predictor, batch_size, device, path, temperature=1.0, train_dataloader=None, val_dataloaders=[], val_weights=[], test_dataloader=None, **kwargs)[source]¶
Render the decision boundary over the true generative factors.
Unlike the base implementation, which fits a model in an estimated latent space, this evaluates
predictoron images synthesised directly from the grid: for each of the nine square positions a 100x100 grid of(ColorA, ColorB)pairs is rendered withlatent_to_square_imageand the softmax probability of class 0 is averaged over the positions. The grid is cached next to the plot as a.npyfile and reused on later calls. Train and validation samples are then overlaid at their measured foreground and background intensity, which needs hints and therefore temporarily enables them on the given loaders.- Parameters:
predictor (
torch.nn.Module) – Classifier, evaluated ondevicein batches ofbatch_size.batch_size (
int) – Batch size of the grid evaluation.device (
torch.deviceorstr) – Device the predictor runs on.path (
str) – Path of the non-precise plot;.pngis replaced by_precise.pngand the cached grid by_precise.npy.temperature (
float, optional) – Softmax temperature applied to the logits.train_dataloader (
DataLoaderorDataloaderMixer, optional) – Up to 8 batches are plotted as triangles.val_dataloaders (
listorWeightedDataloaderList, optional) – Validation loaders, plotted as squares; each gets a budget of8 // val_weights[i]batches.val_weights (
listoffloat, optional) – Weights ofval_dataloaders; taken from the list itself when it is aWeightedDataloaderList.test_dataloader (
DataLoader, optional) – Unused; kept for interface compatibility.
- check_foreground(x, hint)[source]¶
Mean red intensity inside the hint mask, i.e. the square’s interior.
- Parameters:
x (
torch.Tensor) – Image[..., C, H, W]in the dataset’s own value range; only channel 0 is read.hint (
torch.Tensor) – Binary mask of the inner square with the same shape.
- Returns:
Scalar mean of the masked red channel.
- Return type:
Notes
In contrast to
check_backgroundthe image is not projected to the pytorch default range first.
- check_background(x, hint)[source]¶
Mean intensity outside the hint mask, i.e. the background level.
xis mapped to the[0, 1]pytorch default range first and averaged over all channels where the mask is zero.- Parameters:
x (
torch.Tensor) – Image[..., C, H, W]in the dataset’s own value range.hint (
torch.Tensor) – Binary mask of the inner square.
- Returns:
Scalar mean intensity of the unmasked area.
- Return type:
- sample_to_2d_latent(sample, mask=None)[source]¶
Map a sample to its two generative factors.
With a hint mask the factors are measured directly as
[check_foreground, check_background]. Without a mask the oracle classifier is applied instead and, ifconfig.confounding_factorsis set, its outputs are reduced to the attributes named there.- Parameters:
sample (
torch.Tensor) – Image[C, H, W]or batch[N, C, H, W]; a single image is unsqueezed for the oracle and squeezed again afterwards.mask (
torch.Tensor, optional) – Hint mask of the inner square.
- Returns:
The two latent values.
- Return type:
- global_counterfactual_visualization(filename, x_list, counterfactuals, y_target_start_confidence, y_target_end_confidence, y_list, hint_list, attempts=1)[source]¶
Render the counterfactual arrow plot twice: from the oracle and the hints.
The base implementation is first called with hints disabled and without masks (latents from the oracle), then again with
hint_listwriting to*_precise.png- matching the two grids thatvisualize_decision_boundarysaves.- Parameters:
filename (
str) – Output path of the oracle version; the precise one replaces.pngby_precise.png.x_list (
listoftorch.Tensor) – Factual and counterfactual images.counterfactuals (
listoftorch.Tensor) – Factual and counterfactual images.y_target_start_confidence (
listoffloat) – Target-class confidence before and after the edit.y_target_end_confidence (
listoffloat) – Target-class confidence before and after the edit.y_list (
list) – Ground truth labels.hint_list (
listoftorch.Tensor) – Masks used by the precise version.attempts (
int, optional) – Counterfactual attempts per sample (one subplot each).
- 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, idx_to_info=None, **kwargs)[source]¶
Contrastive collage annotated with the oracle and hint latents.
When no
idx_to_infocallback is passed, one is built that reports per confounding factor the factual and counterfactual value as measured by the oracle and by the hint mask, e.g.ColorA: 0.2 (Oracle) / 0.21 (Hint) -> 0.8 (Oracle) / 0.79 (Hint). Everything else is delegated toImageDataset.generate_contrastive_collage.- Parameters:
x_list (
listoftorch.Tensor) – Factual and counterfactual images.x_counterfactual_list (
listoftorch.Tensor) – Factual and counterfactual images.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 of the collages.idx_to_info (
callable, optional) –(x, x_counterfactual, hint) -> strused for the title suffix.**kwargs – Passed through to the base implementation.
- Returns:
Whatever the base implementation returns.
- Return type: