-
Notifications
You must be signed in to change notification settings - Fork 13
/
train_depth.py
320 lines (269 loc) · 10.6 KB
/
train_depth.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
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
"""
MIT License
Copyright (c) 2024 Mohamed El Banani
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import annotations
import os
from datetime import datetime
from pathlib import Path
import hydra
import torch
import torch.multiprocessing as mp
from hydra.utils import instantiate
from loguru import logger
from omegaconf import DictConfig, OmegaConf
from torch.distributed import destroy_process_group, init_process_group
from torch.nn.functional import interpolate
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim.lr_scheduler import LambdaLR
from tqdm import tqdm
from evals.datasets.builder import build_loader
from evals.utils.losses import DepthLoss
from evals.utils.metrics import evaluate_depth, match_scale_and_shift
from evals.utils.optim import cosine_decay_linear_warmup
def ddp_setup(rank: int, world_size: int, port: int):
"""
Args:
rank: Unique identifier of each process
world_size: Total number of processes
"""
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = str(port)
init_process_group(backend="nccl", rank=rank, world_size=world_size)
torch.cuda.set_device(rank)
def train(
model,
probe,
train_loader,
optimizer,
scheduler,
n_epochs,
detach_model,
loss_fn,
rank=0,
world_size=1,
valid_loader=None,
scale_invariant=False,
):
for ep in range(n_epochs):
if world_size > 1:
train_loader.sampler.set_epoch(ep)
train_loss = 0
pbar = tqdm(train_loader) if rank == 0 else train_loader
for i, batch in enumerate(pbar):
images = batch["image"].to(rank)
target = batch["depth"].to(rank)
optimizer.zero_grad()
if detach_model:
with torch.no_grad():
feats = model(images)
if isinstance(feats, (tuple, list)):
feats = [_f.detach() for _f in feats]
else:
feats = feats.detach()
else:
feats = model(images)
pred = probe(feats)
pred = interpolate(pred, size=target.shape[-2:], mode="bilinear")
if scale_invariant:
pred = match_scale_and_shift(pred, target)
pred = pred.clamp(min=0.001, max=10.0)
loss = loss_fn(pred, target)
loss.backward()
optimizer.step()
scheduler.step()
pr_lr = optimizer.param_groups[0]["lr"]
loss = loss.item()
train_loss += loss
if rank == 0:
_loss = train_loss / (i + 1)
pbar.set_description(
f"{ep} | loss: {loss:.4f} ({_loss:.4f}) probe_lr: {pr_lr:.2e}"
)
train_loss /= len(train_loader)
if rank == 0:
logger.info(f"train loss {ep} | {train_loss:.4f}")
if valid_loader is not None:
val_loss, val_metrics = validate(
model, probe, valid_loader, loss_fn, scale_invariant=scale_invariant
)
logger.info(f"valid loss {ep} | {val_loss:.4f}")
for metric in val_metrics:
logger.info(f"valid SA {metric:10s} | {val_metrics[metric]:.4f}")
def validate(
model, probe, loader, loss_fn, verbose=True, scale_invariant=False, aggregate=True
):
total_loss = 0.0
metrics = None
with torch.inference_mode():
pbar = tqdm(loader, desc="Evaluation") if verbose else loader
for batch in pbar:
images = batch["image"].cuda()
target = batch["depth"].cuda()
feat = model(images)
pred = probe(feat).detach()
pred = interpolate(pred, size=target.shape[-2:], mode="bilinear")
loss = loss_fn(pred, target)
total_loss += loss.item()
batch_metrics = evaluate_depth(
pred, target, scale_invariant=scale_invariant
)
if metrics is None:
metrics = {
key: [
value,
]
for key, value in batch_metrics.items()
}
else:
for key, value in batch_metrics.items():
metrics[key].append(value)
# aggregate
total_loss = total_loss / len(loader)
for key in metrics:
metric_key = torch.cat(metrics[key], dim=0)
metrics[key] = metric_key.mean() if aggregate else metric_key
return total_loss, metrics
def train_model(rank, world_size, cfg):
if world_size > 1:
ddp_setup(rank, world_size, cfg.system.port)
# ===== GET DATA LOADERS =====
# validate and test on single gpu
trainval_loader = build_loader(cfg.dataset, "trainval", cfg.batch_size, world_size)
test_loader = build_loader(cfg.dataset, "test", cfg.batch_size, 1)
trainval_loader.dataset.__getitem__(0)
# ===== Get models =====
model = instantiate(cfg.backbone)
probe = instantiate(
cfg.probe, feat_dim=model.feat_dim, max_depth=trainval_loader.dataset.max_depth
)
# setup experiment name
# === job info
timestamp = datetime.now().strftime("%d%m%Y-%H%M")
train_dset = trainval_loader.dataset.name
test_dset = test_loader.dataset.name
model_info = [
f"{model.checkpoint_name:40s}",
f"{model.patch_size:2d}",
f"{str(model.layer):5s}",
f"{model.output:10s}",
]
probe_info = [f"{probe.name:25s}"]
batch_size = cfg.batch_size * cfg.system.num_gpus
train_info = [
f"{cfg.optimizer.n_epochs:3d}",
f"{cfg.optimizer.warmup_epochs:4.2f}",
f"{str(cfg.optimizer.probe_lr):>10s}",
f"{str(cfg.optimizer.model_lr):>10s}",
f"{batch_size:4d}",
f"{train_dset:10s}",
f"{test_dset:10s}",
]
# define exp_name
exp_name = "_".join([timestamp] + model_info + probe_info + train_info)
exp_name = f"{exp_name}_{cfg.note}" if cfg.note != "" else exp_name
exp_name = exp_name.replace(" ", "") # remove spaces
# ===== SETUP LOGGING =====
if rank == 0:
exp_path = Path(__file__).parent / f"depth_exps/{exp_name}"
exp_path.mkdir(parents=True, exist_ok=True)
logger.add(exp_path / "training.log")
logger.info(f"Config: \n {OmegaConf.to_yaml(cfg)}")
# move to cuda
model = model.to(rank)
probe = probe.to(rank)
# very hacky ... SAM gets some issues with DDP finetuning
model_name = model.checkpoint_name
if "sam" in model_name or "vit-mae" in model_name:
h, w = trainval_loader.dataset.__getitem__(0)["image"].shape[-2:]
model.resize_pos_embed(image_size=(h, w))
# move to DDP
if world_size > 1:
model = DDP(model, device_ids=[rank], find_unused_parameters=True)
probe = DDP(probe, device_ids=[rank])
if cfg.optimizer.model_lr == 0:
optimizer = torch.optim.AdamW(
[{"params": probe.parameters(), "lr": cfg.optimizer.probe_lr}]
)
else:
optimizer = torch.optim.AdamW(
[
{"params": probe.parameters(), "lr": cfg.optimizer.probe_lr},
{"params": model.parameters(), "lr": cfg.optimizer.model_lr},
]
)
lambda_fn = lambda epoch: cosine_decay_linear_warmup( # noqa: E731
epoch,
cfg.optimizer.n_epochs * len(trainval_loader),
cfg.optimizer.warmup_epochs * len(trainval_loader),
)
scheduler = LambdaLR(optimizer, lr_lambda=lambda_fn)
loss_fn = DepthLoss()
train(
model,
probe,
trainval_loader,
optimizer,
scheduler,
cfg.optimizer.n_epochs,
detach_model=(cfg.optimizer.model_lr == 0),
loss_fn=loss_fn,
rank=rank,
world_size=world_size,
# valid_loader=test_loader,
)
if rank == 0:
logger.info(f"Evaluating on test split of {test_dset}")
test_sa_loss, test_sa_metrics = validate(model, probe, test_loader, loss_fn)
logger.info(f"Scale-Aware Final test loss | {test_sa_loss:.4f}")
for metric in test_sa_metrics:
logger.info(f"Final test SA {metric:10s} | {test_sa_metrics[metric]:.4f}")
results_sa = ", ".join([f"{test_sa_metrics[_m]:.4f}" for _m in test_sa_metrics])
# get scale invariant
test_si_loss, test_si_metrics = validate(
model, probe, test_loader, loss_fn, scale_invariant=True
)
logger.info(f"Scale-Invariant Final test loss | {test_si_loss:.4f}")
for metric in test_si_metrics:
logger.info(f"Final test SI {metric:10s} | {test_si_metrics[metric]:.4f}")
results_si = ", ".join([f"{test_si_metrics[_m]:.4f}" for _m in test_si_metrics])
# log experiments
exp_info = ", ".join(model_info + probe_info + train_info)
log = f"{timestamp}, {exp_info}, {results_sa}, {results_si} \n"
with open(f"depth_results_{test_dset}.log", "a") as f:
f.write(log)
# save final model
ckpt_path = exp_path / "ckpt.pth"
checkpoint = {
"cfg": cfg,
"model": model.module.state_dict(),
"probe": probe.module.state_dict(),
}
torch.save(checkpoint, ckpt_path)
logger.info(f"Saved checkpoint at {ckpt_path}")
if world_size > 1:
destroy_process_group()
@hydra.main(config_name="depth_training", config_path="./configs", version_base=None)
def main(cfg: DictConfig):
world_size = cfg.system.num_gpus
if world_size > 1:
mp.spawn(train_model, args=(world_size, cfg), nprocs=world_size)
else:
train_model(0, world_size, cfg)
if __name__ == "__main__":
main()