-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathval.py
116 lines (91 loc) · 3.51 KB
/
val.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import utils
import logging
import argparse
import importlib
import torch
import torch.distributed
import torch.distributed as dist
import torch.backends.cudnn as cudnn
from mmcv import Config
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import load_checkpoint
from mmdet.apis import set_random_seed, multi_gpu_test, single_gpu_test
from mmdet3d.datasets import build_dataset, build_dataloader
from mmdet3d.models import build_model
def evaluate(dataset, results):
metrics = dataset.evaluate(results, jsonfile_prefix=None)
logging.info('--- Evaluation Results ---')
for k, v in metrics.items():
logging.info('%s: %.4f' % (k, v))
return metrics
def main():
parser = argparse.ArgumentParser(description='Validate a detector')
parser.add_argument('--config', required=True)
parser.add_argument('--weights', required=True)
parser.add_argument('--local_rank', type=int, default=0)
parser.add_argument('--world_size', type=int, default=1)
parser.add_argument('--batch_size', type=int, default=1)
args = parser.parse_args()
# parse configs
cfgs = Config.fromfile(args.config)
# register custom module
importlib.import_module('models')
importlib.import_module('loaders')
# MMCV, please shut up
from mmcv.utils.logging import logger_initialized
logger_initialized['root'] = logging.Logger(__name__, logging.WARNING)
logger_initialized['mmcv'] = logging.Logger(__name__, logging.WARNING)
# you need GPUs
assert torch.cuda.is_available()
# determine local_rank and world_size
if 'LOCAL_RANK' not in os.environ:
os.environ['LOCAL_RANK'] = str(args.local_rank)
if 'WORLD_SIZE' not in os.environ:
os.environ['WORLD_SIZE'] = str(args.world_size)
local_rank = int(os.environ['LOCAL_RANK'])
world_size = int(os.environ['WORLD_SIZE'])
if local_rank == 0:
utils.init_logging(None, cfgs.debug)
else:
logging.root.disabled = True
logging.info('Using GPU: %s' % torch.cuda.get_device_name(local_rank))
torch.cuda.set_device(local_rank)
if world_size > 1:
logging.info('Initializing DDP with %d GPUs...' % world_size)
dist.init_process_group('nccl', init_method='env://')
logging.info('Setting random seed: 0')
set_random_seed(0, deterministic=True)
cudnn.benchmark = True
logging.info('Loading validation set from %s' % cfgs.data.val.data_root)
val_dataset = build_dataset(cfgs.data.val)
val_loader = build_dataloader(
val_dataset,
samples_per_gpu=args.batch_size,
workers_per_gpu=cfgs.data.workers_per_gpu,
num_gpus=world_size,
dist=world_size > 1,
shuffle=False,
seed=0,
)
logging.info('Creating model: %s' % cfgs.model.type)
model = build_model(cfgs.model)
model.cuda()
if world_size > 1:
model = MMDistributedDataParallel(model, [local_rank], broadcast_buffers=False)
else:
model = MMDataParallel(model, [0])
if os.path.isfile(args.weights):
logging.info('Loading checkpoint from %s' % args.weights)
load_checkpoint(
model, args.weights, map_location='cuda', strict=True,
logger=logging.Logger(__name__, logging.ERROR)
)
if world_size > 1:
results = multi_gpu_test(model, val_loader, gpu_collect=True)
else:
results = single_gpu_test(model, val_loader)
if local_rank == 0:
evaluate(val_dataset, results)
if __name__ == '__main__':
main()