secmlt.trackers package
Submodules
secmlt.trackers.image_trackers module
Image-specific trackers.
- class secmlt.trackers.image_trackers.GradientsTracker[source]
Bases:
Tracker
Tracker for gradient images.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.Tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the gradients at the current iteration as images.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.image_trackers.SampleTracker[source]
Bases:
Tracker
Tracker for adversarial images.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.Tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the adversarial examples at the current iteration as images.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
secmlt.trackers.tensorboard_tracker module
Tensorboard tracking utilities.
- class secmlt.trackers.tensorboard_tracker.TensorboardTracker(logdir: str, trackers: list[Tracker] | None = None)[source]
Bases:
Tracker
Tracker for Tensorboard. Uses other trackers as subscribers.
- __init__(logdir: str, trackers: list[Tracker] | None = None) None [source]
Create tensorboard tracker.
- Parameters:
logdir (str) – Folder to store tensorboard logs.
trackers (list[Tracker] | None, optional) – List of trackers subsctibed to the updates, by default None.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Update all subscribed trackers.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
secmlt.trackers.trackers module
Trackers for attack metrics.
- class secmlt.trackers.trackers.GradientNormTracker(p: LpPerturbationModels = 'l2')[source]
Bases:
Tracker
Tracker for gradients.
- __init__(p: LpPerturbationModels = 'l2') None [source]
Create gradient norm tracker.
- Parameters:
p (LpPerturbationModels, optional) – Perturbation model to compute the norm, by default LpPerturbationModels.L2.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the sample-wise gradient of the loss w.r.t delta.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.trackers.LossTracker[source]
Bases:
Tracker
Tracker for attack loss.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the sample-wise loss of the attack at the current iteration.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.trackers.PerturbationNormTracker(p: LpPerturbationModels = 'l2')[source]
Bases:
Tracker
Tracker for perturbation norm.
- __init__(p: LpPerturbationModels = 'l2') None [source]
Create perturbation norm tracker.
- Parameters:
p (LpPerturbationModels, optional) – Perturbation model to compute the norm, by default LpPerturbationModels.L2.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the perturbation norm at the current iteration.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.trackers.PredictionTracker[source]
Bases:
Tracker
Tracker for model predictions.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the sample-wise model predictions at the current iteration.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.trackers.ScoresTracker(y: int | torch.Tensor | None = None)[source]
Bases:
Tracker
Tracker for model scores.
- track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the sample-wise model scores at the current iteration.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
- class secmlt.trackers.trackers.Tracker(name: str, tracker_type: str = 'scalar')[source]
Bases:
ABC
Class implementing the trackers for the attacks.
- __init__(name: str, tracker_type: str = 'scalar') None [source]
Create tracker.
- Parameters:
name (str) – Tracker name.
tracker_type (str, optional) – Type of tracker (mostly used for tensorboard functionalities), by default SCALAR. Available: SCALAR, IMAGE, MULTI_SCALAR.
- get() torch.Tensor [source]
Get the current tracking history.
- Returns:
History of tracked parameters.
- Return type:
torch.Tensor
- get_last_tracked() None | torch.Tensor [source]
Get last element tracked.
- Returns:
Returns the last tracked element if anything was tracked.
- Return type:
None | torch.Tensor
- abstract track(iteration: int, loss: torch.Tensor, scores: torch.Tensor, x_adv: torch.tensor, delta: torch.Tensor, grad: torch.Tensor) None [source]
Track the history of given attack observable parameters.
- Parameters:
iteration (int) – The attack iteration number.
loss (torch.Tensor) – The value of the (per-sample) loss of the attack.
scores (torch.Tensor) – The output scores from the model.
x_adv (torch.tensor) – The adversarial examples at the current iteration.
delta (torch.Tensor) – The adversarial perturbations at the current iteration.
grad (torch.Tensor) – The gradient of delta at the given iteration.
Module contents
Module implementing trackers for adversarial attacks.