-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
139 lines (104 loc) · 3.83 KB
/
evaluate.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
from __future__ import division
from math import sqrt as sqrt
from itertools import product as product
import torch
import numpy as np
import os
from xml.dom import minidom
os.environ['GLOG_minloglevel'] = '2'
import os, sys, cv2
import argparse
import shutil
import sys ,os
from drawBoxes import read_xml
import os, shutil
def files_with_ext(mypath, ext):
files = [os.path.join(mypath, f) for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f)) and os.path.splitext(os.path.join(mypath, f))[1] == ext]
return files
class PriorBox(object):
"""Compute priorbox coordinates in center-offset form for each source
feature map.
"""
def __init__(self, cfg):
super(PriorBox, self).__init__()
self.image_size = cfg['min_dim']
# number of priors for feature map location (either 4 or 6)
self.num_priors = len(cfg['aspect_ratios'])
self.variance = cfg['variance'] or [0.1]
self.feature_maps = cfg['feature_maps']
self.min_sizes = cfg['min_sizes']
self.max_sizes = cfg['max_sizes']
self.steps = cfg['steps']
self.aspect_ratios = cfg['aspect_ratios']
self.clip = cfg['clip']
self.version = cfg['name']
for v in self.variance:
if v <= 0:
raise ValueError('Variances must be greater than 0')
def forward(self):
mean = []
for k, f in enumerate(self.feature_maps):
for i, j in product(range(f), repeat=2):
f_k = self.image_size / self.steps[k]
# unit center x,y
cx = (j + 0.5) / f_k
cy = (i + 0.5) / f_k
# aspect_ratio: 1
# rel size: min_size
s_k = self.min_sizes[k]/self.image_size
# mean += [cx, cy, s_k, s_k]
# aspect_ratio: 1
# rel size: sqrt(s_k * s_(k+1))
s_k_prime = sqrt(s_k * (self.max_sizes[k]/self.image_size))
average = (s_k + s_k_prime)/2
mean += [cx, cy, average/sqrt(1.5), average*sqrt(1.5)]
# rest of aspect ratios
# for ar in self.aspect_ratios[k]:
# mean += [cx, cy, s_k*sqrt(ar), s_k/sqrt(ar)]
# mean += [cx, cy, s_k/sqrt(ar), s_k*sqrt(ar)]
# back to torch land
output = torch.Tensor(mean).view(-1, 4)
if self.clip:
output.clamp_(max=1, min=0)
return output
voc = {
'num_classes': 21,
'lr_steps': (80000, 100000, 120000),
'max_iter': 120000,
'feature_maps': [38, 19, 10, 5, 3, 1],
'min_dim': 300,
'steps': [8, 16, 32, 64, 100, 300],
'min_sizes': [0, 30, 30, 45, 45, 45], #[45, 45, 45, 45, 45, 45],
'max_sizes': [30, 45, 45, 90, 90, 90], #[75, 75, 75, 75, 75, 75],
'aspect_ratios': [[1]], #[[2], [2, 3], [2, 3], [2, 3], [2], [2]],
'variance': [0.1, 0.2],
'clip': True,
'name': 'VOC',
}
pbox = PriorBox(voc)
boxes = pbox.forward()
#boxes_numpy = boxes.squeeze()
boxes[:, 0] = boxes[:, 0] - boxes[:, 2]/2
boxes[:, 1] = boxes[:, 1] - boxes[:, 3]/2
boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
boxes_priors = boxes*300
from box_utils import match
import cv2, sys, os
images = files_with_ext(sys.argv[1], '.JPG')
xmls = files_with_ext(sys.argv[2], '.xml')
scores = 0
for image in images:
print(image)
image_name = image
xml_name = os.path.join(sys.argv[2], os.path.basename(image_name).replace('.JPG', '.xml'))
objects, width, height = read_xml(xml_name)
boxes = []
for obj in objects:
cboxes = objects[obj]
newboxes = [[int(i) for i in box] for box in cboxes]
boxes += newboxes
match_score = match(0.9, torch.FloatTensor(boxes), boxes_priors)
scores += match_score
print(match_score)
print("total scores:", scores)