-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
314 lines (261 loc) · 12.5 KB
/
trainer.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
import os
import sys
import argparse
import torch
import numpy as np
import random
from random import shuffle
from collections import OrderedDict
import dataloaders
from dataloaders.utils import *
from torch.utils.data import DataLoader
import learners
class Trainer:
def __init__(self, args, seed, metric_keys, save_keys):
# process inputs
self.seed = seed
self.metric_keys = metric_keys
self.save_keys = save_keys
self.log_dir = args.log_dir
self.batch_size = args.batch_size
self.workers = args.workers
# model load directory
self.model_top_dir = args.log_dir
# select dataset
self.grayscale_vis = False
self.top_k = 1
if args.dataset == 'CIFAR10':
Dataset = dataloaders.iCIFAR10
num_classes = 10
self.dataset_size = [32,32,3]
elif args.dataset == 'CIFAR100':
Dataset = dataloaders.iCIFAR100
num_classes = 100
self.dataset_size = [32,32,3]
elif args.dataset == 'ImageNet_R':
Dataset = dataloaders.iIMAGENET_R
num_classes = 200
self.dataset_size = [224,224,3]
self.top_k = 1
else:
raise ValueError('Dataset not implemented!')
# upper bound flag
if args.upper_bound_flag:
args.other_split_size = num_classes
args.first_split_size = num_classes
# load tasks
class_order = np.arange(num_classes).tolist()
class_order_logits = np.arange(num_classes).tolist()
if self.seed > 0 and args.rand_split:
print('=============================================')
print('Shuffling....')
# print('pre-shuffle:' + str(class_order))
random.seed(self.seed)
random.shuffle(class_order)
# print('post-shuffle:' + str(class_order))
print('=============================================')
self.tasks = []
self.tasks_logits = []
p = 0
while p < num_classes and (args.max_task == -1 or len(self.tasks) < args.max_task):
inc = args.other_split_size if p > 0 else args.first_split_size
self.tasks.append(class_order[p:p+inc])
self.tasks_logits.append(class_order_logits[p:p+inc])
p += inc
self.num_tasks = len(self.tasks)
self.task_names = [str(i+1) for i in range(self.num_tasks)]
# number of tasks to perform
if args.max_task > 0:
self.max_task = min(args.max_task, len(self.task_names))
else:
self.max_task = len(self.task_names)
# datasets and dataloaders
k = 1 # number of transforms per image
if args.model_name.startswith('vit'):
resize_imnet = True
else:
resize_imnet = False
train_transform = dataloaders.utils.get_transform(dataset=args.dataset, phase='train', aug=args.train_aug, resize_imnet=resize_imnet)
test_transform = dataloaders.utils.get_transform(dataset=args.dataset, phase='test', aug=args.train_aug, resize_imnet=resize_imnet)
self.train_dataset = Dataset(args.dataroot, train=True, lab = True, tasks=self.tasks,
download_flag=True, transform=train_transform,
seed=self.seed, rand_split=args.rand_split, validation=args.validation)
self.test_dataset = Dataset(args.dataroot, train=False, tasks=self.tasks,
download_flag=False, transform=test_transform,
seed=self.seed, rand_split=args.rand_split, validation=args.validation)
# for oracle
self.oracle_flag = args.oracle_flag
self.add_dim = 0
# Prepare the self.learner (model)
self.learner_config = {'num_classes': num_classes,
'lr': args.lr,
'debug_mode': args.debug_mode == 1,
'momentum': args.momentum,
'weight_decay': args.weight_decay,
'schedule': args.schedule,
'schedule_type': args.schedule_type,
'model_type': args.model_type,
'model_name': args.model_name,
'optimizer': args.optimizer,
'gpuid': args.gpuid,
'memory': args.memory,
'temp': args.temp,
'out_dim': num_classes,
'overwrite': args.overwrite == 1,
'DW': args.DW,
'batch_size': args.batch_size,
'upper_bound_flag': args.upper_bound_flag,
'tasks': self.tasks_logits,
'top_k': self.top_k,
'prompt_param':[self.num_tasks,args.prompt_param],
'query': args.query
}
self.learner_type, self.learner_name = args.learner_type, args.learner_name
self.learner = learners.__dict__[self.learner_type].__dict__[self.learner_name](self.learner_config)
def task_eval(self, t_index, local=False, task='acc'):
val_name = self.task_names[t_index]
# print('validation split name:', val_name)
# eval
self.test_dataset.load_dataset(t_index, train=True)
test_loader = DataLoader(self.test_dataset, batch_size=self.batch_size, shuffle=False, drop_last=False, num_workers=self.workers)
if local:
return self.learner.validation(test_loader, task_in = self.tasks_logits[t_index], task_metric=task)
else:
return self.learner.validation(test_loader, task_metric=task)
def train(self, avg_metrics):
# temporary results saving
temp_table = {}
for mkey in self.metric_keys: temp_table[mkey] = []
temp_dir = self.log_dir + '/temp/'
if not os.path.exists(temp_dir): os.makedirs(temp_dir)
# for each task
for i in range(self.max_task):
# save current task index
self.current_t_index = i
# print name
train_name = self.task_names[i]
print('======================', train_name, '=======================')
# load dataset for task
task = self.tasks_logits[i]
if self.oracle_flag:
self.train_dataset.load_dataset(i, train=False)
self.learner = learners.__dict__[self.learner_type].__dict__[self.learner_name](self.learner_config)
self.add_dim += len(task)
else:
self.train_dataset.load_dataset(i, train=True)
self.add_dim = len(task)
# set task id for model (needed for prompting)
try:
self.learner.model.module.task_id = i
except:
self.learner.model.task_id = i
# add valid class to classifier
self.learner.add_valid_output_dim(self.add_dim)
# load dataset with memory
self.train_dataset.append_coreset(only=False)
# load dataloader
train_loader = DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True, drop_last=True, num_workers=int(self.workers))
# increment task id in prompting modules
if i > 0:
try:
if self.learner.model.module.prompt is not None:
self.learner.model.module.prompt.process_task_count()
except:
if self.learner.model.prompt is not None:
self.learner.model.prompt.process_task_count()
# learn
self.test_dataset.load_dataset(i, train=False)
test_loader = DataLoader(self.test_dataset, batch_size=self.batch_size, shuffle=False, drop_last=False, num_workers=self.workers)
model_save_dir = self.model_top_dir + '/models/repeat-'+str(self.seed+1)+'/task-'+self.task_names[i]+'/'
if not os.path.exists(model_save_dir): os.makedirs(model_save_dir)
avg_train_time = self.learner.learn_batch(train_loader, self.train_dataset, model_save_dir, test_loader)
# save model
self.learner.save_model(model_save_dir)
# evaluate acc
# acc_table = []
# acc_table_ssl = []
# self.reset_cluster_labels = True
# for j in range(i+1):
# acc_table.append(self.task_eval(j))
# temp_table['acc'].append(np.mean(np.asarray(acc_table)))
# temp_table['acc'].append(np.mean(np.asarray(acc_table)))
# save temporary acc results
# for mkey in ['acc']:
# save_file = temp_dir + mkey + '.csv'
# np.savetxt(save_file, np.asarray(temp_table[mkey]), delimiter=",", fmt='%.2f')
if avg_train_time is not None: avg_metrics['time']['global'][i] = avg_train_time
return avg_metrics
def summarize_acc(self, acc_dict, acc_table, acc_table_pt):
# unpack dictionary
avg_acc_all = acc_dict['global']
avg_acc_pt = acc_dict['pt']
avg_acc_pt_local = acc_dict['pt-local']
# Calculate average performance across self.tasks
# Customize this part for a different performance metric
avg_acc_history = [0] * self.max_task
acc_matrix = np.zeros((self.max_task, self.max_task))
for i in range(self.max_task):
train_name = self.task_names[i]
cls_acc_sum = 0
for j in range(i+1):
val_name = self.task_names[j]
acc_matrix[i,j] = acc_table[val_name][train_name]
cls_acc_sum += acc_table[val_name][train_name]
avg_acc_pt[j,i,self.seed] = acc_table[val_name][train_name]
avg_acc_pt_local[j,i,self.seed] = acc_table_pt[val_name][train_name]
avg_acc_history[i] = cls_acc_sum / (i + 1)
# get forget score
f_score = self.cal_fscore(acc_matrix)
# Gather the final avg accuracy
avg_acc_all[:,self.seed] = avg_acc_history
# repack dictionary and return
return {'global': avg_acc_all,'pt': avg_acc_pt,'pt-local': avg_acc_pt_local}, f_score
def evaluate(self, avg_metrics):
self.learner = learners.__dict__[self.learner_type].__dict__[self.learner_name](self.learner_config)
# store results
metric_table = {}
metric_table_local = {}
for mkey in self.metric_keys:
metric_table[mkey] = {}
metric_table_local[mkey] = {}
for i in range(self.max_task):
# increment task id in prompting modules
if i > 0:
try:
if self.learner.model.module.prompt is not None:
self.learner.model.module.prompt.process_task_count()
except:
if self.learner.model.prompt is not None:
self.learner.model.prompt.process_task_count()
# load model
model_save_dir = self.model_top_dir + '/models/repeat-'+str(self.seed+1)+'/task-'+self.task_names[i]+'/'
self.learner.task_count = i
self.learner.add_valid_output_dim(len(self.tasks_logits[i]))
self.learner.pre_steps()
self.learner.load_model(model_save_dir)
# set task id for model (needed for prompting)
try:
self.learner.model.module.task_id = i
except:
self.learner.model.task_id = i
# evaluate acc
metric_table['acc'][self.task_names[i]] = OrderedDict()
metric_table_local['acc'][self.task_names[i]] = OrderedDict()
self.reset_cluster_labels = True
for j in range(i+1):
val_name = self.task_names[j]
metric_table['acc'][val_name][self.task_names[i]] = self.task_eval(j)
for j in range(i+1):
val_name = self.task_names[j]
metric_table_local['acc'][val_name][self.task_names[i]] = self.task_eval(j, local=True)
# summarize metrics
avg_metrics['acc'], f_score = self.summarize_acc(avg_metrics['acc'], metric_table['acc'], metric_table_local['acc'])
return avg_metrics, f_score
def cal_fscore(self, y):
index = y.shape [1]
fgt = 0
for t in range(1, index):
for i in range(t):
fgt += (y[t-1,i]-y[t,i])*(1/t)
fgt = fgt/(index-1)
return fgt