-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
259 lines (200 loc) · 9.64 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
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):
# process inputs
self.seed = seed
self.metric_keys = metric_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
elif args.dataset == 'DomainNet':
Dataset = dataloaders.iDOMAIN_NET
num_classes = 345
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()
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)
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,
'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],
't_model':args.t_model,
's_model':args.s_model,
'Soft_T': args.Soft_T,
'kd_alpha': args.kd_alpha,
'learner_name':args.learner_name,
}
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.task_acc_pre = np.zeros([self.num_tasks])
self.task_acc_pro = np.zeros([self.num_tasks])
def task_eval(self, t_index, 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)
acc_avg,s_acc_avg=self.learner.validation(test_loader, task_metric=task)
return acc_avg, s_acc_avg
def train(self, args):
# Results saving
temp_table = {}
s_temp_table = {}
for mkey in self.metric_keys:
temp_table[mkey] = []
s_temp_table[mkey] = []
temp_dir = self.log_dir + '/Results/'
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] #[task_id*20:(task_id+1)*20]
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
self.learner.s_model.module.task_id = i
except:
self.learner.model.task_id = i
self.learner.s_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()
try:
if self.learner.s_model.module.prompt is not None:
self.learner.s_model.module.prompt.process_task_count()
except:
if self.learner.s_model.prompt is not None:
self.learner.s_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/task-'+self.task_names[i]+'/'
if not os.path.exists(model_save_dir): os.makedirs(model_save_dir)
self.learner.learn_batch(train_loader, self.train_dataset, model_save_dir, args, test_loader)
#save model
self.learner.save_model(model_save_dir)
# evaluate acc
acc_table = []
s_acc_table = []
self.reset_cluster_labels = True
for j in range(i+1):
t_eval, s_eval = self.task_eval(j)
acc_table.append(t_eval)
s_acc_table.append(s_eval)
temp_table['acc'].append(np.mean(np.asarray(acc_table)))
s_temp_table['acc'].append(np.mean(np.asarray(s_acc_table)))
self.task_acc_pre[i] = s_acc_table[-1]
# 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')
s_save_file = temp_dir + mkey + '_s.csv'
np.savetxt(s_save_file, np.asarray(s_temp_table[mkey]), delimiter=",", fmt='%.2f')
# Print the summary so far
if(i == (self.max_task-1)):
self.task_acc_pro = np.array(s_acc_table)
self.forget = (np.sum(self.task_acc_pre - self.task_acc_pro)) / self.num_tasks
print('===Summary of student experiment===')
print('Acc | mean:', s_temp_table['acc'][-1])
print('Forgetting:', self.forget)