ain-soph / trojanzoo

TrojanZoo provides a universal pytorch platform to conduct security researches (especially backdoor attacks/defenses) of image classification in deep learning.
https://ain-soph.github.io/trojanzoo
GNU General Public License v3.0
279 stars 63 forks source link

problem about saving the intermediate results and config problem #185

Closed andyclsr closed 1 year ago

andyclsr commented 1 year ago

hi, your work is excellent but i encounter some problems. first , i read similar problems https://github.com/ain-soph/trojanzoo/issues/153 and your https://ain-soph.github.io/trojanzoo/trojanvision/configs.html ,but constructions about configs are hard to understand and the example you provide is limited like this : https://github.com/ain-soph/trojanzoo/blob/main/trojanzoo/configs/dataset.yml.

my problem is i don't know how to store intermediate results and i think it is my config problem i do something like this : and for example attack.yml is only " attack_dir: /data/attack/" and my file structure: image my main.py : image i still wonder about the use of config file and it seem like nothing was stored. i hope you could give a concrete example about the use of it. thx anyway

andyclsr commented 1 year ago

can concretely tell me how to store the attacked model after this step: image

ain-soph commented 1 year ago

attack_dir sets the folder_path of Attack instance, which is attack.folder_path. This property is used in many places including saving method (see following) https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanvision/attacks/abstract.py#L110-L112

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanvision/attacks/abstract.py#L185-L192

The save_fn is passed to model._train method and used in https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanzoo/utils/train.py#L201-L203


This structure is to save model during training intervals. If you simply want to save model in your main script after training, you can always use model._model, which is a standard torch.nn.Module. You can use torch.save(model._model.state_dict(), save_path)

ain-soph commented 1 year ago

To control the interval to save models, use validate_interval parameter (command-line argument as well)

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanzoo/utils/train.py#L180

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanzoo/trainer.py#L123

andyclsr commented 1 year ago

To control the interval to save models, use validate_interval parameter (command-line argument as well)

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanzoo/utils/train.py#L180

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanzoo/trainer.py#L123

what if i want to save multiple models? not just updating one model.

ain-soph commented 1 year ago

@andyclsr You have 2 options:

  1. pass your own save_fn to model._train in attack.attack
  2. overload save_fn in your own Attack class

https://github.com/ain-soph/trojanzoo/blob/1f417cc0da0c4ad0483a6f050f18930d0813f9e3/trojanvision/attacks/abstract.py#L110-L112

andyclsr commented 1 year ago

ok I know it. really thank you for your generous explanations! wish you a good day!