---
title: Sharpness-Aware Geometric Defense (SaGD)
url: https://www.emergentmind.com/topics/sharpness-aware-geometric-defense-sagd
type: topic
---

# Sharpness-Aware Geometric Defense (SaGD)

Sharpness-aware Geometric Defense (SaGD) is a training-time defense and geometry-aware scoring framework for robust out-of-distribution (OOD) detection in the setting where both in-distribution (ID) and OOD samples are subjected to adversarial perturbations. It was introduced to address a failure mode of contemporary geometry-projection OOD detectors: adversarially perturbed ID samples can be pushed far from their class prototype or neighborhood in latent space and are therefore incorrectly flagged as OOD. SaGD smooths the rugged loss landscape induced by adversarial training and learns multi-geometry latent embeddings whose projected structure remains separable for ID, including adversarial ID, versus OOD. Its central components are multi-geometry projection, Riemannian Sharpness-aware Minimization (RSAM), Jitter-based adversarial training, and a nearest-neighbor geometric OOD score in the learned embedding space [2508.17174].

## 1. Problem setting and motivation

OOD detection is intended to ensure safe and reliable model deployment by distinguishing samples drawn from the training distribution from inputs lying outside it. In geometry-projection OOD detection, a sample is mapped into a latent geometry and scored by distance or density relative to the ID manifold. Under adversarial perturbations, however, an ID image $x$ can be pushed far from its class prototype or neighborhood in latent space, making its OOD score large. As a result, many geometry-based detectors misclassify adversarial ID as OOD, conflating “maliciously perturbed ID” with “unknown-class OOD” and thereby increasing false positives on adversarial ID [2508.17174].

SaGD is designed for this underexplored but practically critical regime. The stated goal is to distinguish adversarial ID samples from OOD ones rather than treating both as a single rejected category. The motivation rests on the observation that adversarial training, while a strong defense for classification, often sharpens the loss landscape: the inner maximization introduces large gradient norms and increases curvature around local minima, which degrades convergence and stability. In a geometry-projection setting, this appears as unstable latent embeddings, poor prototype compactness, and reduced separability between ID and OOD embeddings, especially under attack [2508.17174].

The framework therefore targets two coupled objectives. First, it seeks to smooth the adversarial loss landscape directly in geometry-projection learning. Second, it seeks to preserve ID compactness and inter-class disparity while maintaining large geometric margins to OOD so that adversarial ID remains near the ID manifold and OOD remains far from it. This suggests that SaGD should be understood not merely as an adversarially trained classifier with an auxiliary detector, but as a joint geometric representation-learning and robust OOD-detection procedure.

## 2. Multi-geometry latent representation

SaGD uses a backbone encoder and projection heads to learn two latent geometries simultaneously. Let $f_\theta$ denote the backbone encoder and heads collectively. Given an input $x$, the method extracts a penultimate embedding $z = f_\theta(x) \in \mathbb{R}^d$, which is $L_2$-normalized for scoring, and simultaneously learns two geometry projections [2508.17174].

The first projection is a hypersphere head that produces $z_s$ on $S^{d-1}$ via a vMF-based classifier with class prototypes $\{\mu_k\}$. The class-conditional vMF score for unit $z_s$ is

$$
p_d(\mathbf{z}_s;\boldsymbol{\mu}_k) = \tau\,\exp\Big(\frac{\boldsymbol{\mu}_k^\top\mathbf{z}_s}{\tau}\Big),
$$

with classification probabilities

$$
\mathcal{P}(y=k\mid\mathbf{z}_s)=\frac{\exp(\boldsymbol{\mu}_k^\top\mathbf{z}_s/\tau)}{\sum_{j=1}^{K}\exp(\boldsymbol{\mu}_j^\top\mathbf{z}_s/\tau)}.
$$

Its compactness loss encourages $z_s$ to align with the correct class prototype,

$$
\mathcal{L}_{\text{com} = -\frac{1}{N}\log\frac{\exp(\boldsymbol{\mu}_k^\top\mathbf{z}_s/\tau)}{\sum_{j=1}^{K}\exp(\boldsymbol{\mu}_j^\top\mathbf{z}_s/\tau)},
$$

and its disparity loss pushes prototypes apart,

$$
\mathcal{L}_{\text{dis} = -\frac{1}{K}\sum_{i=1}^{K}\log\frac{1}{K-1}\sum_{j=1}^K\mathbf{1}_{ji}
\exp\Big(\frac{\boldsymbol{\mu}_i^\top \boldsymbol{\mu}_j}{\tau}\Big),\quad \mathbf{1}_{ji}=\mathbb{1}[j\neq i].
$$

The hypersphere loss is

$$
\mathcal{L}_{\text{sph}=\mathcal{L}_{\text{com}+\mathcal{L}_{\text{dis}.
$$

The second projection is a hyperbolic head that produces $z_h$ on a Poincaré ball $D_c \subset \mathbb{R}^d$ with curvature $c > 0$, corresponding to constant negative curvature $-c$. With Möbius operations $(\oplus_c, \otimes_c)$ and geodesic distance

$$
D(\mathbf{u},\mathbf{v})=\frac{2}{\sqrt{c}\arctanh\Big(\sqrt{c}\,\big\|-\mathbf{u}\oplus_c\mathbf{v}\big\|\Big),
$$

the supervised hyperbolic contrastive loss on an augmented set $\mathcal{I} = X \cup A$ is

$$
\mathcal{L}_{\text{hypb}=
-\sum_{i\in\mathcal{I}\frac{1}{|P(i)|}\sum_{p\in P(i)}\log\frac{\exp\big(-D(\mathbf{z}_h^{(i)},\mathbf{z}_h^{(p)})/\tau\big)}{\sum_{a\in \mathcal{A}\exp\big(-D(\mathbf{z}_h^{(i)},\mathbf{z}_h^{(a)})/\tau\big)}.
$$

The two geometry heads are combined with cross-entropy in a multi-geometry projection (MGP) objective,

$$
\mathcal{L} = \mathcal{L}_{\text{sph}+\mathcal{L}_{\text{hypb} + \mathcal{L}_{\text{ce}.
$$

The stated purpose of this MGP design is to learn richer latent structure than a single geometry. In the formulation of SaGD, that richer structure is intended to improve ID characterization and preserve separability under attack [2508.17174].

## 3. Sharpness-aware optimization and adversarial example generation

A defining feature of SaGD is the use of Riemannian Sharpness-aware Minimization to regularize adversarial training on the manifold defined by the geometry heads. The framework defines manifold sharpness as

$$
\mathcal{L}_S(\theta)=\max_{\|\delta\|_{\theta}^2\le\rho}\Big(\mathcal{L}\big(R_\theta(\delta)\big)-\mathcal{L}(\theta)\Big),
$$

where $\theta$ lies on a manifold $M$ with retraction $R_\theta$ and metric tensor $g_M$. Using a first-order approximation,

$$
\mathcal{L}\big(R_\theta(\delta)\big)\approx \mathcal{L}(\theta)+\langle \nabla_{\theta}\mathcal{L}(\theta),\delta\rangle_{\theta},
$$

the inner maximizer is the normalized Riemannian gradient,

$$
\delta^\star=\rho\,\frac{\nabla_{\theta}\mathcal{L}(\theta)}{\big\|\nabla_{\theta}\mathcal{L}(\theta)\big\|_{\theta},\quad
\theta^\star=R_\theta(\delta^\star).
$$

The outer update performs Riemannian gradient descent on the perturbed loss,

$$
\theta' = R_{\theta}\Big(-\eta\,\nabla_{\theta}\mathcal{L}(\theta^\star)\Big).
$$

In the method’s interpretation, RSAM flattens sharp minima across the multi-geometry heads, stabilizes latent embeddings, and improves ID/OOD separation under attack. The paper further frames this in gradient-Hessian terms: with adversarial samples $x^\star = x + \gamma$, the gradients $g_W$ and $g_W^\star$ differ by $\Delta g_W$, and the curvature term $H_z$ appears in the change of feature gradients. Large $H_z$ and large $\|\tilde g_x\|$ increase sharpness and harm convergence during adversarial training [2508.17174].

SaGD instantiates adversarial training with Jitter attack rather than standard PGD or FGSM. The standard min-max objective is written as

$$
\min_{\theta}\;\mathbb{E}_{(x,y)\sim\mathcal{D}_{\text{ID}\Big[\max_{\|\delta\|_p\le\epsilon}\,\mathcal{L}\big(f_{\theta}(x+\delta),y\big)\Big].
$$

Adversarial examples are generated as $x_\gamma = x + \gamma$ with $\|\gamma\|_\infty \le \epsilon$, $\epsilon = 8/255$, step size $2/255$, and $10$ steps. Let $h \in \mathbb{R}^C$ be logits, and define

$$
\hat{\mathbf{h} = \mathrm{softmax}\Big(\alpha\,\frac{\mathbf{h}}{\|\mathbf{h}\|_{\infty}\Big),\quad \alpha=10.
$$

Jitter aims to maximize the distance to the one-hot ground-truth $y$ with Gaussian jitter noise $\xi \sim \mathcal{N}(0,\sigma)$ injected into the softmax output:

$$
L_{\mathcal{N} = \big\|\hat{\mathbf{h}+\xi - \mathbf{y}\big\|_2.
$$

Its adaptive perturbation rule is

$$
L_{\text{Jitter} = \begin{cases}
\displaystyle \frac{\big\|\hat{\mathbf{h}+\xi-\mathbf{y}\big\|_2}{\beta}, & \text{if } f_\theta(x_\gamma) = y \\
\big\|\hat{\mathbf{h}+\xi-\mathbf{y}\big\|_2, & \text{otherwise}
\end{cases}
$$

where $\beta > 1$ reduces perturbation magnitude once the attack is effective. The stated reason is to keep adversarial examples close to the ID manifold and prevent distribution shift that could harm embedding learning. Empirically, Jitter-based adversarial training is reported to generalize better to unseen attacks than training based on standard PGD or FGSM [2508.17174].

## 4. OOD scoring, training algorithm, and implementation choices

After training, SaGD constructs an ID embedding bank from penultimate-layer features and scores a test sample by nearest-neighbor geometric distance. With normalized embedding $z$ from the penultimate layer, the OOD score is

$$
s(x)=\|\mathbf{z}-\mathbf{z}_k\|_2,
$$

where $z_k$ is the $k$-th nearest neighbor among ID training embeddings. A threshold $\lambda$ determines OOD versus ID. In the main experiments, $k$ is taken as the nearest neighbor. The method reports that this score is empirically robust, computationally simple, and more stable than alternatives adopted in some baselines, such as Mahalanobis variants [2508.17174].

The training procedure comprises adversarial example generation, joint geometry learning, and RSAM optimization. For each minibatch, adversarial examples are first generated via Jitter with Gaussian softmax jitter and adaptive factor $\beta$. Clean and adversarial inputs are then forwarded through the backbone and geometry heads to produce $z_s$, $z_h$, and penultimate $z$. The multi-geometry loss is computed, followed by an RSAM inner step that computes the Riemannian gradient, perturbs parameters within a tangent-ball of radius $\rho$, retracts to $\theta^\star$, and evaluates the worst-case loss. An RSAM outer step then updates the parameters via retraction-based gradient descent on that worst-case loss. At inference time, the ID embedding bank is built from L2-normalized penultimate features, and threshold calibration is performed on a held-out ID set, optionally with a small validation OOD set, to report FPR@95%TPR, AUROC, and AUPR [2508.17174].

The implementation details reported for the main experiments are specific. CIFAR-10 uses ResNet-18, and CIFAR-100 uses ResNet-34. Optimization uses SGD with momentum $0.9$, weight decay $1\mathrm{e}{-4}$, initial learning rate $0.5$, and RSAM regularization. Training lasts $500$ epochs with batch size $512$, penultimate dimension $128$, and hyperbolic curvature $c = 0.01$. Feature clipping of the form $x' = \min\{1, r/\|x\|\}\cdot x$ is applied to stabilize training near the ball boundary. Attacks considered in training and evaluation include PGD, FGSM, FAB, Jitter, and CW via TorchAttacks, all with $\epsilon = 8/255$, step size $2/255$, and $10$ steps; additional evaluation includes APGD-100, APGD-1000, and AutoAttack [2508.17174].

The paper also provides practical tuning guidance. It recommends Jitter-based adversarial training for robustness to unseen attacks; small RSAM radii, exemplified by $\rho \in [0.05, 0.2]$ relative to gradient norms; curvature $c \approx 0.01$ for stability; temperatures $\tau \in [0.05, 0.2]$ for vMF and contrastive losses; and small $k$ in k-NN, with $k = 1$ in the main scoring. RSAM is reported to roughly double the per-iteration cost compared to vanilla SGD, since it requires one extra forward/backward pass at $\theta^\star$ plus retraction, while Jitter adversarial generation adds $10$ inner steps per batch [2508.17174].

## 5. Empirical evaluation

The experimental setup uses CIFAR-10 and CIFAR-100 as ID datasets and six OOD datasets: Tiny-ImageNet, Places365, LSUN, LSUN-Resize, iSUN, and Textures. For ATD and ATOM, which are exposure-based baselines, Food-101 is used for auxiliary outlier training and SVHN for validation, following their protocols. The primary metrics are FPR@95%TPR (FPR95) and AUROC, with additional reporting of Inlier AUC and Outlier AUC [2508.17174].

The main reported quantitative results are averages over the six OOD datasets. For CIFAR-10 ID, without adversarial training, MGP-Mahalanobis achieves FPR95 $47.41$ and AUC $90.23$, and KNN+, ASH, ODIN, and GODIN are reported to degrade significantly under attacks. With adversarial training, ATOM obtains FPR95 $56.13$ and AUC $79.08$, ATD obtains FPR95 $42.59$ and AUC $87.36$, and SaGD obtains FPR95 $27.68$ and AUC $94.83$. The gains over ATD are reported as $-14.91$ FPR95 and $+7.47$ AUC. Per-condition results for SaGD on CIFAR-10 are: Clean $22.46/95.77$, PGD $28.69/94.70$, Jitter $26.43/95.05$, FAB $28.99/94.68$, FGSM $37.19/92.99$, and CW $22.32/95.80$ [2508.17174].

For CIFAR-100 ID, without adversarial training, CIDER-Maha reports FPR95 $68.40$ and AUC $80.03$, while MGP-KNN reports FPR95 $73.02$ and AUC $77.52$. With adversarial training, ATOM reports FPR95 $76.82$ and AUC $80.12$, ATD reports FPR95 $67.58$ and AUC $77.41$, and SaGD reports FPR95 $49.87$ and AUC $87.59$. The gains over ATD are $-17.71$ FPR95 and $+10.18$ AUC, and the gains over ATOM are $-26.95$ FPR95 and $+7.47$ AUC [2508.17174].

On CIFAR-10 under adaptive attacks, the average adversarial results over five attacks are ATD: FPR95 $42.59$, AUC $87.36$, AUCIn $88.69$, AUCOut $89.03$; and SaGD: FPR95 $27.68$, AUC $94.83$, AUCIn $95.71$, AUCOut $95.86$. Under APGD-100, ATD reports $43.89/88.41$ versus SaGD $28.67/94.18$; under APGD-1000, ATD reports $44.25/85.36$ versus SaGD $29.13/94.50$; and under AutoAttack, ATD reports $47.95/83.86$ versus SaGD $32.18/93.01$ [2508.17174].

| Setting | Comparator | SaGD |
|---|---:|---:|
| CIFAR-10 average over six OOD datasets | ATD: 42.59 / 87.36 | 27.68 / 94.83 |
| CIFAR-100 average over six OOD datasets | ATD: 67.58 / 77.41 | 49.87 / 87.59 |
| CIFAR-10 AutoAttack | ATD: 47.95 / 83.86 | 32.18 / 93.01 |

The table entries are FPR95 / AUC. The reported pattern is that SaGD improves both false-positive behavior and ranking quality across clean, standard adversarial, and adaptive attack settings. A plausible implication is that the method improves both separability and score calibration in robust OOD detection, although the paper itself states this more directly in terms of reduced FPR and increased AUC [2508.17174].

## 6. Ablations, interpretation, and limitations

The ablation study on CIFAR-10 isolates the contributions of RSAM and Jitter. MGP + Jitter without RSAM achieves average FPR95 $32.35$ and AUC $94.48$. MGP + RSAM without Jitter achieves average FPR95 $73.35$ and AUC $80.37$, which the paper interprets as evidence that sharpness reduction alone without adversarial training is insufficient. CIDER + RSAM + Jitter achieves average FPR95 $47.89$ and AUC $91.39$, indicating that both RSAM and Jitter help even with single-geometry heads. SaGD, combining MGP + RSAM + Jitter, achieves the best result: average FPR95 $27.68$ and AUC $94.83$ [2508.17174].

A separate ablation shows that the choice of adversarial training attack matters. RSAM+PGD training yields average FPR95 $78.65$ and AUC $71.66$ but is very strong on FGSM, with FPR95 $20.25$ and AUC $95.19$, indicating overfitting to attack type. RSAM+FAB yields average FPR95 $68.35$ and AUC $74.58$; RSAM+FGSM yields average FPR95 $86.44$ and AUC $63.76$; and RSAM+CW yields average FPR95 $59.08$ and AUC $87.32$. The reported conclusion is that Jitter-based adversarial training generalizes better across attack families than these alternatives [2508.17174].

The paper also studies attack intensity sensitivity under PGD on CIFAR-10. Increasing $\epsilon$ from $8/255$ to $16/255$ is reported to double FPR95 and reduce AUC by approximately $6.72\%$. SaGD is described as remaining relatively stable in AUROC while FPR95 is more sensitive, which the authors use to reinforce the importance of reporting FPR@95%TPR in robust OOD detection [2508.17174].

Visualization results further support the method’s interpretation. Histograms of $s(x)$ for ID versus OOD under clean and adversarial settings, specifically FGSM and FAB, show that SaGD maintains clear separation, while MGP without defense and CIDER-RSAM-Jitter collapse under FGSM and FAB. RSAM+PGD yields distinct peaks only against PGD but fails on FAB, again illustrating overfitting of standard adversarial training to the seen attack [2508.17174].

The limitations and deployment guidance are stated with similar specificity. FGSM remains a relatively challenging case because of its single-step, large-direction perturbations, although SaGD still improves AUROC and maintains separability. Stronger attacks with larger $\epsilon$ increase FPR, so both FPR95 and AUROC should be reported. The framework is recommended for systems in which adversarial ID must be kept as ID while OOD rejection is also required, such as safety-critical gating or triage. It is also presented as advantageous when outlier exposure is impractical, because it avoids dependence on large auxiliary OOD datasets while outperforming exposure-based baselines ATD and ATOM in the reported experiments [2508.17174].

Source: https://www.emergentmind.com/topics/sharpness-aware-geometric-defense-sagd