peal.adaptors.clarc.ClArC¶
- class peal.adaptors.clarc.ClArC[source]¶
Bases:
AdaptorBase class of the ClArC adaptors: data setup, sweep loop and evaluation.
Subclasses implement
_run()(one correction for a given layer and strength) andget_evaluation_filename().- Parameters:
adaptor_config (
ClArCConfig) – SeeClArCConfig.- Variables:
original_model (
torch.nn.Module) – The loaded, uncorrected predictor;modelis 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 withx,yandhas_confounderand the train set is restricted toattacked_classwhen that is set.test_data_unpoisoned (
DataLoaderorNone) – Test loader ofconfig.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
Collect pooled activations and artifact labels from the train loader.
Return the csv file name for the results on
dataset_name.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 bestavg_group_acconoriginal-val. Writes one csv per evaluation dataset (named byget_evaluation_filename()) andbest_model_result.txtintoconfig.base_dir.
- get_evaluation_filename(dataset_name)[source]¶
Return the csv file name for the results on
dataset_name.
- 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 atconfig.max_samples. Pooling followsconfig.cav_mode.annotations (
torch.Tensor) – Shape(N,)with 0 for non-confounder and 1 for confounder (flipped ifconfig.reverse_cav_direction).
- Return type:
- get_stats(dataloader, model)¶
Compute accuracy per class and artifact group on a dataloader.
- Parameters:
dataloader (
DataLoader) – Yields dict batches withx,yandhas_confounder.model (
torch.nn.Module) – Model to evaluate (switched to eval mode).
- Returns:
n,artifact_freq,accuracy,artifact_accuracy,non-artifact_accuracyplus per classc{y}_n,c{y}_artifact_freq,c{y}_accuracy,c{y}_artifact_accuracy,c{y}_non-artifact_accuracy, and the aggregatesavg_group_acc/worst_group_accover all (class, artifact) groups that are non-empty.- Return type: