-
Notifications
You must be signed in to change notification settings - Fork 0
/
bannd.py
282 lines (255 loc) · 9.02 KB
/
bannd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import argparse
import random
from typing import Literal, Optional
import numpy as np
import torch
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import settings
from aggregate_gradients import *
from backdoor import *
from learning import *
from nets import *
from utils import *
torch.manual_seed(settings.SEED)
random.seed(settings.SEED)
np.random.seed(settings.SEED)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
def get_data_loaders(
dataset: datasets.VisionDataset,
inplace_or_merge: str = settings.DEFAULT_INPLACE_OR_MERGE,
batch_size: int = settings.DEFAULT_BATCH_SIZE,
poison_rate: float = settings.DEFAULT_POISON_RATE,
):
# Load the dataset
transform = transforms.Compose([transforms.ToTensor()])
train_data = dataset(root="data", train=True, download=True, transform=transform)
test_data = dataset(root="data", train=False, download=True, transform=transform)
train_loader_clean = DataLoader(train_data, batch_size=batch_size, shuffle=True)
test_loader_clean = DataLoader(test_data, batch_size=batch_size, shuffle=True)
# add a small p% of poisoned samples to the train data
train_loader_poisoned = DataLoader(
PoisonedDataset(
train_data,
poison_rate,
"all_to_target",
target_class=settings.TARGET_CLASS,
inplace_or_merge=inplace_or_merge,
),
batch_size=batch_size,
shuffle=True,
)
# poison all samples to test the attacks success rate
test_loader_poisoned = DataLoader(
PoisonedDataset(
test_data,
1.0,
"all_to_target",
target_class=settings.TARGET_CLASS,
inplace_or_merge="inplace",
),
batch_size=batch_size,
shuffle=True,
)
return (
train_loader_clean,
train_loader_poisoned,
test_loader_clean,
test_loader_poisoned,
)
def bannd(
runtype: Literal["baseline", "attack", "defense"],
dataset: Literal["MNIST", "CIFAR10"] = "MNIST",
# inplace_or_merge=settings.DEFAULT_INPLACE_OR_MERGE,
# batch_size=settings.DEFAULT_BATCH_SIZE,
poison_rate: float = settings.DEFAULT_POISON_RATE,
# save_name=None,
# epochs=settings.DEFAULT_TRAINING_EPOCHS,
# calc_every_n_iter=10,
# calc_stats_on="test",
# similarity="cosine",
hard_threshold: Optional[float] = None,
quantile_threshold: Optional[float] = None,
plots_dir: str = "./plots/",
):
if runtype == "defense":
assert (hard_threshold is None and quantile_threshold is not None) or (
hard_threshold is not None and quantile_threshold is None
), "pass either --hard-threshold or --quantile-threshold, not both!"
run_title = "_".join(
[
runtype,
dataset,
*([f"p{poison_rate}"] if runtype in ["attack", "defense"] else []),
*(
[
f"ht{hard_threshold}"
if hard_threshold is not None
else f"qt{quantile_threshold}"
]
if runtype == "defense"
else []
),
# f"p{poison_rate}-{inplace_or_merge}",
# f"e{epochs}",
# f"b{batch_size}",
# f"s-{similarity}",
# f"every-{calc_every_n_iter}-on-{calc_stats_on}",
]
)
print(bold(f"title: {run_title}"))
inplace_or_merge = settings.DEFAULT_INPLACE_OR_MERGE
batch_size = settings.DEFAULT_BATCH_SIZE
epochs = settings.DEFAULT_TRAINING_EPOCHS
calc_every_n_iter = 10
calc_stats_on = "test"
similarity = "cosine"
print(
bold(
f"using default configurations (not part of title, for brevity): p-{inplace_or_merge}_e{epochs}_b{batch_size}_s-{similarity}_every-{calc_every_n_iter}-on-{calc_stats_on}"
)
)
if dataset == "MNIST":
dataset = datasets.MNIST
elif dataset == "CIFAR10":
dataset = datasets.CIFAR10
else:
raise NotImplementedError()
(
train_loader_clean,
train_loader_poisoned,
test_loader_clean,
test_loader_poisoned,
) = get_data_loaders(dataset, inplace_or_merge, batch_size, poison_rate)
# Initialize the network
model = SimpleConvNet()
if runtype == "baseline":
train_loader = train_loader_clean
print("training model on clean dataset, establishing model's baseline")
assert (
calc_stats_on == "test"
), "baseline stats can only be calculated on train dataset; run again with `--calc-stats-on test`"
elif runtype == "attack":
train_loader = train_loader_poisoned
print("training model on poisoned dataset, establishing attack's baseline")
elif runtype == "defense":
train_loader = train_loader_poisoned
print(
"training model on poisoned dataset and defending against it, establishing defense's success"
)
train(
device=device,
model=model,
epochs=epochs,
defend=runtype == "defense",
similarity=similarity,
hard_threshold=hard_threshold,
quantile_threshold=quantile_threshold,
#
train_loader=train_loader,
test_loader_clean=test_loader_clean,
test_loader_poisoned=test_loader_poisoned,
#
should_save_model=True,
model_file_name=f"cnn_{run_title}",
#
should_save_stats=True,
stats_file_name=f"stats_{run_title}",
plots_dir=plots_dir,
calc_stats_every_nth_iter=calc_every_n_iter,
calc_stats_on_train_or_test=calc_stats_on,
)
def main():
parser = argparse.ArgumentParser(description="Train, attack, and defend a CNN.")
parser.add_argument(
"--runtype", choices=["baseline", "attack", "defense"], help="Type of run"
)
parser.add_argument(
"--dataset",
choices=["MNIST", "CIFAR10"],
default="MNIST",
help="Dataset to use (default: %(default)s)",
)
# parser.add_argument(
# "--inplace_or_merge",
# choices=["inplace", "merge"],
# default=settings.DEFAULT_INPLACE_OR_MERGE,
# help="Inplace or merge operation (default: %(default)s)",
# )
# parser.add_argument(
# "--batch_size",
# type=int,
# default=settings.DEFAULT_BATCH_SIZE,
# help="Batch size for training (default: %(default)d)",
# )
parser.add_argument(
"--poison_rate",
type=float,
default=settings.DEFAULT_POISON_RATE,
help="Rate of poisoned samples in the dataset (default: %(default)f)",
)
# parser.add_argument(
# "--save_name",
# type=str,
# default=None,
# help="Save name for statistics (default: stats_{run_title}_accuracy_and_attack_success_rate)",
# )
# parser.add_argument(
# "--epochs",
# type=int,
# default=settings.DEFAULT_TRAINING_EPOCHS,
# help="Number of epochs to run training (default: %(default)d)",
# )
# parser.add_argument(
# "--calc_every_n_iter",
# type=int,
# default=10,
# help="Save stats every given number of batches (default: %(default)d)",
# )
# parser.add_argument(
# "--calc-stats-on",
# choices=["test", "train"],
# default="test",
# help="Calculate stats (accuracy, attack success rate) on test/train dataset (default: %(default)s)",
# )
# parser.add_argument(
# "--similarity",
# choices=["cosine", "l2"],
# default="cosine",
# help="Choose the similarity function (default: %(default)s)",
# )
parser.add_argument(
"--hard-threshold",
type=float,
help="Threshold to discard samples that have less than that in their similarity score. `softmax()` is applied to the samples according to their similarity score after applying the threshold. Pass `0` for no threshold, i.e., use all samples.",
)
parser.add_argument(
"--quantile-threshold",
type=float,
help="Quantile Threshold to discard samples that have less than that in their similarity score. `softmax()` is applied to the samples according to their similarity score after applying the threshold. Pass `0` for no threshold, i.e., use all samples.",
)
parser.add_argument(
"--plots-dir",
type=str,
default="./plots/",
help="Output directory of plots (hint: `from google.colab import drive; drive.mount('/content/drive')` and then save the plots to drive in case the colab session disconnects) (default: %(default)s)",
)
args = parser.parse_args()
bannd(
args.runtype,
args.dataset,
# args.inplace_or_merge,
# args.batch_size,
args.poison_rate,
# args.save_name,
# args.epochs,
# args.calc_every_n_iter,
# args.calc_stats_on,
# args.similarity,
args.hard_threshold,
args.quantile_threshold,
args.plots_dir,
)
if __name__ == "__main__":
main()