-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·54 lines (42 loc) · 1.43 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
import numpy as np
import torch
import hydra
from omegaconf import OmegaConf, DictConfig
from trainer import build_trainer
import time
@hydra.main(config_path="config", config_name="config")
def main(args: DictConfig):
print(OmegaConf.to_yaml(args))
if args.cuda:
try:
args.gpu_ids = [int(s) for s in args.gpu_ids.split(',')]
except ValueError:
raise ValueError('Argument --gpu_ids must be a comma-separated list of integers only')
# set random seed
torch.autograd.set_detect_anomaly(True)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
trainer = build_trainer(args)
if args.mode == 'train':
train(args, trainer)
trainer.writer.close()
elif args.mode == 'eval':
evaluate(trainer)
trainer.writer.close()
elif args.mode == 'test':
test(trainer)
def train(args, trainer):
print('Starting Epoch:', args.start_epoch)
print('Total Epoches:', args.epochs)
for epoch in range(args.start_epoch, args.epochs):
trainer.training(epoch)
if epoch % args.eval_interval == (args.eval_interval - 1):
trainer.val(epoch)
def evaluate(trainer):
trainer.val(0)
def test(trainer):
start_time = time.time()
trainer.val(0, test=True)
print("--- %s seconds ---" % (time.time() - start_time))
if __name__ == "__main__":
main()