attacks¶
- trojanzoo.attacks.add_argument(parser, attack_name=None, attack=None, class_dict={})[source]¶
- Add attack arguments to argument parser.For specific arguments implementation, see
Attack.add_argument()
.- Parameters:
- Returns:
argparse._ArgumentGroup – The argument group.
- trojanzoo.attacks.create(attack_name=None, attack=None, dataset_name=None, dataset=None, model_name=None, model=None, config=config, class_dict={}, **kwargs)[source]¶
- Create an attack instance.For arguments not included in
kwargs
, use the default values inconfig
.The default value offolder_path
is'{attack_dir}/{dataset.data_type}/{dataset.name}/{model.name}/{attack.name}'
.For attack implementation, seeAttack
.- Parameters:
attack_name (str) – The attack name.
attack (str | Attack) – The attack instance or attack name (as the alias of attack_name).
dataset_name (str) – The dataset name.
dataset (str | Dataset) – Dataset instance or dataset name (as the alias of dataset_name).
model_name (str) – The model name.
model (str | Model) – The model instance or model name (as the alias of model_name).
config (Config) – The default parameter config.
class_dict (dict[str, type[Attack]]) – Map from attack name to attack class. Defaults to
{}
.**kwargs – The keyword arguments passed to attack init method.
- Returns:
Attack – The attack instance.
- class trojanzoo.attacks.Attack(dataset=None, model=None, folder_path=None, **kwargs)[source]¶
- An abstract class representing an attack.It inherits
trojanzoo.utils.module.ModelProcess
.Note
This is the implementation of attack. For users, please use
create()
instead, which is more user-friendly.- classmethod add_argument(group)[source]¶
Add attack arguments to argument parser group. View source to see specific arguments.
Note
This is the implementation of adding arguments. The concrete attack class may override this method to add more arguments. For users, please use
add_argument()
instead, which is more user-friendly.
- generate_target(_input, idx=1, same=False, **kwargs)[source]¶
Generate target labels of a batched input based on the classification confidence ranking index.
- Parameters:
_input (torch.Tensor) – The input tensor.
idx (int) – The classification confidence rank of target class. Defaults to
1
.same (bool) – Generate the same label for all samples using mod. Defaults to
False
.
- Returns:
torch.Tensor – The generated target label with shape
(N)
.
See also
This method calls
trojanzoo.models.Model.generate_target()
.The implementation is in
trojanzoo.utils.model.generate_target()
.