-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathrun_training.py
273 lines (233 loc) · 7.78 KB
/
run_training.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
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
import os
import os.path as osp
import shutil
from dataclasses import asdict, dataclass
from datetime import datetime
from typing import Annotated
import numpy as np
import torch
import tyro
import yaml
from loguru import logger as guru
from torch.utils.data import DataLoader
from tqdm import tqdm
from flow3d.configs import LossesConfig, OptimizerConfig, SceneLRConfig
from flow3d.data import (
BaseDataset,
DavisDataConfig,
CustomDataConfig,
get_train_val_datasets,
iPhoneDataConfig,
NvidiaDataConfig,
)
from flow3d.data.utils import to_device
from flow3d.init_utils import (
init_bg,
init_fg_from_tracks_3d,
init_motion_params_with_procrustes,
run_initial_optim,
vis_init_params,
init_trainable_poses,
)
from flow3d.scene_model import SceneModel
from flow3d.tensor_dataclass import StaticObservations, TrackObservations
from flow3d.trainer import Trainer
from flow3d.validator import Validator
from flow3d.vis.utils import get_server
from flow3d.params import CameraScales
torch.set_float32_matmul_precision("high")
def set_seed(seed):
# Set the seed for generating random numbers
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
set_seed(42)
@dataclass
class TrainConfig:
work_dir: str
data: (
Annotated[iPhoneDataConfig, tyro.conf.subcommand(name="iphone")]
| Annotated[DavisDataConfig, tyro.conf.subcommand(name="davis")]
| Annotated[CustomDataConfig, tyro.conf.subcommand(name="custom")]
| Annotated[NvidiaDataConfig, tyro.conf.subcommand(name="nvidia")]
)
lr: SceneLRConfig
loss: LossesConfig
optim: OptimizerConfig
num_fg: int = 40_000
num_bg: int = 100_000
num_motion_bases: int = 10
num_epochs: int = 500
port: int | None = None
vis_debug: bool = False
batch_size: int = 8
num_dl_workers: int = 4
validate_every: int = 50
save_videos_every: int = 50
use_2dgs: bool = False
def main(cfg: TrainConfig):
backup_code(cfg.work_dir)
train_dataset, train_video_view, val_img_dataset, val_kpt_dataset = (
get_train_val_datasets(cfg.data, load_val=True)
)
guru.info(f"Training dataset has {train_dataset.num_frames} frames")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# save config
os.makedirs(cfg.work_dir, exist_ok=True)
with open(f"{cfg.work_dir}/cfg.yaml", "w") as f:
yaml.dump(asdict(cfg), f, default_flow_style=False)
# if checkpoint exists
ckpt_path = f"{cfg.work_dir}/checkpoints/last.ckpt"
initialize_and_checkpoint_model(
cfg,
train_dataset,
device,
ckpt_path,
vis=cfg.vis_debug,
port=cfg.port,
)
trainer, start_epoch = Trainer.init_from_checkpoint(
ckpt_path,
device,
cfg.use_2dgs,
cfg.lr,
cfg.loss,
cfg.optim,
work_dir=cfg.work_dir,
port=cfg.port,
)
train_loader = DataLoader(
train_dataset,
batch_size=cfg.batch_size,
num_workers=cfg.num_dl_workers,
persistent_workers=True,
collate_fn=BaseDataset.train_collate_fn,
)
validator = None
if (
train_video_view is not None
or val_img_dataset is not None
or val_kpt_dataset is not None
):
validator = Validator(
model=trainer.model,
device=device,
train_loader=(
DataLoader(train_video_view, batch_size=1) if train_video_view else None
),
val_img_loader=(
DataLoader(val_img_dataset, batch_size=1) if val_img_dataset else None
),
val_kpt_loader=(
DataLoader(val_kpt_dataset, batch_size=1) if val_kpt_dataset else None
),
save_dir=cfg.work_dir,
)
guru.info(f"Starting training from {trainer.global_step=}")
for epoch in (
pbar := tqdm(
range(start_epoch, cfg.num_epochs),
initial=start_epoch,
total=cfg.num_epochs,
)
):
trainer.set_epoch(epoch)
for batch in train_loader:
batch = to_device(batch, device)
loss = trainer.train_step(batch)
pbar.set_description(f"Loss: {loss:.6f}")
if validator is not None:
if (epoch > 0 and epoch % cfg.validate_every == 0) or (
epoch == cfg.num_epochs - 1
):
val_logs = validator.validate()
trainer.log_dict(val_logs)
if (epoch > 0 and epoch % cfg.save_videos_every == 0) or (
epoch == cfg.num_epochs - 1
):
validator.save_train_videos(epoch)
def initialize_and_checkpoint_model(
cfg: TrainConfig,
train_dataset: BaseDataset,
device: torch.device,
ckpt_path: str,
vis: bool = False,
port: int | None = None,
):
if os.path.exists(ckpt_path):
guru.info(f"model checkpoint exists at {ckpt_path}")
return
fg_params, motion_bases, bg_params, tracks_3d = init_model_from_tracks(
train_dataset,
cfg.num_fg,
cfg.num_bg,
cfg.num_motion_bases,
vis=vis,
port=port,
)
# run initial optimization
Ks = train_dataset.get_Ks().to(device)
w2cs = train_dataset.get_w2cs().to(device)
run_initial_optim(fg_params, motion_bases, tracks_3d, Ks, w2cs)
if vis and cfg.port is not None:
server = get_server(port=cfg.port)
vis_init_params(server, fg_params, motion_bases)
camera_poses = init_trainable_poses(w2cs)
model = SceneModel(
Ks,
w2cs,
fg_params,
motion_bases,
camera_poses,
bg_params,
cfg.use_2dgs,
)
guru.info(f"Saving initialization to {ckpt_path}")
os.makedirs(os.path.dirname(ckpt_path), exist_ok=True)
torch.save({"model": model.state_dict(), "epoch": 0, "global_step": 0}, ckpt_path)
def init_model_from_tracks(
train_dataset,
num_fg: int,
num_bg: int,
num_motion_bases: int,
vis: bool = False,
port: int | None = None,
):
tracks_3d = TrackObservations(*train_dataset.get_tracks_3d(num_fg))
print(
f"{tracks_3d.xyz.shape=} {tracks_3d.visibles.shape=} "
f"{tracks_3d.invisibles.shape=} {tracks_3d.confidences.shape} "
f"{tracks_3d.colors.shape}"
)
if not tracks_3d.check_sizes():
import ipdb
ipdb.set_trace()
rot_type = "6d"
cano_t = int(tracks_3d.visibles.sum(dim=0).argmax().item())
guru.info(f"{cano_t=} {num_fg=} {num_bg=} {num_motion_bases=}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
motion_bases, motion_coefs, tracks_3d = init_motion_params_with_procrustes(
tracks_3d, num_motion_bases, rot_type, cano_t, vis=vis, port=port
)
motion_bases = motion_bases.to(device)
fg_params = init_fg_from_tracks_3d(cano_t, tracks_3d, motion_coefs)
fg_params = fg_params.to(device)
bg_params = None
if num_bg > 0:
bg_points = StaticObservations(*train_dataset.get_bkgd_points(num_bg))
assert bg_points.check_sizes()
bg_params = init_bg(bg_points)
bg_params = bg_params.to(device)
tracks_3d = tracks_3d.to(device)
return fg_params, motion_bases, bg_params, tracks_3d
def backup_code(work_dir):
root_dir = osp.abspath(osp.join(osp.dirname(__file__)))
tracked_dirs = [osp.join(root_dir, dirname) for dirname in ["flow3d", "scripts"]]
dst_dir = osp.join(work_dir, "code", datetime.now().strftime("%Y-%m-%d-%H%M%S"))
for tracked_dir in tracked_dirs:
if osp.exists(tracked_dir):
shutil.copytree(tracked_dir, osp.join(dst_dir, osp.basename(tracked_dir)))
if __name__ == "__main__":
main(tyro.cli(TrainConfig))