-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
78 lines (63 loc) · 2.21 KB
/
train.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
import logging
import os
import hydra
import pytorch_lightning as pl
import torch
from utils.callbacks import IncreaseSequenceLengthCallback
from utils.utils import *
logger = logging.getLogger(__name__)
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
torch.set_num_threads(1)
torch.backends.cudnn.benchmark = True
@hydra.main(config_path="configs", config_name="train_defaults")
def train(cfg):
pl.seed_everything(1234)
# Update configuration dicts with common keys
propagate_keys(cfg)
logger.info("\n" + OmegaConf.to_yaml(cfg))
# Instantiate model and dataloaders
model = hydra.utils.instantiate(
cfg.model,
_recursive_=False,
)
if cfg.checkpoint_path.lower() != "none":
# This code gives error
# The classmethod TrackerNetC.load_from_checkpoint cannot be called on an instance.
# Please call it on the class type and make sure the return value is used.
'''
# Load weights
model = model.load_from_checkpoint(checkpoint_path=cfg.checkpoint_path)
'''
model = model._class_.load_from_checkpoint(checkpoint_path=cfg.checkpoint_path)
# Override stuff for fine-tuning
model.hparams.optimizer.lr = cfg.model.optimizer.lr
model.hparams.optimizer._target_ = cfg.model.optimizer._target_
model.debug = True
model.unrolls = cfg.init_unrolls
model.max_unrolls = cfg.max_unrolls
model.pose_mode = cfg.model.pose_mode
data_module = hydra.utils.instantiate(cfg.data)
# Logging
if cfg.logging:
training_logger = pl.loggers.TensorBoardLogger(
".", "", "", log_graph=True, default_hp_metric=False
)
else:
training_logger = None
# Training schedule
callbacks = [
IncreaseSequenceLengthCallback(
unroll_factor=cfg.unroll_factor, schedule=cfg.unroll_schedule
),
pl.callbacks.LearningRateMonitor(logging_interval="epoch"),
]
trainer = pl.Trainer(
**OmegaConf.to_container(cfg.trainer),
devices=[0],
accelerator="gpu",
callbacks=callbacks,
logger=training_logger
)
trainer.fit(model, datamodule=data_module)
if __name__ == "__main__":
train()