secmlt.trackers package#

Submodules#

secmlt.trackers.image_trackers module#

Image-specific trackers.

class secmlt.trackers.image_trackers.ImageGradientsTracker[source]#

Bases: GradientsTracker

Tracker for gradient images.

class secmlt.trackers.image_trackers.ImageSampleTracker[source]#

Bases: SampleTracker

Tracker 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: Tracker

Tracker for Tensorboard. Uses other trackers as subscribers.

end_tracking() None[source]#

End tracking for current batch and update global sample offset.

get_last_tracked() NotImplementedError[source]#

Not implemented for this tracker.

init_tracking() None[source]#

Initialize tracking for a new batch.

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.

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: Tracker

Generic 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: Tracker

Tracker 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: Tracker

Tracker 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: Tracker

Tracker 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: Tracker

Generic 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: Tracker

Tracker 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: ABC

Class implementing the trackers for the attacks.

end_tracking() None[source]#

Finalize the current batch and append its history to _batches.

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

init_tracking() None[source]#

Initialize tracking for a new batch (clears the per-batch buffer).

reset() None[source]#

Clear all tracking history across all batches.

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.