IA Utils — Adversarial Vector Attack¶
A gradient-based (PGD-style) stress test for
enhanced_dense_healing_hybrid's
Phi-Trigger decision. evaluate_phi_trigger
(dense_evolution.healing) thresholds |v_dinamic| with a
hard step (not differentiable at the boundary), but v_dinamic itself is
built entirely from JAX-differentiable operations — this crafts the
minimal perturbation (projected into an L2 epsilon-ball) that flips the
trigger either direction, rather than adding random noise and hoping.
Adapted from IGME's chained-differentiable-attack idea (arXiv:2607.27465, "Efficient Chained Method Ensemble for Transferable Semantic Segmentation Attacks", He & Zhang), applied here to vector sequences instead of image segmentation.
adversarial_vector_attack ¶
Gradient-based adversarial stress-testing for enhanced_dense_healing_hybrid's Phi-Trigger decision -- a targeted, crafted perturbation instead of the random NaN/Inf corruption this healing pipeline is normally tested against, adapted from IGME's chained-differentiable-attack idea (arXiv:2607.27465, "IGME: Efficient Chained Method Ensemble for Transferable Semantic Segmentation Attacks") applied to vector sequences instead of image segmentation.
evaluate_phi_trigger (dense_evolution.healing) makes its keep-vs-median- replace decision by thresholding |v_dinamic| against NON_STATIC_THRESHOLD_A -- a hard step, not differentiable at the boundary. But v_dinamic itself (via calculate_phi_ab -> calculate_vettore_dinamico) is built entirely from norms, dot products, log, and clip -- all JAX-differentiable. This crafts a minimal perturbation to a single vector in the sequence, via gradient ascent/descent on |v_dinamic| (projected back into an epsilon L2-ball each step, the standard PGD pattern), that flips the trigger's decision at that point:
- "flip_to_dynamic": push an originally-static point (would be replaced by the local median) across the threshold so the trigger keeps it as-is instead -- the more security-relevant direction, since it represents a worst-case corruption crafted to evade the healer by looking like genuine motion, rather than obvious noise.
- "flip_to_static": push an originally-dynamic point (would be kept) across the threshold so the trigger discards it as noise instead -- the failure mode of genuine signal getting wrongly suppressed.
This does not attack the median-filter fallback itself, only the Phi-Trigger's keep-vs-replace decision -- see craft_adversarial_healing_ perturbation's own docstring for what "success" means precisely.
craft_adversarial_healing_perturbation ¶
craft_adversarial_healing_perturbation(
vettori: ndarray,
target_idx: int,
radius_baseline: Optional[int] = None,
epsilon: float = 0.1,
n_steps: int = 50,
step_size: Optional[float] = None,
direction: str = "flip_to_dynamic",
) -> dict
Crafts a minimal adversarial perturbation to vettori[target_idx], within an L2 epsilon-ball, that flips enhanced_dense_healing_hybrid's Phi-Trigger decision at that index -- a targeted stress test, not random noise.
Reproduces enhanced_dense_healing_hybrid's own per-step computation at target_idx exactly (same baseline_mean window, same adaptive radius default, same inter-point-gradient vector) so the crafted perturbation is faithful to what the real healing pipeline would actually see, not a simplified stand-in.
BUG FIX: step_size used to default to 2*epsilon/n_steps -- tying the per-step move size to the epsilon budget. Verified directly this makes LARGER epsilon give WORSE (higher final |v_dinamic|) results, not better: a bigger budget means a bigger step, which overshoots and oscillates around the minimum instead of converging to it, which is the opposite of what a bigger budget should ever do for a correct optimizer. step_size is now independent of epsilon (a small fixed default, tuned to v_dinamic's typical local scale) -- epsilon only bounds where the iterate is allowed to end up (via projection after each step), not how big each step is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vettori
|
ndarray
|
array-like, shape (n_steps, dim), the (unperturbed) vector sequence. Not modified in place. |
required |
target_idx
|
int
|
index to attack; must be >= 2 (the healing loop's own starting point) and < len(vettori). |
required |
radius_baseline
|
Optional[int]
|
same meaning as enhanced_dense_healing_hybrid's own parameter; None uses the same adaptive default. |
None
|
epsilon
|
float
|
L2-norm budget for the perturbation (the attack is projected back into this ball after every gradient step). |
0.1
|
n_steps
|
int
|
number of PGD-style gradient steps. |
50
|
step_size
|
Optional[float]
|
per-step move size along the normalized gradient; None uses a small fixed default (0.02) independent of epsilon -- see the bug-fix note above for why. |
None
|
direction
|
str
|
"flip_to_dynamic" (evade -- make static-looking input pass through unhealed) or "flip_to_static" (suppress -- make dynamic-looking input get median-replaced instead). |
'flip_to_dynamic'
|
Returns:
| Type | Description |
|---|---|
dict
|
dict with: perturbed_vettori: copy of vettori with vettori[target_idx] replaced by the crafted perturbation. success: bool, True only if the trigger decision actually flipped in the requested direction (a small epsilon or too few steps can fail to cross the threshold). original_trigger_active / final_trigger_active: bool, the Phi-Trigger's decision (True = dynamic/kept) before and after the perturbation. original_magnitude / final_magnitude: float, |v_dinamic| before and after. perturbation_norm: float, actual L2 norm of the applied perturbation (<= epsilon). |
Source code in tools/ia_utils/adversarial_vector_attack.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
Two directions of attack:
flip_to_dynamic(evade) — make static-looking corruption pass through unhealed.flip_to_static(suppress) — make genuine dynamic signal get wrongly median-replaced.
Two real bugs were found and fixed during this utility's own
verification, not assumed correct: the default step_size used to scale
with the epsilon budget (a larger budget converged to a worse
result — verified directly, non-monotonic in epsilon — now a small fixed
default independent of epsilon), and calculate_phi_ab's [0,1] clip
saturating for inputs whose semantic distance exceeds
MAX_SEMANTIC_DISTANCE, giving an exact-zero gradient (a real property
of the formula, now detected and reported — perturbation_norm == 0,
success == False — rather than silently misreported as "no better
point found").
See also: ia_utils.vector_healing for
the function under test, and dense_evolution.healing for
the underlying Phi-Trigger primitives.