peal.web.dfr

Last-layer correction (DFR, deep feature reweighting) of a web-demo job:

python -m peal.web.dfr <job_dir>

The network stays frozen; only its final linear layer (found by peal.architectures.onnx_predictor.find_final_linear) is refitted, as a regularised logistic regression on the penultimate features of

  • the uploaded images with their class labels, and

  • the verified counterfactuals of the directions the user judged: a “false” (spurious) direction’s counterfactual keeps its ORIGINAL class, since that edit must not change the prediction; a “true” direction’s counterfactual gets its TARGET class.

Three convex solvers (solver), each with its regularisation chosen by stratified 5-fold cross-validation over the originals and counterfactuals (scored by the mean of the balanced accuracy on the original images and the accuracy on the counterfactuals of the held-out fold):

  • logistic: L2 logistic regression, as in the DFR paper (Kirichenko et al., 2023);

  • svm: soft-margin linear SVM (hinge loss);

  • ridge: least-squares classifier on +-1 labels, the closed-form option.

The SVM and ridge scores are not probabilities, so a 1-D Platt scaling fitted on their cross-validated scores turns them into calibrated logits; it is folded into the same linear layer. Writes run/model.onnx (the corrected model; the uncorrected one stays as run/model_original.onnx) and dfr.json with accuracies and counterfactual consistency before and after.

Module attributes

C_GRID

alpha = 1 / C).

CF_WEIGHT

Total weight of the counterfactuals relative to the original images.

Functions

correct

Refit the final layer of <job_dir>/model.onnx; see the module docstring.