forked from clovaai/rainbow-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (168 loc) · 6.73 KB
/
main.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
"""
rainbow-memory
Copyright 2021-present NAVER Corp.
GPLv3
"""
import logging.config
import os
import random
from collections import defaultdict
import numpy as np
import torch
from randaugment import RandAugment
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from configuration import config
from utils.augment import Cutout, select_autoaugment
from utils.data_loader import get_test_datalist, get_statistics
from utils.data_loader import get_train_datalist
from utils.method_manager import select_method
def main():
args = config.base_parser()
# Save file name
tr_names = ""
for trans in args.transforms:
tr_names += "_" + trans
save_path = f"{args.dataset}/{args.mode}_{args.mem_manage}_{args.stream_env}_msz{args.memory_size}_rnd{args.rnd_seed}{tr_names}"
logging.config.fileConfig("./configuration/logging.conf")
logger = logging.getLogger()
os.makedirs(f"logs/{args.dataset}", exist_ok=True)
fileHandler = logging.FileHandler("logs/{}.log".format(save_path), mode="w")
formatter = logging.Formatter(
"[%(levelname)s] %(filename)s:%(lineno)d > %(message)s"
)
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
writer = SummaryWriter("tensorboard")
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
logger.info(f"Set the device ({device})")
# Fix the random seeds
# https://hoya012.github.io/blog/reproducible_pytorch/
torch.manual_seed(args.rnd_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(args.rnd_seed)
random.seed(args.rnd_seed)
# Transform Definition
mean, std, n_classes, inp_size, _ = get_statistics(dataset=args.dataset)
train_transform = []
if "cutout" in args.transforms:
train_transform.append(Cutout(size=16))
if "randaug" in args.transforms:
train_transform.append(RandAugment())
if "autoaug" in args.transforms:
train_transform.append(select_autoaugment(args.dataset))
train_transform = transforms.Compose(
[
transforms.Resize((inp_size, inp_size)),
transforms.RandomCrop(inp_size, padding=4),
transforms.RandomHorizontalFlip(),
*train_transform,
transforms.ToTensor(),
transforms.Normalize(mean, std),
]
)
logger.info(f"Using train-transforms {train_transform}")
test_transform = transforms.Compose(
[
transforms.Resize((inp_size, inp_size)),
transforms.ToTensor(),
transforms.Normalize(mean, std),
]
)
logger.info(f"[1] Select a CIL method ({args.mode})")
criterion = nn.CrossEntropyLoss(reduction="mean")
method = select_method(
args, criterion, device, train_transform, test_transform, n_classes
)
logger.info(f"[2] Incrementally training {args.n_tasks} tasks")
task_records = defaultdict(list)
for cur_iter in range(args.n_tasks):
if args.mode == "joint" and cur_iter > 0:
return
print("\n" + "#" * 50)
print(f"# Task {cur_iter} iteration")
print("#" * 50 + "\n")
logger.info("[2-1] Prepare a datalist for the current task")
task_acc = 0.0
eval_dict = dict()
# get datalist
cur_train_datalist = get_train_datalist(args, cur_iter)
cur_test_datalist = get_test_datalist(args, args.exp_name, cur_iter)
# Reduce datalist in Debug mode
if args.debug:
random.shuffle(cur_train_datalist)
random.shuffle(cur_test_datalist)
cur_train_datalist = cur_train_datalist[:2560]
cur_test_datalist = cur_test_datalist[:2560]
logger.info("[2-2] Set environment for the current task")
method.set_current_dataset(cur_train_datalist, cur_test_datalist)
# Increment known class for current task iteration.
method.before_task(cur_train_datalist, cur_iter, args.init_model, args.init_opt)
# The way to handle streamed samles
logger.info(f"[2-3] Start to train under {args.stream_env}")
if args.stream_env == "offline" or args.mode == "joint" or args.mode == "gdumb":
# Offline Train
task_acc, eval_dict = method.train(
cur_iter=cur_iter,
n_epoch=args.n_epoch,
batch_size=args.batchsize,
n_worker=args.n_worker,
)
if args.mode == "joint":
logger.info(f"joint accuracy: {task_acc}")
elif args.stream_env == "online":
# Online Train
logger.info("Train over streamed data once")
method.train(
cur_iter=cur_iter,
n_epoch=1,
batch_size=args.batchsize,
n_worker=args.n_worker,
)
method.update_memory(cur_iter)
# No stremed training data, train with only memory_list
method.set_current_dataset([], cur_test_datalist)
logger.info("Train over memory")
task_acc, eval_dict = method.train(
cur_iter=cur_iter,
n_epoch=args.n_epoch,
batch_size=args.batchsize,
n_worker=args.n_worker,
)
method.after_task(cur_iter)
logger.info("[2-4] Update the information for the current task")
method.after_task(cur_iter)
task_records["task_acc"].append(task_acc)
# task_records['cls_acc'][k][j] = break down j-class accuracy from 'task_acc'
task_records["cls_acc"].append(eval_dict["cls_acc"])
# Notify to NSML
logger.info("[2-5] Report task result")
writer.add_scalar("Metrics/TaskAcc", task_acc, cur_iter)
np.save(f"results/{save_path}.npy", task_records["task_acc"])
# Accuracy (A)
A_avg = np.mean(task_records["task_acc"])
A_last = task_records["task_acc"][args.n_tasks - 1]
# Forgetting (F)
acc_arr = np.array(task_records["cls_acc"])
# cls_acc = (k, j), acc for j at k
cls_acc = acc_arr.reshape(-1, args.n_cls_a_task).mean(1).reshape(args.n_tasks, -1)
for k in range(args.n_tasks):
forget_k = []
for j in range(args.n_tasks):
if j < k:
forget_k.append(cls_acc[:k, j].max() - cls_acc[k, j])
else:
forget_k.append(None)
task_records["forget"].append(forget_k)
F_last = np.mean(task_records["forget"][-1][:-1])
# Intrasigence (I)
I_last = args.joint_acc - A_last
logger.info(f"======== Summary =======")
logger.info(f"A_last {A_last} | A_avg {A_avg} | F_last {F_last} | I_last {I_last}")
if __name__ == "__main__":
main()