peal.teachers.model2model_teacher.Model2ModelTeacher

class peal.teachers.model2model_teacher.Model2ModelTeacher[source]

Bases: TeacherInterface

Teacher that asks a reference classifier whether a counterfactual is real.

For every counterfactual the reference model is evaluated on the original image and on the edit. Verdicts are returned as strings: "true" when the reference prediction changed, "false" when it did not, and one of several rejection reasons when the sample never qualified in the first place ("student originally wrong!", "teacher originally wrong!", "student not swapped!").

Parameters:
  • model (torch.nn.Module) – Reference classifier producing class logits. It is switched to eval mode for the duration of get_feedback and restored afterwards; its device determines self.device.

  • dataset – Dataset used for calculate_outlier_score and, at high tracking levels, generate_contrastive_collage.

  • tracking_level (int, optional) – From level 5 on, a contrastive collage of the judged counterfactuals is written below base_dir.

  • counterfactual_type (str, optional) – "1sided" (default) additionally rejects samples whose ground-truth label already disagrees with the student’s source prediction.

Methods

get_feedback

Return one verdict per counterfactual.

__init__(model, dataset, tracking_level=0, counterfactual_type='1sided')[source]

Store the reference model and dataset and derive the model’s device.

get_feedback(x_counterfactual_list, y_source_list, x_list, y_list, y_target_end_confidence_list, base_dir=None, y_target_list=None, student=None, mode='train', **kwargs)[source]

Return one verdict per counterfactual.

Parameters:
  • x_counterfactual_list (sequence of torch.Tensor) – Counterfactual images of shape (C, H, W), aligned with x_list.

  • y_source_list (sequence) – The student’s predicted class for each original image.

  • x_list (sequence of torch.Tensor) – The original images.

  • y_list (sequence) – Ground-truth labels of the original images.

  • y_target_end_confidence_list (sequence of float) – The student’s confidence in the target class after the edit; a value below 0.5 means the edit failed to flip the student.

  • base_dir (str, optional) – Directory the contrastive collage is written to.

  • y_target_list (sequence, optional) – The class each counterfactual was steered towards.

  • student (torch.nn.Module, optional) – The classifier under repair; used for the logged before/after predictions.

  • mode (str, optional) – "train" or "validation"; only selects whether a collage is produced.

  • **kwargs – Forwarded to dataset.generate_contrastive_collage.

Returns:

One verdict per counterfactual, in input order.

Return type:

list of str