-
Notifications
You must be signed in to change notification settings - Fork 4
/
retrieval_eval.py
134 lines (123 loc) · 4.9 KB
/
retrieval_eval.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
import torch
from datasets import CrossViewiNATBirdsFineTune
from models import CVEMAEMeta, CVMMAEMeta, MoCoGeo
import json
import pandas as pd
from config import cfg
from utils import seed_everything
from tqdm import tqdm
def retrieval_eval():
torch.cuda.empty_cache()
seed_everything()
test_json = json.load(open("data/val_birds.json"))
test_labels = pd.read_csv("data/val_birds_labels.csv")
test_dataset = CrossViewiNATBirdsFineTune(test_json, test_labels, val=True)
if cfg.retrieval.model_type == "MOCOGEO":
model = MoCoGeo.load_from_checkpoint(
cfg.retrieval.ckpt,
train_dataset=None,
val_dataset=test_dataset,
queue_dataset=None,
)
elif cfg.retrieval.model_type == "CVEMAE":
model = CVEMAEMeta.load_from_checkpoint(
cfg.retrieval.ckpt,
train_dataset=None,
val_dataset=test_dataset,
)
elif cfg.retrieval.model_type == "CVMMAE":
model_filter = CVMMAEMeta.load_from_checkpoint(
cfg.retrieval.ckpt,
train_dataset=None,
val_dataset=test_dataset,
)
model_filter.eval()
model_filter = model_filter.cuda()
model = CVEMAEMeta.load_from_checkpoint(
cfg.retrieval.ckpt,
train_dataset=None,
val_dataset=test_dataset,
)
model.eval()
model = model.cuda()
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=cfg.retrieval.batch_size,
shuffle=False,
num_workers=cfg.retrieval.num_workers,
drop_last=True,
)
recall = 0
for batch in tqdm(test_loader):
if cfg.retrieval.mode == "full_metadata":
_, img_overhead, label, *_ = batch
else:
_, img_overhead, label = batch
z = 0
running_val = 0
running_label = 0
for batch2 in tqdm(test_loader):
if (
cfg.retrieval.mode == "full_metadata"
and cfg.retrieval.model_type != "MOCOGEO"
):
img_ground, _, label2, geoloc, date = batch2
ground_embeddings, overhead_embeddings = model.forward_features(
img_ground.cuda(), img_overhead.cuda(), geoloc.cuda(), date.cuda()
)
else:
img_ground, _, label2 = batch2
ground_embeddings, overhead_embeddings = model.forward_features(
img_ground.cuda(), img_overhead.cuda()
)
similarity = torch.einsum(
"ij,kj->ik", ground_embeddings, overhead_embeddings
)
if z == 0:
running_val = similarity.detach().cpu()
running_label = label2
z += 1
else:
running_val = torch.cat((running_val, similarity.detach().cpu()), dim=0)
running_label = torch.cat((running_label, label2), dim=0)
if (
cfg.retrieval.model_type == "CVEMAE"
or cfg.retrieval.model_type == "MOCOGEO"
):
_, ind = torch.topk(running_val, cfg.retrieval.topk, dim=0)
# Hierarchical Retrieval
elif cfg.retrieval.model_type == "CVMMAE":
assert cfg.retrieval.hierarchical_filter > cfg.retrieval.topk
_, ind = torch.topk(running_val, cfg.retrieval.hierarchical_filter, dim=0)
if cfg.retrieval.mode == "full_metadata":
img_ground, _, label2, geoloc, date = test_dataset[ind]
else:
img_ground, _, label2 = test_dataset[ind]
similarity = torch.zeros(
(cfg.retrieval.hierarchical_filter, cfg.retrieval.hierarchical_filter)
)
idx = torch.arange(cfg.retrieval.hierarchical_filter)
for i in range(cfg.retrieval.hierarchical_filter):
img_ground_rolled = torch.roll(img_ground, i, 0)
idx_rolled = torch.roll(idx, i, 0)
if cfg.retrieval.mode == "full_metadata":
_, scores = model_filter.forward_features(
img_ground_rolled.cuda(),
img_overhead.cuda(),
geoloc.cuda(),
date.cuda(),
)
else:
_, scores = model_filter.forward_features(
img_ground_rolled.cuda(), img_overhead.cuda()
)
similarity[
idx_rolled, torch.arange(cfg.retrieval.hierarchical_filter)
] = (scores.squeeze(0).detach().cpu())
_, ind = torch.topk(similarity, cfg.retrieval.topk, dim=0)
running_label = label2
preds = running_label[ind]
recall += sum(
[1 if label[i] in preds[:, i] else 0 for i in range(label.shape[0])]
)
print(f"Current Recall Score: {recall/len(test_dataset)}")