-
Notifications
You must be signed in to change notification settings - Fork 12
/
trainer.py
287 lines (243 loc) · 11.9 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
import os
import torch
import numpy as np
import random
from collections import OrderedDict
import dataloaders
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
# for generative models, pre-process data to be 0...1; otherwise, pre-process data to be zero mean, unit variance
if args.learner_type == 'dgr':
self.dgr = True
else:
self.dgr = False
# model load directory
if args.load_model_dir is not None:
self.model_first_dir = args.load_model_dir
else:
self.model_first_dir = args.log_dir
self.model_top_dir = args.log_dir
# select dataset
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':
Dataset = dataloaders.iIMAGENET
num_classes = 1000
self.dataset_size = [224,224,3]
self.top_k = 5
elif args.dataset == 'TinyImageNet':
Dataset = dataloaders.iTinyIMNET
num_classes = 200
self.dataset_size = [64,64,3]
else:
raise ValueError('Dataset not implemented!')
# load tasks
class_order = np.arange(num_classes).tolist()
class_order_logits = np.arange(num_classes).tolist()
if args.rand_split:
print('=============================================')
print('Shuffling....')
print('pre-shuffle:' + str(class_order))
if args.dataset == 'ImageNet':
np.random.seed(1993)
np.random.shuffle(class_order)
else:
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
train_transform = dataloaders.utils.get_transform(dataset=args.dataset, phase='train', aug=args.train_aug, dgr=self.dgr)
test_transform = dataloaders.utils.get_transform(dataset=args.dataset, phase='test', aug=args.train_aug, dgr=self.dgr)
self.train_dataset = Dataset(args.dataroot, train=True, tasks=self.tasks,
download_flag=True, transform=train_transform,
seed=self.seed, validation=args.validation)
self.test_dataset = Dataset(args.dataroot, train=False, tasks=self.tasks,
download_flag=False, transform=test_transform,
seed=self.seed, validation=args.validation)
# save this for E2E baseline
self.train_dataset.simple_transform = dataloaders.utils.get_transform(dataset=args.dataset, phase='test', aug=args.train_aug, dgr=self.dgr)
# 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,
'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,
'gen_model_type': args.gen_model_type,
'gen_model_name': args.gen_model_name,
'optimizer': args.optimizer,
'gpuid': args.gpuid,
'memory': args.memory,
'temp': args.temp,
'out_dim': num_classes,
'overwrite': args.overwrite == 1,
'beta': args.beta,
'mu': args.mu,
'DW': args.DW,
'batch_size': args.batch_size,
'power_iters': args.power_iters,
'deep_inv_params': args.deep_inv_params,
'tasks': self.tasks_logits,
'top_k': self.top_k,
}
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)
self.learner.print_model()
def task_eval(self, t_index, local=False):
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])
else:
return self.learner.validation(test_loader)
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
# set seeds
random.seed(self.seed*100 + i)
np.random.seed(self.seed*100 + i)
torch.manual_seed(self.seed*100 + i)
torch.cuda.manual_seed(self.seed*100 + 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)
# tell learner number of tasks we are doing
self.learner.max_task = self.max_task
# 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))
# 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)
if i == 0:
model_save_dir = self.model_first_dir + '/models/repeat-'+str(self.seed+1)+'/task-'+self.task_names[i]+'/'
else:
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 = []
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)))
# save temporary results
for mkey in self.metric_keys:
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
avg_metrics['mem']['global'][:] = self.learner.count_memory(self.dataset_size)
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
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]
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)
# 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}
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):
# load model
if i == 0:
model_save_dir = self.model_first_dir + '/models/repeat-'+str(self.seed+1)+'/task-'+self.task_names[i]+'/'
else:
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)
# 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'] = self.summarize_acc(avg_metrics['acc'], metric_table['acc'], metric_table_local['acc'])
return avg_metrics