-
Notifications
You must be signed in to change notification settings - Fork 1
/
SIN_train.py
87 lines (68 loc) · 2.4 KB
/
SIN_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
import argparse
import os
import random
from shutil import copyfile
import cv2
import numpy as np
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from SIN_src.SIN_trainer import *
from SIN_src.config import Config
def main_worker(gpu, args):
rank = args.node_rank * args.gpus + gpu
torch.cuda.set_device(gpu)
if args.DDP:
dist.init_process_group(backend='nccl',
init_method='env://',
world_size=args.world_size,
rank=rank,
group_name='mtorch')
# load config file
config = Config(args.config_path)
config.MODE = 1
config.nodes = args.nodes
config.gpus = args.gpus
config.GPU_ids = args.GPU_ids
config.DDP = args.DDP
if config.DDP:
config.world_size = args.world_size
else:
config.world_size = 1
torch.backends.cudnn.benchmark = True # cudnn auto-tuner
cv2.setNumThreads(0)
# initialize random seed
torch.manual_seed(config.SEED)
torch.cuda.manual_seed_all(config.SEED)
np.random.seed(config.SEED)
random.seed(config.SEED)
# build the model and initialize
model = SINTrainer(config, gpu, rank)
# model training
if rank == 0:
config.print()
print('\nstart training...\n')
model.train()
cleanup()
def cleanup():
dist.destroy_process_group()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--config_path', type=str, default='',
help='path of configuration path')
parser.add_argument('--nodes', type=int, default=1, help='how many machines')
parser.add_argument('--gpus', type=int, default=1, help='how many GPUs in one node')
parser.add_argument('--GPU_ids', type=str, default='0')
parser.add_argument('--node_rank', type=int, default=0, help='the id of this machine')
parser.add_argument('--DDP', action='store_true', help='DDP')
args = parser.parse_args()
config_path = args.config_path
args.config_path = config_path
os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU_ids
if args.DDP:
args.world_size = args.nodes * args.gpus
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '22323'
else:
args.world_size = 1
mp.spawn(main_worker, nprocs=args.world_size, args=(args,))