-
Notifications
You must be signed in to change notification settings - Fork 3
/
attack_runner.py
executable file
·60 lines (48 loc) · 1.68 KB
/
attack_runner.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import sys
import shlex
import os
from pathlib import Path
def main():
parser = argparse.ArgumentParser(
description='run PGD attack with given args'
)
parser.add_argument('gpu_list', help='available GPU IDs list')
parser.add_argument('output_dir', help='training output directory')
parser.add_argument('slot_id', type=int,
help='job slot ID to decide the GPU')
parser.add_argument('args',
help='one line from the task file for args, in the '
'format dir_name eps,eps,...,eps [hardtanh]')
args = parser.parse_args()
gpus = args.gpu_list.split(',')
os.environ['CUDA_VISIBLE_DEVICES'] = gpus[args.slot_id % len(gpus)]
dir_name, eps, *hardtanh = shlex.split(args.args)
extra_args = []
if hardtanh:
assert hardtanh == ['hardtanh']
outname = 'attack-hardtanh.json'
extra_args.append('--use-hardtanh-grad')
else:
outname = 'attack.json'
eps = eps.split(',')
for i in eps:
extra_args.extend(['-e', i])
work_dir = Path(args.output_dir) / dir_name
if not (work_dir / 'finish_mark').exists():
print(f'skip unfinished task {work_dir}')
return
if (work_dir / outname).exists():
print(f'skip finished task {outname} in {work_dir}')
return
sub_args = (
['python', '-m', 'eevbnn', 'attack', str(work_dir / 'last.pth'),
'--write-result', str(work_dir / outname),
'--pgd-steps=100', '--pgd',
] + extra_args
)
os.execve(sys.executable, sub_args, os.environ)
if __name__ == '__main__':
main()