-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.py
234 lines (180 loc) · 7.21 KB
/
evaluator.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
from typing import NamedTuple, List, Any, Optional, Dict
from itertools import chain
from dataclasses import dataclass
import itertools
import os
import torch
import torch.nn.functional as F
from tqdm.auto import tqdm
import numpy as np
#from matplotlib import pyplot as plt
from schedulers import Scheduler, LRSchedule
from models import Prober, build_mlp
from configs import ConfigBase
from dataset import WallDataset
from normalizer import Normalizer
@dataclass
class ProbingConfig(ConfigBase):
probe_targets: str = "locations"
lr: float = 0.0002
epochs: int = 20
schedule: LRSchedule = LRSchedule.Cosine
sample_timesteps: int = 30
prober_arch: str = "256"
class ProbeResult(NamedTuple):
model: torch.nn.Module
average_eval_loss: float
eval_losses_per_step: List[float]
plots: List[Any]
default_config = ProbingConfig()
def location_losses(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
assert pred.shape == target.shape
mse = (pred - target).pow(2).mean(dim=0)
return mse
class ProbingEvaluator:
def __init__(
self,
device: "cuda",
model: torch.nn.Module,
probe_train_ds,
probe_val_ds: dict,
config: ProbingConfig = default_config,
quick_debug: bool = False,
):
self.device = device
self.config = config
self.model = model
self.model.eval()
self.quick_debug = quick_debug
self.ds = probe_train_ds
self.val_ds = probe_val_ds
self.normalizer = Normalizer()
def train_pred_prober(self):
"""
Probes whether the predicted embeddings capture the future locations
"""
repr_dim = self.model.repr_dim
dataset = self.ds
model = self.model
config = self.config
epochs = config.epochs
if self.quick_debug:
epochs = 1
test_batch = next(iter(dataset))
prober_output_shape = getattr(test_batch, "locations")[0, 0].shape
prober = Prober(
repr_dim,
config.prober_arch,
output_shape=prober_output_shape,
).to(self.device)
all_parameters = []
all_parameters += list(prober.parameters())
optimizer_pred_prober = torch.optim.Adam(all_parameters, config.lr)
step = 0
batch_size = dataset.batch_size
batch_steps = None
scheduler = Scheduler(
schedule=self.config.schedule,
base_lr=config.lr,
data_loader=dataset,
epochs=epochs,
optimizer=optimizer_pred_prober,
batch_steps=batch_steps,
batch_size=batch_size,
)
for epoch in tqdm(range(epochs), desc=f"Probe prediction epochs"):
for batch in tqdm(dataset, desc="Probe prediction step"):
################################################################################
# TODO: Forward pass through your model
init_states = batch.states[:, 0:1] # BS, 1, C, H, W
_, _, pred_encs = model(states=init_states, actions=batch.actions) # T, BS, D
pred_encs = pred_encs.transpose(0, 1) # # BS, T, D --> T, BS, D
# Make sure pred_encs has shape (T, BS, D) at this point
################################################################################
pred_encs = pred_encs.detach()
n_steps = pred_encs.shape[0]
bs = pred_encs.shape[1]
losses_list = []
target = getattr(batch, "locations").cuda() # BS, T, 2
target = self.normalizer.normalize_location(target)
if (
config.sample_timesteps is not None
and config.sample_timesteps < n_steps
):
sample_shape = (config.sample_timesteps,) + pred_encs.shape[1:]
# we only randomly sample n timesteps to train prober.
# we most likely do this to avoid OOM
sampled_pred_encs = torch.empty(
sample_shape,
dtype=pred_encs.dtype,
device=pred_encs.device,
)
sampled_target_locs = torch.empty(bs, config.sample_timesteps, 2)
for i in range(bs):
indices = torch.randperm(n_steps)[: config.sample_timesteps]
sampled_pred_encs[:, i, :] = pred_encs[indices, i, :]
sampled_target_locs[i, :] = target[i, indices]
pred_encs = sampled_pred_encs
target = sampled_target_locs.cuda()
pred_locs = torch.stack([prober(x) for x in pred_encs], dim=1) # BS, T, 2
losses = location_losses(pred_locs, target)
per_probe_loss = losses.mean()
if step % 100 == 0:
print(f"normalized pred locations loss {per_probe_loss.item()}")
losses_list.append(per_probe_loss)
optimizer_pred_prober.zero_grad()
loss = sum(losses_list)
loss.backward()
optimizer_pred_prober.step()
lr = scheduler.adjust_learning_rate(step)
step += 1
if self.quick_debug and step > 2:
break
return prober
@torch.no_grad()
def evaluate_all(
self,
prober,
):
"""
Evaluates on all the different validation datasets
"""
avg_losses = {}
for prefix, val_ds in self.val_ds.items():
avg_losses[prefix] = self.evaluate_pred_prober(
prober=prober,
val_ds=val_ds,
prefix=prefix,
)
return avg_losses
@torch.no_grad()
def evaluate_pred_prober(
self,
prober,
val_ds,
prefix="",
):
quick_debug = self.quick_debug
config = self.config
model = self.model
probing_losses = []
prober.eval()
for idx, batch in enumerate(tqdm(val_ds, desc="Eval probe pred")):
################################################################################
# TODO: Forward pass through your model
init_states = batch.states[:, 0:1] # BS, 1 C, H, W
_, _, pred_encs = model(states=init_states, actions=batch.actions)
# # BS, T, D --> T, BS, D
pred_encs = pred_encs.transpose(0, 1)
# Make sure pred_encs has shape (T, BS, D) at this point
################################################################################
target = getattr(batch, "locations").cuda()
target = self.normalizer.normalize_location(target)
pred_locs = torch.stack([prober(x) for x in pred_encs], dim=1)
losses = location_losses(pred_locs, target)
probing_losses.append(losses.cpu())
losses_t = torch.stack(probing_losses, dim=0).mean(dim=0)
losses_t = self.normalizer.unnormalize_mse(losses_t)
losses_t = losses_t.mean(dim=-1)
average_eval_loss = losses_t.mean().item()
return average_eval_loss