-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.py
278 lines (205 loc) · 12.6 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
import os
from itertools import product
import matplotlib.pyplot as plt
from typing import Any
import math
import hydra
import torch
import torchvision
from pytorch_lightning.lite import LightningLite
from PIL import Image
from omegaconf import OmegaConf, DictConfig
import numpy as np
from pyeer.eer_info import get_eer_stats
from pyeer.report import generate_eer_report
from utils.helpers import ensure_path_join, normalize_to_neg_one_to_one
import sys
from utils.iresnet import iresnet100
sys.path.insert(0, 'IDiff-Face/')
class EvaluatorLite(LightningLite):
def run(self, cfg) -> Any:
self.seed_everything(cfg.evaluation.seed)
eer_stats = {}
for model_name in cfg.evaluation.model_names:
for frm_name in cfg.evaluation.frm_names:
synthetic_context_short = cfg.evaluation.synthetic_contexts_name.replace("random_", "").replace("_5000", "")
if cfg.evaluation.aligned:
eval_dir = ensure_path_join("evaluation", f"ffhq_aligned_{synthetic_context_short}", model_name, frm_name)
synthetic_preencoded_data_dir = os.path.join("samples", "aligned", "embeddings", model_name,
cfg.evaluation.synthetic_contexts_name, frm_name)
authentic_preencoded_data_dir = os.path.join("samples", "aligned", "embeddings", model_name,
cfg.evaluation.authentic_contexts_name, frm_name)
else:
eval_dir = ensure_path_join("evaluation", f"ffhq_{synthetic_context_short}", model_name, frm_name)
synthetic_preencoded_data_dir = os.path.join("samples", "embeddings", model_name,
cfg.evaluation.synthetic_contexts_name, frm_name)
authentic_preencoded_data_dir = os.path.join("samples", "embeddings", model_name,
cfg.evaluation.authentic_contexts_name, frm_name)
synthetic_embeddings = torch.load(os.path.join(synthetic_preencoded_data_dir, "embeddings.npy"))
synthetic_labels = torch.load(os.path.join(synthetic_preencoded_data_dir, "labels.npy"))
authentic_embeddings = torch.load(os.path.join(authentic_preencoded_data_dir, "embeddings.npy"))
authentic_labels = torch.load(os.path.join(authentic_preencoded_data_dir, "labels.npy"))
# load real data embeddings
real_contexts = cfg.evaluation.real_contexts.get(frm_name)
print("Real Contexts:", real_contexts)
real_embeddings_dict = torch.load(real_contexts.real_contexts_path if not cfg.evaluation.aligned else real_contexts.real_contexts_aligned_path)
real_labels = list(real_embeddings_dict.keys())
real_embeddings = [real_embeddings_dict[label] for label in real_labels]
if cfg.evaluation.authentic_real_comparison:
print("Starting REAL vs. VARIATIONS comparison ...")
genuine_scores, imposter_scores = [], []
#for i, j in self.generate_genuine_pairs(authentic_labels, real_labels):
# e1, e2 = authentic_embeddings[i], real_embeddings[j]
# cos_sim = np.dot(e1, e2)
# genuine_scores.append(cos_sim)
#for i, j in self.generate_random_imposter_pairs(authentic_labels, real_labels, n=len(genuine_scores)):
# # TODO: save imposter pairing
# e1, e2 = authentic_embeddings[i], real_embeddings[j]
# cos_sim = np.dot(e1, e2)
# imposter_scores.append(cos_sim)
comparison_scores = []
for i, e1 in enumerate(synthetic_embeddings[::16][:1000]):
print(i)
class_sim = []
for e2 in real_embeddings:
cos_sim = np.dot(e1, e2)
class_sim.append(cos_sim)
comparison_scores.append(np.sort(np.array(class_sim))[-1])
#plt.clf()
#plt.hist(genuine_scores, bins=np.arange(-1, 1, 0.1), label="genuine", color="green", alpha=0.5)
#plt.hist(imposter_scores, bins=np.arange(-1, 1, 0.1), label="imposter", color="red", alpha=0.5)
#plt.hist(comparison_scores, bins=np.arange(-1, 1, 0.1), label="real nn", color="purple", alpha=0.5)
#plt.xlim(-1, 1)
#plt.legend()
#plt.savefig(ensure_path_join(eval_dir, "real_vs_variations_distributions.png"), dpi=512)
#genuine_file_path = os.path.join(eval_dir, f"real_vs_variations_genuine_scores.txt")
#imposter_file_path = os.path.join(eval_dir, f"real_vs_variations_imposter_scores.txt")
nn_file_path = os.path.join(eval_dir, f"real_nn_vs_synthetic_scores.txt")
#with open(genuine_file_path, "w") as f:
# for score in genuine_scores:
# f.write(f"{score}\n")
#with open(imposter_file_path, "w") as f:
# for score in imposter_scores:
# f.write(f"{score}\n")
with open(nn_file_path, "w") as f:
for score in comparison_scores:
f.write(f"{score}\n")
#real_vs_variations_eer_stats = get_eer_stats(genuine_scores, imposter_scores)
#eer_stats[f"{model_name}_{frm_name}_real_vs_variations"] = real_vs_variations_eer_stats#
# real_nn_vs_synthetic_eer_stats = get_eer_stats(comparison_scores, imposter_scores)
# eer_stats[f"{model_name}_{frm_name}_real_nn_vs_synthetic"] = real_nn_vs_synthetic_eer_stats
#del genuine_scores
#del imposter_scores
del comparison_scores
if cfg.evaluation.synthetic_real_comparison:
print("Starting REAL vs. SYNTHETIC comparisons ...")
comparison_scores = []
for i, j in self.generate_random_imposter_pairs(synthetic_labels, real_labels, n=1_000_000):
e1, e2 = synthetic_embeddings[i], real_embeddings[j]
cos_sim = np.dot(e1, e2)
comparison_scores.append(cos_sim)
#comparison_scores = []
#for i, e1 in enumerate(np.random.choice(synthetic_embeddings[::16], 1000, replace=False)):
# print(i)
# class_sim = []
# for e2 in real_embeddings:
# cos_sim = np.dot(e1, e2)
# class_sim.append(cos_sim)
# comparison_scores.append(np.sort(np.array(class_sim))[-1])
plt.clf()
plt.hist(comparison_scores, bins=np.arange(-1, 1, 0.1), color="orange", alpha=0.5)
plt.xlim(-1, 1)
plt.savefig(ensure_path_join(eval_dir, "real_vs_synthetic_distributions.png"), dpi=512)
scores_file_path = os.path.join(eval_dir, f"real_vs_synthetic_scores.txt")
with open(scores_file_path, "w") as f:
for score in comparison_scores:
f.write(f"{score}\n")
del comparison_scores
# simpler exhaustive comparison pairings on subset of synthetic images
comparison_scores = []
for i in range(100):
for j in range(len(real_labels)):
e1, e2 = synthetic_embeddings[i * 16], real_embeddings[j]
cos_sim = np.dot(e1, e2)
comparison_scores.append(cos_sim)
plt.clf()
plt.hist(comparison_scores, bins=np.arange(-1, 1, 0.1), color="orange", alpha=0.5)
plt.xlim(-1, 1)
plt.savefig(ensure_path_join(eval_dir, "real_vs_synthetic_special_distributions.png"), dpi=512)
scores_file_path = os.path.join(eval_dir, f"real_vs_synthetic_special_scores.txt")
with open(scores_file_path, "w") as f:
for score in comparison_scores:
f.write(f"{score}\n")
del comparison_scores
if cfg.evaluation.synthetic_synthetic_comparison:
print("Starting SYNTHETIC vs. SYNTHETIC comparisons ...")
genuine_scores, imposter_scores, nn_scores = [], [], []
for i, j in self.generate_genuine_pairs(synthetic_labels, synthetic_labels):
e1, e2 = synthetic_embeddings[i], synthetic_embeddings[j]
cos_sim = np.dot(e1, e2)
genuine_scores.append(cos_sim)
for i, j in self.generate_random_imposter_pairs(synthetic_labels, synthetic_labels, n=len(genuine_scores)):
# TODO: save imposter pairing
e1, e2 = synthetic_embeddings[i], synthetic_embeddings[j]
cos_sim = np.dot(e1, e2)
imposter_scores.append(cos_sim)
plt.clf()
plt.hist(genuine_scores, bins=np.arange(-1, 1, 0.1), label="genuine", color="green", alpha=0.5)
plt.hist(imposter_scores, bins=np.arange(-1, 1, 0.1), label="imposter", color="red", alpha=0.5)
plt.xlim(-1, 1)
plt.legend()
plt.savefig(ensure_path_join(eval_dir, "synthetic_vs_synthetic_distributions.png"), dpi=512)
genuine_file_path = os.path.join(eval_dir, f"synthetic_vs_synthetic_genuine_scores.txt")
imposter_file_path = os.path.join(eval_dir, f"synthetic_vs_synthetic_imposter_scores.txt")
with open(genuine_file_path, "w") as f:
for score in genuine_scores:
f.write(f"{score}\n")
with open(imposter_file_path, "w") as f:
for score in imposter_scores:
f.write(f"{score}\n")
synthetic_vs_synthetic_eer_stats = get_eer_stats(genuine_scores, imposter_scores)
eer_stats[f"{model_name}_{frm_name}_synthetic_vs_synthetic"] = synthetic_vs_synthetic_eer_stats
del genuine_scores
del imposter_scores
report_path = os.path.join("evaluation", f"ffhq_aligned_{synthetic_context_short}", "pyeer_report.html") if cfg.evaluation.aligned else os.path.join("evaluation", f"ffhq_{synthetic_context_short}", "pyeer_report.html")
generate_eer_report(list(eer_stats.values()), list(eer_stats.keys()), report_path)
@staticmethod
def generate_genuine_pairs(labels_1, labels_2, n=None, same_idx_okay=False):
cnt = 0
labels_1 = np.array(labels_1)
labels_2 = np.array(labels_2)
for label_1 in np.unique(labels_1):
if n is not None and cnt >= n:
break
idxs_1 = np.where(labels_1 == label_1)[0]
idxs_2 = np.where(labels_2 == label_1)[0]
if len(idxs_1) * len(idxs_2) == 0:
continue
for i, j in product(idxs_1, idxs_2):
if not same_idx_okay and i == j:
continue
if n is not None and cnt >= n:
break
cnt += 1
yield i, j
@staticmethod
def generate_random_imposter_pairs(labels_1, labels_2, n=1024, ensure_no_duplicates=False):
labels_1 = np.array(labels_1)
labels_2 = np.array(labels_2)
seen = set()
i_list = np.random.choice(list(range(len(labels_1))), size=n, replace=True)
for i in i_list:
j = np.random.choice(np.where(labels_2 != labels_1[i])[0])
if ensure_no_duplicates:
if (i, j) not in seen:
seen.add((i, j))
else:
continue
yield i, j
@hydra.main(config_path='configs', config_name='evaluate_config', version_base=None)
def evaluate(cfg: DictConfig):
print(OmegaConf.to_yaml(cfg))
evaluator = EvaluatorLite(devices="auto", accelerator="auto")
evaluator.run(cfg)
if __name__ == "__main__":
evaluate()