-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
557 lines (467 loc) · 19.9 KB
/
run.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import os
import global_args as gargs
from training_utils import get_attack_name, get_model_name
from utils import run_commands
_kernels = gargs.KERNEL_SIZES
_acts = gargs.ACTIVATION_FUNCTIONS
_ratios = gargs.PRUNING_RATIOS
_struct = [False]
_input_types = ["delta", "x_adv"]
def gen_commands_train_victim(dataset, arch, robust):
_data_arch_name = f"{dataset}_{arch}"
_model_dir = os.path.join(gargs.MODEL_DIR, _data_arch_name)
commands = []
for k in _kernels:
for a in _acts:
for s in _struct:
for r in _ratios:
if r == 0.0 and s:
continue
command = f"python main_victim.py --kernel-size {k} --act-func {a} --pruning-ratio {r} --tensorboard"
command += f" --dataset-dir {gargs.DATASET_DIRS[dataset]}"
command += f" --num-classes {gargs.DATASET_NUM_CLASSES[dataset]}"
command += f" --dataset {dataset}"
command += f" --save-dir {_model_dir}"
command += f" --arch {arch}"
command += gargs.TRAINING_ARGS[dataset]
if s:
command += " --structured-pruning"
if robust:
command += " --robust-train"
model_name = get_model_name(2, k, a, r, s, robust)
epochs = 100 if dataset == "tinyimagenet" else 75
path = os.path.join(
_model_dir, f"{model_name}_omp_2/checkpoint_{epochs}.pt"
)
# commands.append(command)
if not os.path.exists(path):
commands.append(command)
return commands
def gen_commands_attack_victim(dataset, arch, attacks, robust):
_data_arch_name = f"{dataset}_{arch}"
_atk_dir = os.path.join(gargs.ATK_DIR, _data_arch_name)
_model_dir = os.path.join(gargs.MODEL_DIR, _data_arch_name)
commands = []
for idx, atk in enumerate(attacks):
for k in _kernels:
for a in _acts:
for s in _struct:
for r in _ratios:
if r == 0.0 and s:
continue
command = f"python main_victim.py --kernel-size {k} --act-func {a} --pruning-ratio {r} --tensorboard"
command += f" --dataset-dir {gargs.DATASET_DIRS[dataset]}"
command += (
f" --num-classes {gargs.DATASET_NUM_CLASSES[dataset]}"
)
command += f" --dataset {dataset}"
command += f" --save-dir {_model_dir}"
command += f" --arch {arch}"
command += gargs.TRAINING_ARGS[dataset]
if s:
command += " --structured-pruning"
if robust:
command += " --robust-train"
for key, val in atk.items():
command += f" --{key} {val}"
akt_name = get_attack_name(atk)
atk_path = os.path.join(_atk_dir, akt_name)
command += f" --attack-save-dir {atk_path}"
model_name = get_model_name(2, k, a, r, s, robust)
epochs = 100 if dataset == "tinyimagenet" else 75
path = os.path.join(
_model_dir, f"{model_name}_omp_2/checkpoint_{epochs}.pt"
)
# commands.append(command)
if os.path.exists(path):
if not (
os.path.exists(
os.path.join(atk_path, model_name, "ori_pred.pt")
)
and os.path.exists(
os.path.join(atk_path, model_name, "attack_acc.log")
)
):
if idx == 0 or idx > 0 and os.path.exists(path):
commands.append(command)
return commands
def gen_commands_parsing(exp, attr_arch, specific_type=None):
dataset = exp["data"]
arch = exp["arch"]
setting = exp["setting"]
attacks = exp["attacks"]
_data_arch_name = f"{dataset}_{arch}"
_atk_dir = os.path.join(gargs.ATK_DIR, _data_arch_name)
_model_dir = os.path.join(gargs.MODEL_DIR, _data_arch_name)
_parsing_dir = os.path.join(gargs.PARSING_DIR, attr_arch, _data_arch_name)
_grep_dir = os.path.join(gargs.GREP_DIR, _data_arch_name)
_log_dir = os.path.join(gargs.PARSING_LOG_DIR, attr_arch, _data_arch_name)
input_types = [specific_type] if specific_type else _input_types
commands = []
for atk in attacks:
for tp in input_types:
akt_name = get_attack_name(atk)
grep_path = os.path.join(_grep_dir, setting, akt_name)
output_path = os.path.join(_parsing_dir, setting, akt_name, tp)
if not os.path.exists(grep_path):
continue
if not os.path.exists(os.path.join(output_path, "final.pt")):
command = f"python main_parser.py --input_folder {grep_path} --input-type {tp} --save_folder {output_path}"
command += f" --attr-arch {attr_arch}"
command += f" --dataset {dataset}"
commands.append(command)
return commands
def gen_commands_large_set(dataset, arch, setting, attr_arch, specific_type=None):
_data_arch_name = f"{dataset}_{arch}"
_atk_dir = os.path.join(gargs.ATK_DIR, _data_arch_name)
_model_dir = os.path.join(gargs.MODEL_DIR, _data_arch_name)
_parsing_dir = os.path.join(gargs.PARSING_DIR, attr_arch, _data_arch_name)
_grep_dir = os.path.join(gargs.GREP_DIR, _data_arch_name)
_log_dir = os.path.join(gargs.PARSING_LOG_DIR, attr_arch, _data_arch_name)
setting_dir = os.path.join(_grep_dir, setting)
if not os.path.exists(setting_dir):
return []
attack_names = os.listdir(setting_dir)
input_types = [specific_type] if specific_type else _input_types
commands = []
for tp in input_types:
for atk_name in attack_names:
grep_path = os.path.join(setting_dir, atk_name)
output_path = os.path.join(_parsing_dir, setting, atk_name, tp)
if not os.path.exists(grep_path):
continue
if not os.path.exists(os.path.join(output_path, "final.pt")):
command = f"python main_parser.py --input_folder {grep_path} --input-type {tp} --save_folder {output_path}"
command += f" --attr-arch {attr_arch}"
command += f" --dataset {dataset}"
commands.append(command)
return commands
def gen_commands_eval_parsing_cross(exp_model, exp_data, attr_arch, specific_type=None):
if exp_model["data"] != exp_data["data"]:
return []
if (
exp_model["arch"] != exp_data["arch"]
and exp_model["setting"] != exp_data["setting"]
):
return []
dataset = exp_model["data"]
arch_model = exp_model["arch"]
setting_model = exp_model["setting"]
attacks = [atk for atk in exp_model["attacks"] if atk in exp_data["attacks"]]
arch_data = exp_data["arch"]
setting_data = exp_data["setting"]
_data_arch_model = os.path.join(f"{dataset}_{arch_model}", setting_model)
_data_arch_data = os.path.join(f"{dataset}_{arch_data}", setting_data)
_log_dir = os.path.join(
f"{dataset}_{arch_model}"
if arch_model == arch_data
else f"{dataset}_model_{arch_model}_data_{arch_data}",
setting_model
if setting_model == setting_data
else f"model_{setting_model}_data_{setting_data}",
)
_parsing_dir = os.path.join(gargs.PARSING_DIR, attr_arch, _data_arch_model)
_grep_dir = os.path.join(gargs.GREP_DIR, _data_arch_data)
_log_dir = os.path.join(gargs.PARSING_LOG_DIR, attr_arch, _log_dir)
input_types = [specific_type] if specific_type else _input_types
commands = []
for tp in input_types:
for data_atk in attacks:
for model_atk in attacks:
data_atk_name = get_attack_name(data_atk)
atk_path = os.path.join(_grep_dir, data_atk_name)
model_atk_name = get_attack_name(model_atk)
output_path = os.path.join(_parsing_dir, model_atk_name, tp)
log_dir = os.path.join(_log_dir)
command = f"python main_parser_eval.py --input_folder {atk_path} --input-type {tp} --save_folder {output_path} --log_dir {log_dir}"
command += f" --attr-arch {attr_arch}"
command += f" --dataset {dataset}"
if os.path.exists(
os.path.join(output_path, "final.pt")
) and os.path.exists(atk_path):
if not os.path.exists(
os.path.join(
log_dir,
f"data_{data_atk_name}___model_{model_atk_name}__{tp}.log",
)
):
commands.append(command)
return commands
def gen_commands_eval_parsing(exp, attr_arch, specific_type=None):
dataset = exp["data"]
arch = exp["arch"]
setting = exp["setting"]
attacks = exp["attacks"]
_data_arch_model = os.path.join(f"{dataset}_{arch}", setting)
_data_arch_data = os.path.join(f"{dataset}_{arch}", setting)
_log_dir = os.path.join(f"{dataset}_{arch}", setting)
_parsing_dir = os.path.join(gargs.PARSING_DIR, attr_arch, _data_arch_model)
_grep_dir = os.path.join(gargs.GREP_DIR, _data_arch_data)
_log_dir = os.path.join(gargs.PARSING_LOG_DIR, attr_arch, _log_dir)
input_types = [specific_type] if specific_type else _input_types
commands = []
for tp in input_types:
for data_atk in attacks:
model_atk = data_atk
data_atk_name = get_attack_name(data_atk)
atk_path = os.path.join(_grep_dir, data_atk_name)
model_atk_name = get_attack_name(model_atk)
output_path = os.path.join(_parsing_dir, model_atk_name, tp)
log_dir = os.path.join(_log_dir)
command = f"python main_parser_eval.py --input_folder {atk_path} --input-type {tp} --save_folder {output_path} --log_dir {log_dir}"
command += f" --attr-arch {attr_arch}"
command += f" --dataset {dataset}"
if os.path.exists(os.path.join(output_path, "final.pt")) and os.path.exists(
atk_path
):
if not os.path.exists(
os.path.join(
log_dir,
f"data_{data_atk_name}___model_{model_atk_name}__{tp}.log",
)
):
commands.append(command)
return commands
def gen_commands_large_set_test(dataset, arch, setting, attr_arch, specific_type=None):
_data_arch_name = f"{dataset}_{arch}"
_parsing_dir = os.path.join(gargs.PARSING_DIR, attr_arch, _data_arch_name, setting)
_grep_dir = os.path.join(gargs.GREP_DIR, _data_arch_name, setting)
_log_dir = os.path.join(gargs.PARSING_LOG_DIR, attr_arch, _data_arch_name, setting)
if not os.path.exists(_grep_dir):
return []
attack_names = os.listdir(_grep_dir)
input_types = [specific_type] if specific_type else _input_types
commands = []
for tp in input_types:
for data_atk_name in attack_names:
for model_atk_name in attack_names:
atk_path = os.path.join(_grep_dir, data_atk_name)
output_path = os.path.join(_parsing_dir, model_atk_name, tp)
log_dir = os.path.join(_log_dir)
command = f"python main_parser_eval.py --input_folder {atk_path} --input-type {tp} --save_folder {output_path} --log_dir {log_dir}"
command += f" --attr-arch {attr_arch}"
command += f" --dataset {dataset}"
if os.path.exists(
os.path.join(output_path, "final.pt")
) and os.path.exists(atk_path):
if not os.path.exists(
os.path.join(
log_dir,
f"data_{data_atk_name}___model_{model_atk_name}__{tp}.log",
)
):
commands.append(command)
# commands = []
# for atk_name in attack_names:
# for tp in input_types:
# grep_path = os.path.join(setting_dir, atk_name)
# output_path = os.path.join(_parsing_dir, setting, atk_name, tp)
# if not os.path.exists(grep_path):
# continue
# if not os.path.exists(os.path.join(output_path, "final.pt")):
# command = f"python main_parser.py --input_folder {grep_path} --input-type {tp} --save_folder {output_path}"
# command += f" --attr-arch {attr_arch}"
# command += f" --dataset {dataset}"
# commands.append(command)
return commands
def train_victim_commands():
commands = []
for exp in gargs.EXPS:
if exp["setting"] not in "origin robust":
continue
robust = "robust" in exp["setting"]
commands += gen_commands_train_victim(
dataset=exp["data"], arch=exp["arch"], robust=robust
)
print(len(commands))
return commands
def attack_victim_commands():
commands = []
for exp in gargs.EXPS:
if exp["setting"] not in "origin robust":
continue
robust = "robust" in exp["setting"]
commands += gen_commands_attack_victim(
dataset=exp["data"], arch=exp["arch"], attacks=exp["attacks"], robust=robust
)
print(len(commands))
return commands
def train_parsing_commands(attr_arch, specific_type=None):
commands = []
if attr_arch not in ["conv4"]:
commands += gen_commands_parsing(gargs.EXPS[0], attr_arch, specific_type)
else:
for exp in gargs.EXPS:
commands += gen_commands_parsing(exp, attr_arch, specific_type)
print(len(commands))
return commands
def train_large_set_parsing_commands(attr_arch, specific_type=None):
commands = []
exps = [
("cifar10", "full_archs", "origin"),
("cifar10", "partial_archs", "origin"),
("cifar10", "resnet9", "grouped_attack_origin"),
("cifar100", "full_archs", "origin"),
]
for data, arch, setting in exps:
commands += gen_commands_large_set(
data, arch, setting, attr_arch, specific_type
)
print("ext: ", len(commands))
return commands
def test_large_set_parsing_commands(attr_arch, specific_type=None):
commands = []
exps = [
("cifar10", "full_archs", "origin"),
("cifar10", "partial_archs", "origin"),
("cifar10", "resnet9", "grouped_attack_origin"),
("cifar100", "full_archs", "origin"),
]
for data, arch, setting in exps:
commands += gen_commands_large_set_test(
data, arch, setting, attr_arch, specific_type
)
print("ext: ", len(commands))
return commands
def test_parsing_commands(attr_arch):
commands = []
for exp in gargs.EXPS:
commands += gen_commands_eval_parsing(exp=exp, attr_arch=attr_arch)
print(len(commands))
return commands
def cross_test_parsing_commands(attr_arch, specific_type=None):
commands = []
for exp1 in gargs.EXPS[:5]:
for exp2 in gargs.EXPS[:5]:
commands += gen_commands_eval_parsing_cross(
exp1, exp2, attr_arch, specific_type
)
exp1 = gargs.EXPS[0]
for exp2 in gargs.EXPS[5:]:
commands += gen_commands_eval_parsing_cross(
exp2, exp2, attr_arch, specific_type
)
for exp2 in gargs.EXPS[5:]:
commands += gen_commands_eval_parsing_cross(
exp1, exp2, attr_arch, specific_type
)
commands += gen_commands_eval_parsing_cross(
exp2, exp1, attr_arch, specific_type
)
print(len(commands))
return commands
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser("train")
parser.add_argument(
"--stage",
type=int,
help="To decide which part of commands to execute. (1, 2, 3)",
)
parser.add_argument(
"--gpus",
type=str,
default="0,1,2,3,4,5,6,7",
help="Run on which gpus. e.g.: --gpus 0,1,2,3",
)
parser.add_argument(
"--thread",
type=int,
default=1,
help="Number of commands running parallel in one gpu.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Only generate commands without executing if tagged.",
)
parser.add_argument(
"--denoise",
action="store_true",
help="Using denoiser when training attribute models.",
)
args = parser.parse_args()
debug = args.debug
stage = args.stage
th = args.thread
gpus = [int(g) for g in args.gpus.split(",")]
# call each code block seperatly
if stage == 0:
# victim training
ext = f" --ffcv-dir {gargs.FFCV_FORMAT}"
commands = train_victim_commands()
run_commands(
gpus * th if not debug else [0],
commands,
call=not debug,
ext_command=ext,
suffix="commands0",
shuffle=False,
delay=1,
)
elif stage == 1:
# victim training
ext = f" --ffcv-dir {gargs.FFCV_FORMAT}_1"
commands = attack_victim_commands()
run_commands(
gpus * th if not debug else [0],
commands,
call=not debug,
ext_command=ext,
suffix="commands1",
shuffle=False,
delay=1,
)
elif stage == 2:
# parsing training
# need call grep_data.py before training parsing models
commands = []
# commands += train_parsing_commands(attr_arch="conv4")
commands += train_large_set_parsing_commands(attr_arch="conv4")
# for at_arch in gargs.VALID_ATTR_ARCHS:
# if at_arch != "conv4":
# commands += train_parsing_commands(attr_arch=at_arch)
# commands += train_parsing_commands(attr_arch="mlp")
if args.denoise:
commands = []
print("denoise")
# commands += train_parsing_commands("conv4", "denoise")
commands += train_large_set_parsing_commands(
attr_arch="conv4", specific_type="denoise"
)
run_commands(
gpus * th if not debug else [0],
commands,
call=not debug,
suffix="commands2",
shuffle=False,
delay=1,
)
elif stage == 3:
commands = []
# parsing cross testing
# commands += cross_test_parsing_commands(attr_arch="attrnet")
# commands += cross_test_parsing_commands(attr_arch="conv4")
# commands += test_parsing_commands(attr_arch="mlp")
# parsing testing
# for at_arch in gargs.VALID_ATTR_ARCHS:
# if at_arch != "conv4":
# commands += test_parsing_commands(attr_arch=at_arch)
commands += test_large_set_parsing_commands(attr_arch="conv4")
if args.denoise:
commands = []
print("denoise")
# commands += gen_commands_eval_parsing_cross(gargs.EXPS[0], gargs.EXPS[0], "conv4", "denoise")
# print(len(commands))
commands += cross_test_parsing_commands("conv4", "denoise")
commands += test_large_set_parsing_commands(
attr_arch="conv4", specific_type="denoise"
)
run_commands(
gpus * th if not debug else [0],
commands,
call=not debug,
suffix="commands3",
shuffle=False,
delay=2,
)