API Reference: Attacks¶
Base class¶
auditml.attacks.base.BaseAttack
¶
Bases: ABC
Abstract base for all privacy attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
Module
|
The trained model being attacked. Must be in |
required |
config
|
Optional AuditML configuration. When provided (YAML / CLI
workflow) the attack reads its params from it. When |
None
|
|
device
|
device | str
|
Torch device the model lives on. |
'cpu'
|
Source code in src/auditml/attacks/base.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 197 198 199 200 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 | |
run(member_loader: DataLoader, nonmember_loader: DataLoader) -> AttackResult
abstractmethod
¶
Execute the attack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member_loader
|
DataLoader
|
DataLoader over samples the target model WAS trained on. |
required |
nonmember_loader
|
DataLoader
|
DataLoader over samples the target model was NOT trained on. |
required |
Returns:
| Type | Description |
|---|---|
AttackResult
|
Predictions, ground truth, and confidence scores. |
Source code in src/auditml/attacks/base.py
evaluate() -> dict[str, float]
¶
Compute standard metrics from the most recent run().
Returns:
| Type | Description |
|---|---|
dict
|
Keys: accuracy, precision, recall, f1, auc_roc, auc_pr, tpr_at_1fpr, tpr_at_01fpr. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/base.py
get_model_outputs(loader: DataLoader) -> tuple[np.ndarray, np.ndarray, np.ndarray]
¶
Run the target model on every sample in loader.
Returns:
| Type | Description |
|---|---|
(probabilities, logits, labels)
|
|
Source code in src/auditml/attacks/base.py
get_loss_values(loader: DataLoader) -> np.ndarray
¶
Compute per-sample cross-entropy loss for every sample.
This is critical for threshold-based MIA: training samples typically have lower loss because the model has seen them before.
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
Source code in src/auditml/attacks/base.py
Attack results¶
auditml.attacks.results.AttackResult
dataclass
¶
Container for the outputs of a single attack run.
Attributes:
| Name | Type | Description |
|---|---|---|
predictions |
ndarray
|
Binary array — the attack's guess for each sample.
For membership inference: 1 = predicted member, 0 = predicted
non-member. Length equals |
ground_truth |
ndarray
|
Binary array — the true label for each sample. For membership inference: 1 = actual member, 0 = actual non-member. |
confidence_scores |
ndarray
|
Continuous score per sample indicating how confident the attack is. Higher = more confident the sample is a member (for MIA) or more confident in the predicted attribute (for attribute inference). Used for ROC curves and threshold-independent evaluation. |
attack_name |
str
|
Human-readable name, e.g. |
metadata |
dict[str, Any]
|
Free-form dict for attack-specific extras (e.g. reconstructed images for model inversion, per-class breakdowns, etc.). |
Source code in src/auditml/attacks/results.py
__post_init__() -> None
¶
Validate array lengths match.
Source code in src/auditml/attacks/results.py
Threshold MIA¶
auditml.attacks.mia_threshold.ThresholdMIA
¶
Bases: BaseAttack
Threshold-based Membership Inference Attack.
Supports three signal metrics:
"loss"— per-sample cross-entropy loss (lower → more likely member)"confidence"— max softmax probability (higher → more likely member)"entropy"— prediction entropy (lower → more likely member)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
The trained model to attack. |
required | |
config
|
AuditML config. Reads |
None
|
|
device
|
Torch device. |
'cpu'
|
Source code in src/auditml/attacks/mia_threshold.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 197 198 199 200 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 286 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
run(member_loader: DataLoader, nonmember_loader: DataLoader) -> AttackResult
¶
Execute the threshold MIA.
Steps: 1. Compute the chosen metric (loss/confidence/entropy) for every member and non-member sample. 2. Find the optimal threshold that maximises accuracy. 3. Classify each sample as member or non-member using that threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member_loader
|
DataLoader
|
DataLoader for samples the model WAS trained on. |
required |
nonmember_loader
|
DataLoader
|
DataLoader for samples the model was NOT trained on. |
required |
Returns:
| Type | Description |
|---|---|
AttackResult
|
|
Source code in src/auditml/attacks/mia_threshold.py
evaluate_per_class() -> dict[int, dict[str, float]]
¶
Compute evaluation metrics separately for each class.
This reveals whether the attack works better on certain classes. For example, rare classes might be easier to identify as members because the model memorises them more.
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, float]]
|
Mapping from class label → metric dictionary. Each inner dict
has the same keys as |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/mia_threshold.py
generate_report(output_dir: str | Path) -> Path
¶
Generate a complete evaluation report with metrics and plots.
Creates the following files in output_dir:
metrics.json— overall evaluation metricsper_class_metrics.json— per-class breakdownroc_curve.png— ROC curve plotscore_distributions.png— histogram of member vs non-member scoresper_class_accuracy.png— bar chart of per-class attack accuracysummary.txt— human-readable text summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Directory where all report files are saved. Created if it doesn't exist. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The output directory. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/mia_threshold.py
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
Shadow Model MIA¶
auditml.attacks.mia_shadow.ShadowMIA
¶
Bases: BaseAttack
Shadow-model Membership Inference Attack.
Workflow executed by run():
- Train shadow models — each on a different random split of the
same dataset distribution. The number and epochs come from
config.attack_params.mia_shadow. - Collect attack data — for each shadow model, gather its softmax outputs on its own members (label 1) and non-members (label 0).
- Train attack MLP — a small binary classifier on the collected (probability_vector, membership_label) dataset.
- Attack the target — run the target model on the supplied member and non-member loaders, then classify each sample with the trained attack model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
Module
|
The trained model being audited. |
required |
config
|
Full AuditML configuration. |
None
|
|
device
|
device | str
|
Torch device. |
'cpu'
|
shadow_dataset
|
Dataset | None
|
The dataset from which shadow model training data is drawn.
This should be the same distribution as the target's training
data (e.g. the full CIFAR-10 training set). If |
None
|
shadow_models
|
list[tuple[Module, DataLoader, DataLoader]] | None
|
Pre-trained shadow models. If provided, skips the training step.
Each entry is |
None
|
Source code in src/auditml/attacks/mia_shadow.py
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 197 198 199 200 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 286 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
run(member_loader: DataLoader, nonmember_loader: DataLoader) -> AttackResult
¶
Execute the full shadow-model MIA pipeline.
Steps: 1. Train shadow models (or use pre-trained ones) 2. Collect (output, membership) pairs from all shadows 3. Train the attack MLP on shadow data 4. Use the attack MLP to classify target model outputs
Source code in src/auditml/attacks/mia_shadow.py
evaluate_per_class() -> dict[int, dict[str, float]]
¶
Compute evaluation metrics separately for each class.
Groups all samples by their original class label and computes the full metric suite for each class. This reveals which classes are most vulnerable to the shadow model attack.
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, float]]
|
Mapping from class label to metric dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/mia_shadow.py
generate_report(output_dir: str | Path) -> Path
¶
Generate a complete evaluation report with metrics and plots.
Creates the following files in output_dir:
metrics.json— overall evaluation metricsper_class_metrics.json— per-class breakdownroc_curve.png— ROC curve plotconfidence_distributions.png— histogram of attack confidenceper_class_accuracy.png— bar chart of per-class accuracysummary.txt— human-readable text summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Directory where all report files are saved. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The output directory. |
Source code in src/auditml/attacks/mia_shadow.py
Model Inversion¶
auditml.attacks.model_inversion.ModelInversion
¶
Bases: BaseAttack
Gradient-based Model Inversion attack.
For each target class, optimises a synthetic image so that the model classifies it with maximum confidence. The reconstructed images reveal what the model has learned — and potentially memorised — about each class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
Module
|
The trained model to attack (white-box — needs gradients). |
required |
config
|
Full AuditML configuration. |
None
|
|
device
|
device | str
|
Torch device. |
'cpu'
|
input_shape
|
tuple[int, ...] | None
|
Shape of the model's input, e.g. |
None
|
Source code in src/auditml/attacks/model_inversion.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 197 198 199 200 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 286 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | |
run(member_loader: DataLoader, nonmember_loader: DataLoader) -> AttackResult
¶
Execute the model inversion attack.
For each target class, reconstruct an image and measure how confidently the model classifies it. Then use the member and non-member loaders to evaluate: does the model assign higher confidence to reconstructions of classes it trained on?
The member_loader and nonmember_loader are used to
compute a membership-like signal: for each sample, we measure
the similarity between the model's output on that sample and
the reconstructed class prototype. Members tend to produce
outputs closer to the reconstruction.
Source code in src/auditml/attacks/model_inversion.py
invert_class(target_class: int, num_iterations: int | None = None) -> tuple[torch.Tensor, float]
¶
Reconstruct an image for a single target class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_class
|
int
|
The class label to reconstruct. |
required |
num_iterations
|
int | None
|
Override the config value. Uses |
None
|
Returns:
| Type | Description |
|---|---|
(reconstructed_image, confidence)
|
|
Source code in src/auditml/attacks/model_inversion.py
generate_report(output_dir: str | Path) -> Path
¶
Generate a complete model inversion report.
Creates the following files in output_dir:
metrics.json— overall evaluation metricsreconstructions.png— grid of reconstructed imagesreconstruction_confidence.png— per-class confidence bar chartsimilarity_distributions.png— member vs non-member similarityroc_curve.png— ROC curvesummary.txt— human-readable text summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Directory where all report files are saved. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The output directory. |
Source code in src/auditml/attacks/model_inversion.py
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
Attribute Inference¶
auditml.attacks.attribute_inference.AttributeInference
¶
Bases: BaseAttack
Attribute inference attack via model output analysis.
For each sample the attacker observes the target model's softmax probability vector and tries to predict a sensitive attribute that the model was not designed to reveal. The attack trains a small MLP on the member (training) data, then evaluates whether members' attributes are more predictable than non-members'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_model
|
Module
|
The trained model being audited. |
required |
config
|
Full AuditML configuration. |
None
|
|
device
|
device | str
|
Torch device. |
'cpu'
|
num_groups
|
int | None
|
Override for the number of sensitive-attribute groups.
If |
None
|
class_to_group
|
dict[int, int] | None
|
Explicit mapping |
None
|
Source code in src/auditml/attacks/attribute_inference.py
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 197 198 199 200 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 286 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
run(member_loader: DataLoader, nonmember_loader: DataLoader) -> AttackResult
¶
Execute the attribute inference attack.
Steps:
- Extract the target model's softmax outputs for all samples.
- Map class labels → sensitive attribute (group labels).
- Train an attack MLP on member outputs → group.
- Score every sample by the confidence of the correct group prediction. Members should score higher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member_loader
|
DataLoader
|
DataLoader over training (member) samples. |
required |
nonmember_loader
|
DataLoader
|
DataLoader over non-member samples. |
required |
Returns:
| Type | Description |
|---|---|
AttackResult
|
|
Source code in src/auditml/attacks/attribute_inference.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
predict_attributes(probs: np.ndarray) -> np.ndarray
¶
Predict the sensitive attribute for each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probs
|
ndarray
|
Softmax outputs from the target model, shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Predicted group IDs, shape |
Source code in src/auditml/attacks/attribute_inference.py
evaluate_per_class() -> dict[int, dict[str, float]]
¶
Compute evaluation metrics separately for each original class.
Groups all samples by their original class label and computes the full metric suite for each class. This reveals which classes are most vulnerable to the attribute inference attack.
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, float]]
|
Mapping from class label to metric dictionary. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/attribute_inference.py
evaluate_per_group() -> dict[str, dict[int, float]]
¶
Compute attribute prediction accuracy for each group.
Returns separate accuracy dictionaries for members and non-members. A gap between the two signals privacy leakage.
Returns:
| Type | Description |
|---|---|
dict with keys ``"member"`` and ``"nonmember"``, each mapping
|
|
group ID to prediction accuracy on that group.
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/auditml/attacks/attribute_inference.py
generate_report(output_dir: str | Path) -> Path
¶
Generate a complete evaluation report with metrics and plots.
Creates the following files in output_dir:
metrics.json— overall evaluation metricsper_class_metrics.json— per-class breakdownper_group_accuracy.json— per-group attribute accuracyroc_curve.png— ROC curve plotconfidence_distributions.png— member vs non-member histogramper_class_accuracy.png— bar chart of per-class accuracyattribute_accuracy.png— per-group member vs non-member accuracysummary.txt— human-readable text summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str | Path
|
Directory where all report files are saved. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The output directory. |
Source code in src/auditml/attacks/attribute_inference.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 | |
Visualisation helpers¶
auditml.attacks.visualization
¶
Visualization utilities for AuditML attack results.
Provides reusable plotting functions that any attack can call.
All functions optionally save to disk and return a matplotlib.figure.Figure
so callers can further customise or display interactively.
The module uses the Agg backend by default so that plots can be generated
on headless servers (e.g. Colab, CI) without requiring a display.
plot_roc_curve(ground_truth: np.ndarray, confidence_scores: np.ndarray, save_path: str | Path | None = None, title: str = 'ROC Curve — Membership Inference Attack') -> plt.Figure
¶
Plot the Receiver Operating Characteristic curve.
The ROC curve shows the trade-off between True Positive Rate (TPR) and False Positive Rate (FPR) at every possible threshold. The Area Under the Curve (AUC) summarises overall attack effectiveness: 0.5 = random guessing, 1.0 = perfect attack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ground_truth
|
ndarray
|
Binary array (1 = member, 0 = non-member). |
required |
confidence_scores
|
ndarray
|
Continuous scores where higher = more likely member. |
required |
save_path
|
str | Path | None
|
If given, the plot is saved to this path. |
None
|
title
|
str
|
Plot title. |
'ROC Curve — Membership Inference Attack'
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/auditml/attacks/visualization.py
plot_score_distributions(member_scores: np.ndarray, nonmember_scores: np.ndarray, metric_name: str = 'loss', threshold: float | None = None, save_path: str | Path | None = None, title: str | None = None) -> plt.Figure
¶
Plot overlapping histograms of member vs non-member scores.
This is the most intuitive visualisation for threshold MIA: if the two distributions overlap a lot, the attack is weak (can't tell members from non-members). If they're well-separated, the attack is strong.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member_scores
|
ndarray
|
Raw signal values for training members. |
required |
nonmember_scores
|
ndarray
|
Raw signal values for non-members. |
required |
metric_name
|
str
|
Name of the metric (for axis label). |
'loss'
|
threshold
|
float | None
|
If given, a vertical line is drawn at this value. |
None
|
save_path
|
str | Path | None
|
If given, saves the figure. |
None
|
title
|
str | None
|
Plot title. Auto-generated if |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/auditml/attacks/visualization.py
plot_per_class_metrics(per_class_metrics: dict[int, dict[str, float]], metric_key: str = 'accuracy', save_path: str | Path | None = None, title: str | None = None) -> plt.Figure
¶
Bar chart showing a chosen metric for each class.
Useful for identifying which classes are most vulnerable to membership inference (higher accuracy = more vulnerable).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
per_class_metrics
|
dict[int, dict[str, float]]
|
Output of |
required |
metric_key
|
str
|
Which metric to plot (default |
'accuracy'
|
save_path
|
str | Path | None
|
If given, saves the figure. |
None
|
title
|
str | None
|
Plot title. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/auditml/attacks/visualization.py
plot_reconstructions(reconstructions: dict[int, np.ndarray], confidences: dict[int, float] | None = None, save_path: str | Path | None = None, title: str = 'Model Inversion — Reconstructed Images') -> plt.Figure
¶
Display reconstructed images in a grid, one per class.
This is the key visual for model inversion: if the images look like recognisable digits/objects, the model has leaked training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconstructions
|
dict[int, ndarray]
|
Mapping from class label to image array of shape
|
required |
confidences
|
dict[int, float] | None
|
Optional mapping from class label to reconstruction confidence. Displayed below each image. |
None
|
save_path
|
str | Path | None
|
If given, saves the figure. |
None
|
title
|
str
|
Plot title. |
'Model Inversion — Reconstructed Images'
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/auditml/attacks/visualization.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
plot_reconstruction_confidence(confidences: dict[int, float], save_path: str | Path | None = None, title: str = 'Reconstruction Confidence per Class') -> plt.Figure
¶
Bar chart of model confidence on each reconstructed image.
Higher confidence means the optimisation was more successful at producing an image the model strongly associates with that class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
confidences
|
dict[int, float]
|
Mapping from class label to confidence (softmax probability). |
required |
save_path
|
str | Path | None
|
If given, saves the figure. |
None
|
title
|
str
|
Plot title. |
'Reconstruction Confidence per Class'
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/auditml/attacks/visualization.py
plot_attribute_accuracy(member_accuracy: dict[int, float], nonmember_accuracy: dict[int, float], save_path: str | Path | None = None, title: str = 'Attribute Prediction Accuracy — Members vs Non-Members') -> plt.Figure
¶
Side-by-side bar chart comparing attribute accuracy for members and non-members.
A large gap between member and non-member accuracy indicates that the model's outputs reveal more about the sensitive attribute for training data — a privacy leak.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
member_accuracy
|
dict[int, float]
|
Mapping from group ID to attribute prediction accuracy on members. |
required |
nonmember_accuracy
|
dict[int, float]
|
Mapping from group ID to attribute prediction accuracy on non-members. |
required |
save_path
|
str | Path | None
|
If given, saves the figure. |
None
|
title
|
str
|
Plot title. |
'Attribute Prediction Accuracy — Members vs Non-Members'
|
Returns:
| Type | Description |
|---|---|
Figure
|
|