-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
94 lines (78 loc) · 2.81 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import sys
import argparse
from utils.config import get_config
from utils.setup import setup
from utils.logger import get_logger
from trainer import Trainer
def parse_args():
parser = argparse.ArgumentParser(description='PaddleEBM')
parser.add_argument('-c',
'--config-file',
metavar="FILE",
help='config file path')
# cuda setting
parser.add_argument('--no-cuda',
action='store_true',
default=False,
help='disables CUDA training')
# checkpoint and log
parser.add_argument('--resume',
type=str,
default=None,
help='put the path to resuming file if needed')
parser.add_argument('--load',
type=str,
default=None,
help='put the path to resuming file if needed')
# for evaluation
parser.add_argument('--val-interval',
type=int,
default=1,
help='run validation every interval')
parser.add_argument('--evaluate-only',
action='store_true',
default=False,
help='skip validation during training')
# config options
parser.add_argument('opts',
help='See config for all options',
default=None,
nargs=argparse.REMAINDER)
#for inference
parser.add_argument("--source_path",
default="",
metavar="FILE",
help="path to source image")
parser.add_argument("--reference_dir",
default="",
help="path to reference images")
parser.add_argument("--model_path", default=None, help="model for loading")
args = parser.parse_args()
return args
def main(args, cfg):
# init environment, include logger, dynamic graph, seed, device, train or test mode...
setup(args, cfg)
logger = get_logger()
logger.info(cfg)
# build trainer
trainer = Trainer(cfg)
# continue train or evaluate, checkpoint need contain epoch and optimizer info
if args.resume:
trainer.resume(args.resume)
# evaluate or finute, only load generator weights
elif args.load:
trainer.load(args.load)
if args.evaluate_only:
trainer.test()
return
# training, when keyboard interrupt save weights
try:
trainer.train()
except KeyboardInterrupt as e:
trainer.save(trainer.current_epoch)
trainer.close()
if __name__ == '__main__':
args = parse_args()
cfg = get_config(args.config_file)
main(args, cfg)