diff --git a/src/torchattack/admix.py b/src/torchattack/admix.py index 42f899c..86432b1 100644 --- a/src/torchattack/admix.py +++ b/src/torchattack/admix.py @@ -11,6 +11,21 @@ class Admix(Attack): From the paper 'Admix: Enhancing the Transferability of Adversarial Attacks', https://arxiv.org/abs/2102.00436 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + portion: Portion for the mixed image. Defaults to 0.2. + size: Number of randomly sampled images. Defaults to 3. + num_classes: Number of classes of the dataset used. Defaults to 1001. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -29,24 +44,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the Admix attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - portion: Portion for the mixed image. Defaults to 0.2. - size: Number of randomly sampled images. Defaults to 3. - num_classes: Number of classes of the dataset used. Defaults to 1001. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/deepfool.py b/src/torchattack/deepfool.py index 99c6861..0c1fab7 100644 --- a/src/torchattack/deepfool.py +++ b/src/torchattack/deepfool.py @@ -11,6 +11,16 @@ class DeepFool(Attack): From the paper 'DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks' https://arxiv.org/abs/1511.04599 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + steps: Number of steps. Defaults to 100. + overshoot: Overshoot parameter for noise control. Defaults to 0.02. + num_classes: Number of classes to consider. Defaults to 10. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. """ def __init__( @@ -24,19 +34,6 @@ def __init__( clip_min: float = 0.0, clip_max: float = 1.0, ) -> None: - """Initialize the DeepFool attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - steps: Number of steps. Defaults to 100. - overshoot: Overshoot parameter for noise control. Defaults to 0.02. - num_classes: Number of classes to consider. Defaults to 10. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/difgsm.py b/src/torchattack/difgsm.py index f5955b0..1463a04 100644 --- a/src/torchattack/difgsm.py +++ b/src/torchattack/difgsm.py @@ -12,6 +12,26 @@ class DIFGSM(Attack): From the paper 'Improving Transferability of Adversarial Examples with Input Diversity' https://arxiv.org/abs/1803.06978 + + Note: + Key parameters include `resize_rate` and `diversity_prob`, which defines the + scale size of the resized image and the probability of applying input + diversity. The default values are set to 0.9 and 1.0 respectively (implying + that input diversity is always applied). + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + resize_rate: The resize rate. Defaults to 0.9. + diversity_prob: Applying input diversity with probability. Defaults to 1.0. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -29,29 +49,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the DI-FGSM attack. - - Note: - Key parameters include `resize_rate` and `diversity_prob`, which defines the - scale size of the resized image and the probability of applying input - diversity. The default values are set to 0.9 and 1.0 respectively (implying - that input diversity is always applied). - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - resize_rate: The resize rate. Defaults to 0.9. - diversity_prob: Applying input diversity with probability. Defaults to 1.0. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/fgsm.py b/src/torchattack/fgsm.py index 539b4d1..5ab5984 100644 --- a/src/torchattack/fgsm.py +++ b/src/torchattack/fgsm.py @@ -11,6 +11,15 @@ class FGSM(Attack): From the paper 'Explaining and Harnessing Adversarial Examples', https://arxiv.org/abs/1412.6572 + + Args: + model: A torch.nn.Module network model. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: Maximum perturbation measured by Linf. Defaults to 8/255. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -23,18 +32,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the FGSM attack. - - Args: - model: A torch.nn.Module network model. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: Maximum perturbation measured by Linf. Defaults to 8/255. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/mifgsm.py b/src/torchattack/mifgsm.py index 5df6d38..ec9bf8c 100644 --- a/src/torchattack/mifgsm.py +++ b/src/torchattack/mifgsm.py @@ -11,6 +11,18 @@ class MIFGSM(Attack): From the paper 'Boosting Adversarial Attacks with Momentum', https://arxiv.org/abs/1710.06081 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -26,21 +38,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the MI-FGSM attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/nifgsm.py b/src/torchattack/nifgsm.py index 507e516..7f0f40c 100644 --- a/src/torchattack/nifgsm.py +++ b/src/torchattack/nifgsm.py @@ -15,6 +15,18 @@ class NIFGSM(Attack): From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial Attacks' https://arxiv.org/abs/1908.06281 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -30,21 +42,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the NI-FGSM attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/pgd.py b/src/torchattack/pgd.py index 88713bf..48b0e9f 100644 --- a/src/torchattack/pgd.py +++ b/src/torchattack/pgd.py @@ -11,6 +11,18 @@ class PGD(Attack): From the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks' https://arxiv.org/abs/1706.06083 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + random_start: Start from random uniform perturbation. Defaults to True. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -26,21 +38,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the PGD attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - random_start: Start from random uniform perturbation. Defaults to True. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/pgdl2.py b/src/torchattack/pgdl2.py index 34a651d..042a226 100644 --- a/src/torchattack/pgdl2.py +++ b/src/torchattack/pgdl2.py @@ -13,6 +13,18 @@ class PGDL2(Attack): From the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks', https://arxiv.org/abs/1706.06083 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation, measured in L2. Defaults to 1.0. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + random_start: Start from random uniform perturbation. Defaults to True. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -28,21 +40,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the PGD attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation, measured in L2. Defaults to 1.0. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - random_start: Start from random uniform perturbation. Defaults to True. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/sinifgsm.py b/src/torchattack/sinifgsm.py index 85567b0..717b7d5 100644 --- a/src/torchattack/sinifgsm.py +++ b/src/torchattack/sinifgsm.py @@ -9,8 +9,21 @@ class SINIFGSM(Attack): """The SI-NI-FGSM (Scale-invariant Nesterov-accelerated Iterative FGSM) attack. - From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial - Attacks' https://arxiv.org/abs/1908.06281 + From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial + Attacks' https://arxiv.org/abs/1908.06281 + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + m: Number of scaled copies of the image. Defaults to 3. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -27,22 +40,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the SI-NI-FGSM attack. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - m: Number of scaled copies of the image. Defaults to 3. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/tifgsm.py b/src/torchattack/tifgsm.py index 28d2215..a8fd8bb 100644 --- a/src/torchattack/tifgsm.py +++ b/src/torchattack/tifgsm.py @@ -13,6 +13,25 @@ class TIFGSM(Attack): From the paper 'Evading Defenses to Transferable Adversarial Examples by Translation-Invariant Attacks' https://arxiv.org/abs/1904.02884 + + Note: + Key parameters include `kernel_len` and `n_sig`, which defines the size and + the radius of the gaussian kernel. The default values are set to 15 and 3 + respectively, which are best according to the paper. + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + kern_len: Length of the kernel (should be an odd number). Defaults to 15. + n_sig: Radius of the gaussian kernel. Defaults to 3. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -30,28 +49,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the TI-FGSM attack. - - Note: - Key parameters include `kernel_len` and `n_sig`, which defines the size and - the radius of the gaussian kernel. The default values are set to 15 and 3 - respectively, which are best according to the paper. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - kern_len: Length of the kernel (should be an odd number). Defaults to 15. - n_sig: Radius of the gaussian kernel. Defaults to 3. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/vmifgsm.py b/src/torchattack/vmifgsm.py index 65190b8..9b2e82b 100644 --- a/src/torchattack/vmifgsm.py +++ b/src/torchattack/vmifgsm.py @@ -12,6 +12,25 @@ class VMIFGSM(Attack): From the paper 'Enhancing the Transferability of Adversarial Attacks through Variance Tuning' https://arxiv.org/abs/2103.15571 + + Note: + Key parameters are `n` and `beta`, where `n` is the number of sampled + examples for variance tuning and `beta` is the upper bound of the + neighborhood for varying the perturbation. + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + n: Number of sampled examples for variance tuning. Defaults to 5. + beta: The upper bound of the neighborhood. Defaults to 1.5. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -29,28 +48,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the VMI-FGSM attack. - - Note: - Key parameters are `n` and `beta`, where `n` is the number of sampled - examples for variance tuning and `beta` is the upper bound of the - neighborhood for varying the perturbation. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - n: Number of sampled examples for variance tuning. Defaults to 5. - beta: The upper bound of the neighborhood. Defaults to 1.5. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model diff --git a/src/torchattack/vnifgsm.py b/src/torchattack/vnifgsm.py index d8103a7..9a6e894 100644 --- a/src/torchattack/vnifgsm.py +++ b/src/torchattack/vnifgsm.py @@ -11,6 +11,25 @@ class VNIFGSM(Attack): From the paper 'Enhancing the Transferability of Adversarial Attacks through Variance Tuning' https://arxiv.org/abs/2103.15571 + + Note: + Key parameters are `n` and `beta`, where `n` is the number of sampled + examples for variance tuning and `beta` is the upper bound of the + neighborhood for varying the perturbation. + + Args: + model: The model to attack. + normalize: A transform to normalize images. + device: Device to use for tensors. Defaults to cuda if available. + eps: The maximum perturbation. Defaults to 8/255. + steps: Number of steps. Defaults to 10. + alpha: Step size, `eps / steps` if None. Defaults to None. + decay: Decay factor for the momentum term. Defaults to 1.0. + n: Number of sampled examples for variance tuning. Defaults to 5. + beta: The upper bound of the neighborhood. Defaults to 1.5. + clip_min: Minimum value for clipping. Defaults to 0.0. + clip_max: Maximum value for clipping. Defaults to 1.0. + targeted: Targeted attack if True. Defaults to False. """ def __init__( @@ -28,28 +47,6 @@ def __init__( clip_max: float = 1.0, targeted: bool = False, ) -> None: - """Initialize the VNI-FGSM attack. - - Note: - Key parameters are `n` and `beta`, where `n` is the number of sampled - examples for variance tuning and `beta` is the upper bound of the - neighborhood for varying the perturbation. - - Args: - model: The model to attack. - normalize: A transform to normalize images. - device: Device to use for tensors. Defaults to cuda if available. - eps: The maximum perturbation. Defaults to 8/255. - steps: Number of steps. Defaults to 10. - alpha: Step size, `eps / steps` if None. Defaults to None. - decay: Decay factor for the momentum term. Defaults to 1.0. - n: Number of sampled examples for variance tuning. Defaults to 5. - beta: The upper bound of the neighborhood. Defaults to 1.5. - clip_min: Minimum value for clipping. Defaults to 0.0. - clip_max: Maximum value for clipping. Defaults to 1.0. - targeted: Targeted attack if True. Defaults to False. - """ - super().__init__(normalize, device) self.model = model