-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
157 lines (113 loc) · 5.66 KB
/
test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import argparse
import os
import time
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import yaml
from torch.nn.parallel import DistributedDataParallel as DDP
from data.coco import COCODetection
from data.utils.data_aug import SSDTransformer
from data.utils.utils import DefaultBoxes, Encoder
from ssd.evaluate import evaluate
from ssd.model import SSD300, ResNet
from ssd.train_utils import load_checkpoint
from utils.Logger import Logger
from utils.multi_gpu import init_distributed_mode
# https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/Detection/SSD
def main():
parser = argparse.ArgumentParser(description='Train Single Shot MultiBox Detector on COCO')
parser.add_argument('--model_name', default='SSD300', type=str,
help='The model name')
parser.add_argument('--model_config', default='configs/SSD300.yaml',
metavar='FILE', help='path to model cfg file', type=str,)
parser.add_argument('--data_config', default='data/coco.yaml',
metavar='FILE', help='path to data cfg file', type=str,)
parser.add_argument('--device_gpu', default='1', type=str,
help='Cuda device, i.e. 0 or 0,1,2,3')
parser.add_argument('--checkpoint', default='nvidia_ssdpyt_fp16_190826.pt', help='The checkpoint path')
# Hyperparameters
parser.add_argument('--batch_size', '--bs', type=int, default=64,
help='number of examples for each iteration')
parser.add_argument('--num_workers', type=int, default=8)
parser.add_argument('--backbone', type=str, default='resnet50',
choices=['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152'])
parser.add_argument('--backbone-path', type=str, default=None,
help='Path to chekcpointed backbone. It should match the'
' backbone model declared with the --backbone argument.'
' When it is not provided, pretrained model from torchvision'
' will be downloaded.')
# Multi Gpu
parser.add_argument('--multi_gpu', default=False, type=bool,
help='Whether to use multi gpu to train the model, if use multi gpu, please use by sh.')
#others
parser.add_argument('--amp', action='store_true', default = False,
help='Whether to enable AMP ops. When false, uses TF32 on A100 and FP32 on V100 GPUS.')
args = parser.parse_args()
data_cfg_path = open(args.data_config)
# 引入EasyDict 可以让你像访问属性一样访问dict里的变量。
from easydict import EasyDict as edict
data_cfg = yaml.full_load(data_cfg_path)
data_cfg = edict(data_cfg)
args.data = data_cfg
cfg_path = open(args.model_config)
cfg = yaml.full_load(cfg_path)
cfg = edict(cfg)
args.model = cfg
# Initialize Multi GPU
if args.multi_gpu == True :
init_distributed_mode(args)
else:
# Use Single Gpu
os.environ['CUDA_VISIBLE_DEVICES'] = args.device_gpu
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Using {device} device')
args.device = device
args.NUM_gpu = 1
args.local_rank = 0
#The learning rate is automatically scaled
# (in other words, multiplied by the number of GPUs and multiplied by the batch size divided by 32).
#Logger
log_path = 'Test-{}-lr-{}-{}'.format(args.model_name, data_cfg.NAME, time.strftime('%Y%m%d-%H'))
log = Logger('logs/'+log_path+'.log',level='debug')
#Initial Logging
if args.local_rank == 0:
log.logger.info('gpu device = %s' % args.device_gpu)
log.logger.info('args = %s', args)
log.logger.info('data_cfgs = %s', data_cfg)
if torch.cuda.is_available():
# This flag allows you to enable the inbuilt cudnn auto-tuner to
# find the best algorithm to use for your hardware.
torch.backends.cudnn.benchmark = True
# Pre dataset dboxes : 8732 default box
dboxes = DefaultBoxes(args.model.figsize, args.model.feat_size,
args.model.steps, args.model.scales, args.model.aspect_ratios)
encoder = Encoder(dboxes)
val_dataset = COCODetection(root=args.data.DATASET_PATH,image_set='val2017',
transform=SSDTransformer(dboxes, val=True))
if args.multi_gpu:
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
else:
val_sampler = None
val_loader = torch.utils.data.DataLoader(val_dataset,
batch_size=args.batch_size,
shuffle=False, # Note: distributed sampler is shuffled :(
sampler=val_sampler,
num_workers=args.num_workers)
cocoGt = val_dataset.coco
inv_map = {v: k for k, v in val_dataset.label_map.items()} # label map 90 ids -> 80 classes
# Load model
ssd300 = SSD300(backbone=ResNet(args.backbone, args.backbone_path))
ssd300 = ssd300.cuda()
if args.multi_gpu:
# DistributedDataParallel
ssd300 = DDP(ssd300, device_ids=[args.local_rank], output_device=args.local_rank)
if args.checkpoint is not None:
if os.path.isfile(args.checkpoint):
load_checkpoint(ssd300.module if args.multi_gpu else ssd300, args.checkpoint)
else:
print('Provided checkpoint is not path to a file')
return
acc = evaluate(ssd300, val_loader, cocoGt, encoder, inv_map, args, log)
if __name__ == '__main__':
main()