-
Notifications
You must be signed in to change notification settings - Fork 16
/
incremental_topic_signet_procedure.py
314 lines (265 loc) · 13.7 KB
/
incremental_topic_signet_procedure.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
# 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 dataloader_alice.data_utils import * # topic split
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)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-opt',type=str, required=True, help='Path to option YAML file.')
# for alice, topic
parser.add_argument('--batch_size', type=int, default=512, help='batch_size')
parser.add_argument('--batch_size_new', type=int, default=0, help='set 0 will use all the availiable training image for new')
parser.add_argument('--workers', type=int, default=8, help='num of workers to use')
parser.add_argument('--data_transform', action='store_true', help='Whether use 2 set of transformed data per input image to do calculation.')
parser.add_argument('--data_root', type=str, help='path to dataset directory')
parser.add_argument('--dataset', type=str, default='cifar100', choices=['mini_imagenet', 'cub200', 'cifar100'])
parser.add_argument('--current_session', default=0, type=int)
parser.add_argument('--used_img', default=500, type=int) # 500, 5, 1
parser.add_argument('--balanced', default=0, type=int)
parser.add_argument('--world-size', default=-1, type=int,
help='number of nodes for distributed training')
parser.add_argument('--multiprocessing-distributed', action='store_true',
help='Use multi-processing distributed training to launch '
'N processes per node, which has N GPUs. This is the '
'fastest way to use PyTorch for either single node or'
'multi node data parallel training')
args = parser.parse_args()
opt = parse(args.opt, is_train=False, is_incremental=True)
assert opt['datasets']['train']['name'] in ['cifar100', 'cub200', 'mini_imagenet']
args.dataset = opt['datasets']['train']['name']
args.data_root = opt['datasets']['train']['dataroot']
args.used_img = opt['train']['shots']
args = set_up_datasets(args)
args.distributed = args.world_size > 1 or args.multiprocessing_distributed
print(vars(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]
for phase, dataset_opt in opt['datasets'].items():
# load training data for topic split
dataset_opt['task_id'] = 0
args.current_session = dataset_opt['task_id']
train_set, train_loader, val_set, val_loader = get_incremental_dataset_fs(args, session=args.current_session)
print('length of the trainset: {0}'.format(len(train_set)))
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)
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)
print(opt['name'])
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
#-----------------------------------------------
model = create_model(opt)
opt['task_id'] = task_id
val_classes = random_class_perm[:bases + task_id * num_class_per_task]
# creating the dataset
# --------------------------------------------
#for phase, dataset_opt in opt['datasets'].items():
# topic, alice split
args.current_session = task_id
train_set, train_loader, val_set, val_loader = get_incremental_dataset_fs(args, session=args.current_session)
print('length of the trainset: {0}'.format(len(train_set)))
# --------------------------------------------
# finetune
model.incremental_init(train_set, val_set)
assert opt['subnet_type'] in ['hardnet', 'softnet']
if opt['subnet_type'] == 'softnet' and opt['task_id'] > 0:
model.incremental_fine_tune(train_dataset=train_set,
train_loader=train_loader,
val_dataset=val_set,
val_loader=val_loader,
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] = None
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(flag=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
# del opt
# opt = deepcopy(opt_old)
# # update the path of saving models
# # model.set_the_saving_files_path(opt=opt, task_id=task_id, test_id=test_id)
# # logger.info(f'Successfully saving the model of session {task_id}')
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()