-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathincremental_subnet_procedure_cub.py
394 lines (323 loc) · 15.8 KB
/
incremental_subnet_procedure_cub.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
# coding=utf-8
from __future__ import absolute_import, print_function
import argparse
import datetime
import logging
import math
import random
import os
import time
import torch
from utils import get_time_str
from os import path as osp
import numpy as np
from copy import deepcopy, copy
from data import create_dataloader, create_dataset, create_sampler
from methods import create_model
from utils.options import dict2str, parse
from utils import (MessageLogger, get_env_info, get_root_logger,
init_tb_logger, init_wandb_logger, check_resume,
make_exp_dirs, set_random_seed, set_gpu, Averager,
safe_load, safe_save)
from dataloader.data_utils import *
def generate_training_dataset(opt, task_id, test_id):
random_class_perm = opt['class_permutation']
total_classes = opt['datasets']['train']['total_classes']
Random = opt['Random']
seed = opt['manual_seed']
bases = opt['train']['bases']
num_tasks = opt['train']['tasks']
num_shots = opt['train']['shots']
num_class_per_task = int((total_classes - bases) / (num_tasks - 1))
dataset_opt = opt['datasets']['train']
dataset_opt['all_classes'] = random_class_perm
train_set = None
if opt['train']['novel_exemplars'] > 0:
for i in range(1, task_id+1):
dataset_opt['task_id'] = i
selected_classes = random_class_perm[bases + (i - 1) * num_class_per_task:
bases + i * num_class_per_task]
dataset_opt['selected_classes'] = selected_classes
train_set_novel = create_dataset(dataset_opt)
session_path_root, _ = os.path.split(dataset_opt['dataroot'])
index_root = osp.join(session_path_root,
f'Random{Random}_seed{seed}_bases{bases}_tasks{num_tasks}_shots{num_shots}',
f'test_{test_id}', f'session_{i}', 'index.pt')
index = torch.load(index_root)
train_set_novel.sample_the_buffer_data_with_index(index)
if i < task_id:
train_set_novel.sample_the_buffer_data(opt['train']['novel_exemplars'])
if train_set is not None:
train_set.combine_another_dataset(train_set_novel)
else:
train_set = train_set_novel
else:
selected_classes = random_class_perm[bases + (task_id-1) * num_class_per_task:
bases + task_id * num_class_per_task]
dataset_opt['selected_classes'] = selected_classes
train_set = create_dataset(dataset_opt)
session_path_root, _ = os.path.split(dataset_opt['dataroot'])
index_root = osp.join(session_path_root,
f'bases{bases}_tasks{num_tasks}_shots{num_shots}',
f'test_{test_id}', f'session_{task_id}', 'index.pt')
index = torch.load(index_root)
train_set.sample_the_buffer_data_with_index(index)
sampler_opt = dataset_opt['sampler']
if sampler_opt.get('num_classes', None) is None:
sampler_opt['num_classes'] = task_id * num_class_per_task
dataset_opt['batch_size'] = len(train_set)
# dataset_opt['batch_size'] = 100
train_sampler = create_sampler(train_set, sampler_opt)
train_loader = create_dataloader(
train_set,
dataset_opt,
sampler=train_sampler,
seed=opt['manual_seed'])
return train_set, train_loader
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-opt',type=str, required=True, help='Path to option YAML file.')
parser.add_argument('-dataset', type=str, default='cub200',
choices=['mini_imagenet', 'cub200', 'cifar100'])
parser.add_argument('-dataroot', type=str, default='DataSet/')
parser.add_argument('--base_class', type=int, default=100)
parser.add_argument('--num_classes', type=int, default=200)
parser.add_argument('--way', type=int, default=10)
parser.add_argument('--shot', type=int, default=5)
parser.add_argument('--sessions', type=int, default=11)
parser.add_argument('--batch_size_base', type=int, default=256)
parser.add_argument('--batch_size_new', type=int, default=0)
parser.add_argument('--test_batch_size', type=int, default=100)
parser.add_argument('--num_workers', type=int, default=8)
parser.add_argument('--autoaug', type=int, default=0)
parser.add_argument('--train', type=bool, default=True)
parser.add_argument('--epoch', type=int, default=10)
parser.add_argument('--tau_idx', type=int, default=2)
parser.add_argument('--met_w', type=float, default=1.0)
args = parser.parse_args()
opt = parse(args.opt, is_train=False, is_incremental=True, args=None)
# split-FSLL for base session
args = set_up_datasets(args)
args.autoaug=0
args.train=False
train_set, train_loader, val_set, val_loader = get_base_dataloader(args)
rank = 0
opt['rank'] = 0
opt['world_size'] = 1
make_exp_dirs(opt)
log_file = osp.join(opt['path']['log'],
f"incremental_{opt['name']}_{get_time_str()}.log")
logger = get_root_logger(
logger_name='FS-IL', log_level=logging.INFO, log_file=log_file)
logger.info(get_env_info())
logger.info(dict2str(opt))
# initialize tensorboard logger and wandb logger
tb_logger = None
if opt['logger'].get('use_tb_logger') and 'debug' not in opt['name']:
log_dir = './tb_logger_{}/'.format(opt['datasets']['train']['name']) + opt['name']
tb_logger = init_tb_logger(log_dir=log_dir)
if (opt['logger'].get('wandb')
is not None) and (opt['logger']['wandb'].get('project')
is not None) and ('debug' not in opt['name']):
assert opt['logger'].get('use_tb_logger') is True, (
'should turn on tensorboard when using wandb')
wandb_logger = init_wandb_logger(opt)
else:
wandb_logger = None
opt['wandb_logger'] = wandb_logger
# set random seed
seed = opt['manual_seed']
if seed is None:
seed = random.randint(1, 10000)
opt['manual_seed'] = seed
logger.info(f'Random seed: {seed}')
set_random_seed(seed + rank)
torch.set_num_threads(1)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
# define the variables for incremental few-shot learning
total_classes = opt['datasets']['train']['total_classes']
bases = opt['train']['bases']
num_tasks = opt['train']['tasks']
num_shots = opt['train']['shots']
# ---------------------
fine_tune_epoch = opt['train'].get('fine_tune_epoch', None)
num_class_per_task = int((total_classes - bases) / (num_tasks - 1))
opt['train']['num_class_per_task'] = num_class_per_task
if opt.get('Random', True):
random_class_perm = np.random.permutation(total_classes)
else:
random_class_perm = np.arange(total_classes)
# randomly generate the sorting of categories
opt['class_permutation'] = random_class_perm
# deep copy the opt
try:
opt_old = deepcopy(opt)
except:
opt_old = copy(opt)
# Test the session 1 and save the prototypes
opt['task_id'] = -1
opt['test_id'] = 0
model = create_model(opt)
opt['task_id'] = 0
val_classes = random_class_perm[:bases]
selected_classes = random_class_perm[:bases]
model.incremental_init(train_set, val_set)
if opt['path'].get('pretrain_prototypes', None) is None:
model.incremental_update(novel_dataset=train_set)
if opt.get('Test1', True):
if opt.get('details', False):
acc, acc_former_ave, acc_former_all_ave, acc_novel_all_ave = model.incremental_test(val_set, 0, 0)
else:
if opt.get('nondist', False):
train_set.set_aug(False)
acc = model.validation(train_set, val_loader, 0, tb_logger, mask=None)
else:
acc = model.incremental_test(val_set, 0, 0)
else:
acc = 0
if opt['path'].get('pretrain_prototypes', None) is None:
pt_path, _ = os.path.split(opt['path']['base_model'])
pt_path = osp.join(pt_path, 'pretrain_prototypes.pt')
torch.save(model.prototypes_dict, pt_path)
model.save(epoch=-1, current_iter=0, name=f'test{0}_session', dataset=train_set)
num_tests = opt['train']['num_test']
acc_avg = [Averager() for i in range(num_tasks)]
acc_former_ave_avg = [Averager() for i in range(num_tasks)]
acc_novel_all_ave_avg = [Averager() for i in range(num_tasks)]
acc_former_all_ave_avg = [Averager() for i in range(num_tasks)]
acc_avg[0].add(acc)
if opt.get('details', False):
acc_former_ave_avg[0].add(acc_former_ave)
acc_novel_all_ave_avg[0].add(acc_novel_all_ave)
acc_former_all_ave_avg[0].add(acc_former_all_ave)
print('*'*60)
print("task_id:{}, acc:{}".format(0, acc))
if wandb_logger is not None:
task_id = 0
wandb_logger.log({f'sessions_acc': acc}, step=task_id)
logger.info(f'sessions{task_id}_acc:{acc}')
if opt.get('details', False):
wandb_logger.log({f'sessions_former_acc': acc_former_ave}, step=task_id)
wandb_logger.log({f'sessions_former_all_acc': acc_former_all_ave}, step=task_id)
wandb_logger.log({f'sessions_novel_all_acc': acc_novel_all_ave}, step=task_id)
logger.info(f'sessions{task_id}_former_acc:{acc_former_ave}')
logger.info(f'sessions{task_id}_former_all_acc:{acc_former_all_ave}')
logger.info(f'sessions{task_id}_novel_all_acc:{acc_novel_all_ave}')
print('*'*60)
per_task_masks = {}
for test_id in range(num_tests):
for task_id in range(1, num_tasks):
# initialize per task_masks
per_task_masks[task_id] = None
try:
opt = deepcopy(opt_old)
except:
opt = copy(opt_old)
opt['test_id'] = test_id
# Load the model of former session
# 'task_id = -1' indicates that the program will not load the prototypes, and just load the base model
opt['task_id'] = task_id - 1
# The path of model that is updated on former task
if task_id == 1:
save_filename_g = f'test{0}_session_{task_id - 1}.pth'
else:
save_filename_g = f'test{test_id}_session_{task_id-1}.pth'
# save_filename_g = f'test{0}_session_{0}.pth'
save_path_g = osp.join(opt['path']['models'], save_filename_g)
opt['path']['base_model'] = save_path_g
opt['train']['novel_examplers'] = 0
#-----------------------------------------------
model = create_model(opt)
opt['task_id'] = task_id
val_classes = random_class_perm[:bases + task_id * num_class_per_task]
# creating the dataset
# --------------------------------------------
args.autoaug=1
args.train=True
train_set, train_loader, val_set, val_loader = get_new_dataloader(
args, task_id)
# --------------------------------------------
# finetune
model.incremental_init(train_set, val_set)
if opt['subnet_type'] == 'softnet':
model.incremental_fine_tune(train_loader,
val_dataset=val_set,
num_epoch=fine_tune_epoch,
task_id=task_id,
test_id=test_id,
tb_logger=None,
mask=None)
logger.info('fine-tune procedure is finished!')
# get model masks
per_task_masks[task_id] = model.get_masks(
sess_id=task_id, # get updated mask
sparsity=opt['new_sparsity'],
layer=opt['finetune_layer'])
del train_set
del train_loader
del val_set
del val_loader
args.autoaug=0
args.train=False
train_set, train_loader, val_set, val_loader = get_new_dataloader(
args, task_id)
model.incremental_update(novel_dataset=train_set,mask=per_task_masks[task_id])
if opt.get('details', False):
acc, acc_former_ave, acc_former_all_ave, acc_novel_all_ave = model.incremental_test(val_set, task_id, test_id)
acc_former_ave_avg[task_id].add(acc_former_ave)
acc_novel_all_ave_avg[task_id].add(acc_novel_all_ave)
acc_former_all_ave_avg[task_id].add(acc_former_all_ave)
else:
if opt.get('nondist', False):
#train_set.set_aug(False)
acc = model.validation(train_set, val_loader,
task_id, tb_logger,
mask=per_task_masks[task_id])
else:
acc = model.incremental_test(val_set,
task_id,
test_id,
mask=per_task_masks[task_id])
print('task_id:{}, acc:{}'.format(task_id, acc))
# save the accuracy
acc_avg[task_id].add(acc)
model.save(epoch=-1, current_iter=task_id, name=f'test{test_id}_session',
dataset=train_set, mask=per_task_masks, task_id=task_id)
# # reset the opt for creating the model in the next session
if wandb_logger is not None:
wandb_logger.log({f'sessions_acc': acc}, step=task_id)
logger.info(f'sessions{task_id}_acc:{acc}')
if opt.get('details', False):
wandb_logger.log({f'sessions_former_acc': acc_former_ave}, step=task_id)
wandb_logger.log({f'sessions_former_all_acc': acc_former_all_ave}, step=task_id)
wandb_logger.log({f'sessions_novel_all_acc': acc_novel_all_ave}, step=task_id)
logger.info(f'sessions{task_id}_former_acc:{acc_former_ave}')
logger.info(f'sessions{task_id}_former_all_acc:{acc_former_all_ave}')
logger.info(f'sessions{task_id}_novel_all_acc:{acc_novel_all_ave}')
print('*'*60)
message = f'--------------------------Final Avg Acc-------------------------'
logger.info(message)
for i, acc in enumerate(acc_avg):
data = acc.obtain_data()
m = np.mean(data)
std = np.std(data)
pm = 1.96 * (std / np.sqrt(len(data)))
if opt.get('details', False):
message = f'Session {i+1}: {m*100:.2f}+-{pm*100:.2f}' \
f'[acc of former classes: {acc_former_ave_avg[i].item():.4f}]' \
f'[acc of former samples in all classes: {acc_former_all_ave_avg[i].item():.4f}]\n' \
f'[acc of novel samples in all classes: {acc_novel_all_ave_avg[i].item():.4f}]'
else:
message = f'Session {i + 1}: {m * 100:.2f}+-{pm * 100:.2f}'
logger.info(message)
if tb_logger:
tb_logger.add_scalar(f'sessions_acc', acc.item(), i)
if wandb_logger is not None:
wandb_logger.log({f'sessions_acc': acc.item()}, step=i)
logger.info(f'random seed: {seed}')
print('finish!!')
print(opt)
if __name__ == '__main__':
main()