forked from dd604/refinedet.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_refinedet.py
175 lines (154 loc) · 6.31 KB
/
eval_refinedet.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
from __future__ import print_function
import sys
import os
import argparse
import numpy as np
import time
import pickle
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.utils.data as data
from PIL import Image
from libs.networks.vgg_refinedet import VGGRefineDet
from libs.networks.resnet_refinedet import ResNetRefineDet
from libs.utils.config import voc320, voc512, coco320, coco512, MEANS
from libs.data_layers.transform import detection_collate, BaseTransform
from libs.data_layers.roidb import combined_roidb, get_output_dir
from libs.data_layers.blob_dataset import BlobDataset
import pdb
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class Timer(object):
"""A simple timer."""
def __init__(self):
self.total_time = 0.
self.calls = 0
self.start_time = 0.
self.diff = 0.
self.average_time = 0.
def tic(self):
# using time.time instead of time.clock because time time.clock
# does not normalize for multithreading
self.start_time = time.time()
def toc(self, average=True):
self.diff = time.time() - self.start_time
self.total_time += self.diff
self.calls += 1
self.average_time = self.total_time / self.calls
if average:
return self.average_time
else:
return self.diff
def str2bool(v):
return v.lower() in ('yes', 'true', 't', '1')
parser = argparse.ArgumentParser(
description='RefineDet Test With Pytorch')
parser.add_argument('--dataset', default='voc', choices=['voc', 'coco'],
type=str, help='voc or coco')
parser.add_argument('--network', default='vgg16',
help='Base network')
parser.add_argument('--input_size', default=320, type=int,
help='Input size for evaluation')
parser.add_argument('--batch_size', default=1, type=int,
help='Batch size for evaluation')
parser.add_argument('--model_path', default=None, type=str,
help='Checkpoint state_dict file to test from')
parser.add_argument('--result_path', default='./detection_output', type=str,
help='Path to store detection results in evaluation')
parser.add_argument('--cuda', default=True, type=str2bool,
help='Use CUDA to evaluate model')
args = parser.parse_args()
if torch.cuda.is_available():
print('CUDA devices: ', torch.cuda.device)
print('GPU numbers: ', torch.cuda.device_count())
num_gpus = torch.cuda.device_count()
num_gpus = 1
def eval_net():
# Assign imdb_name and imdbval_name according to args.dataset.
if args.dataset == "voc":
args.imdb_name = "voc_2007_trainval+voc_2012_trainval"
args.imdbval_name = "voc_2007_test"
elif args.dataset == "coco":
args.imdb_name = "coco_2014_train+coco_2014_valminusminival"
args.imdbval_name = "coco_2014_minival"
# Import config
if args.dataset == 'coco':
cfg = (coco320, coco512)[args.input_size==512]
elif args.dataset == 'voc':
cfg = (voc320, voc512)[args.input_size==512]
# Create imdb, roidb and blob_dataset
print('Create or load an evaluted imdb.')
imdb, roidb = combined_roidb(args.imdbval_name, False)
imdb.competition_mode(on=True)
print('{:d} roidb entries'.format(len(roidb)))
blob_dataset = BlobDataset(
imdb, roidb, transform=BaseTransform(cfg['min_dim'], MEANS),
target_normalization=True)
# Construct networks.
print('Construct {}_refinedet network.'.format(args.network))
if args.network == 'vgg16':
refinedet = VGGRefineDet(cfg['num_classes'], cfg)
elif args.network == 'resnet101':
refinedet = ResNetRefineDet(cfg['num_classes'], cfg)
refinedet.create_architecture()
# For CPU
net = refinedet
# For GPU/GPUs
if args.cuda:
net = refinedet.cuda()
if num_gpus > 1:
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
# Load weights
net.load_weights(args.model_path)
net.eval()
print('Test RefineDet on:', args.imdbval_name)
print('Using the specified args:')
print(args)
num_images = len(imdb.image_index)
num_classes = imdb.num_classes
all_boxes = [[[] for _ in range(num_images)]
for _ in range(num_classes)]
empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
output_dir = get_output_dir(imdb, args.result_path)
_t = {'im_detect': Timer(), 'misc': Timer()}
det_file = os.path.join(output_dir, 'detections.pkl')
# set no grad for variables
torch.set_grad_enabled(False)
for idx in range(num_images):
img, gt, h, w = blob_dataset.pull_item(idx)
input = Variable(img.unsqueeze(0))
if args.cuda:
input = input.cuda()
# timers forward
_t['im_detect'].tic()
detection = net(input)
detect_time = _t['im_detect'].toc(average=True)
print('im_detect: {:d}/{:d} {:.3f}s'.format(
idx + 1, num_images, detect_time))
# skip jc = 0, because it's the background class
for jc in range(1, num_classes):
dets = detection[0, jc, :]
mask = dets[:, 0].gt(0.).expand(5, dets.size(0)).t()
dets = torch.masked_select(dets, mask).view(-1, 5)
if (len(dets) > 0) and (dets.dim() > 0):
boxes = dets[:, 1:]
boxes[:, 0] *= w
boxes[:, 2] *= w
boxes[:, 1] *= h
boxes[:, 3] *= h
scores = dets[:, 0].cpu().numpy()
cls_dets = np.hstack((boxes.cpu().numpy(),
scores[:, np.newaxis])).astype(np.float32,
copy=False)
all_boxes[jc][idx] = cls_dets
else:
all_boxes[jc][idx] = empty_array
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
imdb.evaluate_detections(all_boxes, output_dir)
if __name__ == '__main__':
eval_net()