-
Notifications
You must be signed in to change notification settings - Fork 2
/
defense_runner.py
105 lines (77 loc) · 2.65 KB
/
defense_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
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
import os
import sys
sys.path.append('./src/')
sys.path.append('./src/fl-train/')
sys.path.append('./src/training/')
sys.path.append('./src/dataset/*')
import argparse
from fl_train import FLTrainer
from globalUtils import *
import copy
from multiprocessing import Process
def getConfParamVal(key,conf):
keys = key.split('.')
y = conf
for k in keys:
y = y[k]
return y
def runConf(conf):
print('running conf with defense '+conf['defenseTechnique'])
seed(42)
od = conf['outputDir']
if(od.startswith('$')):
od = od[1:]
x = od.split('/')
name = '_'.join( [ '{}_{}'.format(k, getConfParamVal(k,conf)) for k in x[-1].split('_') ] )
od= '/'.join(x[:-1])+'/'+name
conf['outputDir'] = od
if not os.path.exists(od):
os.makedirs(od)
print('Will be saving output and logs in directory {}'.format(od))
stdoutFlag = False
logger = getLogger("{}/fl.log".format(od), stdoutFlag, logging.INFO)
print('Log File: {}/fl.log'.format(od))
#print(conf['ckptEpochs'])
flTrainer = FLTrainer(conf,logger)
flTrainer.trainNEpochs()
import time
def ParallelRun(lstConf):
lstP = []
for conf in lstConf:
conf = copy.deepcopy(conf) # ensure no shit happens
p = Process(target = runConf, args=(conf,))
p.start()
lstP.append(p)
for p in lstP:
p.join()
def SeqRun(lstConf):
for conf in lstConf:
runConf(conf)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process args')
parser.add_argument('--config', type=str,
help='The conf file to be used for the training')
args = parser.parse_args()
confFilePath = args.config
conf0 = loadConfig(confFilePath)
defenses = ['noDefense','normClipping','krum','multiKrum','rfa']
normBounds = {'noDefense':10, 'normClipping':1.5, 'krum':1.5,'multiKrum':1.5,'rfa':1.5}
lstConf = []
parBatch = 2
#conf0['attackFromEpoch']=100
conf0['numFLEpochs'] = 500
conf0['enableCkpt'] = False
method = conf0['attackerTrainConfig']['method']
op_pfx = './outputs/yorgos_backdoor_defenses_'+method
for defense in defenses:
conf = copy.deepcopy(conf0)
conf['defenseTechnique'] = defense
conf['normBound'] = normBounds[defense]
conf['outputDir'] = op_pfx+ '_'+defense+'_'+str(normBounds[defense])+'/'
lstConf.append(conf)
if(parBatch>1):
for i in range(len(lstConf)):
ParallelRun(lstConf[i*parBatch:(i+1)*parBatch])
else:
SeqRun(lstConf)
print('Here')