-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
71 lines (58 loc) · 2.17 KB
/
main.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
import torch
import argparse
import warnings
warnings.filterwarnings("ignore")
from config_.config_manager import Configuration
from src.model.model_selector import model_selector
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks import LearningRateMonitor
# parsing arguments
parser = argparse.ArgumentParser(description='Configuration : Dual-Pixel Face Reconstruction')
parser.add_argument('--config', type=str, required=True, help='config to run')
parser.add_argument('--workspace', type=str, required=True, help='workspace name')
parser.add_argument('--load_model', type=str, help='model path to load')
args = Configuration(parser.parse_args())
opt = args.get_config()
def main():
# seed initialize : for reproducibility
seed_everything(1)
# model selection
model = model_selector(opt)
# setup logger
logger = pl_loggers.TensorBoardLogger(str(opt.logger_path))
lr_monitor = LearningRateMonitor(logging_interval='step')
callbacks = [lr_monitor]
if opt.mode == 'train':
callbacks.append(ModelCheckpoint(
dirpath=str(opt.workspace_path),
filename='checkpoint_{epoch:02d}',
save_top_k=-1,
period=1
))
# run the model
runner = Trainer(
logger=logger,
checkpoint_callback=opt.mode == 'train',
callbacks=callbacks,
resume_from_checkpoint=opt.load_model if opt.load_model is not None and opt.load_strict else None, # resume training
check_val_every_n_epoch=1,
accelerator=opt.accelerator,
benchmark=True,
deterministic=False,
gpus=torch.cuda.device_count(),
precision=opt.precision,
max_epochs=opt.epoch,
sync_batchnorm=opt.sync_batch,
amp_level='O2',
profiler="pytorch"
)
if opt.mode == 'train':
runner.fit(model=model)
elif opt.mode == 'test':
runner.test(model=model, verbose=True)
else:
raise NotImplementedError('Wrong mode !!')
if __name__ == '__main__':
main()