-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_binary_classifier.py
More file actions
378 lines (317 loc) · 13.9 KB
/
evaluate_binary_classifier.py
File metadata and controls
378 lines (317 loc) · 13.9 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""Evaluate the trained binary classifier and report common metrics.
The script loads the best model from ``results/binary-classifier/params``
and computes several verification metrics on the test split of either the
L3SFV2Augmented or PolyU DBII dataset. The computed metrics are saved in
``metrics.csv`` and ROC/PR curves are written as PNG files in the same
directory.
"""
from pathlib import Path
import argparse
import logging
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_curve,
auc,
precision_recall_curve,
confusion_matrix,
)
import torch
from utils.matching import build_matches
from src.benchmark import L3SFV2AugmentedBenchmark, PolyUDBIIBenchmark, PolyUDBIBenchmark, L3SFBenchmark
from src.gmdataset import GMDataset, get_dataloader, TestDataset
from src.model.ngm import Net
from utils.data_to_cuda import data_to_cuda
from utils.models_sl import load_model
from utils.visualize import visualize_stochastic_matrix, visualize_match, to_grayscale_cv2_image
def evaluate(dataset_name: str, data_root: str):
"""Run evaluation using the best classifier model for the chosen dataset.
"""
dataset_len = None
if dataset_name == "PolyU-DBII":
benchmark = PolyUDBIIBenchmark(
sets="test",
obj_resize=(320, 240),
train_root=data_root,
task="classify",
)
elif dataset_name == "PolyU-DBI":
benchmark = PolyUDBIBenchmark(
sets="test",
obj_resize=(320, 240),
train_root=data_root,
task="classify",
)
elif dataset_name == "L3-SF":
benchmark = L3SFBenchmark(
sets="test",
obj_resize=(320, 240),
train_root=data_root,
task="classify",
)
else:
dataset_name = "L3SFV2Augmented"
benchmark = L3SFV2AugmentedBenchmark(
sets="test",
obj_resize=(320, 240),
train_root=data_root,
task="classify",
name =dataset_name,
)
dataset = GMDataset(dataset_name, benchmark, dataset_len, True, None, "2GM", augment=False)
dataloader = get_dataloader(dataset, shuffle=True, fix_seed=True)
match_net = Net(regression=True)
model_path = Path("results/binary-classifier/params/best_model.pt")
if model_path.exists():
load_model(match_net, str(model_path))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
match_net.to(device).eval()
all_labels = []
all_probs = []
all_raw_k = []
iteration = 0
with torch.no_grad():
for batch in dataloader:
iteration += 1
batch = data_to_cuda(batch)
outputs = match_net(batch)
if "cls_prob" in outputs:
prob = outputs["cls_prob"].detach()
print("using cls_prob")
if "perm_mat" in outputs:
perm_mat = outputs["perm_mat"].detach()
k_pred = perm_mat.sum(dim=(1, 2)).float()
else:
k_pred = torch.zeros(prob.shape[0], dtype=torch.float32, device=prob.device)
else:
# Fallback to using the ratio of predicted correspondences
perm_mat = outputs["perm_mat"].detach()
k_pred = perm_mat.sum(dim=(1, 2)).float()
ns = batch["ns"]
min_points = torch.min(ns[0], ns[1]).float()
prob = (k_pred / min_points).clamp(0, 1)
all_probs.append(prob.cpu())
all_labels.append(batch["label"].cpu())
all_raw_k.append(k_pred.cpu())
if iteration % 5 == 0:
print(f"Processed {iteration} batches...")
all_probs = torch.cat(all_probs).numpy()
all_labels = torch.cat(all_labels).numpy()
# Debug: Check label distribution
print(f"Total samples: {len(all_labels)}")
print(f"Genuine matches (label=1): {np.sum(all_labels == 1)}")
print(f"Imposter matches (label=0): {np.sum(all_labels == 0)}")
print(f"Unique labels: {np.unique(all_labels)}")
# If no genuine matches, let's check the first few batches manually
if np.sum(all_labels == 1) == 0:
print("No genuine matches found! Checking first few batches...")
debug_dataloader = get_dataloader(dataset, shuffle=False, fix_seed=True)
for i, batch in enumerate(debug_dataloader):
if i >= 5: # Check first 5 batches
break
batch = data_to_cuda(batch)
labels = batch["label"].cpu().numpy()
print(f"Batch {i}: labels = {labels}")
fpr, tpr, thresholds = roc_curve(all_labels, all_probs)
fnr = 1 - tpr
eer_idx = np.nanargmin(np.abs(fnr - fpr))
eer_threshold = thresholds[eer_idx]
preds = (all_probs >= eer_threshold).astype(np.int32)
accuracy = accuracy_score(all_labels, preds)
precision = precision_score(all_labels, preds)
recall = recall_score(all_labels, preds)
f1 = f1_score(all_labels, preds)
roc_auc = auc(fpr, tpr)
prec_curve, rec_curve, _ = precision_recall_curve(all_labels, all_probs)
pr_auc = auc(rec_curve, prec_curve)
tn, fp, fn, tp = confusion_matrix(all_labels, preds).ravel()
far = fp / (fp + tn) if (fp + tn) > 0 else 0.0
frr = fn / (tp + fn) if (tp + fn) > 0 else 0.0
out_dir = Path(f"results/binary-classifier/{dataset_name}")
out_dir.mkdir(parents=True, exist_ok=True)
# Visualize one genuine match (label == 1) from the network
for i, (label, prob) in enumerate(zip(all_labels, all_probs)):
if label == 1:
# Re-run the dataloader to get the corresponding batch and visualize
count = 0
for batch in get_dataloader(dataset, shuffle=False, fix_seed=True):
batch = data_to_cuda(batch)
batch_label = batch["label"].cpu().numpy()[0]
if batch_label == 1:
# Run the model to get outputs
with torch.no_grad():
outputs = match_net(batch)
# Get keypoints
if 'Ps' in batch:
kp0 = batch['Ps'][0][0].cpu().numpy()
kp1 = batch['Ps'][1][0].cpu().numpy()
else:
# Fallback keypoints if not available
kp0 = np.array([[100, 100], [150, 150], [200, 200]])
kp1 = np.array([[110, 110], [160, 160], [210, 210]])
# Get images
if "images" in batch:
img0 = batch["images"][0][0]
img1 = batch["images"][1][0]
img0 = to_grayscale_cv2_image(img0)
img1 = to_grayscale_cv2_image(img1)
else:
# Create placeholder images if not available
img0 = np.zeros((240, 320), dtype=np.uint8)
img1 = np.zeros((240, 320), dtype=np.uint8)
# Get matching matrices
ds_mat = outputs["ds_mat"].cpu().numpy()[0]
per_mat = outputs["perm_mat"].cpu().numpy()[0]
# Build matches using the same function as train.py
matches = build_matches(ds_mat, per_mat)
# Visualize matches using the same function as train.py
visualize_match(img0, img1, kp0, kp1, matches,
prefix=str(out_dir) + "/",
filename="genuine_match_example")
# Also visualize the stochastic matrix
print(f"Genuine match visualization saved with {len(matches)} matches")
print(f"Probability: {prob:.4f}")
break
count += 1
break
# Do the same for an imposter match (label == 0)
for i, (label, prob) in enumerate(zip(all_labels, all_probs)):
if label == 0:
count = 0
for batch in get_dataloader(dataset, shuffle=False, fix_seed=True):
batch = data_to_cuda(batch)
batch_label = batch["label"].cpu().numpy()[0]
if batch_label == 0:
with torch.no_grad():
outputs = match_net(batch)
if 'Ps' in batch:
kp0 = batch['Ps'][0][0].cpu().numpy()
kp1 = batch['Ps'][1][0].cpu().numpy()
else:
kp0 = np.array([[100, 100], [150, 150], [200, 200]])
kp1 = np.array([[110, 110], [160, 160], [210, 210]])
if "images" in batch:
img0 = batch["images"][0][0]
img1 = batch["images"][1][0]
img0 = to_grayscale_cv2_image(img0)
img1 = to_grayscale_cv2_image(img1)
else:
img0 = np.zeros((240, 320), dtype=np.uint8)
img1 = np.zeros((240, 320), dtype=np.uint8)
ds_mat = outputs["ds_mat"].cpu().numpy()[0]
per_mat = outputs["perm_mat"].cpu().numpy()[0]
matches = build_matches(ds_mat, per_mat)
visualize_match(img0, img1, kp0, kp1, matches,
prefix=str(out_dir) + "/",
filename="imposter_match_example")
print(f"Imposter match visualization saved with {len(matches)} matches")
print(f"Probability: {prob:.4f}")
break
count += 1
break
logging.basicConfig(
filename=str(out_dir / "eval.log"),
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# ROC curve
plt.figure()
plt.plot(fpr, tpr, label=f"ROC AUC = {roc_auc:.4f}")
plt.plot([0, 1], [0, 1], "--", color="gray")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.savefig(out_dir / "roc_curve.png", bbox_inches="tight", pad_inches=0)
plt.close()
# PR curve
plt.figure()
plt.plot(rec_curve, prec_curve, label=f"PR AUC = {pr_auc:.4f}")
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.title("Precision-Recall Curve")
plt.legend(loc="lower left")
plt.savefig(out_dir / "pr_curve.png", bbox_inches="tight", pad_inches=0)
plt.close()
# Concatenate raw k values
all_raw_k = torch.cat(all_raw_k).numpy()
# Histogram of normalized k values
plt.figure(figsize=(10, 6))
genuine_probs = all_probs[all_labels == 1]
imposter_probs = all_probs[all_labels == 0]
# Use bins that cover the range from 0 to 1
bins = np.linspace(0, 1, 30)
plt.hist(imposter_probs, bins=bins, alpha=0.5, label='Imposter Matches', color='red')
plt.hist(genuine_probs, bins=bins, alpha=0.5, label='Genuine Matches', color='green')
plt.xlabel('Normalized k Value (k_pred/min_points)')
plt.ylabel('Frequency')
plt.title('Distribution of Normalized k Values')
plt.grid(alpha=0.3)
plt.axvline(x=eer_threshold, color='black', linestyle='--',
label=f'EER Threshold ({eer_threshold:.3f})')
plt.legend()
plt.savefig(out_dir / "normalized_k_histogram.png", bbox_inches="tight", pad_inches=0)
plt.close()
# Histogram of raw k values
plt.figure(figsize=(10, 6))
genuine_raw_k = all_raw_k[all_labels == 1]
imposter_raw_k = all_raw_k[all_labels == 0]
# Find appropriate bin range for raw k values
max_k = np.max(all_raw_k) * 1.05 # Add a small margin
bins = np.linspace(0, max_k, 30)
plt.hist(imposter_raw_k, bins=bins, alpha=0.5, label='Imposter Matches', color='red')
plt.hist(genuine_raw_k, bins=bins, alpha=0.5, label='Genuine Matches', color='green')
plt.xlabel('Raw k Value (Number of Matched Keypoints)')
plt.ylabel('Frequency')
plt.title('Distribution of Raw k Values')
plt.grid(alpha=0.3)
plt.legend()
plt.savefig(out_dir / "raw_k_histogram.png", bbox_inches="tight", pad_inches=0)
plt.close()
metrics = {
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"f1_score": f1,
"roc_auc": roc_auc,
"pr_auc": pr_auc,
"far": far,
"frr": frr,
}
pd.DataFrame([metrics]).to_csv(out_dir / "metrics.csv", index=False)
print("Evaluation metrics:")
for k, v in metrics.items():
print(f"{k}: {v:.4f}")
logger.info("%s: %.4f", k, v)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Evaluate the trained binary classifier")
parser.add_argument(
"--dataset",
choices=["L3SFV2Augmented", "PolyU-DBII", "PolyU-DBI", "L3-SF"],
default="L3SFV2Augmented",
help="Dataset to evaluate on",
)
parser.add_argument(
"--data-root",
default=None,
help="Root directory of the dataset. If omitted a sensible default is used.",
)
args = parser.parse_args()
if args.data_root is None:
if args.dataset == "PolyU-DBII":
data_root = "dataset/PolyU/DBII"
elif args.dataset == "PolyU-DBI":
data_root = "dataset/PolyU/DBI"
elif args.dataset == "L3-SF":
data_root = "dataset/L3-SF"
else:
data_root = "dataset/Synthetic"
else:
data_root = args.data_root
evaluate(args.dataset, data_root)