-
Notifications
You must be signed in to change notification settings - Fork 10
/
eval.py
197 lines (185 loc) · 6.21 KB
/
eval.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
#os.environ["CUDA_VISIBLE_DEVICES"] = "1,0"
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.nn.init as init
import argparse
from torch.autograd import Variable
import torch.utils.data as data
from data import COCODetection, VOCDetection, detection_collate, BaseTransform, preproc
from layers.modules import MultiBoxLoss, HSDMultiBoxLoss
from layers.functions import Detect
from utils.nms_wrapper import nms, soft_nms
from configs.config import cfg, cfg_from_file
import numpy as np
import time
import os
import sys
import pickle
import datetime
from models.model_builder import SSD
import yaml
def arg_parse():
parser = argparse.ArgumentParser(
description='Hierachical shot detection')
parser.add_argument(
'--weights',
default='weights/hsd_vgg312_coco.pth',
type=str,
help='Trained state_dict file path to open')
parser.add_argument(
'--cfg',
dest='cfg_file',
required=True,
help='Config file for training (and optionally testing)')
parser.add_argument(
'--save_folder',
default='eval/',
type=str,
help='File path to save results')
parser.add_argument(
'--num_workers',
default=8,
type=int,
help='Number of workers used in dataloading')
parser.add_argument(
'--retest', default=False, type=bool, help='test cache results')
args = parser.parse_args()
return args
def eval_net(val_dataset,
val_loader,
net,
detector,
cfg,
transform,
max_per_image=300,
thresh=0.01,
batch_size=1):
net.eval()
num_images = len(val_dataset)
num_classes = cfg.MODEL.NUM_CLASSES
eval_save_folder = "./eval/"
if not os.path.exists(eval_save_folder):
os.mkdir(eval_save_folder)
all_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]
det_file = os.path.join(eval_save_folder, 'detections.pkl')
if args.retest:
f = open(det_file, 'rb')
all_boxes = pickle.load(f)
print('Evaluating detections')
val_dataset.evaluate_detections(all_boxes, eval_save_folder)
return
for idx, (imgs, _, img_info) in enumerate(val_loader):
with torch.no_grad():
x = imgs
x = x.cuda()
torch.cuda.synchronize()
t1 = time.time()
output = net(x)
torch.cuda.synchronize()
t4 = time.time()
boxes, scores = detector.forward(output)
torch.cuda.synchronize()
t2 = time.time()
for k in range(boxes.size(0)):
i = idx * batch_size + k
boxes_ = boxes[k]
scores_ = scores[k]
boxes_ = boxes_.cpu().numpy()
scores_ = scores_.cpu().numpy()
img_wh = img_info[k]
scale = np.array([img_wh[0], img_wh[1], img_wh[0], img_wh[1]])
boxes_ *= scale
for j in range(1, num_classes):
inds = np.where(scores_[:, j] > thresh)[0]
if len(inds) == 0:
all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
continue
c_bboxes = boxes_[inds]
c_scores = scores_[inds, j]
c_dets = np.hstack((c_bboxes,
c_scores[:, np.newaxis])).astype(
np.float32, copy=False)
keep = nms(c_dets, cfg.TEST.NMS_OVERLAP, force_cpu=True)
keep = keep[:50]
c_dets = c_dets[keep, :]
all_boxes[j][i] = c_dets
torch.cuda.synchronize()
t3 = time.time()
detect_time = t2 - t1
nms_time = t3 - t2
forward_time = t4 - t1
if idx % 10 == 0:
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s {:.3f}s'.format(
i + 1, num_images, forward_time, detect_time, nms_time))
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
val_dataset.evaluate_detections(all_boxes, eval_save_folder)
print("detect time: ", time.time() - st)
def main():
global args
args = arg_parse()
cfg_from_file(args.cfg_file)
bgr_means = cfg.TRAIN.BGR_MEAN
dataset_name = cfg.DATASETS.DATA_TYPE
batch_size = cfg.TEST.BATCH_SIZE
num_workers = args.num_workers
if cfg.DATASETS.DATA_TYPE == 'VOC':
trainvalDataset = VOCDetection
top_k = 200
else:
trainvalDataset = COCODetection
top_k = 300
dataroot = cfg.DATASETS.DATAROOT
if cfg.MODEL.SIZE == '300':
size_cfg = cfg.SMALL
else:
size_cfg = cfg.BIG
valSet = cfg.DATASETS.VAL_TYPE
num_classes = cfg.MODEL.NUM_CLASSES
save_folder = args.save_folder
if not os.path.exists(save_folder):
os.mkdir(save_folder)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
cfg.TRAIN.TRAIN_ON = False
net = SSD(cfg)
checkpoint = torch.load(args.weights)
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
detector = Detect(cfg)
ValTransform = BaseTransform(size_cfg.IMG_WH, bgr_means, (2, 0, 1))
val_dataset = trainvalDataset(dataroot, valSet, ValTransform, "val")
val_loader = data.DataLoader(
val_dataset,
batch_size,
shuffle=False,
num_workers=num_workers,
collate_fn=detection_collate)
top_k = 300
thresh = cfg.TEST.CONFIDENCE_THRESH
eval_net(
val_dataset,
val_loader,
net,
detector,
cfg,
ValTransform,
top_k,
thresh=thresh,
batch_size=batch_size)
if __name__ == '__main__':
st = time.time()
main()
print("final time", time.time() - st)