Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem about saving the intermediate results and config problem #185

Closed
andyclsr opened this issue Apr 1, 2023 · 6 comments
Closed

problem about saving the intermediate results and config problem #185

andyclsr opened this issue Apr 1, 2023 · 6 comments

Comments

@andyclsr
Copy link

andyclsr commented Apr 1, 2023

hi, your work is excellent but i encounter some problems.
first , i read similar problems #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
Copy link
Author

andyclsr commented Apr 1, 2023

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

@ain-soph
Copy link
Owner

ain-soph commented Apr 1, 2023

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)

def attack(self, epochs: int, **kwargs):
kwargs['validate_fn'] = kwargs.get('validate_fn', self.validate_fn)
kwargs['save_fn'] = kwargs.get('save_fn', self.save)

def save(self, filename: str = None, **kwargs):
r"""Save attack results to files."""
filename = filename or self.get_filename(**kwargs)
file_path = os.path.join(self.folder_path, filename)
np.save(file_path + '.npy', self.mark.mark.detach().cpu().numpy())
F.to_pil_image(self.mark.mark).save(file_path + '.png')
self.model.save(file_path + '.pth')
print('attack results saved at: ', file_path)

The save_fn is passed to model._train method and used in

if save:
save_fn(file_path=file_path, folder_path=folder_path,
suffix=suffix, verbose=verbose)


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
Copy link
Owner

ain-soph commented Apr 2, 2023

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

if validate_interval != 0 and (_epoch % validate_interval == 0 or _epoch == epochs):

group.add_argument('--validate_interval', type=int,

@andyclsr
Copy link
Author

andyclsr commented Apr 2, 2023

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

if validate_interval != 0 and (_epoch % validate_interval == 0 or _epoch == epochs):

group.add_argument('--validate_interval', type=int,

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

@ain-soph
Copy link
Owner

ain-soph commented Apr 2, 2023

@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

def attack(self, epochs: int, **kwargs):
kwargs['validate_fn'] = kwargs.get('validate_fn', self.validate_fn)
kwargs['save_fn'] = kwargs.get('save_fn', self.save)

@andyclsr
Copy link
Author

andyclsr commented Apr 2, 2023

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

@andyclsr andyclsr closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants