-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
182 lines (163 loc) · 8.01 KB
/
train.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
# For graph learning
import torch
from torch_geometric import seed_everything
# Utility
import os
import argparse
import sys
import numpy as np
from utilities import *
from data_stream import Streaming
from torch_geometric.data import Batch
from backbones.gnn import train_node_classifier, train_node_classifier_batch, eval_node_classifier
def evaluate(args, dataset, data_stream, memory_banks, flush=True):
APs = []
AFs = []
mAPs = []
Ps = []
for i in range(args.repeat):
memory_bank = memory_banks[i]
# Initialize the performance matrix.
performace_matrix = torch.zeros(len(memory_bank), len(memory_bank))
model = get_backbone_model(dataset, data_stream, args)
cgl_model = get_cgl_model(model, data_stream, args)
tasks = cgl_model.tasks
opt = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=5e-4)
mAP = 0
for k in range(len(memory_bank)):
# train
if args.dataset_name == "products" and args.cgl_method == "joint":
max_cls = torch.unique(memory_bank[k].y)[-1]
batches = memory_bank[:k+1]
for data in batches:
data.to(args.device)
model = train_node_classifier_batch(model, batches, opt, n_epoch=args.cls_epoch, incremental_cls=(0, max_cls+1))
else:
if args.tim:
if args.batch:
replayed_graphs = memory_bank[:k+1]
else:
replayed_graphs = Batch.from_data_list(memory_bank[:k+1])
else:
if args.batch:
replayed_graphs = memory_bank[:k] + [tasks[k]]
else:
replayed_graphs = Batch.from_data_list(memory_bank[:k] + [tasks[k]])
if args.batch:
max_cls = torch.unique(memory_bank[k].y)[-1]
batches = replayed_graphs
for data in batches:
data.to(args.device)
model = train_node_classifier_batch(model, batches, opt, n_epoch=args.cls_epoch, incremental_cls=(0, max_cls+1))
else:
replayed_graphs.to(args.device, "x", "y", "adj_t")
max_cls = torch.unique(replayed_graphs.y)[-1]
model = train_node_classifier(model, replayed_graphs, opt, weight=None, n_epoch=args.cls_epoch, incremental_cls=(0, max_cls+1))
# Test the model from task 0 to task k
accs = []
AF = 0
for k_ in range(k + 1):
task_ = tasks[k_].to(args.device, "x", "y", "adj_t")
if args.IL == "classIL":
acc = eval_node_classifier(model, task_, incremental_cls=(0, max_cls+1)) * 100
else:
max_cls = torch.unique(task_.y)[-1]
acc = eval_node_classifier(model, task_, incremental_cls=(max_cls+1-data_stream.cls_per_task, max_cls+1)) * 100
accs.append(acc)
task_.to("cpu")
print(f"T{k_} {acc:.2f}", end="|", flush=flush)
performace_matrix[k, k_] = acc
AP = sum(accs) / len(accs)
mAP += AP
print(f"AP: {AP:.2f}", end=", ", flush=flush)
for t in range(k):
AF += performace_matrix[k, t] - performace_matrix[t, t]
AF = AF / k if k != 0 else AF
print(f"AF: {AF:.2f}", flush=flush)
APs.append(AP)
AFs.append(AF)
mAPs.append(mAP/(k+1))
Ps.append(performace_matrix)
print(f"AP: {np.mean(APs):.1f}±{np.std(APs, ddof=1):.1f}", flush=flush)
print(f"mAP: {np.mean(mAPs):.1f}±{np.std(mAPs, ddof=1):.1f}", flush=flush)
print(f"AF: {np.mean(AFs):.1f}±{np.std(AFs, ddof=1):.1f}", flush=flush)
return Ps
def main():
parser = argparse.ArgumentParser()
# Arguments for data.
parser.add_argument('--dataset-name', type=str, default="corafull")
parser.add_argument('--cls-per-task', type=int, default=2)
parser.add_argument('--data-dir', type=str, default="./data")
parser.add_argument('--result-path', type=str, default="./results")
# Argumnets for CGL methods.
parser.add_argument('--tim', action='store_true')
parser.add_argument('--cgl-method', type=str, default="cgm")
parser.add_argument('--cls-epoch', type=int, default=200)
parser.add_argument('--budget', type=int, default=2)
parser.add_argument('--m-update', type=str, default="all")
parser.add_argument('--cgm-args', type=str, default="{}")
parser.add_argument('--ewc-args', type=str, default="{'memory_strength': 100000.}")
parser.add_argument('--mas-args', type=str, default="{'memory_strength': 10000.}")
parser.add_argument('--gem-args', type=str, default="{'memory_strength': 0.5, 'n_memories': 20}")
parser.add_argument('--twp-args', type=str, default="{'lambda_l': 10000., 'lambda_t': 10000., 'beta': 0.01}")
parser.add_argument('--lwf-args', type=str, default="{'lambda_dist': 10., 'T': 20.}")
parser.add_argument('--IL', type=str, default="classIL")
parser.add_argument('--batch', action='store_true')
# Others
parser.add_argument('--device', type=str, default="cuda:0")
parser.add_argument('--repeat', type=int, default=1)
parser.add_argument('--seed', type=int, default=1024)
parser.add_argument('--rewrite', action='store_true')
args = parser.parse_args()
print(args)
seed_everything(args.seed)
# Get file names.
result_file_name = get_result_file_name(args)
memory_bank_file_name = os.path.join(args.result_path, "memory_bank" , result_file_name)
task_file = os.path.join(args.data_dir, "streaming", f"{args.dataset_name}.streaming")
dataset = get_dataset(args)
if os.path.exists(task_file):
data_stream = torch.load(task_file)
else:
data_stream = Streaming(args.cls_per_task, dataset)
torch.save(data_stream, task_file)
if args.cgl_method in ["bare", "ewc", "mas", "gem", "twp", "lwf"]:
APs = []
AFs = []
mAPs = []
for i in range(args.repeat):
model = get_backbone_model(dataset, data_stream, args)
cgl_model = get_cgl_model(model, data_stream, args)
AP, mAP, AF = cgl_model.observer(args.cls_epoch, args.IL)
APs.append(AP)
AFs.append(AF)
mAPs.append(mAP)
print(f"AP: {np.mean(APs):.1f}±{np.std(APs, ddof=1):.1f}", flush=True)
print(f"mAP: {np.mean(mAPs):.1f}±{np.std(mAPs, ddof=1):.1f}", flush=True)
print(f"AF: {np.mean(AFs):.1f}±{np.std(AFs, ddof=1):.1f}", flush=True)
else:
# Get memory banks.
memory_banks = []
for i in range(args.repeat):
if os.path.exists(memory_bank_file_name + f"_repeat_{i}") and not args.rewrite:
memory_bank = torch.load(memory_bank_file_name + f"_repeat_{i}")
memory_banks.append(memory_bank) # load the memory bank from the file.
else:
model = get_backbone_model(dataset, data_stream, args)
cgl_model = get_cgl_model(model, data_stream, args)
memory_bank = cgl_model.observer()
memory_banks.append(memory_bank)
torch.save(memory_bank, memory_bank_file_name + f"_repeat_{i}")
Ps = evaluate(args, dataset, data_stream, memory_banks)
if args.tim:
if args.batch:
torch.save(Ps, os.path.join(args.result_path, "performance_new", f"{result_file_name}_tim_batch.pt"))
else:
torch.save(Ps, os.path.join(args.result_path, "performance_new", f"{result_file_name}_tim.pt"))
else:
if args.batch:
torch.save(Ps, os.path.join(args.result_path, "performance_new", f"{result_file_name}_batch.pt"))
else:
torch.save(Ps, os.path.join(args.result_path, "performance_new", f"{result_file_name}.pt"))
if __name__ == '__main__':
main()