The standard TrojanZoo workflow for performing backdoor defense involves a sequential creation of components using the trojanvision package. The process follows this lifecycle:
- Environment Setup: Initialize the environment with configuration parameters.
- Dataset Creation: Load or prepare the dataset.
- Model Creation: Initialize the model (optionally using a pretrained model).
- Trainer Creation: Set up the trainer for model training or fine-tuning.
- Mark Creation: Define the trigger or 'mark' used for the attack.
- Attack Creation: Configure the attack using the dataset, model, and mark.
- Defense Creation: Configure the defense mechanism using the dataset, model, and attack.
- Execution: Run the defense detection using the trainer instance.
All creation methods (create) accept keyword arguments (**kwargs) parsed from the command line to ensure consistent configuration across all components.
import trojanvision
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Register arguments for all components
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__
# Sequential component creation
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)
# Execute defense
if env['verbose']:
trojanvision.summary(env=env, dataset=dataset, model=model, mark=mark, trainer=trainer, attack=attack, defense=defense)
defense.detect(**trainer)