-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrain_lapo.py
More file actions
473 lines (400 loc) · 15.6 KB
/
Copy pathtrain_lapo.py
File metadata and controls
473 lines (400 loc) · 15.6 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import math
import time
import uuid
from dataclasses import asdict, dataclass
from typing import Optional
import numpy as np
import pyrallis
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchinfo
import wandb
from pyrallis import field
from torch.utils.data import DataLoader
from torchvision.utils import make_grid
from tqdm import trange
from src.augmentations import Augmenter
from src.nn import LAPO, ActionDecoder, Actor
from src.scheduler import linear_annealing_with_warmup
from src.utils import (
DCSInMemoryDataset,
DCSLAPOInMemoryDataset,
create_env_from_df,
get_grad_norm,
get_optim_groups,
normalize_img,
set_seed,
unnormalize_img,
)
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
@dataclass
class LAPOConfig:
num_epochs: int = 100
batch_size: int = 256
future_obs_offset: int = 1
learning_rate: float = 3e-4
weight_decay: float = 0.0
warmup_epochs: int = 5
grad_norm: Optional[float] = None
latent_action_dim: int = 8
encoder_scale: int = 1
encoder_num_res_blocks: int = 1
encoder_deep: bool = True
frame_stack: int = 3
data_path: str = "data/test.hdf5"
@dataclass
class BCConfig:
num_epochs: int = 1
batch_size: int = 64
learning_rate: float = 3e-4
weight_decay: float = 0.0
warmup_epochs: int = 5
encoder_scale: int = 1
encoder_num_res_blocks: int = 2
encoder_deep: bool = False
dropout: float = 0.0
use_aug: bool = True
frame_stack: int = 3
data_path: str = "data/test.hdf5"
dcs_backgrounds_path: str = "DAVIS/JPEGImages/480p"
dcs_backgrounds_split: str = "train"
eval_episodes: int = 10
eval_seed: int = 0
@dataclass
class DecoderConfig:
total_updates: int = 1
batch_size: int = 64
learning_rate: float = 3e-4
weight_decay: float = 0.0
warmup_epochs: int = 5
hidden_dim: int = 128
use_aug: bool = True
data_path: str = "data/test.hdf5"
dcs_backgrounds_path: str = "DAVIS/JPEGImages/480p"
dcs_backgrounds_split: str = "train"
eval_episodes: int = 10
eval_seed: int = 0
@dataclass
class Config:
project: str = "laom"
group: str = "lapo"
name: str = "lapo"
seed: int = 0
lapo: LAPOConfig = field(default_factory=LAPOConfig)
bc: BCConfig = field(default_factory=BCConfig)
decoder: DecoderConfig = field(default_factory=DecoderConfig)
def __post_init__(self):
self.name = f"{self.name}-{str(uuid.uuid4())}"
def train_lapo(config: LAPOConfig):
dataset = DCSLAPOInMemoryDataset(
config.data_path, max_offset=config.future_obs_offset, frame_stack=config.frame_stack, device=DEVICE
)
dataloader = DataLoader(dataset, batch_size=config.batch_size, shuffle=True, drop_last=True)
lapo = LAPO(
shape=(3 * config.frame_stack, dataset.img_hw, dataset.img_hw),
latent_act_dim=config.latent_action_dim,
encoder_scale=config.encoder_scale,
encoder_channels=(16, 32, 64, 128, 256) if config.encoder_deep else (16, 32, 32),
encoder_num_res_blocks=config.encoder_num_res_blocks,
).to(DEVICE)
torchinfo.summary(
lapo,
input_size=[
(1, 3 * config.frame_stack, dataset.img_hw, dataset.img_hw),
(1, 3 * config.frame_stack, dataset.img_hw, dataset.img_hw),
],
)
optim = torch.optim.Adam(params=get_optim_groups(lapo, config.weight_decay), lr=config.learning_rate, fused=True)
# scheduler setup
total_updates = len(dataloader) * config.num_epochs
warmup_updates = len(dataloader) * config.warmup_epochs
scheduler = linear_annealing_with_warmup(optim, warmup_updates, total_updates)
linear_probe = nn.Linear(config.latent_action_dim, dataset.act_dim).to(DEVICE)
probe_optim = torch.optim.Adam(linear_probe.parameters(), lr=config.learning_rate)
start_time = time.time()
total_tokens = 0
total_steps = 0
for epoch in trange(config.num_epochs, desc="Epochs"):
lapo.train()
for batch in dataloader:
total_tokens += config.batch_size
total_steps += 1
obs, next_obs, future_obs, actions, _ = [b.to(DEVICE) for b in batch]
obs = normalize_img(obs.permute((0, 3, 1, 2)))
next_obs = normalize_img(next_obs.permute((0, 3, 1, 2)))
future_obs = normalize_img(future_obs.permute((0, 3, 1, 2)))
# update lapo
with torch.autocast(DEVICE, dtype=torch.bfloat16):
pred_next_obs, latent_action = lapo(obs, future_obs)
loss = F.mse_loss(pred_next_obs, next_obs)
optim.zero_grad(set_to_none=True)
loss.backward()
if config.grad_norm is not None:
torch.nn.utils.clip_grad_norm_(lapo.parameters(), max_norm=config.grad_norm)
optim.step()
scheduler.step()
# update linear probe
with torch.autocast(DEVICE, dtype=torch.bfloat16):
pred_action = linear_probe(latent_action.detach())
probe_loss = F.mse_loss(pred_action, actions)
probe_optim.zero_grad(set_to_none=True)
probe_loss.backward()
probe_optim.step()
wandb.log(
{
"lapo/mse_loss": loss.item(),
"lapo/action_probe_mse_loss": probe_loss.item(),
"lapo/throughput": total_tokens / (time.time() - start_time),
"lapo/learning_rate": scheduler.get_last_lr()[0],
"lapo/grad_norm": get_grad_norm(lapo).item(),
"lapo/epoch": epoch,
"lapo/total_steps": total_tokens,
}
)
# logging reconstruction of next state
obs_example = [unnormalize_img(next_obs[0][i : i + 3]) for i in range(0, 3 * config.frame_stack, 3)]
next_obs_example = [unnormalize_img(pred_next_obs[0][i : i + 3]) for i in range(0, 3 * config.frame_stack, 3)]
reconstruction_img = make_grid(obs_example + next_obs_example, nrow=config.frame_stack, padding=1)
reconstruction_img = reconstruction_img.permute((1, 2, 0))
reconstruction_img = wandb.Image(reconstruction_img.cpu().numpy(), caption="Top: True, Bottom: Pred")
wandb.log(
{
"lapo/next_obs_pred": reconstruction_img,
"lapo/epoch": epoch,
"lapo/total_steps": total_tokens,
}
)
return lapo
@torch.no_grad()
def evaluate_bc(env, actor, num_episodes, seed=0, device="cpu", action_decoder=None):
returns = []
for ep in trange(num_episodes, desc="Evaluating", leave=False):
total_reward = 0.0
obs, info = env.reset(seed=seed + ep)
done = False
while not done:
obs_ = torch.tensor(obs.copy(), device=device)[None].permute(0, 3, 1, 2)
obs_ = normalize_img(obs_)
action, obs_emb = actor(obs_)
if action_decoder is not None:
if isinstance(action_decoder, ActionDecoder):
action = action_decoder(obs_emb, action)
else:
action = action_decoder(action)
obs, reward, terminated, truncated, info = env.step(action.squeeze().cpu().numpy())
done = terminated or truncated
total_reward += reward
returns.append(total_reward)
return np.array(returns)
def train_bc(lam: LAPO, config: BCConfig):
dataset = DCSInMemoryDataset(config.data_path, frame_stack=config.frame_stack, device=DEVICE)
dataloader = DataLoader(
dataset,
batch_size=config.batch_size,
shuffle=True,
drop_last=True,
)
eval_env = create_env_from_df(
config.data_path,
config.dcs_backgrounds_path,
config.dcs_backgrounds_split,
frame_stack=config.frame_stack,
)
print(eval_env.observation_space)
print(eval_env.action_space)
num_actions = lam.latent_act_dim
for p in lam.parameters():
p.requires_grad_(False)
lam.eval()
actor = Actor(
shape=(3 * config.frame_stack, dataset.img_hw, dataset.img_hw),
num_actions=num_actions,
encoder_scale=config.encoder_scale,
encoder_channels=(16, 32, 64, 128, 256) if config.encoder_deep else (16, 32, 32),
encoder_num_res_blocks=config.encoder_num_res_blocks,
dropout=config.dropout,
).to(DEVICE)
optim = torch.optim.AdamW(params=get_optim_groups(actor, config.weight_decay), lr=config.learning_rate, fused=True)
# scheduler setup
total_updates = len(dataloader) * config.num_epochs
warmup_updates = len(dataloader) * config.warmup_epochs
scheduler = linear_annealing_with_warmup(optim, warmup_updates, total_updates)
# for debug
print("Latent action dim:", num_actions)
act_decoder = nn.Sequential(
nn.Linear(num_actions, 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, dataset.act_dim)
).to(DEVICE)
act_decoder_optim = torch.optim.AdamW(params=act_decoder.parameters(), lr=config.learning_rate, fused=True)
act_decoder_scheduler = linear_annealing_with_warmup(act_decoder_optim, warmup_updates, total_updates)
torchinfo.summary(actor, input_size=(1, 3 * config.frame_stack, dataset.img_hw, dataset.img_hw))
if config.use_aug:
augmenter = Augmenter(img_resolution=dataset.img_hw)
start_time = time.time()
total_tokens = 0
total_steps = 0
for epoch in trange(config.num_epochs, desc="Epochs"):
actor.train()
for batch in dataloader:
total_tokens += config.batch_size
total_steps += 1
obs, next_obs, true_actions = [b.to(DEVICE) for b in batch]
# rescale from 0..255 -> -1..1
obs = normalize_img(obs.permute((0, 3, 1, 2)))
next_obs = normalize_img(next_obs.permute((0, 3, 1, 2)))
# label with lapo latent actions
target_actions = lam.label(obs, next_obs)
# augment obs only for bc to make action labels determenistic
if config.use_aug:
obs = augmenter(obs)
# update actor
with torch.autocast(DEVICE, dtype=torch.bfloat16):
pred_actions, _ = actor(obs)
loss = F.mse_loss(pred_actions, target_actions)
optim.zero_grad(set_to_none=True)
loss.backward()
optim.step()
scheduler.step()
# optimizing the probe
with torch.autocast(DEVICE, dtype=torch.bfloat16):
pred_true_actions = act_decoder(pred_actions.detach())
decoder_loss = F.mse_loss(pred_true_actions, true_actions)
act_decoder_optim.zero_grad(set_to_none=True)
decoder_loss.backward()
act_decoder_optim.step()
act_decoder_scheduler.step()
wandb.log(
{
"bc/mse_loss": loss.item(),
"bc/throughput": total_tokens / (time.time() - start_time),
"bc/learning_rate": scheduler.get_last_lr()[0],
"bc/act_decoder_probe_mse_loss": decoder_loss.item(),
"bc/epoch": epoch,
"bc/total_steps": total_steps,
}
)
actor.eval()
eval_returns = evaluate_bc(
eval_env,
actor,
num_episodes=config.eval_episodes,
seed=config.eval_seed,
device=DEVICE,
action_decoder=act_decoder,
)
wandb.log(
{
"bc/eval_returns_mean": eval_returns.mean(),
"bc/eval_returns_std": eval_returns.std(),
"bc/epoch": epoch,
"bc/total_steps": total_steps,
}
)
return actor
def train_act_decoder(actor: Actor, config: DecoderConfig, bc_config: BCConfig):
for p in actor.parameters():
p.requires_grad_(False)
actor.eval()
dataset = DCSInMemoryDataset(config.data_path, frame_stack=bc_config.frame_stack, device=DEVICE)
dataloader = DataLoader(
dataset,
batch_size=config.batch_size,
shuffle=True,
)
# to make equal number of updates for all labeled datasets which vary in size
num_epochs = config.total_updates // len(dataloader)
action_decoder = ActionDecoder(
obs_emb_dim=math.prod(actor.final_encoder_shape),
latent_act_dim=actor.num_actions,
true_act_dim=dataset.act_dim,
hidden_dim=config.hidden_dim,
).to(DEVICE)
optim = torch.optim.AdamW(
params=get_optim_groups(action_decoder, config.weight_decay), lr=config.learning_rate, fused=True
)
eval_env = create_env_from_df(
config.data_path,
config.dcs_backgrounds_path,
config.dcs_backgrounds_split,
frame_stack=bc_config.frame_stack,
)
print(eval_env.observation_space)
print(eval_env.action_space)
# scheduler setup
total_updates = len(dataloader) * num_epochs
warmup_updates = len(dataloader) * config.warmup_epochs
scheduler = linear_annealing_with_warmup(optim, warmup_updates, total_updates)
if config.use_aug:
augmenter = Augmenter(img_resolution=dataset.img_hw)
start_time = time.time()
total_tokens = 0
total_steps = 0
for epoch in trange(num_epochs, desc="Epochs"):
for batch in dataloader:
total_tokens += config.batch_size
total_steps += 1
obs, _, true_actions = [b.to(DEVICE) for b in batch]
# rescale from 0..255 -> -1..1
obs = normalize_img(obs.permute((0, 3, 1, 2)))
if config.use_aug:
obs = augmenter(obs)
# update actor
with torch.autocast(DEVICE, dtype=torch.bfloat16):
with torch.no_grad():
latent_actions, obs_emb = actor(obs)
pred_actions = action_decoder(obs_emb, latent_actions)
loss = F.mse_loss(pred_actions, true_actions)
optim.zero_grad(set_to_none=True)
loss.backward()
optim.step()
scheduler.step()
wandb.log(
{
"decoder/mse_loss": loss.item(),
"decoder/throughput": total_tokens / (time.time() - start_time),
"decoder/learning_rate": scheduler.get_last_lr()[0],
"decoder/epoch": epoch,
"decoder/total_steps": total_steps,
}
)
actor.eval()
eval_returns = evaluate_bc(
eval_env,
actor,
num_episodes=config.eval_episodes,
seed=config.eval_seed,
device=DEVICE,
action_decoder=action_decoder,
)
wandb.log(
{
"decoder/eval_returns_mean": eval_returns.mean(),
"decoder/eval_returns_std": eval_returns.std(),
"decoder/epoch": epoch,
"decoder/total_steps": total_steps,
}
)
return action_decoder
@pyrallis.wrap()
def train(config: Config):
run = wandb.init(
project=config.project,
group=config.group,
name=config.name,
config=asdict(config),
save_code=True,
)
set_seed(config.seed)
# stage 1: pretraining lapo on unlabeled dataset
lapo = train_lapo(config=config.lapo)
# stage 2: pretraining bc on latent actions
actor = train_bc(lam=lapo, config=config.bc)
# stage 3: finetune on labeles ground-truth actions
action_decoder = train_act_decoder(actor=actor, config=config.decoder, bc_config=config.bc)
run.finish()
return lapo, actor, action_decoder
if __name__ == "__main__":
train()