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.

track(iteration: int, loss: Tensor, scores: Tensor, x_adv: Tensor, delta: Tensor, grad: 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.ImageSampleTracker[source]#

Bases: SampleTracker

Tracker for adversarial examples.

secmlt.trackers.model_tracker module#

Model-level tracker for use with external attack libraries.

class secmlt.trackers.model_tracker.ModelTracker(model: BaseModel | torch.nn.Module, trackers: list[Tracker] | Tracker | None = None)[source]#

Bases: BasePyTorchClassifier

Passive tracker that wraps a model to intercept forward calls.

This is an alternative to attack-level trackers, designed for use with external libraries (e.g., Foolbox, Adversarial Library) where modifying the attack loop is not possible. It registers a forward hook on the underlying nn.Module so that every forward pass feeds data to the subscribed trackers.

Notes

Gradient-aware trackers are supported without additional forward/backward queries by attaching a backward hook to the forward input tensor. The hook is executed when the caller performs its regular backward pass.

detach() None[source]#

Remove the forward hook from the model.

end_tracking() None[source]#

End tracking for the current batch.

init_tracking(x_orig: Tensor | None = None, y: Tensor | None = None) None[source]#

Initialize tracking for a new batch.

reset() None[source]#

Reset all subscribed trackers.

property trackers: list[Tracker]#

Return the list of subscribed trackers.

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.

property loss_fn: object | None#

Return the first sub-tracker loss function, if any.

property requires_grad: bool#

True when any sub-tracker needs gradients.

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 | None) – The gradient of delta at the given iteration. The model can optionally pass None for the gradient, in which case this tracker will simply skip tracking for that iteration.

class secmlt.trackers.trackers.LossTracker(loss_fn: Callable | None = None)[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 | None) – The value of the (per-sample) loss of the attack. The model can optionally pass None for the loss, in which case this tracker will attempt to compute the loss using the provided loss_fn. If loss_fn is not provided, it will skip tracking for that iteration.

  • 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.