peal.adaptors.clarc.ClArC

class peal.adaptors.clarc.ClArC[source]

Bases: Adaptor

Base class of the ClArC adaptors: data setup, sweep loop and evaluation.

Subclasses implement _run() (one correction for a given layer and strength) and get_evaluation_filename().

Parameters:

adaptor_config (ClArCConfig) – See ClArCConfig.

Variables:
  • original_model (torch.nn.Module) – The loaded, uncorrected predictor; model is a deep copy that is reset after every sweep step.

  • test_dataloader (train_dataloader, val_dataloader,) – Built with create_dataloaders_from_datasource; train and val return dict batches with x, y and has_confounder and the train set is restricted to attacked_class when that is set.

  • test_data_unpoisoned (DataLoader or None) – Test loader of config.unpoisoned_data.

  • cav_cache (CavCache) – CAV, annotations and activations of the last processed layer so a layer is only encoded once across correction strengths.

Methods

get_annotations_and_activations

Collect pooled activations and artifact labels from the train loader.

get_evaluation_filename

Return the csv file name for the results on dataset_name.

get_stats

run

Sweep all layers and correction strengths and record the results.

__init__(adaptor_config)[source]

Load the model, build the dataloaders and create base_dir.

Parameters:

adaptor_config (ClArCConfig)

run()[source]

Sweep all layers and correction strengths and record the results.

Evaluates the uncorrected model first, then every (layer_index, correction_strength) pair via _run(), keeping the model with the best avg_group_acc on original-val. Writes one csv per evaluation dataset (named by get_evaluation_filename()) and best_model_result.txt into config.base_dir.

get_evaluation_filename(dataset_name)[source]

Return the csv file name for the results on dataset_name.

Parameters:

dataset_name (str)

Return type:

str

get_annotations_and_activations(feature_extractor=None)[source]

Collect pooled activations and artifact labels from the train loader.

Parameters:

feature_extractor (torch.nn.Module, optional) – Prefix of the model up to the correction layer; None means the raw input is used as activation.

Returns:

  • activations (torch.Tensor) – Shape (N, D); non-confounder samples first, then confounder samples, each group capped at config.max_samples. Pooling follows config.cav_mode.

  • annotations (torch.Tensor) – Shape (N,) with 0 for non-confounder and 1 for confounder (flipped if config.reverse_cav_direction).

Return type:

tuple[torch.Tensor, torch.Tensor]

get_stats(dataloader, model)

Compute accuracy per class and artifact group on a dataloader.

Parameters:
  • dataloader (DataLoader) – Yields dict batches with x, y and has_confounder.

  • model (torch.nn.Module) – Model to evaluate (switched to eval mode).

Returns:

n, artifact_freq, accuracy, artifact_accuracy, non-artifact_accuracy plus per class c{y}_n, c{y}_artifact_freq, c{y}_accuracy, c{y}_artifact_accuracy, c{y}_non-artifact_accuracy, and the aggregates avg_group_acc / worst_group_acc over all (class, artifact) groups that are non-empty.

Return type:

dict