-
Notifications
You must be signed in to change notification settings - Fork 315
/
compare.py
executable file
·362 lines (308 loc) · 12.5 KB
/
compare.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
#!/usr/bin/env python3
import os
import numpy as np
# -custom-written code
import main
from utils import checkattr
from params.param_stamp import get_param_stamp_from_args
from params.param_values import check_for_errors,set_default_values
from params import options
from visual import visual_plt
## Function for specifying input-options and organizing / checking them
def handle_inputs():
# Set indicator-dictionary for correctly retrieving / checking input options
kwargs = {'comparison': True, 'compare_all': True}
# Define input options
parser = options.define_args(filename="compare", description='Compare performance of CL strategies.')
parser = options.add_general_options(parser, **kwargs)
parser = options.add_eval_options(parser, **kwargs)
parser = options.add_problem_options(parser, **kwargs)
parser = options.add_model_options(parser, **kwargs)
parser = options.add_train_options(parser, **kwargs)
parser = options.add_cl_options(parser, **kwargs)
# Should some methods not be included in the comparison?
parser.add_argument('--no-context-spec', action='store_true', help="no XdG or Separate Networks")
parser.add_argument('--no-reg', action='store_true', help="no EWC or SI")
parser.add_argument('--no-fromp', action='store_true', help="no FROMP")
parser.add_argument('--no-bir', action='store_true', help="no BI-R")
parser.add_argument('--no-agem', action='store_true', help="no A-GEM")
# Parse, process (i.e., set defaults for unselected options) and check chosen options
args = parser.parse_args()
args.log_per_context = True
set_default_values(args, also_hyper_params=True) # -set defaults, some are based on chosen scenario / experiment
check_for_errors(args, **kwargs) # -check whether incompatible options are selected
return args
## Functions for running experiments and collecting results
def get_results(args):
# -get param-stamp
param_stamp = get_param_stamp_from_args(args)
# -check whether already run; if not do so
file_to_check = '{}/acc-{}{}.txt'.format(args.r_dir, param_stamp,
"--S{}".format(args.eval_s) if checkattr(args, 'gen_classifier') else "")
if os.path.isfile(file_to_check):
print(" already run: {}".format(param_stamp))
elif os.path.isfile("{}/mM-{}".format(args.m_dir, param_stamp)):
args.train = False
print(" ...testing: {}".format(param_stamp))
main.run(args)
else:
args.train = True
print(" ...running: {}".format(param_stamp))
main.run(args)
# -get average accuracy
fileName = '{}/acc-{}{}.txt'.format(args.r_dir, param_stamp,
"--S{}".format(args.eval_s) if checkattr(args, 'gen_classifier') else "")
file = open(fileName)
ave = float(file.readline())
file.close()
# -print average accuracy on screen
print("--> average accuracy: {}".format(ave))
# -return average accuracy
return ave
def collect_all(method_dict, seed_list, args, name=None):
# -print name of method on screen
if name is not None:
print("\n------{}------".format(name))
# -run method for all random seeds
for seed in seed_list:
args.seed = seed
method_dict[seed] = get_results(args)
# -return updated dictionary with results
return method_dict
if __name__ == '__main__':
## Load input-arguments
args = handle_inputs()
# Create plots- and results-directories if needed
if not os.path.isdir(args.r_dir):
os.mkdir(args.r_dir)
if not os.path.isdir(args.p_dir):
os.mkdir(args.p_dir)
#-------------------------------------------------------------------------------------------------#
#--------------------------#
#----- RUN ALL MODELS -----#
#--------------------------#
seed_list = list(range(args.seed, args.seed+args.n_seeds))
###----"BASELINES"----###
## None
args.replay = "none"
NONE = {}
NONE = collect_all(NONE, seed_list, args, name="None")
## JOINT training (using total number of iterations from all contexts)
iters_temp = args.iters
args.iters = args.contexts*iters_temp
args.joint = True
JOINT = {}
JOINT = collect_all(JOINT, seed_list, args, name="Joint")
args.joint = False
args.iters = iters_temp
###----"CONTEXT-SPECIFIC"----####
if args.scenario=="task" and not checkattr(args, 'no_context_spec'):
## Separate network per context
fc_units_temp = args.fc_units
args.fc_units = args.fc_units_sep
args.separate_networks = True
SEP = {}
SEP = collect_all(SEP, seed_list, args, name="Separate Networks")
args.separate_networks = False
args.fc_units = fc_units_temp
## XdG
always_xdg = checkattr(args, 'xdg')
args.xdg = True
XDG = {}
XDG = collect_all(XDG, seed_list, args, name="XdG")
args.xdg = always_xdg
###----"PARAMETER REGULARIZATION"----####
if not checkattr(args, 'no_reg'):
## EWC
args.weight_penalty = True
args.importance_weighting = 'fisher'
args.offline = True
args.reg_strength = args.ewc_lambda
EWC = {}
EWC = collect_all(EWC, seed_list, args, name="EWC")
args.weight_penalty = False
args.offline = False
## SI
args.weight_penalty = True
args.importance_weighting = 'si'
args.reg_strength = args.si_c
SI = {}
SI = collect_all(SI, seed_list, args, name="SI")
args.weight_penalty = False
else:
EWC = SI = None
###----"FUNCTIONAL REGULARIZATION"----####
## LwF
args.replay = "current"
args.distill = True
LWF = {}
LWF = collect_all(LWF, seed_list, args, name="LwF")
args.replay = "none"
args.distill = False
## FROMP
if not checkattr(args, 'no_fromp'):
args.fromp = True
args.sample_selection = "fromp"
FROMP = {}
FROMP = collect_all(FROMP, seed_list, args, name="FROMP")
args.fromp = False
else:
FROMP = None
###----"REPLAY"----###
## DGR
args.replay = "generative"
args.distill = False
DGR = {}
DGR = collect_all(DGR, seed_list, args, name="Deep Generative Replay")
## BI-R
if not checkattr(args, 'no_bir'):
args.replay = "generative"
args.feedback = True
args.hidden = True
args.dg_gates = True
args.prior = "GMM"
args.per_class = True
args.distill = True
BIR = {}
BIR = collect_all(BIR, seed_list, args, name="Brain-Inspired Replay")
args.feedback = False
args.hidden = False
args.dg_gates = False
args.prior = "standard"
args.per_class = False
args.distill = False
else:
BIR = None
## Experience Replay
args.replay = "buffer"
args.sample_selection = "random"
ER = {}
ER = collect_all(ER, seed_list, args, name="Experience Replay (budget = {})".format(args.budget))
args.replay = "none"
## A-GEM
if not checkattr(args, 'no_agem'):
args.replay = "buffer"
args.sample_selection = "random"
args.use_replay = "inequality"
AGEM = {}
AGEM = collect_all(AGEM, seed_list, args, name="A-GEM (budget = {})".format(args.budget))
args.replay = "none"
args.use_replay = "normal"
else:
AGEM = None
###----"TEMPLATE-BASED CLASSIFICATION"----####
if args.scenario=="class" and not args.neg_samples=="current":
## iCaRL
args.bce = True
args.bce_distill = True
args.prototypes = True
args.add_buffer = True
args.sample_selection = "herding"
args.neg_samples = "all-so-far"
ICARL = {}
ICARL = collect_all(ICARL, seed_list, args, name="iCaRL (budget = {})".format(args.budget))
args.bce = False
args.bce_distill = False
args.prototypes = False
args.add_buffer = False
## Generative Classifier
args.gen_classifier = True
classes_per_context = 2 if args.experiment=="splitMNIST" else 10
args.iters = int(args.iters / classes_per_context)
args.fc_units = args.fc_units_gc
args.fc_lay = args.fc_lay_gc
args.z_dim = args.z_dim_gc
args.hidden = True
args.lr = 0.001
GENCLASS = {}
GENCLASS = collect_all(GENCLASS, seed_list, args, name="Generative Classifier")
#-------------------------------------------------------------------------------------------------#
#---------------------------------------------#
#----- COLLECT RESULTS: AVERAGE ACCURACY -----#
#---------------------------------------------#
## For each seed, create list with average test accuracy
ave_acc = {}
for seed in seed_list:
ave_acc[seed] = [NONE[seed], JOINT[seed],
0 if EWC is None else EWC[seed], 0 if SI is None else SI[seed], LWF[seed],
0 if FROMP is None else FROMP[seed],
DGR[seed], 0 if BIR is None else BIR[seed], ER[seed], 0 if AGEM is None else AGEM[seed]]
if args.scenario=="task" and not checkattr(args, 'no_context_spec'):
ave_acc[seed].append(XDG[seed])
ave_acc[seed].append(SEP[seed])
elif args.scenario=="class" and not args.neg_samples=="current":
ave_acc[seed].append(ICARL[seed])
ave_acc[seed].append(GENCLASS[seed])
#-------------------------------------------------------------------------------------------------#
#--------------------------------------------------#
#----- REPORTING / PLOTTING: AVERAGE ACCURACY -----#
#--------------------------------------------------#
# name for plot
plot_name = "summary-{}{}-{}".format(args.experiment, args.contexts, args.scenario)
scheme = "{}-incremental learning".format(args.scenario)
title = "{} - {}".format(args.experiment, scheme)
# select names / colors / ids
names = ["None", "Joint"]
colors = ["grey", "black"]
ids = [0, 1]
if args.scenario=="task" and not checkattr(args, 'no_context_spec'):
names += ['Separate Networks', 'XdG']
colors += ['dodgerblue', 'deepskyblue']
ids += [11, 10]
if not checkattr(args, 'no_reg'):
names += ['EWC', 'SI']
colors += ['darkgreen', 'yellowgreen']
ids += [2, 3]
names.append('LwF')
colors.append('gold')
ids.append(4)
if not checkattr(args, 'no_fromp'):
names.append("FROMP (b={})".format(args.budget))
colors.append('goldenrod')
ids.append(5)
names.append('DGR')
colors.append('indianred')
ids.append(6)
if not checkattr(args, 'no_bir'):
names.append('BI-R')
colors.append('lightcoral')
ids.append(7)
names.append("ER (b={})".format(args.budget))
colors.append('red')
ids.append(8)
if not checkattr(args, 'no_agem'):
names.append("A-GEM (b={})".format(args.budget))
colors.append('orangered')
ids.append(9)
if args.scenario=="class" and not args.neg_samples=="current":
names += ['Generative Classifier', "iCaRL (b={})".format(args.budget)]
colors += ['indigo', 'purple']
ids += [11, 10]
# open pdf
pp = visual_plt.open_pdf("{}/{}.pdf".format(args.p_dir, plot_name))
figure_list = []
# bar-plot
means = [np.mean([ave_acc[seed][id] for seed in seed_list]) for id in ids]
if len(seed_list)>1:
sems = [np.sqrt(np.var([ave_acc[seed][id] for seed in seed_list])/(len(seed_list)-1)) for id in ids]
cis = [1.96*np.sqrt(np.var([ave_acc[seed][id] for seed in seed_list])/(len(seed_list)-1)) for id in ids]
figure = visual_plt.plot_bar(means, names=names, colors=colors, ylabel="average accuracy (after all contexts)",
title=title, yerr=cis if len(seed_list)>1 else None, ylim=(0,1))
figure_list.append(figure)
# print results to screen
print("\n\n"+"#"*60+"\nSUMMARY RESULTS: {}\n".format(title)+"#"*60)
for i,name in enumerate(names):
if len(seed_list) > 1:
print("{:27s} {:.2f} (+/- {:.2f}), n={}".format(name, 100*means[i], 100*sems[i], len(seed_list)))
else:
print("{:27s} {:.2f}".format(name, 100*means[i]))
if i==1:
print("="*60)
print("#"*60)
# add all figures to pdf
for figure in figure_list:
pp.savefig(figure)
# close the pdf
pp.close()
# Print name of generated plot on screen
print("\nGenerated plot: {}/{}.pdf\n".format(args.p_dir, plot_name))