forked from LiyingCV/Long-Range-Grouping-Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
70 lines (55 loc) · 1.92 KB
/
runner.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
# -*- coding: utf-8 -*-
#
# Developed by Liying Yang <[email protected]>
import logging
import matplotlib
import os
import sys
import torch
# Fix problem: no $DISPLAY environment variable
matplotlib.use('Agg')
from argparse import ArgumentParser
from pprint import pprint
from config import cfg
from core.train import train_net
from core.test import test_net
def get_args_from_command_line():
parser = ArgumentParser(description='Parser of Runner')
parser.add_argument('--local_rank', dest='local_rank', type=int)
parser.add_argument('--test', dest='test', help='Test neural networks', action='store_true')
parser.add_argument('--weights', dest='weights', help='Initialize network from the weights file', default=None)
args = parser.parse_args()
return args
def init_distributed_mode():
torch.distributed.init_process_group(backend='nccl')
world_size = int(os.environ['WORLD_SIZE'])
gpu = int(os.environ['LOCAL_RANK'])
torch.cuda.set_device(gpu)
torch.distributed.barrier()
if torch.distributed.get_rank() == 0:
print('world size {}'.format(world_size))
def main():
# Get args from command line
args = get_args_from_command_line()
if args.weights is not None:
cfg.CONST.WEIGHTS = args.weights
if not args.test:
cfg.TRAIN.RESUME_TRAIN = True
# Set GPU and distributed data parallel to use
init_distributed_mode()
# Print config
if torch.distributed.get_rank() == 0:
print('Use config:')
pprint(cfg)
# Start train/test process
if not args.test:
train_net(cfg)
else:
if 'WEIGHTS' in cfg.CONST and os.path.exists(cfg.CONST.WEIGHTS):
test_net(cfg)
else:
logging.error('Please specify the file path of checkpoint.')
sys.exit(2)
if __name__ == '__main__':
logging.basicConfig(format='[%(levelname)s] %(asctime)s %(message)s', level=logging.INFO)
main()