### Initialize TrojanVision Dataset Classes Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/folder.md Examples showing how to instantiate common image classification datasets like CUB200, GTSRB, and ImageNet within the TrojanVision framework. ```python from trojanvision.datasets import CUB200, CUB200_2011, GTSRB, ImageNet # Initialize CUB200 dataset cub = CUB200(data_format='folder', memory=False) # Initialize CUB200_2011 dataset cub_2011 = CUB200_2011(data_format='folder', memory=False) # Initialize GTSRB dataset with custom normalization gtsrb = GTSRB(norm_par={'mean': [0.3403, 0.3121, 0.3214], 'std': [0.2724, 0.2608, 0.2669]}) # Initialize ImageNet dataset imagenet = ImageNet(norm_par={'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}) ``` -------------------------------- ### Setup Environment with TrojanZoo Source: https://context7.com/ain-soph/trojanzoo/llms.txt Configures global settings for TrojanZoo, including device selection, random seeds, and verbosity levels. It uses argparse to parse arguments and create an environment object. ```python import trojanvision import argparse # Create argument parser and add environment arguments parser = argparse.ArgumentParser() trojanvision.environ.add_argument(parser) # Parse arguments and create environment kwargs = vars(parser.parse_args()) env = trojanvision.environ.create(**kwargs) # Access environment variables print(f"Device: {env['device']}") print(f"Verbose level: {env['verbose']}") print(f"Number of GPUs: {env['num_gpus']}") ``` -------------------------------- ### Generate Heatmaps with TrojanVision Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/models/index.md Demonstrates how to generate Grad-CAM and saliency map heatmaps for a PyTorch model. The example includes loading an image, preprocessing, model inference, and saving the resulting visualizations. ```python import trojanvision from trojanvision.utils import superimpose import torchvision import torchvision.transforms as transforms import PIL.Image as Image import os import wget env = trojanvision.environ.create(device='cpu') model = trojanvision.models.create( 'resnet152', data_shape=[3, 224, 224], official=True, norm_par={'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}) transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.PILToTensor(), transforms.ConvertImageDtype(torch.float)]) url = 'https://i.imgur.com/Bvro0YD.png' if not os.path.isfile('african_elephant.png'): wget.download(url, 'african_elephant.png') img = Image.open('african_elephant.png').convert(mode='RGB') _input = transform(img).unsqueeze(0).to(env['device']) _prob = model.get_prob(_input).squeeze() label = _prob.argmax().item() conf = _prob[label].item() print(f'{label=:} {conf=:.2%}') grad_cam = model.get_heatmap(_input, label)[:, :3] saliency_map = model.get_heatmap(_input, label, method='saliency_map')[:, :3] grad_cam_impose = (grad_cam * 0.4 + _input) saliency_map_impose = (saliency_map * 0.4 + _input) grad_cam_impose = grad_cam_impose.div(grad_cam_impose.max()) saliency_map_impose = saliency_map_impose.div(saliency_map_impose.max()) torchvision.utils.save_image(_input, './center_cropped.png') torchvision.utils.save_image(grad_cam, './grad_cam.png') torchvision.utils.save_image(saliency_map, './saliency_map.png') torchvision.utils.save_image(grad_cam_impose, './grad_cam_impose.png') torchvision.utils.save_image(saliency_map_impose, './saliency_map_impose.png') ``` -------------------------------- ### GET /models/available Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/models.md Retrieves a list of all available model names. ```APIDOC ## GET /models/available ### Description Outputs all available model names registered in the system. ### Method GET ### Endpoint trojanzoo.models.output_available_models ### Parameters #### Query Parameters - **class_dict** (dict) - Optional - Map from model name to model class. - **indent** (int) - Optional - Space indentation for output. ### Response #### Success Response (200) - **output** (str) - Formatted string of available model names. ``` -------------------------------- ### Get Data Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/index.md Static method to process image data, placing input and label on the environment's device and transforming the label. ```APIDOC ## Get Data ### Description This static method processes image data. By default, it places the input and label onto the `env['device']` with `non_blocking=True` and transforms the label to `torch.LongTensor`. ### Method `static get_data(data, **kwargs)` ### Parameters #### Request Body - **data** (tuple[torch.Tensor, torch.Tensor]) - Required - A tuple containing the batched input and label. - **\*\*kwargs** - Optional - Any keyword argument (unused). ### Returns - **tuple[torch.Tensor, torch.Tensor]** - A tuple containing the batched input and label, both placed on `env['device']`. The label is transformed into `torch.LongTensor`. ``` -------------------------------- ### NATS-Bench Model Implementation in Python Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/models/nas.md Integrates the NATS-Bench benchmark, proposed by the University of Technology Sydney. This implementation requires additional installations of 'nats_bench' and the AutoDL-Projects repository. Users must also extract a specific dataset file to a designated path. ```python class trojanvision.models.NATSbench(name='nats_bench', model=_NATSbench, model_index=0, model_seed=777, hp=200, dataset=None, dataset_name=None, nats_path=None, search_space='tss', **kwargs) ``` -------------------------------- ### Summarize PyTorch Model Architecture Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/model.md Prints a string summary of a PyTorch module, similar to TensorFlow's Keras Model.summary(). It allows control over traversal depth and verbosity to display auxiliary information or just layer names. Dependencies include PyTorch and torchvision for model examples. ```python import torchvision from trojanzoo.utils.model import summary model=torchvision.models.resnet18() summary(model) summary(model, depth=1) summary(model, depth=1, verbose=False) summary(model, depth=2) ``` -------------------------------- ### NATSbench Model Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/models/nas.md Implements the NATS-Bench benchmark for Neural Architecture Search, proposed by the University of Technology Sydney. Requires specific installation and data setup. ```APIDOC ## trojanvision.models.NATSbench ### Description NATS-Bench proposed by Xuanyi Dong from University of Technology Sydney. This class provides an interface to the NATS-Bench benchmark. ### Method Constructor ### Endpoint N/A (Class definition) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from trojanvision.models import NATSbench # Example usage (assuming prerequisites are met) nats_bench_model = NATSbench(model_index=0, dataset_name='cifar10') ``` ### Response #### Success Response (200) N/A (Class definition) #### Response Example N/A (Class definition) ### Parameters * **name** (str) – Name of the model. Defaults to 'nats_bench'. * **model** () – The model architecture class. * **model_index** (int) – Index of the model architecture in NATS-Bench. Defaults to 0. * **model_seed** (int) – Seed for the model. Defaults to 777. * **hp** (int) – Hyperparameter setting ID. Defaults to 200. * **dataset** (str) – Name of the dataset to use. If None, `dataset_name` is used. * **dataset_name** (str) – Name of the dataset. If None, uses the default dataset associated with `hp`. * **nats_path** (str) – Path to the extracted NATS-Bench dataset files. * **search_space** (str) – The search space to use. Defaults to 'tss'. * ****kwargs** – Additional keyword arguments. ### NOTE There are prerequisites to use the benchmark: * `pip install nats_bench`. * `git clone https://github.com/D-X-Y/AutoDL-Projects.git` and `pip install .` * Extract `NATS-tss-v1_0-3ffb9-full` to `nats_path`. ### Available model names: ```python {'nats_bench'} ``` ``` -------------------------------- ### initialize Method Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/index.md Allows transformation of datasets across different data formats. ```APIDOC ## initialize(*args, **kwargs) You could use this method to transform across different `data_format`. ``` -------------------------------- ### GET /models/summary Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/models.md Retrieves a formatted summary of the model structure. ```APIDOC ## GET /models/summary ### Description Prints or returns a string summary of the model instance, including depth and verbose configuration. ### Method GET ### Endpoint /models/summary ### Parameters #### Query Parameters - **depth** (int) - Optional - The depth of the summary tree. - **verbose** (bool) - Optional - Whether to include detailed information. ### Response #### Success Response (200) - **summary** (string) - The textual representation of the model architecture. ``` -------------------------------- ### Summarize Configuration Information (Python) Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/configs.md Provides a summary of the configuration settings, allowing users to view specific parts of the configuration, such as the final merged configuration, command-line configurations, or configurations from specific keys. ```python summary(keys=['final'], config=None, indent=0) Summary the config information. Parameters: keys (list[str] | str): Keys of configs to summary. 'final': self.full_config 'cmd': self.cmd_config key in self.config_dict.keys() Defaults to ['final']. indent (int): The space indent of entire string. Defaults to 0. Returns: ConfigType: Merged config. ``` -------------------------------- ### GET /get_class Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/models.md Retrieves the class classification result for a given input. ```APIDOC ## GET /get_class ### Description Get the class classification result of _input using torch.argmax. ### Method GET ### Endpoint /get_class ### Parameters #### Query Parameters - **_input** (torch.Tensor) - Required - The batched input tensor. ### Response #### Success Response (200) - **classes** (torch.Tensor) - The classes tensor with shape (N). ``` -------------------------------- ### environ.create Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/environ.md Loads environment values from configuration and command line. ```APIDOC ## POST /ain-soph/trojanzoo/environ/create ### Description Loads environment values from configuration and command line. ### Method POST ### Endpoint /ain-soph/trojanzoo/environ/create ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **cmd_config_path** (str) - Optional - Path to the command configuration file. - **dataset_name** (str) - Optional - The dataset name. - **dataset** (str | trojanzoo.datasets.Dataset) - Optional - Dataset instance or name. - **seed** (int) - Optional - The random seed. - **data_seed** (int) - Optional - Seed for data processing. - **cudnn_benchmark** (bool) - Optional - Whether to enable cuDNN benchmark. - **config** (Config) - Optional - The default parameter config. - **cache_threshold** (float) - Optional - Threshold for CUDA cache clearing. - **verbose** (int) - Optional - Verbosity level. - **color** (bool) - Optional - Whether to use colorful console output. - **device** (str | device) - Optional - The default device to store tensors. - **tqdm** (bool) - Optional - Whether to use tqdm for progress bars. - **\*\*kwargs** - Optional - Additional keyword arguments for optim_args, train_args, writer_args. ### Request Example ```json { "cmd_config_path": "/path/to/config.yaml", "dataset_name": "cifar10", "seed": 42, "device": "cuda" } ``` ### Response #### Success Response (200) - **env** (Env) - The created environment instance. #### Response Example ```json { "env": "" } ``` ``` -------------------------------- ### POST /defenses/create Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/defenses/index.md Initializes a defense mechanism based on the provided defense name or class configuration. ```APIDOC ## POST /defenses/create ### Description Creates and configures a new defense instance for a specific dataset and attack model. ### Method POST ### Endpoint /defenses/create ### Parameters #### Request Body - **defense_name** (string) - Optional - The name of the defense algorithm to instantiate. - **dataset_name** (string) - Optional - The name of the dataset to apply the defense on. - **config** (object) - Optional - Configuration parameters for the defense. ### Request Example { "defense_name": "Strip", "dataset_name": "CIFAR10" } ### Response #### Success Response (200) - **status** (string) - Success message - **defense_instance** (object) - The initialized defense object #### Response Example { "status": "success", "defense_instance": "" } ``` -------------------------------- ### GET /get_all_layer Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/models.md Retrieves all intermediate layer outputs from a model for a given input. ```APIDOC ## GET /get_all_layer ### Description Get all intermediate layer outputs of _input from any intermediate layer. ### Method GET ### Endpoint /get_all_layer ### Parameters #### Query Parameters - **_input** (torch.Tensor) - Required - The batched input tensor. - **layer_input** (str) - Optional - The intermediate layer name. Defaults to 'input'. - **depth** (int) - Optional - The traverse depth. Defaults to -1. - **prefix** (str) - Optional - Prefix string to all elements. Defaults to ''. - **use_filter** (bool) - Optional - Whether to filter out certain layer types. - **non_leaf** (bool) - Optional - Whether to include non-leaf nodes. - **seq_only** (bool) - Optional - Whether to only traverse children of Sequential modules. - **verbose** (int) - Optional - Output level for logging information. ### Response #### Success Response (200) - **outputs** (dict[str, torch.Tensor]) - The dictionary of all layer outputs. ``` -------------------------------- ### GET /defenses/input_filtering/labels Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/defenses/index.md Retrieves predicted and ground-truth labels for input filtering defense modules. ```APIDOC ## GET /defenses/input_filtering/labels ### Description Retrieves the predicted labels and ground-truth labels for test inputs processed by an input filtering defense. ### Method GET ### Endpoint /defenses/input_filtering/labels ### Parameters #### Query Parameters - **defense_id** (string) - Required - The unique identifier of the active defense instance. ### Request Example GET /defenses/input_filtering/labels?defense_id=123 ### Response #### Success Response (200) - **predicted_labels** (array) - Boolean tensor indicating if inputs are poisoned. - **true_labels** (array) - Ground truth labels for the test set. #### Response Example { "predicted_labels": [false, true, false], "true_labels": [false, true, true] } ``` -------------------------------- ### Initialize TrojanVision Datasets Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/normal.md Demonstrates how to instantiate common image datasets like MNIST and CIFAR10 within the TrojanVision framework. These classes handle normalization parameters and metadata automatically. ```python from trojanvision.datasets import MNIST, CIFAR10, CIFAR100, ImageNet16 # Initialize MNIST dataset mnist_ds = MNIST() # Initialize CIFAR10 dataset cifar10_ds = CIFAR10() # Initialize CIFAR100 dataset cifar100_ds = CIFAR100() # Initialize ImageNet16 dataset imagenet16_ds = ImageNet16() ``` -------------------------------- ### sample Method Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/index.md Samples a subset of the image folder dataset based on specified criteria. ```APIDOC ## sample(child_name=None, class_dict=None, sample_num=None, method='folder') Sample a subset image folder dataset. * **Parameters:** * **child_name** ([`str`]) – Name of child subset. Defaults to `'{self.name}_sample{sample_num}'` * **class_dict** ([`dict`] | `None`) – Map from new class name to list of old class names. If `None`, use `sample_num` to random sample a subset (1 to 1). Defaults to `None`. * **sample_num** ([`int`] | `None`) – The number of subset classes to sample if `class_dict` is `None`. Defaults to `None`. * **method** ([`str`]) – `data_format` of new subset to save. Defaults to `'folder'`. ``` -------------------------------- ### GET /attacks/backdoor Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/attacks/backdoor/index.md Retrieves the list of available backdoor attack implementations categorized by attack type. ```APIDOC ## GET /attacks/backdoor ### Description Returns a structured list of backdoor attack modules available in the TrojanZoo framework, including normal, clean_label, and dynamic attack types. ### Method GET ### Endpoint /attacks/backdoor ### Parameters None ### Request Example GET /attacks/backdoor ### Response #### Success Response (200) - **attacks** (object) - A dictionary containing lists of attack classes categorized by type (normal, clean_label, dynamic). #### Response Example { "normal": ["BadNet", "TrojanNN", "IMC", "LatentBackdoor", "TrojanNet"], "clean_label": ["InvisiblePoison", "Refool"], "dynamic": ["InputAwareDynamic"] } ``` -------------------------------- ### GET /attacks/backdoor/get_neuron_jaccard Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/attacks/index.md Calculates the Jaccard Index of neuron activations between clean and poisoned inputs to measure backdoor impact. ```APIDOC ## GET /attacks/backdoor/get_neuron_jaccard ### Description Computes the Jaccard Index comparing neuron activations for clean inputs versus poisoned inputs, identifying the overlap in top-k activated neurons. ### Method GET ### Endpoint /attacks/backdoor/get_neuron_jaccard ### Parameters #### Query Parameters - **k** (int) - Optional - Number of top neurons to consider. - **ratio** (float) - Optional - Percentage of neurons to use if k is not provided. Defaults to 0.5. ### Request Example { "k": 50, "ratio": 0.5 } ### Response #### Success Response (200) - **jaccard_index** (float) - The calculated Jaccard Index value. #### Response Example { "jaccard_index": 0.85 } ``` -------------------------------- ### Dataset Implementation Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/index.md Details on how to use the dataset, including recommended user-friendly methods and available parameters for data normalization and transformation. ```APIDOC ## Dataset Implementation ### Description This section describes the implementation of the dataset. For user convenience, it is recommended to use the `create()` method instead of directly interacting with this implementation. ### Parameters #### Request Body - **norm_par** (dict) - Optional - Data normalization parameters of 'mean' and 'std' (e.g., `{'mean': [0.5, 0.4, 0.6], 'std': [0.2, 0.3, 0.1]}`). Defaults to `None`. - **normalize** (bool) - Optional - Whether to use `torchvision.transforms.Normalize` in dataset transform. Otherwise, use it as model preprocess layer. Defaults to `False`. - **transform** (str) - Optional - The dataset transform type. Options include `None` or `'none'` (uses `PILToTensor` and `ConvertImageDtype`), `'bit'` (transform used in BiT network), and `'pytorch'` (pytorch transform used in ImageNet training). Defaults to `None`. - **auto_augment** (bool) - Optional - Whether to use `torchvision.transforms.AutoAugment`. Defaults to `False`. - **mixup** (bool) - Optional - Whether to use `trojanvision.utils.transforms.RandomMixup`. Defaults to `False`. - **mixup_alpha** (float) - Optional - `alpha` passed to `trojanvision.utils.transforms.RandomMixup`. Defaults to `0.0`. - **cutmix** (bool) - Optional - Whether to use `trojanvision.utils.transforms.RandomCutmix`. Defaults to `False`. - **cutmix_alpha** (float) - Optional - `alpha` passed to `trojanvision.utils.transforms.RandomCutmix`. Defaults to `0.0`. - **cutout** (bool) - Optional - Whether to use `trojanvision.utils.transforms.Cutout`. Defaults to `False`. - **cutout_length** (int) - Optional - Cutout length. Defaults to `None`. - **\*\*kwargs** - Optional - Keyword arguments passed to `trojanzoo.datasets.Dataset`. ### Variables - **data_type** (str) - Defaults to `'image'`. - **num_classes** (int) - Defaults to `1000`. - **data_shape** (list[int]) - The shape of image data `[C, H, W]`. Defaults to `[3, 224, 224]`. ### See Also - [`create()`](#trojanvision.datasets.create) - [`get_transform()`](#trojanvision.datasets.ImageSet.get_transform) ``` -------------------------------- ### GET /utils/model/get_layer Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/model.md Extracts the output tensor from a specific intermediate layer within a PyTorch module given an input tensor. ```APIDOC ## GET /utils/model/get_layer ### Description Retrieves the output of a specific intermediate layer from a torch.nn.Module. This is useful for feature extraction or debugging model internals. ### Method GET ### Endpoint trojanzoo.utils.model.get_layer ### Parameters #### Path Parameters - **module** (torch.nn.Module) - Required - The PyTorch module to process. - **x** (torch.Tensor) - Required - The batched input tensor. #### Query Parameters - **layer_output** (str) - Optional - The name of the layer to extract output from. Defaults to 'classifier'. - **layer_input** (str) - Optional - The name of the layer that outputs the input tensor x. Defaults to 'input'. - **seq_only** (bool) - Optional - Whether to only traverse children of torch.nn.Sequential. Defaults to True. ### Request Example get_layer(model.features, x, layer_input='denseblock1', layer_output='transition3.conv') ### Response #### Success Response (200) - **output** (torch.Tensor) - The resulting tensor from the specified layer. #### Response Example torch.Size([6, 512, 14, 14]) ``` -------------------------------- ### Execute Backdoor Defense Workflow with TrojanZoo Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/tutorials/basic.md This script demonstrates the standard workflow for initializing a TrojanZoo environment, including dataset, model, trainer, attack, and defense components. It uses argparse to dynamically configure these components and performs a detection task. ```python #!/usr/bin/env python3 import trojanvision import argparse if __name__ == '__main__': parser = argparse.ArgumentParser() trojanvision.environ.add_argument(parser) trojanvision.datasets.add_argument(parser) trojanvision.models.add_argument(parser) trojanvision.trainer.add_argument(parser) trojanvision.marks.add_argument(parser) trojanvision.attacks.add_argument(parser) trojanvision.defenses.add_argument(parser) kwargs = parser.parse_args().__dict__ env = trojanvision.environ.create(**kwargs) dataset = trojanvision.datasets.create(**kwargs) model = trojanvision.models.create(dataset=dataset, **kwargs) trainer = trojanvision.trainer.create(dataset=dataset, model=model, **kwargs) mark = trojanvision.marks.create(dataset=dataset, **kwargs) attack = trojanvision.attacks.create(dataset=dataset, model=model, mark=mark, **kwargs) defense = trojanvision.defenses.create(dataset=dataset, model=model, attack=attack, **kwargs) if env['verbose']: trojanvision.summary(env=env, dataset=dataset, model=model, mark=mark, trainer=trainer, attack=attack, defense=defense) defense.detect(**trainer) ``` -------------------------------- ### POST /attacks/create Source: https://context7.com/ain-soph/trojanzoo/llms.txt Initializes a backdoor attack instance, such as BadNet, to poison a model and evaluate attack success rates. ```APIDOC ## POST /attacks/create ### Description Configures a specific backdoor attack method to be applied to a model during training. ### Method POST ### Endpoint /attacks/create ### Parameters #### Request Body - **attack_name** (string) - Required - Name of the attack (e.g., 'badnet'). - **target_class** (int) - Required - The label the model should predict when the trigger is present. - **poison_percent** (float) - Required - Percentage of the dataset to be poisoned. ### Request Example { "attack_name": "badnet", "target_class": 0, "poison_percent": 0.01 } ### Response #### Success Response (200) - **attack** (object) - The initialized attack instance. #### Response Example { "status": "initialized", "attack_name": "badnet" } ``` -------------------------------- ### Config Class Overview Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/configs.md Provides an overview of the Config class, its initialization, and configuration structure. ```APIDOC ## Config Class Configuration class for TrojanZoo. ### WARNING There is already a preset config instance `trojanzoo.configs.config`. NEVER call the class init method to create a new instance (unless you know what you’re doing). ### NOTE ConfigType is `Module[str, Module[str, Any]]` `value = config[config_file][key][dataset_name]` where `dataset_name` is optional (`config[config_file][key]` is [`trojanzoo.utils.module.Param`](utils/module.md#trojanzoo.utils.module.Param) and has default values). ### Class: trojanzoo.configs.Config(cmd_config_path=None, _base=None, **kwargs) * **Parameters:** * **_base** (Config) – The base config instance. `config_dict` of current config instance will inherit `_base.config_dict` and update based on self.config_path. It’s usually the config in father library (e.g., trojanvision config inherits trojanzoo config). Defaults to `None`. * **kwargs** (dict[str, str]) – Map of config paths. * **Variables:** * **cmd_config_path** (str) – Path to `cmd_config`. Defaults to `None`. * **cmd_config** (ConfigType) – Config loaded from path `cmd_config_path`. * **config_path** (dict[str, str]) – Map from config name (e.g., 'package', 'user', 'project') to path string. * **config_dict** (dict[str, ConfigType]) – Map from config name (e.g., 'package', 'user', 'project') to its config. * **full_config** (ConfigType) – Full config with parameters for all datasets by calling [`merge()`](#trojanzoo.configs.Config.merge) to merge different configs in `self.config_dict`. `value = full_config[config_file][key][dataset_name]`. ``` -------------------------------- ### Reset MetricLogger Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/logger.md Resets the internal meters of the MetricLogger by calling the reset method on each SmoothedValue. This is useful for starting fresh metric collection. ```python def reset(): """Reset meter in `self.meters` by calling [`SmoothedValue.reset()`](#trojanzoo.utils.logger.SmoothedValue.reset). * **Returns:** *MetricLogger* – return `self` for stream usage. """ pass ``` -------------------------------- ### Initialize ImageNet32 and STL10 Datasets Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/datasets/normal.md Demonstrates the class instantiation for ImageNet32 and STL10 datasets within the trojanvision.datasets module. These classes inherit from ImageSet and require specific normalization parameters and configuration. ```python from trojanvision.datasets import ImageNet32, STL10 # Initialize ImageNet32 imagenet_ds = ImageNet32(num_classes=1000) # Initialize STL10 stl10_ds = STL10() ``` -------------------------------- ### Output Information Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/optim.md This method is used to output information during the process. It supports different modes like 'start', 'end', 'middle', and 'memory'. ```APIDOC ## output_info ### Description Outputs information during the process with various modes. ### Parameters #### Query Parameters - **mode** (str) - Optional - The output mode (e.g., 'start', 'end', 'middle', 'memory'). Defaults to 'start'. - **_iter** (int) - Optional - Current iteration. Defaults to 0. - **iteration** (int) - Optional - Total iteration. Defaults to 0. - **output** (Iterable[str]) - Optional - Output items. Defaults to self.output. - **indent** (int) - Optional - The space indent for the entire string. Defaults to self.indent. - ***args** - Unused positional arguments. - **&&kwargs** - Unused keyword arguments. ``` -------------------------------- ### Config Methods Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/configs.md Details the methods available for retrieving, loading, merging, and summarizing configurations. ```APIDOC ## Config Methods ### get_config(dataset_name, config=None, **kwargs) Get config for specific dataset. * **Parameters:** * **dataset_name** (str) – Dataset name. * **config** (ConfigType) – The config for all datasets. `value = full_config[config_file][key][dataset_name]`. Defaults to `self.full_config`. * **Returns:** Param[str, Module[str, Any]] – Config for `dataset_name`. `value = full_config[config_file][key]`. ### static load_config(path) Load yaml or json configs from `path`. * **Parameters:** * **path** (str) – Path to config file. * **Returns:** ConfigType – Config loaded from `path`. ### merge(keys=['package', 'user', 'project']) Merge different configs of `keys` in `self.config_dict`. * **Parameters:** * **keys** (list[str]) – Keys of `self.config_dict` to merge. Defaults to `['package', 'user', 'project']`. * **Returns:** ConfigType – Merged config. ### summary(keys=['final'], config=None, indent=0) Summary the config information. * **Parameters:** * **keys** (list[str] | str) – Keys of configs to summary. `'final'`: `self.full_config`, `'cmd'`: `self.cmd_config`, `key in self.config_dict.keys()`. Defaults to `['final']`. * **indent** (int) – The space indent of entire string. Defaults to `0`. ``` -------------------------------- ### Create Image Model Instance (Python) Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/models/index.md This function creates an instance of an image model. It allows specifying the model by name or instance, and can use default parameters from a config object. Keyword arguments can be passed directly to the model's initialization method. The default folder path for model data is derived from the model and dataset configuration. ```python def create(model_name=None, model=None, dataset_name=None, dataset=None, config=config, class_dict=class_dict, **kwargs): """Create a model instance. For arguments not included in `kwargs`, use the default values in `config`. The default value of `folder_path` is `'{model_dir}/{dataset.data_type}/{dataset.name}'`. For model implementation, see [`ImageModel`](#trojanvision.models.ImageModel). """ pass ``` -------------------------------- ### Dataset Creation and Configuration Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/datasets.md This section describes the parameters for creating and configuring datasets in Trojanzoo. It highlights that the `create()` method is the recommended user-facing API. ```APIDOC ## Dataset Creation and Configuration ### Description This section details the parameters for creating and configuring datasets in Trojanzoo. The `create()` method is the recommended user-facing API for dataset instantiation. ### Parameters #### Parameters for Dataset Initialization - **batch_size** (int) - Optional - Batch size of training set (negative number means batch size for each gpu). Defaults to `100`. - **valid_batch_size** (int) - Optional - Batch size of validation set. Defaults to `100`. - **folder_path** (str) - Optional - Folder path to store dataset. Defaults to `None`. Usually in the format `'{data_dir}/{data_type}/{name}`'. - **download** (bool) - Optional - Download dataset if not exist. Defaults to `False`. - **split_ratio** (float) - Optional - Split training set for training and validation if `valid_set` is `False`. The ratio stands for the proportion of the training subset to the total training set. Defaults to `0.8`. - **num_workers** (int) - Optional - Number of workers used in `get_dataloader()`. Defaults to `4`. - **loss_weights** (bool | np.ndarray | torch.Tensor) - Optional - Loss weights with respect to each class. If `True`, uses `get_loss_weights()`. If `False`, set to `None`. If a numpy array or torch tensor, it's set directly. Defaults to `False`. #### Variables (Attributes of Dataset Instance) - **name** (str) - Dataset Name. (Needs overriding by concrete dataset classes). - **loader** (dict[str, DataLoader]) - Preset dataloader containing 'train' and 'valid' loaders. - **batch_size** (int) - Batch size of training set (always positive). - **valid_batch_size** (int) - Batch size of validation set. - **num_classes** (int) - Number of classes. (Needs overriding). - **folder_path** (str) - Folder path to store dataset. - **data_type** (str) - Data type (e.g., 'image'). (Needs overriding). - **label_names** (list[int]) - Optional - Number of classes. - **valid_set** (bool) - Whether having a native validation set. Defaults to `True`. - **split_ratio** (float) - Split ratio for training/validation sets if `valid_set` is `False`. Defaults to `0.8`. - **loss_weights** (torch.Tensor | None) - Loss weights with respect to each class. - **num_workers** (int) - Number of workers for dataloader. Defaults to `4`. - **collate_fn** (Callable | None) - Custom collate function for the dataloader. Defaults to `None`. ### NOTE For users, please use [`create()`](#trojanzoo.datasets.create) instead, which is more user-friendly. ``` -------------------------------- ### POST /models/create Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/models.md Creates a new instance of a model based on the provided configuration and parameters. ```APIDOC ## POST /models/create ### Description Creates a model instance using provided names or objects and configuration settings. ### Method POST ### Endpoint trojanzoo.models.create ### Parameters #### Request Body - **model_name** (str) - Optional - The model name. - **dataset_name** (str) - Optional - The dataset name. - **config** (Config) - Optional - Default parameter config. - **kwargs** (dict) - Optional - Additional keyword arguments for model initialization. ### Response #### Success Response (200) - **model** (Model) - The initialized model instance. ``` -------------------------------- ### Get Layer Names from PyTorch Module Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/model.md Retrieves a list of layer names from a PyTorch `nn.Module`. It supports controlling traversal depth, adding prefixes, filtering specific layer types (like Dropout, Normalize, etc.), and optionally including non-leaf nodes or only traversing `nn.Sequential` children. The function is useful for inspecting model architecture. ```python import torchvision from trojanzoo.utils.model import get_layer_name model = torchvision.models.resnet18() print(get_layer_name(model, depth=1)) print(get_layer_name(model, depth=2, prefix='model')) print(get_layer_name(model, seq_only=True)) print(get_layer_name(model, seq_only=True, non_leaf=True)) print(get_layer_name(model)) ``` -------------------------------- ### POST /defenses/create Source: https://context7.com/ain-soph/trojanzoo/llms.txt Initializes a defense mechanism, such as Neural Cleanse, to detect or mitigate backdoor triggers in a model. ```APIDOC ## POST /defenses/create ### Description Sets up a defense module to analyze a model for potential backdoors. ### Method POST ### Endpoint /defenses/create ### Parameters #### Request Body - **defense_name** (string) - Required - Name of the defense (e.g., 'neural_cleanse'). - **defense_remask_epoch** (int) - Optional - Number of epochs for remasking. - **cost** (float) - Optional - Cost parameter for optimization. ### Request Example { "defense_name": "neural_cleanse", "defense_remask_epoch": 10 } ### Response #### Success Response (200) - **defense** (object) - The initialized defense instance. #### Response Example { "status": "initialized", "defense_name": "neural_cleanse" } ``` -------------------------------- ### Get Intermediate Layer Outputs from PyTorch Module (Python) Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/model.md This function retrieves all intermediate layer outputs from a given PyTorch `nn.Module`. It traverses the module structure, capturing the tensor output of each layer. Parameters allow for controlling the traversal depth, filtering specific layer types (like Dropout or BatchNorm), and specifying whether to include non-leaf nodes or only sequential layers. Verbose output can be enabled to display layer names and shapes during execution. It's recommended to use `torch.nn.Flatten` instead of in-place flatten operations to ensure compatibility. ```python import torch import torchvision from trojanzoo.utils.model import get_all_layer model = torchvision.models.densenet121() x = torch.randn(5, 3, 224, 224) y = get_all_layer(model.features, x, verbose=True) print(y.keys()) ``` -------------------------------- ### Format String with get_str Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/utils/logger.md Generates a formatted string from keyword arguments, applying length constraints and optional colorization based on environment settings. It's useful for creating concise, readable log messages. ```python def get_str(cut_too_long=True, strip=True, **kwargs): """Generate formatted string based on keyword arguments. `key: value` with max length to be `self.meter_length`. The key string is green when `env['color'] == True`. * **Parameters:** * **cut_too_long** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Whether to cut too long values to first 5 characters. Defaults to `True`. * **strip** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Whether to strip trailing whitespaces. Defaults to `True`. * **&&kwargs** – Keyword arguments to generate string. """ pass ``` -------------------------------- ### Load Configuration from File (Python) Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/configs.md A static method within the `Config` class to load configuration data from a specified file path. It supports both YAML and JSON formats. ```python static load_config(path) Load yaml or json configs from `path`. Parameters: path (str): Path to config file. Returns: ConfigType: Config loaded from `path`. ``` -------------------------------- ### Create Dataset Instance Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/datasets.md Factory function to instantiate a dataset object. It merges provided keyword arguments with default configurations to initialize the dataset. ```python from trojanzoo import datasets dataset = datasets.create(dataset_name="mnist", download=True) ``` -------------------------------- ### Env Class Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanzoo/environ.md Represents the dict-like environment and configuration settings. ```APIDOC ## CLASS trojanzoo.environ.Env ### Description The dict-like environment class that inherits `trojanzoo.utils.module.Param`. It should be a singleton in most cases. ### Parameters - **device** (str | device) - The default device to store tensors. Defaults to 'auto'. - 'auto': use gpu if available - 'cpu': use cpu - 'gpu' | 'cuda': use gpu ### Variables - **color** (bool) - Whether to show colorful outputs in console using ASNI escape characters. Defaults to `False`. - **num_gpus** (int) - Number of available GPUs. - **tqdm** (bool) - Whether to use `tqdm.tqdm` to show progress bar. Defaults to `False`. - **verbose** (int) - The output level. Defaults to `0`. - **cudnn_benchmark** (bool) - Whether to use `torch.backends.cudnn.benchmark` to accelerate without deterministic. Defaults to `False`. - **cache_threshold** (float) - The threshold (MB) to call `torch.cuda.empty_cache`. Defaults to `None` (never). - **seed** (int) - The random seed for numpy, torch and cuda. - **data_seed** (int) - Seed to process data (e.g., `trojanzoo.datasets.Dataset.split_dataset`). - **device** (device) - The default device to store tensors. - **world_size** (int) - Number of distributed machines. Defaults to `1`. ### WARNING There is already an environ instance `trojanzoo.environ.env`. Call `create()` to set its value. NEVER call the class init method to create a new instance (unless you know what you’re doing). ### Methods #### classmethod add_argument(group) Adds environ arguments to argument parser group. View source to see specific arguments. **NOTE**: This is the implementation of adding arguments. For users, please use `add_argument()` instead, which is more user-friendly. ``` -------------------------------- ### TrojanZoo Training Process Source: https://github.com/ain-soph/trojanzoo/blob/main/docs/source/trojanvision/attacks/backdoor/dynamic.md This section details the training process for the mask generator and the combined training of the mark generator and the main model. It outlines the optimization steps and loss functions used. ```APIDOC ## TrojanZoo Training Process ### Description This outlines the two-stage training process in TrojanZoo: first training the mask generator, then jointly training the mark generator and the target model. ### Method This describes a training algorithm, not a specific API endpoint. ### Endpoint N/A ### Parameters #### Training Parameters - **train_mask_epochs** (int) - Optional - Epochs to optimize mask generator. Defaults to `25`. - **epochs** (int) - Optional - Total epochs for training the mark generator and model. - **lambda_div** (float) - Optional - Weight of diversity loss during optimization. Defaults to `1.0`. - **lambda_norm** (float) - Optional - Weight of norm loss when optimizing the mask generator. Defaults to `100.0`. - **mask_density** (float) - Optional - Threshold of mask values for norm loss. Defaults to `0.032`. - **cross_percent** (float) - Optional - Percentage of cross inputs in the training set. Defaults to `0.1`. - **poison_percent** (float) - Optional - Percentage of poison inputs in the training set. ### Request Example N/A ### Response N/A ```