secmlt.trackers package#
Submodules#
secmlt.trackers.image_trackers module#
Image-specific trackers.
- class secmlt.trackers.image_trackers.ImageGradientsTracker[source]#
Bases:
GradientsTrackerTracker for gradient images.
- class secmlt.trackers.image_trackers.ImageSampleTracker[source]#
Bases:
SampleTrackerTracker for adversarial examples.
secmlt.trackers.tensorboard_tracker module#
Tensorboard tracking utilities.
- class secmlt.trackers.tensorboard_tracker.TensorboardTracker(logdir: str, trackers: list[Tracker] | None = None)[source]#
Bases:
TrackerTracker for Tensorboard. Uses other trackers as subscribers.
- 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:
TrackerTracker for gradients.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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.GradientsTracker(tracker_type: str = 'multiple_scalars')[source]#
Bases:
TrackerGeneric tracker for gradients.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: Tensor, delta: Tensor, grad: Tensor) None[source]#
Track the gradients 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.LossTracker[source]#
Bases:
TrackerTracker for attack loss.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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:
TrackerTracker for perturbation norm.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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:
TrackerTracker for model predictions.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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.SampleTracker(tracker_type: str = 'multiple_scalars')[source]#
Bases:
TrackerGeneric tracker for adversarial samples.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: Tensor, delta: Tensor, grad: Tensor) None[source]#
Track adversarial examples 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 | Tensor | None = None)[source]#
Bases:
TrackerTracker for model scores.
- track(iteration: int, loss: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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:
ABCClass implementing the trackers for the attacks.
- get() Tensor[source]#
Get the current tracking history.
- Returns:
History of tracked parameters. When multiple batches were tracked, returns a tensor where batches are concatenated along the sample dimension (dim=0) and iterations are along the last dimension.
- Return type:
torch.Tensor
- get_last_tracked() None | 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: Tensor, scores: Tensor, x_adv: tensor, delta: Tensor, grad: 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.