-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
307 lines (262 loc) · 7.81 KB
/
test.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Use machiraj_attks conda environment
import wandb
import sys
import argparse
import torch
# fix torch seed
torch.manual_seed(42)
# fix cuda seed
torch.cuda.manual_seed(42)
# fix cudnn seed for reproducibility
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.empty_cache()
# sys.path.pop()
# wandb.login()
sys.path.insert(0, "..")
sys.path.append("../")
from misc.utils import *
from data.data import *
from models.model_loader import *
from eval.classic_eval import Evaluator
from eval.attack_eval import AttackEvaluator
# Defaults
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--threat_model",
type=str,
default="std",
help="Threat model",
choices=[
"linf",
"l2",
"std",
"untrained",
"prime",
"augmix",
"xcit",
"vit",
"cc_sota",
"da_augmix",
"flc",
"clad",
],
)
parser.add_argument(
"--dataset",
type=str,
default="cifar10",
help="Dataset",
choices=[
"cifar10",
"imagenet",
"cifar100",
"imagenetc",
"cifar10c",
"cifar100c",
"imagenet_9",
"imagenet_9c",
"custom_imagenet",
"tiny_imagenet",
"tiny_imagenetc",
],
)
parser.add_argument(
"--number_of_iterations",
type=int,
default=100,
help="Number of iterations for the Entropy attack",
)
parser.add_argument(
"--pgd_iters", type=int, default=100, help="Number of iterations for PGD attack"
)
parser.add_argument(
"--batch_size", default=256, type=int, help="Batch size for model evaluation",
)
parser.add_argument(
"--lr", default=0.1, type=float, help="Learning rate for y_adv attack",
)
parser.add_argument(
"--lambda_mse",
default=0.0001,
type=float,
help="Regularization for y_adv attack",
)
parser.add_argument(
"--save_mat",
action="store_true",
help="Save the y_quantize matrix for each image",
)
parser.add_argument(
"--n_epochs",
default=100,
type=int,
help="Number of iterations for y adversarial attack",
)
parser.add_argument(
"--print_every",
default=10,
type=int,
help="Print every n iterations for y adversarial attack",
)
parser.add_argument(
"--retain_dc", action="store_true", help="Retain DC component for y_adv attack",
)
parser.add_argument(
"--dc",
default=0,
type=int,
help="Number of DC components to be kept to 1 in y_adv attack",
)
parser.add_argument(
"--atk_type",
choices=[
"clean",
"sharpen",
"gaussian_blur",
'box_blur',
'non_local_means',
'non_local_means_with_noise_estimation',
# "ideal_edge_gray",
# "entropy_l2",
# "entropy_linf",
"entropy_linf_single_scale",
"entropy_anal",
"pure",
"time_anal",
'nlm_time_anal',
'sharpness_time_anal',
"iteration_anal",
'adaptive_eren',
'adaptive_eren_median',
'pca_train_anal',
'adaptive_eren_estimate_p',
'threshold_anal',
],
default="pure",
help="Correction type",
)
parser.add_argument(
"--evaluate_on_corruptions",
action="store_true",
help="Evaluate on corruptions",
)
parser.add_argument(
"--sharpness_factor",
default=2.0,
type=float,
help="Sharpness factor for sharpening",
)
parser.add_argument(
'--blur_factor',
default=1.5,
type=float,
help="Sigma for Gaussian Blur",
)
parser.add_argument(
"--threshold",
default=0.,
type=float,
help="Threshold for entropy loss",
)
parser.add_argument(
"--edge_kernel_size",
default=3,
type=int,
help="Kernel size for edge detection",
)
parser.add_argument("--model_name", default="resnet50", type=str, help="Model name")
parser.add_argument(
"--parallel", action="store_true", help="Use DataParallel for model"
)
parser.add_argument("--verbose", action="store_true", help="Print losses in attack")
parser.add_argument(
"--lambda_prior",
default=1,
type=float,
help="For attacks with prior the coefficient for the loss",
)
parser.add_argument(
"--epsilon", default=8.0, type=float, help="For PGD Linf attack"
)
parser.add_argument(
"--sim_loss",
default="cosine",
type=str,
help="Type of loss for similarity of DCT",
)
parser.add_argument(
"--kappa",
default=0.9,
type=float,
help="Hinge Loss for Cosine Misclassification",
)
parser.add_argument(
"--alpha",
default=0.01,
type=float,
help="Step size for our entropy Linf/L2 attack",
)
parser.add_argument(
'--test_anal',
action="store_true",
help="Run entropy analysis on test set",
)
args = parser.parse_args()
param = vars(args)
if param["atk_type"] == "entropy_anal" or param['atk_type'] == "time_anal":
param["batch_size"] = 1
config_wandb = dict(defense=param)
param["adv_train"] = False
run_name = run_name_generator(param)
save_name = save_name_generator(param)
param['shuffle'] = False
param['train_mode'] = False
logger = wandb.init(
entity="harshitha-machiraju",
project="EREN",
reinit=True,
name=run_name,
config=config_wandb,
)
"""
Set Dataloaders and Model
"""
if param["dataset"][-1] == "c":
param["dataset"] = param["dataset"][:-1]
# param["dataloader"] = None
# elif param["atk_type"] == "entropy_anal" or param["dataset"][-1] != "c":
data_loading = DataLoading(params=param)
trainset, trainloader, testset, testloader = data_loading.get_data()
if (param["atk_type"] == "entropy_anal" or param["atk_type"] =='pca_train_anal') and param["dataset"]!="imagenet" and not param["test_anal"]:
print("Train set loaded for entropy analysis")
param["dataloader"] = trainloader
else:
print("Test set loaded")
param["dataloader"] = testloader
del trainset, trainloader, testset
# else:
# raise NotImplementedError
param["logger"] = logger
model_loading = ModelLoader(params=param, device=device)
net = model_loading.get_model()
net = net.to(device)
net = net.eval()
print("Model loaded")
"""
Evaluations and Attacks
# """
if param["atk_type"] == "clean":
eval = Evaluator(param, device, net, logger)
clean_acc = eval.clean_accuracy(testloader)
else:
eval = AttackEvaluator(device, net, param, logger)
eval.attack_model()
wandb.finish()
# CUDA_VISIBLE_DEVICES=0 python test.py --atk_type entropy --threat_model std --batch_size 32 --dataset cifar10 --model_name resnet50
# CUDA_VISIBLE_DEVICES=0 python test.py --atk_type sharpen --threat_model std --batch_size 32 --dataset cifar10 --model_name resnet50
# CUDA_VISIBLE_DEVICES=0 python test.py --atk_type sharpen --threat_model std --batch_size 64 --dataset imagenet --model_name resnet50
# CUDA_VISIBLE_DEVICES=3 python test.py --atk_type ideal_edge --threat_model std --batch_size 64 --dataset imagenet --model_name resnet50