-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
333 lines (295 loc) · 11.5 KB
/
evaluate.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
import os
import argparse
import numpy as np
import torch as th
import matplotlib.pyplot as plt
import seaborn as sns
from tabulate import tabulate
from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score
import helpers
IGNORE_IN_TOTAL = ("contrast",)
def calc_metrics(labels, pred):
"""
Compute metrics.
:param labels: Label tensor
:type labels: th.Tensor
:param pred: Predictions tensor
:type pred: th.Tensor
:return: Dictionary containing calculated metrics
:rtype: dict
"""
acc, cmat = helpers.ordered_cmat(labels, pred)
metrics = {
"acc": acc,
"cmat": cmat,
"nmi": normalized_mutual_info_score(labels, pred, average_method="geometric"),
"ari": adjusted_rand_score(labels, pred),
}
return metrics
def get_log_params(net):
"""
Get the network parameters we want to log.
:param net: Model
:type net:
:return:
:rtype:
"""
params_dict = {}
weights = []
if getattr(net, "fusion", None) is not None:
with th.no_grad():
weights = net.fusion.get_weights(softmax=True)
elif hasattr(net, "attention"):
weights = net.weights
for i, w in enumerate(helpers.npy(weights)):
params_dict[f"fusion/weight_{i}"] = w
if hasattr(net, "discriminators"):
for i, discriminator in enumerate(net.discriminators):
d0, dv = helpers.npy([discriminator.d0, discriminator.dv])
params_dict[f"discriminator_{i}/d0/mean"] = d0.mean()
params_dict[f"discriminator_{i}/d0/std"] = d0.std()
params_dict[f"discriminator_{i}/dv/mean"] = dv.mean()
params_dict[f"discriminator_{i}/dv/std"] = dv.std()
return params_dict
def get_eval_data(dataset, n_eval_samples, batch_size):
"""
Create a dataloader to use for evaluation
:param dataset: Inout dataset.
:type dataset: th.utils.data.Dataset
:param n_eval_samples: Number of samples to include in the evaluation dataset. Set to None to use all available
samples.
:type n_eval_samples: int
:param batch_size: Batch size used for training.
:type batch_size: int
:return: Evaluation dataset loader
:rtype: th.utils.data.DataLoader
"""
if n_eval_samples is not None:
*views, labels = dataset.tensors
n = views[0].size(0)
idx = np.random.choice(n, min(n, n_eval_samples), replace=False)
views, labels = [v[idx] for v in views], labels[idx]
dataset = th.utils.data.TensorDataset(*views, labels)
eval_loader = th.utils.data.DataLoader(dataset, batch_size=int(batch_size), shuffle=True, num_workers=0,
drop_last=False, pin_memory=False)
return eval_loader
def batch_predict(net, eval_data, batch_size, if_train=True, if_latent=False, if_recon=False, if_softlabel=False):
"""
Compute predictions for `eval_data` in batches. Batching does not influence predictions, but it influences the loss
computations.
:param net: Model
:type net:
:param eval_data: Evaluation dataloader
:type eval_data: th.utils.data.DataLoader
:param batch_size: Batch size
:type batch_size: int
:return: Label tensor, predictions tensor, list of dicts with loss values, array containing mean and std of cluster
sizes.
:rtype:
"""
input_x = []
input_y = []
predictions = []
softlabel = []
labels = []
losses = []
cluster_sizes = []
latent_features = []
fused_features = []
hidden_features = []
totalmeanx = []
totalmeany = []
totaldispx = []
totaldispy = []
totalpix = []
totalpiy = []
net.eval()
with th.no_grad():
for i, (batch, label) in enumerate(eval_data):
pred = net(batch)[0]
latent = net(batch)[1]
mean = net(batch)[2]
disp = net(batch)[3]
pi = net(batch)[4]
fused = net(batch)[5]
hidden = net(batch)[6]
input_x.append(helpers.npy(batch[0][0]))
input_y.append(helpers.npy(batch[0][1]))
labels.append(helpers.npy(label))
softlabel.append(helpers.npy(pred))
predictions.append(helpers.npy(pred).argmax(axis=1))
latent_features.append(helpers.npy(latent))
fused_features.append(helpers.npy(fused))
hidden_features.append(helpers.npy(hidden))
totalmeanx.append(helpers.npy(mean[0]))
totalmeany.append(helpers.npy(mean[1]))
totaldispx.append(helpers.npy(disp[0]))
totaldispy.append(helpers.npy(disp[1]))
totalpix.append(helpers.npy(pi[0]))
totalpiy.append(helpers.npy(pi[1]))
# Only calculate losses for full batches
if label.size(0) == batch_size:
batch_losses = net.calc_losses(ignore_in_total=IGNORE_IN_TOTAL)
losses.append(helpers.npy(batch_losses))
cluster_sizes.append(helpers.npy(pred.sum(dim=0)))
input_x = np.concatenate(input_x, axis=0)
input_y = np.concatenate(input_y, axis=0)
labels = np.concatenate(labels, axis=0)
predictions = np.concatenate(predictions, axis=0)
softlabel = np.concatenate(softlabel, axis=0)
latent_features = np.concatenate(latent_features, axis=1)
fused_features = np.concatenate(fused_features, axis=0)
hidden_features = np.concatenate(hidden_features, axis=0)
totalmeanx = np.concatenate(totalmeanx, axis=0)
totaldispx = np.concatenate(totaldispx, axis=0)
totalpix = np.concatenate(totalpix, axis=0)
totalmeany = np.concatenate(totalmeany, axis=0)
totaldispy = np.concatenate(totaldispy, axis=0)
totalpiy = np.concatenate(totalpiy, axis=0)
if if_recon:
return input_x, input_y, totalmeanx, totalmeany, totaldispx, totaldispy, totalpix, totalpiy
if if_latent:
return labels, predictions, latent_features, fused_features, hidden_features
if if_softlabel:
return softlabel
if if_train:
net.train()
return labels, predictions, losses, np.array(cluster_sizes).sum(axis=0)
def batch_predict_nolabel(net, eval_data, if_train=True, if_latent=False, if_recon=False, if_softlabel=False):
"""
Compute predictions for `eval_data` in batches. Batching does not influence predictions, but it influences the loss
computations.
:param net: Model
:type net:
:param eval_data: Evaluation dataloader
:type eval_data: th.utils.data.DataLoader
:param batch_size: Batch size
:type batch_size: int
:return: Label tensor, predictions tensor, list of dicts with loss values, array containing mean and std of cluster
sizes.
:rtype:
"""
input_x = []
input_y = []
predictions = []
softlabel = []
losses = []
cluster_sizes = []
latent_features = []
fused_features = []
hidden_features = []
totalmeanx = []
totalmeany = []
totaldispx = []
totaldispy = []
totalpix = []
totalpiy = []
net.eval()
with th.no_grad():
for i, batch in enumerate(eval_data):
pred = net(batch)[0]
latent = net(batch)[1]
mean = net(batch)[2]
disp = net(batch)[3]
pi = net(batch)[4]
fused = net(batch)[5]
hidden = net(batch)[6]
input_x.append(helpers.npy(batch[0][0]))
input_y.append(helpers.npy(batch[0][1]))
softlabel.append(helpers.npy(pred))
predictions.append(helpers.npy(pred).argmax(axis=1))
latent_features.append(helpers.npy(latent))
fused_features.append(helpers.npy(fused))
hidden_features.append(helpers.npy(hidden))
totalmeanx.append(helpers.npy(mean[0]))
totalmeany.append(helpers.npy(mean[1]))
totaldispx.append(helpers.npy(disp[0]))
totaldispy.append(helpers.npy(disp[1]))
totalpix.append(helpers.npy(pi[0]))
totalpiy.append(helpers.npy(pi[1]))
if if_recon:
return input_x, input_y, totalmeanx, totalmeany, totaldispx, totaldispy, totalpix, totalpiy
if if_latent:
return predictions, latent_features, fused_features, hidden_features
if if_softlabel:
return softlabel
if if_train:
net.train()
return predictions, losses, np.array(cluster_sizes).sum(axis=0)
def get_logs(net, eval_data, batch_size, eval_interval, iter_losses=None, epoch=None, include_params=True):
if iter_losses is not None:
logs = helpers.add_prefix(helpers.dict_means(iter_losses), "iter_losses")
else:
logs = {}
if (epoch is None) or ((epoch % eval_interval) == 0):
labels, pred, eval_losses, cluster_sizes = batch_predict(net, eval_data, batch_size)
eval_losses = helpers.dict_means(eval_losses)
logs.update(helpers.add_prefix(eval_losses, "eval_losses"))
logs.update(helpers.add_prefix(calc_metrics(labels, pred), "metrics"))
logs.update(helpers.add_prefix({"mean": cluster_sizes.mean(), "sd": cluster_sizes.std()}, "cluster_size"))
if include_params:
logs.update(helpers.add_prefix(get_log_params(net), "params"))
if epoch is not None:
logs["epoch"] = epoch
return logs
def eval_run(cfg, cfg_name, experiment_identifier, run, net, eval_data, callbacks=tuple(), load_best=True):
"""
Evaluate a training run.
:param cfg: Experiment config
:type cfg: config.defaults.Experiment
:param cfg_name: Config name
:type cfg_name: str
:param experiment_identifier: 8-character unique identifier for the current experiment
:type experiment_identifier: str
:param run: Run to evaluate
:type run: int
:param net: Model
:type net:
:param eval_data: Evaluation dataloder
:type eval_data: th.utils.data.DataLoader
:param callbacks: List of callbacks to call after evaluation
:type callbacks: List
:param load_best: Load the "best.pt" model before evaluation?
:type load_best: bool
:return: Evaluation logs
:rtype: dict
"""
if load_best:
model_path = helpers.get_save_dir(cfg_name, experiment_identifier, run) / "best.pt"
if os.path.isfile(model_path):
net.load_state_dict(th.load(model_path))
else:
print(f"Unable to load best model for evaluation. Model file not found: {model_path}")
logs = get_logs(cfg, net, eval_data, include_params=True)
for cb in callbacks:
cb.at_eval(net=net, logs=logs)
return logs
def plot_projection(X, method, hue, ax, title=None, cmap="tab10", legend_title=None, legend_loc=1, **kwargs):
X = project(X, method)
pl = sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=hue, ax=ax, legend="full", palette=cmap, **kwargs)
leg = pl.get_legend()
leg._loc = legend_loc
if title is not None:
ax.set_title(title)
if legend_title is not None:
leg.set_title(legend_title)
def project(X, method):
if method == "pca":
from sklearn.decomposition import PCA
return PCA(n_components=2).fit_transform(X)
elif method == "tsne":
from sklearn.manifold import TSNE
return TSNE(n_components=2).fit_transform(X)
elif method is None:
return X
else:
raise RuntimeError()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", dest="cfg_name", required=True)
parser.add_argument("-t", "--tag", dest="tag", required=True)
parser.add_argument("--plot", action="store_true")
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
eval_experiment(args.cfg_name, args.tag, args.plot)