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

random_perturb(x)[source]

Given a starting input, take a random step within the feasible set

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.

to_image(x)[source]

Given an input (which may be in an alternative parameterization), convert it to a valid image (this is implemented as the identity function by default as most of the time we use the pixel parameterization, but for alternative parameterizations this functino must be overriden).

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}

project(x)[source]
random_perturb(x)[source]
random_uniform(x)[source]
step(x, g)[source]
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}

project(x)[source]
random_perturb(x)[source]
step(x, g)[source]
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

__init__(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]
batch_attack(input_image, label, target_label)[source]
config(**kwargs)[source]
ares.utils.adv.adv_generator(args, images, target, model, eps, attack_steps, attack_lr, random_start, attack_criterion='regular', use_best=True)[source]
ares.utils.adv.clamp(X, lower_limit, upper_limit)[source]
ares.utils.adv.replace_best(loss, bloss, x, bx)[source]
ares.utils.adv.replace_best_reverse(loss, bloss, x, bx)[source]

ares.utils.dataset module

ares.utils.dataset.build_dataset(args, num_aug_splits=0)[source]

The function to build dataset for robust training.

ares.utils.dist module

ares.utils.dist.distributed_init(args)[source]

This function performs the distributed setting.

ares.utils.dist.random_seed(seed=0, rank=0)[source]
ares.utils.dist.setup_for_distributed(is_master)[source]

This function disables printing when not in master process

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.

format(record)[source]

Directly output message without formattion when got ‘simple’ attribute.

class ares.utils.logger.PrintFormatter[source]

Bases: object

This class is used to overwrite the sys.stdout using our custom logger.

__init__()[source]
flush()[source]
write(message)[source]
class ares.utils.logger.format_print[source]

Bases: _DecoratorContextManager

This class is used as a decrator to format output of print func using our custom logger.

ares.utils.logger.setup_logger(save_dir=None, distributed_rank=0, main_only=True)[source]

Setup custom logger to record information.

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.loss.margin_loss(outputs, labels, target_labels, targeted, device)[source]

Define the margin loss.

ares.utils.loss.resolve_amp(args, _logger)[source]

The function to resolve amp parameters for robust training.

ares.utils.metrics module

Eval metrics and related

Hacked together by / Copyright 2020 Ross Wightman

class ares.utils.metrics.AverageMeter[source]

Bases: object

Computes and stores the average and current value

__init__()[source]
reset()[source]
update(val, n=1)[source]
ares.utils.metrics.accuracy(output, target, topk=(1,))[source]

Computes the accuracy over the k top predictions for the specified values of k

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.model.load_pretrained_21k(args, model, logger)[source]

The function to load pretrained 21K checkpoint to 1K model.

ares.utils.model.normalize_fn(tensor, mean, std)[source]

Differentiable version of torchvision.functional.normalize

ares.utils.registry module

class ares.utils.registry.Registry[source]

Bases: object

The class for registry of modules.

classmethod get_attack(name)[source]

Get a attack method by given name.

classmethod get_lr_scheduler(name)[source]

Get a lr scheduler object by given name.

classmethod get_model(name)[source]

Get a model object by given name.

classmethod get_path(name)[source]

Get a path by given name.

classmethod get_transform(name)[source]

Get a transform object by given name.

classmethod list_attacks()[source]

List all attack methods registered.

classmethod list_lr_schedulers()[source]

List all lr schedulers registered.

classmethod list_models()[source]

List all model classes registered.

classmethod list_transforms()[source]

List all transforms registered.

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.

classmethod register_path(name, path)[source]

Register a path to registry with key ‘name’

Parameters:

name (str) – Key with which the path will be registered.

classmethod register_transform(name=None, force=False)[source]

Register a transform 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.