-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
65 lines (51 loc) · 2.04 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
import torch
import os
import yaml
import types
import json
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from data.lightning.MRIDataModule import MRIDataModule
from utils.parse_args import create_arg_parser
from models.lightning.rcGAN import rcGAN
from pytorch_lightning import seed_everything
from pytorch_lightning.loggers import WandbLogger
def load_object(dct):
return types.SimpleNamespace(**dct)
if __name__ == '__main__':
torch.set_float32_matmul_precision('medium')
args = create_arg_parser().parse_args()
seed_everything(0, workers=True)
print(f"Experiment Name: {args.exp_name}")
print(f"Number of GPUs: {args.num_gpus}")
if args.mri:
with open('configs/mri.yml', 'r') as f:
cfg = yaml.load(f, Loader=yaml.FullLoader)
cfg = json.loads(json.dumps(cfg), object_hook=load_object)
dm = MRIDataModule(cfg)
model = rcGAN(cfg, args.exp_name, args.num_gpus)
else:
print("No valid application selected. Please include one of the following args: --mri")
exit()
wandb_logger = WandbLogger(
project="my_project", # TODO: Change to your project name - maybe make this an arg
name=args.exp_name,
log_model="all",
save_dir=cfg.checkpoint_dir + 'wandb'
)
checkpoint_callback_epoch = ModelCheckpoint(
monitor='epoch',
mode='max',
dirpath=cfg.checkpoint_dir + args.exp_name + '/',
filename='checkpoint-{epoch}',
save_top_k=50
)
trainer = pl.Trainer(accelerator="gpu", devices=args.num_gpus, strategy='ddp',
max_epochs=cfg.num_epochs, callbacks=[checkpoint_callback_epoch],
num_sanity_val_steps=2, profiler="simple", logger=wandb_logger, benchmark=False,
log_every_n_steps=10)
if args.resume:
trainer.fit(model, dm,
ckpt_path=cfg.checkpoint_dir + args.exp_name + f'/checkpoint-epoch={args.resume_epoch}.ckpt')
else:
trainer.fit(model, dm)