secmlt.trackers package#
Submodules#
secmlt.trackers.image_trackers module#
Image-specific trackers.
- class secmlt.trackers.image_trackers.ImageGradientsTracker[source]#
Bases:
GradientsTrackerTracker 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:
SampleTrackerTracker 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:
BasePyTorchClassifierPassive 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.Moduleso 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.
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.
- 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:
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 | 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:
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 | 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:
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.