-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_HScore.py
267 lines (199 loc) · 9.1 KB
/
test_HScore.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
import gc
import glob
import os
from pprint import pformat
from typing import Any
from ignite.engine import create_supervised_evaluator
import yaml
from data import setup_data
from ignite.engine import Events
from ignite.metrics import Accuracy, Loss
from ignite.utils import manual_seed
from models import setup_model
from torch import nn, optim
import torch.functional as F
from trainers import setup_evaluator, setup_trainer
from utils import *
from tqdm import tqdm
import hydra
from omegaconf import DictConfig, OmegaConf
def get_model(config, domain, logger):
checkpoint_root = os.path.join("./checkpoints", config.dataset.name)
model = setup_model(config, return_feat_only=True)
candidate_checkpoint = glob.glob(
os.path.join(checkpoint_root, domain, "*.pt")
)
assert len(candidate_checkpoint) == 1
checkpoint_path = candidate_checkpoint[0]
to_save_eval = {"model": model}
resume_from(to_save_eval, checkpoint_path, logger)
if logger is not None:
logger.info(f"checkpoint loaded from {checkpoint_path}")
return model
def normalize(features):
# return (features - features.mean(axis=0))
return (features - features.mean(axis=0)) / features.std(axis=0)
def run(config: Any):
# make a certain seed
manual_seed(config.seed)
logger = logging.getLogger()
dataloader_train, dataloader_test = setup_data(
config, is_test=True, few_shot_num=config.few_shot_num
)
num_classes = config.dataset.num_classes
domains = config.dataset.domains
target_domain = config.dataset.domain
target_domain_index = -1
for i, d in enumerate(domains):
if d == target_domain:
target_domain_index = i
break
if target_domain_index != -1:
del domains[target_domain_index]
# domains = ["v_task2", "v_task1"]
# source_domains = [i for i in config.dataset.domains if i != config.dataset.domain]
all_features_train = torch.zeros((len(domains), len(dataloader_train.dataset), config.model.hidden_dim))
all_label_train = None
print(f"target domain: {config.dataset.domain}")
with torch.no_grad():
# save labels & features
for i, d in enumerate(domains):
print(f"domain: {d}")
model = get_model(config, d, logger).cuda().eval()
print(f"extract features by {d}-trained model")
features_list = []
labels_list = []
for data in tqdm(dataloader_train):
# get the inputs
inputs, labels = data
inputs = inputs.cuda()
# labels = labels.cuda()
features_list.append(normalize(model(inputs).detach().cpu()))
if all_label_train is None:
labels_list.append(labels.detach().cpu())
all_features_train[i, :, :] = torch.cat(features_list, dim=0)
if all_label_train is None:
all_label_train = torch.cat(labels_list, dim=0)
# stop with torch.no_grad()
del inputs, labels, data
del dataloader_train
del model
del features_list, labels_list
# compute H-score and validation accuracy
print(f"\n\n*******start calc H-Score*******")
def get_target_feature(alpha, all_features):
# target_feature: sum of all features weighted by alpha
target_feature = torch.zeros_like(all_features[0, :, :])
for i in range(len(domains)):
target_feature += alpha[i] * all_features[i, :, :]
return target_feature
def get_target_feature_train(alpha, all_features):
target_feature = torch.zeros_like(all_features[0, :, :])
for i in range(len(domains) - 1):
target_feature += alpha[i] * all_features[i, :, :]
i = len(domains) - 1
target_feature += (1 - alpha.sum()) * all_features[i, :, :]
return target_feature
# print("original H-score: dif = torch.trace(torch.pinverse(Covf, rcond=1e-15) @ Covg)")
def get_score(features, labels):
Covf = torch.cov(features.T) # (hidden_dim, hidden_dim)
label_choice = torch.unique(labels)
g = torch.zeros_like(features)
for z in label_choice:
fl = features[labels == z, :]
Ef_z = torch.mean(fl, dim=0) # (hidden_dim)
g[labels == z] = Ef_z
Covg = torch.cov(g.T)
dif = torch.trace(Covg) / torch.trace(Covf)
# orignal hscore
# dif = torch.trace(torch.pinverse(Covf, rcond=1e-15) @ Covg)
return dif
alpha = torch.ones(len(domains) - 1) / len(domains)
# alpha = torch.ones(len(domains)) / len(domains)
# # insert 0 to alpha at target_domain_index
# alpha = torch.cat([alpha[:target_domain_index], torch.zeros(1), alpha[target_domain_index:]], dim=0)
alpha.requires_grad = True
all_features_train.requires_grad = False
all_label_train.requires_grad = False
optimizer = optim.SGD([alpha], lr=0.005)
# optimizer = optim.AdamW([alpha], lr=0.005)
for epoch in range(1000):
optimizer.zero_grad()
target_feature = get_target_feature_train(alpha, all_features_train)
h_score = -get_score(target_feature, all_label_train)
h_score.backward()
optimizer.step()
# print(f"epoch {epoch}: h_score {h_score.item()}")
# alpha.requires_grad = False
# alpha[alpha < 0] = 0
# alpha = alpha / alpha.sum() if alpha.sum() > 1 else alpha
# alpha.requires_grad = True
alpha.requires_grad = False
alpha = torch.cat([alpha, (1 - alpha.sum()).view(-1)], dim=0)
print(f"final alpha: {alpha}")
print(f"\n\n*******get G*******")
with torch.no_grad():
target_feature = get_target_feature(alpha, all_features_train)
# target_feature = normalize(target_feature) # already done in feature_list
# gamma_f = target_feature.T@target_feature / target_feature.shape[0] # (hidden_dim, hidden_dim)
def get_conditional_exp(feature, label, num_classes):
"calculate conditional expectation of fx"
ce_f = torch.zeros((num_classes, feature.shape[1]))
for i in range(num_classes):
fx_i = feature[torch.where(label==i)] - feature.mean(0)
ce_f[i] = fx_i.mean(axis=0)
return ce_f
ce_f = get_conditional_exp( label=all_label_train, feature=target_feature, num_classes=num_classes ) # (num_classes, hidden_dim)
# torch.permute = np.transpose ; torch.transpose = np.swapaxes; torch.mm = np.dot ; torch.inverse = np.linalg.inv;
# g = (torch.inverse(gamma_f) @ (ce_f_s.permute((1,2,0))@alpha).T).T
# g = ( torch.inverse(gamma_f) @ ce_f.T ).T # (hidden_dim, num_classes).T
g = ce_f
del all_features_train,
torch.cuda.empty_cache()
gc.collect()
print(f"\n\n*******start test on {config.dataset.domain}*******")
with torch.no_grad():
score = target_feature @ g.T
acc_test = (torch.argmax(score, dim=1) == all_label_train).sum().item() / len(all_label_train)
print("Train accuracy: ", acc_test)
del score, target_feature, all_label_train
# test_features = torch.zeros(len(dataloader_test.dataset), config.model.hidden_dim).cuda()
features_list = []
labels_list = []
acc_test = 0
for data in tqdm(dataloader_test):
inputs, labels = data
inputs = inputs.cuda()
labels_list.append(labels.detach().cpu())
features_list.append(torch.zeros(inputs.shape[0], config.model.hidden_dim).cuda())
for i, d in enumerate(domains):
model = get_model(config, d, None).cuda().eval()
features = model(inputs).detach()
features = normalize(features)
features_list[-1] += features * alpha[i]
test_features = torch.cat(features_list, dim=0)
test_label = torch.cat(labels_list, dim=0)
del model, features_list, labels_list, features, labels, data, inputs
# g_norm = normalize(g.cuda())
g_norm = g.cuda()
f_norm = normalize(test_features.cuda())
del test_features,
score = f_norm @ g_norm.T
print("Correct num: ", (torch.argmax(score, dim=1) == test_label.cuda()).sum().cpu().item())
print("Incorrect num: ", (torch.argmax(score, dim=1) != test_label.cuda()).sum().cpu().item())
acc_test = (torch.argmax(score, dim=1) == test_label.cuda()).sum().cpu().item() / len(dataloader_test.dataset)
print(f"Test accuracy: {acc_test} ; test sample: {len(dataloader_test.dataset)}")
print(f"*******done {config.dataset.domain} of {domains}*******")
print("#########################################################\n\n\n")
# main entrypoint
@hydra.main(version_base=None, config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
# with idist.Parallel() as p:
# # with idist.Parallel("gloo") as p:
# p.run(run, config=cfg)
run(cfg)
if __name__ == "__main__":
# CUBLAS_WORKSPACE_CONFIG=:4096:8
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
main()