-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
397 lines (361 loc) · 20.2 KB
/
pipeline.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
import os
import pickle
import numpy as np
import torch
from Backbones.model_factory import get_model
from Backbones.utils import evaluate, NodeLevelDataset, evaluate_batch
from training.utils import mkdir_if_missing
from dataset.utils import semi_task_manager
import importlib
import copy, ipdb
import dgl
joint_alias = ['joint', 'Joint', 'joint_replay_all', 'jointtrain']
def get_pipeline(args):
# choose the pipeline for the chosen setting
if args.minibatch:
if args.ILmode == 'classIL':
return pipeline_class_IL_no_inter_edge_minibatch
elif args.ILmode == 'taskIL':
return pipeline_task_IL_no_inter_edge_minibatch
else:
if args.ILmode == 'classIL':
return pipeline_class_IL_no_inter_edge
elif args.ILmode == 'taskIL':
return pipeline_task_IL_no_inter_edge
def data_prepare(args):
"""
check whether the processed data exist or create new processed data
if args.load_check is True, loading data will be tried, else, will only check the existence of the files
"""
torch.cuda.set_device(args.gpu)
dataset = NodeLevelDataset(args.dataset,ratio_valid_test=args.ratio_valid_test,args=args)
args.d_data, args.n_cls = dataset.d_data, dataset.n_cls
cls = [list(range(i, i + args.n_cls_per_task)) for i in range(0, args.n_cls-1, args.n_cls_per_task)]
args.task_seq = cls
args.n_tasks = len(args.task_seq)
n_cls_so_far = 0
# check whether the preprocessed data exist and can be loaded
str_int_tsk = 'inter_tsk_edge' if args.inter_task_edges else 'no_inter_tsk_edge'
for task, task_cls in enumerate(args.task_seq):
n_cls_so_far += len(task_cls)
try:
if args.load_check:
subgraph, ids_per_cls, [train_ids, valid_ids, test_ids] = pickle.load(open(
f'{args.data_path}/{str_int_tsk}/{args.dataset}_{task_cls}.pkl', 'rb'))
else:
if f'{args.dataset}_{task_cls}.pkl' not in os.listdir(f'{args.data_path}/{str_int_tsk}'):
subgraph, ids_per_cls, [train_ids, valid_ids, test_ids] = pickle.load(open(
f'{args.data_path}/{str_int_tsk}/{args.dataset}_{task_cls}.pkl', 'rb'))
except:
# if not exist or cannot be loaded correctly, create new processed data
print(f'preparing data for task {task}')
mkdir_if_missing(f'{args.data_path}/inter_tsk_edge')
mkdir_if_missing(f'{args.data_path}/no_inter_tsk_edge')
if args.inter_task_edges:
cls_retain = []
for clss in args.task_seq[0:task + 1]:
cls_retain.extend(clss)
subgraph, ids_per_cls_all, [train_ids, valid_ids, test_ids] = dataset.get_graph(
tasks_to_retain=cls_retain)
with open(f'{args.data_path}/inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'wb') as f:
pickle.dump([subgraph, ids_per_cls_all, [train_ids, valid_ids, test_ids]], f)
else:
subgraph, ids_per_cls, [train_ids, valid_ids, test_ids] = dataset.get_graph(tasks_to_retain=task_cls)
with open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'wb') as f:
pickle.dump([subgraph, ids_per_cls, [train_ids, valid_ids, test_ids]], f)
def pipeline_task_IL_no_inter_edge(args, valid=False):
# valid=True denotes the evaluation is done on validation set, otherwise on testing set
epochs = args.epochs if valid else 0 # training epochs is zero for testing mode
torch.cuda.set_device(args.gpu)
dataset = NodeLevelDataset(args.dataset,ratio_valid_test=args.ratio_valid_test,args=args)
args.d_data, args.n_cls = dataset.d_data, dataset.n_cls
cls = [list(range(i, i + args.n_cls_per_task)) for i in range(0, args.n_cls-1, args.n_cls_per_task)] # this line will remove the final task if only one class included
args.task_seq = cls
args.n_tasks = len(args.task_seq)
task_manager = semi_task_manager()
model = get_model(dataset, args).cuda(args.gpu) if valid else None
life_model = importlib.import_module(f'Baselines.{args.method}_model')
life_model_ins = life_model.NET(model, task_manager, args) if valid else None
acc_matrix = np.zeros([args.n_tasks, args.n_tasks])
meanas = []
prev_model = None
data_prepare(args)
n_cls_so_far = 0
for task, task_cls in enumerate(args.task_seq):
name, ite = args.current_model_save_path
config_name = name.split('/')[-1]
subfolder_c = name.split(config_name)[-2]
save_model_name = f'{config_name}_{ite}_{task_cls}'
save_model_path = f'{args.result_path}/{subfolder_c}val_models/{save_model_name}.pkl'
n_cls_so_far += len(task_cls)
subgraph, ids_per_cls, [train_ids, valid_ids, test_ids] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'rb'))
subgraph = subgraph.to(device='cuda:{}'.format(args.gpu))
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
task_manager.add_task(task, n_cls_so_far)
for epoch in range(epochs):
if args.method == 'lwf':
life_model_ins.observe_task_IL(args, subgraph, features, labels, task, prev_model, train_ids, ids_per_cls, dataset)
else:
life_model_ins.observe_task_IL(args, subgraph, features, labels, task, train_ids, valid_ids, ids_per_cls, dataset)
if not valid:
try:
model = pickle.load(open(save_model_path,'rb')).cuda(args.gpu)
except:
model.load_state_dict(torch.load(save_model_path.replace('.pkl','.pt')))
acc_mean = []
for t in range(task + 1):
subgraph, ids_per_cls, [train_ids, valid_ids_, test_ids_] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{args.task_seq[t]}.pkl','rb'))
test_ids = valid_ids_ if valid else test_ids_ # whether use validation or test set
subgraph = subgraph.to(device='cuda:{}'.format(args.gpu))
ids_per_cls_test = [list(set(ids).intersection(set(test_ids))) for ids in ids_per_cls]
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
label_offset1, label_offset2 = task_manager.get_label_offset(t - 1)[1], task_manager.get_label_offset(t)[1]
labels = labels - label_offset1
if args.classifier_increase:
acc = evaluate(model, subgraph, features, labels, test_ids, label_offset1, label_offset2, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
else:
# deprecated
acc = evaluate(model, subgraph, features, labels, test_ids, label_offset1, label_offset2, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
acc_matrix[task][t] = round(acc * 100, 2)
acc_mean.append(acc)
print(f"T{t:02d} {acc * 100:.2f}|", end="")
accs = acc_mean[:task + 1]
meana = round(np.mean(accs) * 100, 2)
meanas.append(meana)
acc_mean = round(np.mean(acc_mean) * 100, 2)
print(f"acc_mean: {acc_mean}", end="")
print()
if valid:
mkdir_if_missing(f'{args.result_path}/{subfolder_c}/val_models')
try:
with open(save_model_path, 'wb') as f:
pickle.dump(model, f) # save the best model for each hyperparameter composition
except:
torch.save(model.state_dict(), save_model_path.replace('.pkl','.pt')) # save the best model for each hyperparameter composition
prev_model = copy.deepcopy(model).cuda(args.gpu)
print('AP: ', acc_mean)
backward = []
for t in range(args.n_tasks - 1):
b = acc_matrix[args.n_tasks - 1][t] - acc_matrix[t][t]
backward.append(round(b, 2))
mean_backward = round(np.mean(backward), 2)
print('AF: ', mean_backward)
print('\n')
return acc_mean, mean_backward, acc_matrix
def pipeline_task_IL_no_inter_edge_minibatch(args, valid=False):
epochs = args.epochs if valid else 0
torch.cuda.set_device(args.gpu)
dataset = NodeLevelDataset(args.dataset,ratio_valid_test=args.ratio_valid_test,args=args)
args.d_data, args.n_cls = dataset.d_data, dataset.n_cls
cls = [list(range(i, i + args.n_cls_per_task)) for i in range(0, args.n_cls-1, args.n_cls_per_task)]
args.task_seq = cls
args.n_tasks = len(args.task_seq)
task_manager = semi_task_manager()
model = get_model(dataset, args).cuda(args.gpu)
life_model = importlib.import_module(f'Baselines.{args.method}_model')
life_model_ins = life_model.NET(model, task_manager, args) if valid else None
acc_matrix = np.zeros([args.n_tasks, args.n_tasks])
prev_model = None
n_cls_so_far = 0
data_prepare(args)
for task, task_cls in enumerate(args.task_seq):
name, ite = args.current_model_save_path
config_name = name.split('/')[-1]
subfolder_c = name.split(config_name)[-2]
save_model_name = f'{config_name}_{ite}_{task_cls}'
save_model_path = f'{args.result_path}/{subfolder_c}val_models/{save_model_name}.pkl'
n_cls_so_far += len(task_cls)
subgraph, ids_per_cls, [train_ids, valid_idx, test_ids] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'rb'))
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
task_manager.add_task(task, n_cls_so_far)
# build the dataloader for mini batch training
dataloader = dgl.dataloading.NodeDataLoader(subgraph, train_ids, args.nb_sampler, batch_size=args.batch_size, shuffle=args.batch_shuffle, drop_last=False)
for epoch in range(epochs):
if args.method == 'lwf':
life_model_ins.observe_task_IL_batch(args, subgraph, dataloader, features, labels, task, prev_model, train_ids, ids_per_cls, dataset)
else:
life_model_ins.observe_task_IL_batch(args, subgraph, dataloader, features, labels, task, train_ids, valid_idx, ids_per_cls, dataset)
torch.cuda.empty_cache()
# test
if not valid:
try:
model = pickle.load(open(save_model_path,'rb')).cuda(args.gpu)
except:
model.load_state_dict(torch.load(save_model_path.replace('.pkl','.pt')))
acc_mean = []
for t in range(task + 1):
subgraph, ids_per_cls, [train_ids, valid_ids_, test_ids_] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{args.task_seq[t]}.pkl', 'rb'))
test_ids = valid_ids_ if valid else test_ids_
ids_per_cls_test = [list(set(ids).intersection(set(test_ids))) for ids in ids_per_cls]
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
label_offset1, label_offset2 = task_manager.get_label_offset(t - 1)[1], task_manager.get_label_offset(t)[1]
acc = evaluate_batch(args, model, subgraph, features, labels-label_offset1, test_ids, label_offset1, label_offset2, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
acc_matrix[task][t] = round(acc * 100, 2)
acc_mean.append(acc)
print(f"T{t:02d} {acc * 100:.2f}|", end="")
acc_mean = round(np.mean(acc_mean) * 100, 2)
print(f"acc_mean: {acc_mean}", end="")
print()
if valid:
mkdir_if_missing(f'{args.result_path}/{subfolder_c}/val_models')
try:
with open(save_model_path, 'wb') as f:
pickle.dump(model, f) # save the best model for each hyperparameter composition
except:
torch.save(model.state_dict(), save_model_path.replace('.pkl','.pt'))
prev_model = copy.deepcopy(model).cuda()
print('AP: ', acc_mean)
backward = []
for t in range(args.n_tasks - 1):
b = acc_matrix[args.n_tasks - 1][t] - acc_matrix[t][t]
backward.append(round(b, 2))
mean_backward = round(np.mean(backward), 2)
print('AF: ', mean_backward)
print('\n')
return acc_mean, mean_backward, acc_matrix
def pipeline_class_IL_no_inter_edge(args, valid=False):
epochs = args.epochs if valid else 0
torch.cuda.set_device(args.gpu)
dataset = NodeLevelDataset(args.dataset, ratio_valid_test=args.ratio_valid_test,args=args)
args.d_data, args.n_cls = dataset.d_data, dataset.n_cls
cls = [list(range(i, i + args.n_cls_per_task)) for i in range(0, args.n_cls-1, args.n_cls_per_task)]
args.task_seq = cls
args.n_tasks = len(args.task_seq)
task_manager = semi_task_manager()
model = get_model(dataset, args).cuda(args.gpu)
life_model = importlib.import_module(f'Baselines.{args.method}_model')
life_model_ins = life_model.NET(model, task_manager, args) if valid else None
acc_matrix = np.zeros([args.n_tasks, args.n_tasks])
prev_model = None
n_cls_so_far = 0
data_prepare(args)
for task, task_cls in enumerate(args.task_seq):
name, ite = args.current_model_save_path
config_name = name.split('/')[-1]
subfolder_c = name.split(config_name)[-2]
save_model_name = f'{config_name}_{ite}_{task_cls}'
save_model_path = f'{args.result_path}/{subfolder_c}val_models/{save_model_name}.pkl'
n_cls_so_far+=len(task_cls)
subgraph, ids_per_cls, [train_ids, valid_ids, test_ids] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'rb'))
subgraph = subgraph.to(device='cuda:{}'.format(args.gpu))
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
task_manager.add_task(task, n_cls_so_far)
label_offset1, label_offset2 = task_manager.get_label_offset(task)
# training
for epoch in range(epochs):
if args.method == 'lwf':
life_model_ins.observe(args, subgraph, features, labels, task, prev_model, train_ids, ids_per_cls, dataset)
else:
life_model_ins.observe(args, subgraph, features, labels, task, train_ids, valid_ids, ids_per_cls, dataset)
torch.cuda.empty_cache()
if not valid:
try:
model = pickle.load(open(save_model_path,'rb')).cuda(args.gpu)
except:
model.load_state_dict(torch.load(save_model_path.replace('.pkl','.pt')))
acc_mean = []
# test
for t in range(task+1):
subgraph, ids_per_cls, [train_ids, valid_ids_, test_ids_] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{args.task_seq[t]}.pkl', 'rb'))
subgraph = subgraph.to(device='cuda:{}'.format(args.gpu))
test_ids = valid_ids_ if valid else test_ids_
ids_per_cls_test = [list(set(ids).intersection(set(test_ids))) for ids in ids_per_cls]
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
if args.classifier_increase:
acc = evaluate(model, subgraph, features, labels, test_ids, label_offset1, label_offset2, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
else:
acc = evaluate(model, subgraph, features, labels, test_ids, label_offset1, args.n_cls, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
acc_matrix[task][t] = round(acc*100,2)
acc_mean.append(acc)
print(f"T{t:02d} {acc*100:.2f}|", end="")
acc_mean = round(np.mean(acc_mean)*100,2)
print(f"acc_mean: {acc_mean}", end="")
print()
if valid:
mkdir_if_missing(f'{args.result_path}/{subfolder_c}/val_models')
try:
with open(save_model_path, 'wb') as f:
pickle.dump(model, f) # save the best model for each hyperparameter composition
except:
torch.save(model.state_dict(), save_model_path.replace('.pkl','.pt'))
prev_model = copy.deepcopy(model).cuda()
print('AP: ', acc_mean)
backward = []
for t in range(args.n_tasks-1):
b = acc_matrix[args.n_tasks-1][t]-acc_matrix[t][t]
backward.append(round(b, 2))
mean_backward = round(np.mean(backward),2)
print('AF: ', mean_backward)
print('\n')
return acc_mean, mean_backward, acc_matrix
def pipeline_class_IL_no_inter_edge_minibatch(args, valid=False):
epochs = args.epochs if valid else 0
torch.cuda.set_device(args.gpu)
dataset = NodeLevelDataset(args.dataset,ratio_valid_test=args.ratio_valid_test,args=args)
args.d_data, args.n_cls = dataset.d_data, dataset.n_cls
cls = [list(range(i, i + args.n_cls_per_task)) for i in range(0, args.n_cls-1, args.n_cls_per_task)]
args.task_seq = cls
args.n_tasks = len(args.task_seq)
task_manager = semi_task_manager()
model = get_model(dataset, args).cuda(args.gpu)
life_model = importlib.import_module(f'Baselines.{args.method}_model')
life_model_ins = life_model.NET(model, task_manager, args) if valid else None
acc_matrix = np.zeros([args.n_tasks, args.n_tasks])
prev_model = None
n_cls_so_far = 0
data_prepare(args)
for task, task_cls in enumerate(args.task_seq):
name, ite = args.current_model_save_path
config_name = name.split('/')[-1]
subfolder_c = name.split(config_name)[-2]
save_model_name = f'{config_name}_{ite}_{task_cls}'
save_model_path = f'{args.result_path}/{subfolder_c}val_models/{save_model_name}.pkl'
n_cls_so_far += len(task_cls)
subgraph, ids_per_cls, [train_ids, valid_idx, test_ids] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{task_cls}.pkl', 'rb'))
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
task_manager.add_task(task, n_cls_so_far)
# build the dataloader for mini batch training
dataloader = dgl.dataloading.NodeDataLoader(subgraph, train_ids, args.nb_sampler, batch_size=args.batch_size, shuffle=args.batch_shuffle, drop_last=False)
for epoch in range(epochs):
if args.method == 'lwf':
life_model_ins.observe_class_IL_batch(args, subgraph, dataloader, features, labels, task, prev_model, train_ids, ids_per_cls, dataset)
else:
life_model_ins.observe_class_IL_batch(args, subgraph, dataloader, features, labels, task, train_ids, ids_per_cls, dataset)
torch.cuda.empty_cache() # tracemalloc.stop()
label_offset1, label_offset2 = task_manager.get_label_offset(task)
# test
if not valid:
try:
model = pickle.load(open(save_model_path,'rb')).cuda(args.gpu)
except:
model.load_state_dict(torch.load(save_model_path.replace('.pkl','.pt')))
acc_mean = []
for t in range(task + 1):
subgraph, ids_per_cls, [train_ids, valid_ids_, test_ids_] = pickle.load(open(f'{args.data_path}/no_inter_tsk_edge/{args.dataset}_{args.task_seq[t]}.pkl', 'rb'))
test_ids = valid_ids_ if valid else test_ids_
ids_per_cls_test = [list(set(ids).intersection(set(test_ids))) for ids in ids_per_cls]
features, labels = subgraph.srcdata['feat'], subgraph.dstdata['label'].squeeze()
acc = evaluate_batch(args,model, subgraph, features, labels, test_ids, label_offset1, label_offset2, balance=args.balanced_acc, ids_per_cls=ids_per_cls_test)
acc_matrix[task][t] = round(acc * 100, 2)
acc_mean.append(acc)
print(f"T{t:02d} {acc * 100:.2f}|", end="")
acc_mean = round(np.mean(acc_mean) * 100, 2)
print(f"acc_mean: {acc_mean}", end="")
print()
if valid:
mkdir_if_missing(f'{args.result_path}/{subfolder_c}/val_models')
try:
with open(save_model_path, 'wb') as f:
pickle.dump(model, f) # save the best model for each hyperparameter composition
except:
torch.save(model.state_dict(), save_model_path.replace('.pkl','.pt'))
prev_model = copy.deepcopy(model).cuda()
print('AP: ', acc_mean)
backward = []
for t in range(args.n_tasks - 1):
b = acc_matrix[args.n_tasks - 1][t] - acc_matrix[t][t]
backward.append(round(b, 2))
mean_backward = round(np.mean(backward), 2)
print('AF: ', mean_backward)
print('\n')
return acc_mean, mean_backward, acc_matrix