ares.utils package¶
ares.utils.adv module¶
- class ares.utils.adv.AttackerStep(orig_input, eps, step_size, use_grad=True)[source]¶
Bases:
object
Generic class for attacker steps, under perturbation constraints specified by an “origin input” and a perturbation magnitude. Must implement project, step, and random_perturb
- __init__(orig_input, eps, step_size, use_grad=True)[source]¶
Initialize the attacker step with a given perturbation magnitude.
- Parameters:
eps (float) – the perturbation magnitude
orig_input (torch.Tensor) – the original input
- project(x)[source]¶
Given an input x, project it back into the feasible set
- Parameters:
x (torch.Tensor) – the input to project back into the feasible set.
- Returns:
A torch.Tensor that is the input projected back into the feasible set, that is, .. math:: min_{x’ in S} |x’ - x|_2
- step(x, g)[source]¶
Given a gradient, make the appropriate step according to the perturbation constraint (e.g. dual norm maximization for \(\ell_p\) norms).
- Parameters:
(torch.Tensor) (g) – The input to project back into the feasible set.
(torch.Tensor) – The raw gradient
- Returns:
The new input, a torch.Tensor for the next step.
- class ares.utils.adv.LinfStep(orig_input, eps, step_size, use_grad=True)[source]¶
Bases:
AttackerStep
Attack step for \(\ell_\infty\) threat model. Given \(x_0\) and \(\epsilon\), the constraint set is given by: .. math:: S = {x | |x - x_0|_infty leq epsilon}
- class ares.utils.adv.LinfStepEps(orig_input, eps, step_size, use_grad=True)[source]¶
Bases:
AttackerStep
Attack step for \(\ell_\infty\) threat model. Given \(x_0\) and \(\epsilon\), the constraint set is given by: .. math:: S = {x | |x - x_0|_infty leq epsilon}
- class ares.utils.adv.PgdAttackEps(model, batch_size, goal, distance_metric, magnitude=0.01568627450980392, alpha=0.00392156862745098, iteration=100, num_restart=1, random_start=True, use_best=False, device=None, _logger=None)[source]¶
Bases:
object
ares.utils.dataset module¶
ares.utils.dist module¶
ares.utils.logger module¶
- class ares.utils.logger.CustomFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
Formatter
Class for custom formatter.
- class ares.utils.logger.PrintFormatter[source]¶
Bases:
object
This class is used to overwrite the sys.stdout using our custom logger.
ares.utils.loss module¶
- ares.utils.loss.build_loss(args, mixup_fn, num_aug_splits)[source]¶
The function to build loss function for robust training.
- ares.utils.loss.build_loss_scaler(args, _logger)[source]¶
The function to build loss scaler for robust training.
- ares.utils.loss.loss_adv(loss_name, outputs, labels, target_labels, target, device)[source]¶
The function to create loss function.
ares.utils.metrics module¶
Eval metrics and related
Hacked together by / Copyright 2020 Ross Wightman
ares.utils.model module¶
- class ares.utils.model.NormalizeByChannelMeanStd(mean, std)[source]¶
Bases:
Module
The class of a normalization layer.
- __init__(mean, std)[source]¶
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- extra_repr()[source]¶
Set the extra representation of the module
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- forward(tensor)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class ares.utils.model.SwitchableBatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)[source]¶
Bases:
BatchNorm2d
The class of a batch norm layer.
- __init__(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)[source]¶
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(input: Tensor)[source]¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- ares.utils.model.build_model(args, _logger, num_aug_splits)[source]¶
The function to build model for robust training.
- ares.utils.model.convert_switchablebn_model(module)[source]¶
Recursively traverse module and its children to replace all instances of
torch.nn.modules.batchnorm._BatchNorm
with SplitBatchnorm2d.- Parameters:
module (torch.nn.Module) – input module
num_splits (int) – number of separate batchnorm layers to split input across
- Example::
>>> # model is an instance of torch.nn.Module >>> model = timm.models.convert_splitbn_model(model, num_splits=2)
- ares.utils.model.denormalize(tensor, mean, std)[source]¶
- Parameters:
tensor (torch.Tensor) – Float tensor image of size (B, C, H, W) to be denormalized.
mean (torch.Tensor) – float tensor means of size (C, ) for each channel.
std (torch.Tensor) – float tensor standard deviations of size (C, ) for each channel.
ares.utils.registry module¶
- class ares.utils.registry.Registry[source]¶
Bases:
object
The class for registry of modules.
- mapping = {'attacks': {'autoattack': <class 'ares.attack.autoattack.autoattack.AutoAttack'>, 'bim': <class 'ares.attack.bim.BIM'>, 'boundary': <class 'ares.attack.boundary.BoundaryAttack'>, 'cda': <class 'ares.attack.cda.CDA'>, 'cw': <class 'ares.attack.cw.CW'>, 'deepfool': <class 'ares.attack.deepfool.DeepFool'>, 'dim': <class 'ares.attack.di_fgsm.DI2FGSM'>, 'evolutionary': <class 'ares.attack.evolutionary.Evolutionary'>, 'fgsm': <class 'ares.attack.fgsm.FGSM'>, 'mim': <class 'ares.attack.mim.MIM'>, 'nattack': <class 'ares.attack.nattack.Nattack'>, 'nes': <class 'ares.attack.nes.NES'>, 'pgd': <class 'ares.attack.pgd.PGD'>, 'sgm': <class 'ares.attack.sgm.SGM'>, 'si_ni_fgsm': <class 'ares.attack.si_ni_fgsm.SI_NI_FGSM'>, 'spsa': <class 'ares.attack.spsa.SPSA'>, 'tim': <class 'ares.attack.tim.TIFGSM'>, 'tta': <class 'ares.attack.tta.TTA'>, 'vmi_fgsm': <class 'ares.attack.vmi_fgsm.VMI_fgsm'>}, 'lr_schedulers': {'ALRS': <class 'ares.attack.detection.custom.lr_scheduler.ALRS'>, 'CosineLR': <class 'ares.attack.detection.custom.lr_scheduler.CosineLR'>, 'ExponentialLR': <class 'ares.attack.detection.custom.lr_scheduler.ExponentialLR'>, 'MultiStepLR': <class 'ares.attack.detection.custom.lr_scheduler.MultiStepLR'>, 'PlateauLR': <class 'ares.attack.detection.custom.lr_scheduler.PlateauLR'>, 'warmupALRS': <class 'ares.attack.detection.custom.lr_scheduler.warmupALRS'>}, 'models': {'CifarCLS': <class 'ares.model.cifar10_cls.CifarCLS'>, 'ImageNetCLS': <class 'ares.model.imagenet_cls.ImageNetCLS'>, 'bit_depth_reduction': <class 'ares.defense.bit_depth_reduction.BitDepthReduction'>, 'jpeg_compression': <class 'ares.defense.jpeg_compression.Jpeg_compression'>, 'randomization': <class 'ares.defense.randomization.Randomization'>}, 'paths': {'cache_dir': 'C:\\Users\\liuch\\Documents\\projects\\ares2.0\\cache', 'root_dir': 'C:\\Users\\liuch\\Documents\\projects\\ares2.0'}, 'transforms': {'CutOut': <class 'ares.attack.detection.patch.patch_transform.CutOut'>, 'MedianPool2d': <class 'ares.attack.detection.patch.patch_transform.MedianPool2d'>, 'RandomHorizontalFlip': <class 'ares.attack.detection.patch.patch_transform.RandomHorizontalFlip'>, 'RandomJitter': <class 'ares.attack.detection.patch.patch_transform.RandomJitter'>, 'ScalePatchesToBoxes': <class 'ares.attack.detection.patch.patch_transform.ScalePatchesToBoxes'>}}¶
- classmethod register_attack(name=None, force=False)[source]¶
Register an attack method to registry with key ‘name’
- Parameters:
name (str) – Key with which the attacker will be registered.
force (bool) – Whether to register when the name has already existed in registry.
- classmethod register_lr_scheduler(name=None, force=False)[source]¶
Register a learning rate scheduler to registry with key ‘name’
- Parameters:
name (str) – Key with which the attacker will be registered.
force (bool) – Whether to register when the name has already existed in registry.
- classmethod register_model(name=None, force=False)[source]¶
Register a model to registry with key ‘name’
- Parameters:
name (str) – Key with which the attacker will be registered.
force (bool) – Whether to register when the name has already existed in registry.