Skip to content
khalooeiPublic

About

Self-Adaptive Revisiting Awareness for Enhancing Robustness and Generalization in Classification Tasks

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

SARA: Self-Adaptive Revisiting Awareness

Tests Python PyTorch License: MIT Project page

Official PyTorch implementation of "Self-Adaptive Revisiting Awareness for Enhancing Robustness and Generalization in Classification Tasks" by Mohammad Khalooei, Maryam Amirmazlaghani and Mohammad Mehdi Homayounpour.

Project page: khalooei.github.io/SARA, with an interactive demo of the correction and the main results.

SARA is a training strategy that focuses on the samples a model currently finds hardest. In every iteration it identifies the misclassified and low-confidence samples of the mini-batch, moves each of them to a nearby point that the model classifies correctly with sufficient confidence and margin, and adds the successfully corrected samples to the batch. The correction is driven by the Confidence Guard (CG) loss, and SARA can be combined with standard training or with adversarial training (PGD, TRADES, MART).

SARA corrects a misclassified sample inside its epsilon-box and adds it to the batch, discards a sample that cannot reach the true class, and leaves confidently classified samples unchanged

Highlights

  • Confidence Guard loss. A cross-entropy pull toward the true class plus a hinge penalty on the classification margin. The hinge term vanishes exactly when the margin condition is met, so the inner search receives a gradient until the acceptance criterion holds.
  • Plug-in design. SARA wraps any base loss: cross-entropy, PGD adversarial training, TRADES or MART. A new base loss only needs a compute(model, x, y, epsilon) method.
  • Batched inner correction. All challenging samples of a batch are corrected in one tensor operation (one forward and one backward pass per inner iteration), so the overhead scales with the number of challenging samples rather than with the batch size.
  • Exact projection. Every inner iterate is projected onto the intersection of the L∞ ball and the valid pixel range; corrected samples are detached, so no gradient flows through the inner search.
  • Class-count-relative confidence threshold. The threshold is expressed relative to chance level (ξ_eff = ξ · 10 / C), which keeps it meaningful on datasets with 100 or 200 classes.
  • Corrective alignment. The adversarial prediction of each corrected sample is aligned with the prediction at its accepted correction (--sara-align). The target satisfies the acceptance criterion, so the alignment loss bounds how far the adversarial example falls short of the confidence and margin requirements.
  • Stabilization options. Correction budget, warmup, confidence-based reweighting, success-rate damping and a confidence-capped CG loss, each available as a single flag. A revisiting memory and a robust correction are implemented as further options.
  • Baselines in the same pipeline. GAIRAT, FAT and OHEM share the training and evaluation code of SARA.
  • Tested. Unit tests check the properties the method relies on, including the admissibility of every corrected sample and the bounds satisfied by the CG loss.

Method

flowchart LR
    B["Mini-batch B"] --> S["Challenging set S<br/>misclassified or f_y(x) below ξ"]
    S --> C["Inner correction<br/>κ projected gradient steps<br/>on the CG loss"]
    C --> A{"Accepted?<br/>f_y ≥ ξ and margin ≤ −τ"}
    A -- yes --> Sstar["Corrected set S*"]
    A -- no --> D["Discarded"]
    B --> L["Loss_base(B) + Loss_CG(S*)"]
    Sstar --> L
    L --> U["Parameter update"]
Loading

For a sample (x, y) with true-class probability f_y(x) and margin h(x, y) = max_{j≠y} f_j(x) − f_y(x):

Component Definition
Challenging set S = {(x, y) ∈ B : argmax_j f_j(x) ≠ y or f_y(x) < ξ}
Acceptance region Ω = {x : f_y(x) ≥ ξ and h(x, y) ≤ −τ}
CG loss (per sample) L_CG(x, y) = CE(f(x), y) + λ_margin · ReLU(h(x, y) + τ)
Inner update δ ← Π_Δ(δ − α ∇_δ L_CG(x + δ, y)), with Δ = {δ : ‖δ‖∞ ≤ ε, x + δ ∈ [0, 1]^d}
Outer objective Loss_SARA = Loss_base(B) + mean_{S*} L_CG
Corrective alignment Loss_align = (β / batch size) · Σ_{S*} KL(sg[f(x*)] ‖ f(x_adv)), added to the outer objective

The implementation follows Algorithm 1 of the paper: sara/confidence_guard.py contains the challenging-set identification, the CG loss and the inner correction, and sara/trainer.py contains the outer training step.

Results at a glance

Standard protocol (ResNet18 with a 3×3 stem, ε = 8/255, PGD-10 training, weight averaging; mean of three seeds with identical initializations and mini-batches; SARA settings chosen on a validation split; AutoAttack with all four stages on 2,000 test images; accuracy and calibration error in %):

Clean PGD-20 AutoAttack Calibration error under PGD-20 Cost
CIFAR10, AT-PGD 80.52 49.95 46.80 8.54 1.00×
CIFAR10, AT-PGD + SARA 80.27 49.92 46.78 7.72 1.45×
CIFAR10, SARA without acceptance test 78.22 49.26 45.00 1.78 1.49×
SVHN, AT-PGD 90.91 49.49 41.38 16.84 1.00×
SVHN, AT-PGD + SARA 91.00 49.89 41.27 14.84 1.35×
SVHN, SARA without acceptance test 90.98 50.50 41.13 13.28 1.35×
  • SARA keeps the robustness of PGD adversarial training and lowers its calibration error under attack on every seed (before temperature scaling). It does not raise AutoAttack accuracy at this operating point.
  • On CIFAR10 the acceptance test is decisive: without it, clean and AutoAttack accuracy fall by 2.1 and 1.8 points on every seed (McNemar p = 2·10⁻⁸). On SVHN it has no significant effect.
  • Training AT-PGD for as long as SARA takes lowers its own robustness, so the extra cost does not buy a stronger baseline. There is no sign of gradient masking: the Square attack removes no accuracy after the white-box attacks.

Per-image statistics, temperature-scaled calibration, L2 and corruption results, and every configuration and log are in results/revision2/standard/. The runs of Section 4.6 repeated with the corrected implementation are in results/revision2/main_corrected/.

Tuned and controlled protocols. With the hyperparameters of each method tuned individually, adding SARA increased the robust accuracy of every base method on MNIST, CIFAR10 and SVHN, by up to 16 percentage points; the controlled study of the paper attributes most of this margin to per-method tuning and training settings.

Robust accuracy gained by adding SARA to each base method on MNIST, CIFAR10 and SVHN under the tuned protocol, in percentage points

Under one shared configuration of the controlled protocol, the corrective alignment term raises the AutoAttack accuracy of PGD adversarial training by about one point on CIFAR10 (+1.37 points with the corrected implementation, on every seed); on MNIST and SVHN the effect is within seed-to-seed variation. The project page has an interactive version of the figure above.

Installation

git clone https://github.com/khalooei/SARA.git
cd SARA
pip install -e .            # or: pip install -r requirements.txt

Python 3.9 or later and PyTorch 2.1 or later are required. A CUDA GPU is recommended; on GPUs, forward passes run in bfloat16 autocast.

Quick start

Train LeNet on MNIST with PGD adversarial training and SARA, and evaluate clean, PGD and APGD accuracy:

python train.py --dataset mnist --model lenet --base-loss pgd --sara \
    --epochs 20 --eval-apgd --out-dir runs/mnist/at_pgd_sara

Each run writes its configuration (config.json), one JSON line per epoch (train_log.jsonl) and the final weights (model_final.pt) to --out-dir.

SARA can also be used inside your own training loop:

import torch
from sara import SARAConfig, build_base_loss, sara_training_step

base_loss = build_base_loss("pgd")                      # "ce", "pgd", "trades" or "mart"
cfg = SARAConfig(epsilon=0.2, tau=0.05, xi=0.5, kappa=10)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4)

for x, y in train_loader:
    stats = sara_training_step(model, optimizer, x, y, base_loss, cfg, base_loss_kwargs={"pgd_steps": 7})

stats reports the losses, the number of challenging and corrected samples and the correction success rate of the step.

Datasets

MNIST, CIFAR10, CIFAR100 and SVHN are downloaded automatically by torchvision. For Tiny-ImageNet-200, download tiny-imagenet-200.zip and unzip it into the data directory. Data are stored in ./data by default; set SARA_DATA_ROOT to use another location.

If the SVHN host (ufldl.stanford.edu) is unreachable, build the same data from the authors' copy on the Hugging Face hub. The script checks the image and label counts of the official release:

mkdir -p data/svhn_hf && cd data/svhn_hf
for f in train test; do
  curl -LO https://huggingface.co/datasets/ufldl-stanford/svhn/resolve/main/cropped_digits/$f-00000-of-00001.parquet
done
cd ../.. && pip install pyarrow && python scripts/svhn_from_hf.py data/svhn_hf data

Each dataset is converted once to a uint8 tensor cache (data/<name>_<split>.pt) that the GPU data loader reads.

Dataset Classes Input size Model Default ε (L∞)
MNIST 10 28 × 28 LeNet 0.2
CIFAR10 10 32 × 32 ResNet18 or ResNet18-S 0.03 (8/255 in the standard protocol)
SVHN 10 32 × 32 ResNet18 or ResNet18-S 0.03 (8/255 in the standard protocol)
CIFAR100 100 32 × 32 ResNet18 0.03
Tiny-ImageNet-200 200 64 × 64 ResNet18 0.03

Images are kept in [0, 1] without per-channel normalization, so perturbation budgets are expressed in pixel space.

--model resnet18 is the torchvision ResNet18 used in the controlled protocol of the paper. Its 7 × 7 stride-2 stem and max-pooling reduce a 32 × 32 input to 8 × 8, which limits clean accuracy on CIFAR10 and SVHN. --model resnet18-cifar (ResNet18-S) uses the 3 × 3 stride-1 stem without max-pooling that is standard for 32 × 32 images in the adversarial-training literature.

Training options

Option Default Description
--base-loss {ce,pgd,trades,mart,fat} ce Base training loss
--sara off Enable SARA
--method {default,ohem,fat,gairat} default Use a baseline training step instead
--sara-epsilon AT ε Radius of the correction search
--xi 0.5 Confidence threshold (for 10 classes)
--xi-relative / --no-xi-relative relative Rescale ξ to ξ · 10 / C
--tau 0.05 Margin threshold
--kappa 10 Maximum number of inner iterations
--alpha 2ε / κ Inner step size
--lambda-margin 1.0 Weight of the hinge term of the CG loss
--sara-max-correct 0 (off) Correction budget: maximum challenging samples corrected per batch
--sara-warmup-epochs 0 Initial epochs trained with the base loss only
--gamma-reweight 0 (off) Confidence-based reweighting of the base loss
--cg-damping off Scale the CG loss by (1 − correction success rate)
--cg-confidence-cap off Cap the cross-entropy term of the outer CG loss at ξ
--sara-align 0 (off) Weight β of the corrective alignment term
--sara-align-natural 0 (off) Align the natural prediction with the accepted correction
--nat-weight 0 (off) Extra weight of natural cross-entropy, added to both the base method and SARA
--revisit-memory 0 (off) Capacity of the memory of accepted corrections, rescored with the capped loss
--robust-correction off Add corrected samples to the input of the adversarial base loss
--at-epsilon dataset default Radius of adversarial training (the SARA radius defaults to it)
--at-eps-warmup 0 (off) Ramp the training and correction radii linearly over this many epochs
--ema-decay 0 (off) Exponential moving average of the weights; the averaged model is the evaluated one
--sara-align-eval off Score the adversarial examples of the alignment term in evaluation mode, as its targets
--sara-accept-all off Ablation: keep every correction as a target, skipping the acceptance test
--val-size 0 Hold out this many training images for model selection
--eval-autoattack 0 (off) Evaluate standard AutoAttack on this many test images after training
--eval-apgd off Also evaluate APGD-CE accuracy (PGD-20 is always evaluated)
--data-loader {gpu,torch} gpu Keep the dataset on the GPU and augment there, or use a torch DataLoader

Run python train.py --help for the complete list.

Reproducing the experiments

The scripts in experiments/ contain the exact commands of the controlled-protocol experiments of the paper. Outputs are written to runs/ (set OUT to change it).

Script Content
controlled_mnist.sh SARA with each base loss and the OHEM, FAT and GAIRAT baselines on MNIST
controlled_cifar10.sh SARA with each base loss and GAIRAT on CIFAR10
controlled_svhn.sh SARA with each base loss on SVHN
controlled_cifar100.sh Class-count-relative and unscaled confidence thresholds on CIFAR100
controlled_tinyimagenet.sh Standard training with and without SARA on Tiny-ImageNet-200
stabilization.sh Correction budget, warmup, reweighting, damping and confidence cap
analysis.sh Hyperparameter ablation, multi-seed runs, hyperparameter search, overhead, corruptions
standard_protocol.sh ResNet18-S at ε = 8/255 on CIFAR10 and SVHN: SARA, compute-matched AT-PGD and ablations (Appendix D)

experiments/summarize_standard.py RUNS_DIR summarizes the standard-protocol runs, and scripts/measure_alignment_gap.py --run-dir RUN measures the alignment gap η of Proposition 4 on a trained model.

GPU kernels are not bit-wise deterministic, so re-runs can differ slightly from run to run. The released records, without model weights, are in results/. experiments/make_tables.py regenerates the LaTeX rows of the second-revision tables from final.json.

Repository structure

SARA/
├── sara/                    # the package
│   ├── confidence_guard.py  # challenging set, CG loss, batched inner correction
│   ├── trainer.py           # SARA training step (outer objective)
│   ├── losses.py            # base losses: CE, PGD-AT, TRADES, MART
│   ├── baselines.py         # GAIRAT, FAT and OHEM training steps
│   ├── attacks.py           # PGD and APGD
│   ├── evaluate.py          # clean, PGD and APGD accuracy
│   ├── lipschitz.py         # local Lipschitz estimate
│   ├── models.py            # LeNet, ResNet18 and ResNet18-S
│   ├── data.py              # data loaders
│   └── tiny_imagenet.py     # Tiny-ImageNet-200 dataset
├── train.py                 # training entry point
├── scripts/                 # analysis experiments
├── experiments/             # commands of the paper's experiments
└── tests/                   # unit tests

Tests

pip install -e ".[dev]"
pytest

Citation

If you use this code, please cite:

@misc{khalooei2026sara,
  title  = {Self-Adaptive Revisiting Awareness for Enhancing Robustness and Generalization in Classification Tasks},
  author = {Khalooei, Mohammad and Amirmazlaghani, Maryam and Homayounpour, Mohammad Mehdi},
  year   = {2026},
  howpublished = {\url{https://github.com/khalooei/SARA}}
}

License

This project is released under the MIT License.

About

Self-Adaptive Revisiting Awareness for Enhancing Robustness and Generalization in Classification Tasks

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages