ASRAttacker¶
ASRAttacker ¶
Run white-box CTC attacks against an :class:~asr_attacks.backends.ASRBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ASRBackend
|
Model wrapper that can encode targets, decode audio, and return differentiable CTC logits. |
required |
verbose
|
bool
|
Show progress bars and early-stop messages. |
True
|
Source code in src/asr_attacks/attacker.py
decode ¶
Greedy CTC decode of a waveform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
One utterance, shape |
required |
Returns:
| Type | Description |
|---|---|
str
|
Transcript string. wav2vec2-style word boundaries appear as |
Source code in src/asr_attacks/attacker.py
fgsm ¶
fgsm(audio: Tensor | ndarray, target: str | list[str] | None = None, epsilon: float = 0.2, targeted: bool = False, label: str | list[str] | None = None, norm: float | str = 'inf') -> np.ndarray
One-step Fast Gradient Sign / Fast Gradient Method.
x_adv = clip_[-1, 1](x + epsilon * s * normalize_p(grad_x CTC(f(x), y)))
with s = +1 untargeted and s = -1 targeted. normalize_p is
sign(g) (L-inf, FGSM of Goodfellow et al., 2015),
g / ||g||_2 or g / ||g||_1 (L2 / L1 Fast Gradient Method of
Kurakin et al., 2017; L1 follows ART).
Targeted FGSM is also from Kurakin et al., 2017.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
One utterance, shape |
required |
target
|
str | list[str] | None
|
Target transcript when |
None
|
epsilon
|
float
|
Step size, measured in the chosen |
0.2
|
targeted
|
bool
|
If true, minimize CTC loss toward |
False
|
label
|
str | list[str] | None
|
Ground-truth transcript for untargeted attacks. Defaults to
the model's clean prediction. Raises |
None
|
norm
|
float | str
|
|
'inf'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
CPU NumPy waveform, clipped to |
Source code in src/asr_attacks/attacker.py
bim ¶
bim(audio: Tensor | ndarray, target: str | list[str] | None = None, epsilon: float = 0.2, alpha: float | None = None, num_iter: int | None = None, nested: bool = True, targeted: bool = False, early_stop: bool = False, label: str | list[str] | None = None, norm: float | str = 'inf') -> np.ndarray
Basic Iterative Method (Kurakin et al., 2017).
Iterative signed/normalized gradient steps projected onto the L-norm
ball of radius epsilon, then clipped to [-1, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
Clean waveform. |
required |
target
|
str | list[str] | None
|
Target transcript when |
None
|
epsilon
|
float
|
Maximum perturbation in the chosen |
0.2
|
alpha
|
float | None
|
Per-step size. Defaults to |
None
|
num_iter
|
int | None
|
Steps. Defaults to the paper rule
|
None
|
nested
|
bool
|
If true, hide the progress bar when this call sits inside another tqdm loop. |
True
|
targeted
|
bool
|
Targeted vs untargeted objective. |
False
|
early_stop
|
bool
|
Stop when the greedy decode matches (targeted) or differs from (untargeted) the reference transcript. |
False
|
label
|
str | list[str] | None
|
Ground-truth transcript for untargeted attacks, used by the loss and the early-stop check. Defaults to the clean decode. |
None
|
norm
|
float | str
|
|
'inf'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
CPU NumPy waveform. |
Source code in src/asr_attacks/attacker.py
pgd ¶
pgd(audio: Tensor | ndarray, target: str | list[str] | None = None, epsilon: float = 0.3, alpha: float | None = None, num_iter: int = 40, nested: bool = True, targeted: bool = False, early_stop: bool = False, random_start: bool = True, label: str | list[str] | None = None, norm: float | str = 'inf', restarts: int = 1) -> np.ndarray
Projected Gradient Descent (Madry et al., 2018).
Same inner loop as BIM with an optional random start inside the
L-norm ball. Pass random_start=False for BIM-style init.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
Clean waveform. |
required |
target
|
str | list[str] | None
|
Target transcript when |
None
|
epsilon
|
float
|
Maximum perturbation in the chosen |
0.3
|
alpha
|
float | None
|
Per-step size. Defaults to |
None
|
num_iter
|
int
|
Number of gradient steps. |
40
|
nested
|
bool
|
Hide progress bar when nested in another loop. |
True
|
targeted
|
bool
|
Targeted vs untargeted objective. |
False
|
early_stop
|
bool
|
Stop on transcription success. |
False
|
random_start
|
bool
|
Sample the initial point inside the L- |
True
|
label
|
str | list[str] | None
|
Ground-truth transcript for untargeted attacks, used by the loss and the early-stop check. Defaults to the clean decode. |
None
|
norm
|
float | str
|
|
'inf'
|
restarts
|
int
|
Independent random starts; keep the strongest. Requires
|
1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
CPU NumPy waveform. |
Source code in src/asr_attacks/attacker.py
cw ¶
cw(audio: Tensor | ndarray, target: str | list[str] | None = None, epsilon: float = _CW_EPS, c: float = 1.0, learning_rate: float = _CW_LR, num_iter: int = 5000, decrease_factor_eps: float = 0.8, num_iter_decrease_eps: int = 10, check_every: int | None = None, optimizer: str | None = 'adam', nested: bool = True, early_stop: bool = False, search_eps: bool = True, targeted: bool = False, label: str | list[str] | None = None, db_bound: float | None = None, loss: str = 'ctc', kappa: float = 0.0) -> np.ndarray
Audio Carlini & Wagner attack (arXiv:1801.01944).
Sec. III-B minimizes ||delta||_2^2 + c * CTC(x+delta, t) inside an
L-inf box (epsilon, or db_bound), with Adam and paper-style
bound shrinking. Untargeted mode flips the CTC sign (package extension).
Sec. III-C (loss="margin", targeted only) refines a CTC solution
with a per-frame hinge on logits. Sec. III-F allows target=""
(silence-token hinge; success is an empty transcript).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
Clean waveform. |
required |
target
|
str | list[str] | None
|
Target transcript when |
None
|
epsilon
|
float
|
Absolute L-inf box (default ≈ 2000/32768). Ignored when
|
_CW_EPS
|
c
|
float
|
Weight on the CTC (or silence / margin) term. Default |
1.0
|
learning_rate
|
float
|
Optimizer step size (default ≈ 10/32768). |
_CW_LR
|
num_iter
|
int
|
Optimization steps (default 5000). |
5000
|
decrease_factor_eps
|
float
|
Multiplier applied to |
0.8
|
num_iter_decrease_eps
|
int
|
Used as |
10
|
check_every
|
int | None
|
Decode / bound-shrink interval. Defaults to
|
None
|
optimizer
|
str | None
|
|
'adam'
|
nested
|
bool
|
Hide progress bar when nested in another loop. |
True
|
early_stop
|
bool
|
Stop on transcription success. Cannot be combined with
|
False
|
search_eps
|
bool
|
Shrink |
True
|
targeted
|
bool
|
Targeted vs untargeted objective. |
False
|
label
|
str | list[str] | None
|
Ground-truth transcript for untargeted attacks. Defaults to the clean decode. |
None
|
db_bound
|
float | None
|
If set, |
None
|
loss
|
str
|
|
'ctc'
|
kappa
|
float
|
Margin in the Sec. III-C hinge. |
0.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
CPU NumPy waveform. |
Source code in src/asr_attacks/attacker.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
imperceptible ¶
imperceptible(audio: Tensor | ndarray, target: str | list[str] | None = None, epsilon: float = _EPS_QIN, c: float | None = None, learning_rate1: float = _LR1_QIN, learning_rate2: float = _LR2_QIN, num_iter1: int = 1000, num_iter2: int = 4000, decrease_factor_eps: float = 0.8, num_iter_decrease_eps: int = 10, check_every: int | None = None, optimizer1: str | None = 'sgd', optimizer2: str | None = 'adam', nested: bool = True, early_stop_cw: bool = False, search_eps_cw: bool = True, alpha: float = 0.05, sample_rate: int = 16000, targeted: bool = True, label: str | list[str] | None = None, mode: str = 'imperceptible', rooms: RoomSimulator | None = None, robust_delta: float = _ROBUST_DELTA, num_iter_r1: int = 2000, num_iter_r2: int = 4000, learning_rate_r1: float = _LR_R1, learning_rate_r2: float = _LR_R2, num_iter_ir1: int = 4000, num_iter_ir2: int = 6000, learning_rate_ir1: float = _LR2_QIN, learning_rate_ir2: float = _LR_IR2) -> np.ndarray
Qin et al. imperceptible / robust ASR attack (arXiv:1903.10346).
Default mode="imperceptible" is the offline Sec. 4 attack: stage 1
is a CTC-only L-inf bound search with signed gradient steps; stage 2
minimizes CTC + alpha * l_theta (CTC stands in for the paper's
Lingvo cross-entropy). mode="robust" / "imperceptible_robust"
need a :class:~asr_attacks.rooms.RoomSimulator via rooms=.
Targeted mode follows Qin et al. Untargeted (targeted=False) is a
package extension. c is deprecated and ignored (warns if passed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio
|
Tensor | ndarray
|
Clean waveform. |
required |
target
|
str | list[str] | None
|
Target transcript when |
None
|
epsilon
|
float
|
Stage-1 / R1 L-inf start bound (default ≈ 0.061). |
_EPS_QIN
|
c
|
float | None
|
Deprecated; ignored with a warning when not |
None
|
learning_rate1
|
float
|
Stage-1 step size (default ≈ 100/32768). |
_LR1_QIN
|
learning_rate2
|
float
|
Stage-2 Adam step size (default ≈ 1/32768). |
_LR2_QIN
|
num_iter1
|
int
|
Stage-1 steps (default 1000). |
1000
|
num_iter2
|
int
|
Stage-2 steps (default 4000). |
4000
|
decrease_factor_eps
|
float
|
Bound shrink factor on success. |
0.8
|
num_iter_decrease_eps
|
int
|
Used as |
10
|
check_every
|
int | None
|
Bound-shrink / success-check interval. In the robust modes it is how often the M rooms are decoded (R1, R2, IR1, IR2); the gradient steps still use M rooms every iteration. |
None
|
optimizer1
|
str | None
|
Stage-1 optimizer ( |
'sgd'
|
optimizer2
|
str | None
|
Stage-2 optimizer (default |
'adam'
|
nested
|
bool
|
Hide progress bars when nested. |
True
|
early_stop_cw
|
bool
|
Early-stop stage 1. |
False
|
search_eps_cw
|
bool
|
Epsilon search in stage 1. |
True
|
alpha
|
float
|
Initial weight on the psychoacoustic penalty (default 0.05). |
0.05
|
sample_rate
|
int
|
Waveform sample rate for the masking threshold. |
16000
|
targeted
|
bool
|
Targeted (default) vs untargeted (extension). |
True
|
label
|
str | list[str] | None
|
Ground-truth for untargeted attacks. |
None
|
mode
|
str
|
|
'imperceptible'
|
rooms
|
RoomSimulator | None
|
Room simulator required for robust modes. |
None
|
robust_delta
|
float
|
Extra bound for R2 / IR stages (≈ 300/32768). |
_ROBUST_DELTA
|
num_iter_r1
|
int
|
Robust stage R1 iterations. |
2000
|
num_iter_r2
|
int
|
Robust stage R2 iterations. |
4000
|
learning_rate_r1
|
float
|
R1 step size. |
_LR_R1
|
learning_rate_r2
|
float
|
R2 step size. |
_LR_R2
|
num_iter_ir1
|
int
|
Imperceptible+robust IR1 iterations. |
4000
|
num_iter_ir2
|
int
|
Imperceptible+robust IR2 iterations. |
6000
|
learning_rate_ir1
|
float
|
IR1 Adam step size. |
_LR2_QIN
|
learning_rate_ir2
|
float
|
IR2 Adam step size. |
_LR_IR2
|
Returns:
| Type | Description |
|---|---|
ndarray
|
CPU NumPy waveform (best successful stage-2 / IR iterate, or the |
ndarray
|
stage-1 / robust fallback). |
Source code in src/asr_attacks/attacker.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
wer ¶
Mean Levenshtein word error rate over a batch.
| in transcripts is treated as a space before scoring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
references
|
list[str]
|
Ground-truth transcripts, one per utterance. |
required |
audios
|
list[ndarray]
|
Waveforms decoded with :meth: |
required |
Returns:
| Type | Description |
|---|---|
float
|
Pair of |
list[tuple[int, int, int]]
|
|