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

Speedup adversarial training with a simple perturber #89

Merged
merged 28 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
929ddea
Hide perturbation from module.parameters().
mzweilin Mar 2, 2023
dfbfbcd
Speed up FGSM by using perturbation in NCHW.
mzweilin Mar 2, 2023
b887aa3
Use an entirety threat model.
mzweilin Mar 2, 2023
7c82ab7
Entirety->batched
mzweilin Mar 6, 2023
9b12e50
Make the simpler perturber as the default perturber.
mzweilin Mar 6, 2023
d966d93
Merge branch 'main' into entirety_perturber
mzweilin Mar 7, 2023
1a3d0a2
Add ignored modules
dxoigmn Mar 8, 2023
b562535
cleanup
dxoigmn Mar 8, 2023
c326d10
Use proper model summary statistics
dxoigmn Mar 8, 2023
35a7fe3
Remove named_modules hack
dxoigmn Mar 8, 2023
e88ce89
Add test for hidden params in perturber.
mzweilin Mar 8, 2023
17df480
test_perturber_hidden_params() -> test_adversary_perturber_hidden_par…
mzweilin Mar 8, 2023
3e60f5e
Fix deprecation warning
dxoigmn Mar 8, 2023
3afaa36
Register perturbation as non-persistent buffer
dxoigmn Mar 8, 2023
39577c8
comment
dxoigmn Mar 8, 2023
8232bfa
comment
dxoigmn Mar 8, 2023
5ea7b95
Remove extra perturbation initialization.
mzweilin Mar 8, 2023
7c66c92
Make PGD more efficient.
mzweilin Mar 8, 2023
0ecefb1
Use buffer instead of parameter so it runs on the GPU too
dxoigmn Mar 8, 2023
554d1a3
fix test
dxoigmn Mar 8, 2023
a829a0b
Add test_adversary_perturbation().
mzweilin Mar 8, 2023
aacac72
Initialize perturbation for each attack batch.
mzweilin Mar 8, 2023
c17306f
Implement Perturber as a Callback.
mzweilin Mar 8, 2023
1a0348d
Use Callback interface of Perturber instead.
mzweilin Mar 8, 2023
a349f3a
Remove commented code.
mzweilin Mar 8, 2023
40a7a0b
Delete Perturber.initialize_parameters().
mzweilin Mar 8, 2023
6542c42
Use Callback interface of Perturber instead.
mzweilin Mar 8, 2023
755fc62
Comment.
mzweilin Mar 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mart/attack/perturber/perturber.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __init__(
self.projector = projector
self.optim_params = optim_params

self.perturbation = torch.nn.UninitializedParameter()
# Register perturbation as a non-persistent buffer even though we will optimize it. This is because it is not
# a parameter of the underlying model but a parameter of the adversary.
self.register_buffer("perturbation", torch.nn.UninitializedParameter(), persistent=False)

def projector_wrapper(perturber_module, args):
if isinstance(perturber_module.perturbation, torch.nn.UninitializedParameter):
Expand Down
4 changes: 2 additions & 2 deletions mart/configs/attack/classification_eps1.75_fgsm.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defaults:
- iterative_sgd
- perturber: batch
- perturber: default
- perturber/initializer: constant
- perturber/gradient_modifier: sign
- perturber/projector: linf_additive_range
- objective: misclassification
- gain: cross_entropy
- threat_model: batch_additive
- threat_model: additive

optimizer:
lr: 1.75
Expand Down
4 changes: 4 additions & 0 deletions mart/configs/attack/perturber/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_target_: mart.attack.Perturber
initializer: ???
gradient_modifier: ???
projector: ???
13 changes: 6 additions & 7 deletions mart/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pytorch_lightning import Callback
from pytorch_lightning.loggers import LightningLoggerBase
from pytorch_lightning.utilities import rank_zero_only
from pytorch_lightning.utilities.model_summary import summarize

from mart.utils import pylogger, rich_utils

Expand Down Expand Up @@ -164,13 +165,11 @@ def log_hyperparameters(object_dict: Dict[str, Any]) -> None:
hparams["model"] = cfg["model"]

# save number of model parameters
hparams["model/params/total"] = sum(p.numel() for p in model.parameters())
hparams["model/params/trainable"] = sum(
p.numel() for p in model.parameters() if p.requires_grad
)
hparams["model/params/non_trainable"] = sum(
p.numel() for p in model.parameters() if not p.requires_grad
)
summary = summarize(model)

hparams["model/params/total"] = summary.total_parameters
hparams["model/params/trainable"] = summary.trainable_parameters
hparams["model/params/non_trainable"] = summary.total_parameters - summary.trainable_parameters

hparams["datamodule"] = cfg["datamodule"]
hparams["trainer"] = cfg["trainer"]
Expand Down
25 changes: 25 additions & 0 deletions tests/test_adversary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import mart
from mart.attack import Adversary, NoAdversary
from mart.attack.perturber import Perturber


def test_no_adversary(input_data, target_data):
Expand Down Expand Up @@ -80,3 +81,27 @@ def test_adversary_with_model(input_data, target_data, perturbation):
assert perturber.call_count == 2

torch.testing.assert_close(output_data, input_data + perturbation)


def test_adversary_perturber_hidden_params(input_data, target_data):
initializer = Mock()
perturber = Perturber(initializer)
perturber(input_data, target_data)

threat_model = mart.attack.threat_model.Additive()
optimizer = Mock()
gain = Mock(return_value=torch.tensor(0.0, requires_grad=True))
model = Mock(return_value={})

adversary = Adversary(
threat_model=threat_model, perturber=perturber, optimizer=optimizer, max_iters=1, gain=gain
)
output_data = adversary(input_data, target_data, model=model)

# Adversarial perturbation should not be updated by a regular training optimizer.
params = [p for p in adversary.parameters()]
assert len(params) == 0

# Adversarial perturbation should not be saved to the model checkpoint.
state_dict = adversary.state_dict()
assert "perturber.perturbation" not in state_dict