peal.adaptors.counterfactual_knowledge_distillation.CFKDConfig¶
- class peal.adaptors.counterfactual_knowledge_distillation.CFKDConfig[source]¶
Bases:
AdaptorConfigThe config template for an running the CFKD adaptor.
Loaded with
load_yaml_config(..., CFKDConfig)from a yaml whoseadaptor_typeis"CFKD". Each field is described by the string literal that follows it in the class body, which is what Sphinx renders as that field’s attribute documentation. The few fields without such a string are:- Parameters:
sparse_dictionary (
SparseDictionaryConfig,dictorNone) – If set, this dictionary is loaded and attached to the generator, overriding whatever the generator loaded itself;component_indicesindex into it.correct_clusters (
listofint) – Cluster indices treated as correct by teachers that judge clusters.use_true_counterfactuals (
bool) – Whether “true” counterfactuals (labelled with the target class) are added to the finetune dataset besides the “false” ones.component_indices (
listofintorNone) – Dictionary components the explainer edits along; when given, the explainer’scomponent_indicesandnum_attemptsare set from it.
Notes
Fields such as
current_iteration,feedback_accuracies,group_accuraciesandmodel_pathare progress logs that CFKD writes back intobase_dir/config.yamlafter every iteration, which is what makes a run resumable.Methods
Attributes
The adaptor_type for CFKDConfig has to be CFKD to find the CFKDConfig class when loading from a yaml file.
The attribution threshold when using the SegmentationMask teacher.
The average group accuracies of the model after every finetuning iteration.
The base directory where the run of CFKD is stored.
What batch_size is used for creating the counterfactuals?
Logging of the Feedback Accuracy.
Whether to calculate group accuracies or not.
The clustering strategy used by the counterfactual explainer
Whether to continue training from the current student model or start training from scratch again.
What type of counterfactuals are valid.
Logging of the current finetune iteration
The config of the data used to create the counterfactuals from.
The config of the counterfactual explainer that is used.
The cluster indices used for calculating explainer stats (sparsity, diversity, feedback metrics).
The number of finetune iterations when executing the adaptor.
The config of the generator used.
The performance of the generative model measured e.g. in FID score.
The group accuracies of the model after every finetuning iteration.
Dummy field to be able to use it as a model config!
Whether to always give feedback directly after creating validation counterfactuals or whether to wait until the next train feedback shall be given as well (which means less interruptions!)
The maximum number of test batches.
The maximum number of validation samples that are used for tracking stats every iteration.
The minimum number of samples used for finetuning in every iteration.
How aggressively to change the model based on the counterfactual samples.
The path of the last finetuned model
Whether to overwrite the logs and cache intermediate results.
Optional.
The path of the student used.
The config of the task the student model shall solve.
The type of teacher used.
The config of the test data used evaluate the real progress on.
The config of the training used for finetuning the student model.
The restriction to interesting counterfactual transitions.
Whether to draw samples for counterfactual creation according to the error matrix or not.
The number validation runs used for evaluating CFKD.
Whether to visualize the individual latent diversity scores as collages.
Whether to visualize the individual latent sparsity scores as collages.
- adaptor_type: str = 'CFKD'¶
The adaptor_type for CFKDConfig has to be CFKD to find the CFKDConfig class when loading from a yaml file.
- min_train_samples: pydantic.PositiveInt = 800¶
The minimum number of samples used for finetuning in every iteration. The actual number could be higher since not for every sample a counterfactual can be found and processing is done in batches.
- max_validation_samples: pydantic.PositiveInt = 200¶
The maximum number of validation samples that are used for tracking stats every iteration.
- max_test_batches: None | pydantic.PositiveInt = None¶
The maximum number of test batches. If set to None the test will be done on the full test set.
- finetune_iterations: int = 1¶
The number of finetune iterations when executing the adaptor. If set to 0 only the explanation and no adaption is done.
- task: TaskConfig | None = None¶
The config of the task the student model shall solve.
- explainer: dict | ExplainerConfig¶
The config of the counterfactual explainer that is used. All parameters regarding paths, where the generator is from etc in there are overwritten by CFKD and only used if the information is not available for CFKD
- training: TrainingConfig | None¶
The config of the training used for finetuning the student model. If not set student config can be used.
- data: DataConfig = None¶
The config of the data used to create the counterfactuals from.
- test_data: DataConfig = None¶
The config of the test data used evaluate the real progress on. Often this data has a distribution shift compared to the training data or comes from a totally different data source. Hence the option to give its own config. If set to None the normal data config is taken.
- student: PredictorConfig | str | None = None¶
The path of the student used. Can be either the path to a PyTorch or an onnx model directly or the path to a predictor config or a PredictorConfig object.
- generator: GeneratorConfig | None = None¶
The config of the generator used. This value will be overwritten if Generator is given via constructor directly. If the Generator is not given via constructor and this value is set to None explainer config is searched for generator config.
- base_dir: str = 'peal_runs/cfkd'¶
The base directory where the run of CFKD is stored. All the visualizations and the caching is stored here.
- continuous_learning: str = 'finetune'¶
Whether to continue training from the current student model or start training from scratch again. Can e.g. be “retrain”, which retrains model on original data and counterfactuals from scratch, “finetune”, which starts of at the weights of the uncorrected student or “deep_feature_reweighting”, which only finetunes the last layer of the the uncorrected student.
- use_confusion_matrix: bool = False¶
Whether to draw samples for counterfactual creation according to the error matrix or not. Makes particular sense in the multiclass setting where some classes might be in very different modes and one only wants to restrict to connected modes.
- attribution_threshold: float = 0.0¶
The attribution threshold when using the SegmentationMask teacher. Setting it to 0.0 means that every counterfactual that did on average bigger changes inside than outside the mask is considered a True counterfactual and every counterfactual that does not is considered a false counterfactual. If it is set higher the bar for True Counterfactual is set higher and if is set lower the bar is set lower as well.
- batch_size: pydantic.PositiveInt = 1¶
What batch_size is used for creating the counterfactuals?
- validation_runs: pydantic.PositiveInt = 1¶
The number validation runs used for evaluating CFKD.
- calculate_group_accuracies: bool = False¶
Whether to calculate group accuracies or not. This can only be done if confounding factors are known.
- overwrite: bool = True¶
Whether to overwrite the logs and cache intermediate results. If overwrite is set to False cached results are loaded. If CFKDConfig is stored as yaml on disk overwrite is automatically set to False so that CFKD can be continued at the last cached result. Using this feature dramatically improves ability to debug!
- mixing_ratio: float = 0.5¶
How aggressively to change the model based on the counterfactual samples. 0 -> No change, 1 -> Full change
- avg_group_accuracies: list = []¶
The average group accuracies of the model after every finetuning iteration.
- counterfactual_type: str = '1sided'¶
What type of counterfactuals are valid. 1sided means that we can only start from samples with correct prediction, 2sided also allows that we start from samples with wrong original prediction.
- lazy_feedback: bool = True¶
Whether to always give feedback directly after creating validation counterfactuals or whether to wait until the next train feedback shall be given as well (which means less interruptions!)
- generator_performance: dict = {}¶
The performance of the generative model measured e.g. in FID score.
- transition_restrictions: list | None = None¶
The restriction to interesting counterfactual transitions. Helpful in the case of datasets with a lot of classes and heavy modes like ImageNet.
- clustering_strategy: str | None = 'attempt_nr'¶
The clustering strategy used by the counterfactual explainer
- sparse_dictionary: SparseDictionaryConfig | dict | None = None¶
- seed: int | None = 0¶
Optional. If True (and seed is set), get_batch reseeds from seed * 1000003 + a per-call counter instead of OS entropy, so the choice of samples is reproducible per seed and each call still draws a different shuffle. Used by the Camelyon17 seed-variability runs. False (the default) keeps the original behaviour: torch.manual_seed(torch.seed()).
- visualize_latent_sparsity: bool = True¶
Whether to visualize the individual latent sparsity scores as collages. Each collage contains: factual, counterfactual, target end confidence, sparsity score.
- visualize_latent_diversity: bool = True¶
Whether to visualize the individual latent diversity scores as collages. Each collage contains: factual, cf1, cf2, target end confidences, diversity score.