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

Fix adversarial image visualizer with canonical batches #227

Merged
merged 3 commits into from
Sep 28, 2023

Conversation

mzweilin
Copy link
Contributor

@mzweilin mzweilin commented Sep 25, 2023

What does this PR do?

This PR fixes the adversarial image visualizer after canonicalizing adversarial batches.

  • Fetch canonical input and target in the adversarial image visualizer.
  • Use Adversary.forward() to get adversarial examples.
  • Fix test.

Type of change

Please check all relevant options.

  • Improvement (non-breaking)
  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing

Please describe the tests that you ran to verify your changes. Consider listing any relevant details of your test configuration.

  • pytest
  • CUDA_VISIBLE_DEVICES=0 python -m mart experiment=CIFAR10_CNN_Adv trainer=gpu trainer.precision=16 reports 70% (21 sec/epoch).
  • CUDA_VISIBLE_DEVICES=0,1 python -m mart experiment=CIFAR10_CNN_Adv trainer=ddp trainer.precision=16 trainer.devices=2 model.optimizer.lr=0.2 trainer.max_steps=2925 datamodule.ims_per_batch=256 datamodule.world_size=2 reports 70% (14 sec/epoch).

Before submitting

  • The title is self-explanatory and the description concisely explains the PR
  • My PR does only one thing, instead of bundling different changes together
  • I list all the breaking changes introduced by this pull request
  • I have commented my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have run pre-commit hooks with pre-commit run -a command without errors

Did you have fun?

Make sure you had fun coding 🙃

@mzweilin mzweilin requested a review from dxoigmn September 25, 2023 20:34
@mzweilin mzweilin changed the title Fetch canonical input and target in adversarial image visualizer Fix adversarial image visualizer Sep 25, 2023
@mzweilin mzweilin changed the title Fix adversarial image visualizer Fix adversarial image visualizer with canonical batches Sep 25, 2023
Copy link
Contributor

@dxoigmn dxoigmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this visualizer goes away one day.

self.target = batch["target"]
# Save canonical input and target for on_train_end
self.input = batch[0]
self.target = batch[1]

def on_train_end(self, trainer, model):
# FIXME: We should really just save this to outputs instead of recomputing adv_input
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully we do this one day...

Comment on lines -33 to +36
visualizer.on_train_batch_end(trainer, model, outputs, batch, 0)
visualizer.on_train_end(trainer, model)
visualizer.on_train_batch_end(trainer, adversary, outputs, batch, 0)
visualizer.on_train_end(trainer, adversary)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean this no longer works with universal perturbations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this writes to disk (which is a bad idea). I wish this wrote to tensorboard. I swear there was a PR that did that...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will revisit the visualizer later when I have a chance.

@mzweilin mzweilin merged commit 8b70e88 into main Sep 28, 2023
@mzweilin mzweilin deleted the fix_adv_img_visualizer branch September 28, 2023 16:22
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

Successfully merging this pull request may close these issues.

2 participants