IntelLabs / MART

Modular Adversarial Robustness Toolkit
BSD 3-Clause "New" or "Revised" License
16 stars 0 forks source link

Add Adversary.batch_c15n #215

Closed mzweilin closed 10 months ago

mzweilin commented 11 months ago

What does this PR do?

Adversary assumes batches look like (input, target), but target models may work on very different forms of batches.

This PR adds a configurable Adversary.batch_c15n() that converts raw batches into the canonical form (input, target), so Adversary can extract input and target. Adversary.batch_c15n.revert() would convert batches back into the original form before feeding the batch to the target model.

The batch_c15n not only converts list/tuple/dict input into the canonical tuple and vice versa, but also supports transform/untransform on input/target/batch. The flexibility allows us to reuse the same Adversary for attacking very different models in external projects. For example, we can denormalize input in attacking Anomalib models.

Type of change

Please check all relevant options.

Testing

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

Before submitting

Did you have fun?

Make sure you had fun coding 🙃

mzweilin commented 10 months ago

You'll probably want to rename this batch_canonicalization or batch_c18n. Also, since you control what you feed as the train_dataloader to Adversary, why not canonicalize on batch being a dict instead of a tuple? That feels infinitely more flexible. Will you also get rid of NormalizedAdversaryAdapter?

Thanks for the suggestion. I have renamed it to batch_c15n for simplicity.

I think it's good to keep the tuple (input, target) as the canonical batch form, because the two are required by many sub-components in Adversary, such as Enforcer. Making it a dictionary may create an illusion that the two parameters are not required.

I would like to keep NormalizedAdversaryAdapter for comparison with other adversary implementations, but I am going to update it in a separate PR to match the revised interface in Adversary.