-
Notifications
You must be signed in to change notification settings - Fork 19
/
retrieval.py
186 lines (151 loc) · 6.05 KB
/
retrieval.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
import os
from omegaconf import DictConfig
import logging
import hydra
import yaml
from tqdm import tqdm
logger = logging.getLogger(__name__)
def save_metric(path, metrics):
strings = yaml.dump(metrics, indent=4, sort_keys=False)
with open(path, "w") as f:
f.write(strings)
def compute_sim_matrix(model, dataset, keyids, batch_size=256):
import torch
import numpy as np
from src.data.collate import collate_text_motion
from src.model.tmr import get_sim_matrix
device = model.device
nsplit = int(np.ceil(len(dataset) / batch_size))
with torch.inference_mode():
all_data = [dataset.load_keyid(keyid) for keyid in keyids]
all_data_splitted = np.array_split(all_data, nsplit)
# by batch (can be too costly on cuda device otherwise)
latent_texts = []
latent_motions = []
sent_embs = []
for data in tqdm(all_data_splitted, leave=False):
batch = collate_text_motion(data, device=device)
# Text is already encoded
text_x_dict = batch["text_x_dict"]
motion_x_dict = batch["motion_x_dict"]
sent_emb = batch["sent_emb"]
# Encode both motion and text
latent_text = model.encode(text_x_dict, sample_mean=True)
latent_motion = model.encode(motion_x_dict, sample_mean=True)
latent_texts.append(latent_text)
latent_motions.append(latent_motion)
sent_embs.append(sent_emb)
latent_texts = torch.cat(latent_texts)
latent_motions = torch.cat(latent_motions)
sent_embs = torch.cat(sent_embs)
sim_matrix = get_sim_matrix(latent_texts, latent_motions)
returned = {
"sim_matrix": sim_matrix.cpu().numpy(),
"sent_emb": sent_embs.cpu().numpy(),
}
return returned
@hydra.main(version_base=None, config_path="configs", config_name="retrieval")
def retrieval(newcfg: DictConfig) -> None:
protocol = newcfg.protocol
threshold_val = newcfg.threshold
device = newcfg.device
run_dir = newcfg.run_dir
ckpt_name = newcfg.ckpt
batch_size = newcfg.batch_size
assert protocol in ["all", "normal", "threshold", "nsim", "guo"]
if protocol == "all":
protocols = ["normal", "threshold", "nsim", "guo"]
else:
protocols = [protocol]
save_dir = os.path.join(run_dir, "contrastive_metrics")
os.makedirs(save_dir, exist_ok=True)
# Load last config
from src.config import read_config
import src.prepare # noqa
cfg = read_config(run_dir)
import pytorch_lightning as pl
import numpy as np
from hydra.utils import instantiate
from src.load import load_model_from_cfg
from src.model.metrics import all_contrastive_metrics, print_latex_metrics
pl.seed_everything(cfg.seed)
logger.info("Loading the model")
model = load_model_from_cfg(cfg, ckpt_name, eval_mode=True, device=device)
datasets = {}
results = {}
for protocol in protocols:
# Load the dataset if not already
if protocol not in datasets:
if protocol in ["normal", "threshold", "guo"]:
dataset = instantiate(cfg.data, split="test")
datasets.update(
{key: dataset for key in ["normal", "threshold", "guo"]}
)
elif protocol == "nsim":
datasets[protocol] = instantiate(cfg.data, split="nsim_test")
dataset = datasets[protocol]
# Compute sim_matrix for each protocol
if protocol not in results:
if protocol in ["normal", "threshold"]:
res = compute_sim_matrix(
model, dataset, dataset.keyids, batch_size=batch_size
)
results.update({key: res for key in ["normal", "threshold"]})
elif protocol == "nsim":
res = compute_sim_matrix(
model, dataset, dataset.keyids, batch_size=batch_size
)
results[protocol] = res
elif protocol == "guo":
keyids = sorted(dataset.keyids)
N = len(keyids)
# make batches of 32
idx = np.arange(N)
np.random.seed(0)
np.random.shuffle(idx)
idx_batches = [
idx[32 * i : 32 * (i + 1)] for i in range(len(keyids) // 32)
]
# split into batches of 32
# batched_keyids = [ [32], [32], [...]]
results["guo"] = [
compute_sim_matrix(
model,
dataset,
np.array(keyids)[idx_batch],
batch_size=batch_size,
)
for idx_batch in idx_batches
]
result = results[protocol]
# Compute the metrics
if protocol == "guo":
all_metrics = []
for x in result:
sim_matrix = x["sim_matrix"]
metrics = all_contrastive_metrics(sim_matrix, rounding=None)
all_metrics.append(metrics)
avg_metrics = {}
for key in all_metrics[0].keys():
avg_metrics[key] = round(
float(np.mean([metrics[key] for metrics in all_metrics])), 2
)
metrics = avg_metrics
protocol_name = protocol
else:
sim_matrix = result["sim_matrix"]
protocol_name = protocol
if protocol == "threshold":
emb = result["sent_emb"]
threshold = threshold_val
protocol_name = protocol + f"_{threshold}"
else:
emb, threshold = None, None
metrics = all_contrastive_metrics(sim_matrix, emb, threshold=threshold)
print_latex_metrics(metrics)
metric_name = f"{protocol_name}.yaml"
path = os.path.join(save_dir, metric_name)
save_metric(path, metrics)
logger.info(f"Testing done, metrics saved in:\n{path}")
if __name__ == "__main__":
retrieval()