forked from Ellinier/DSOD-Pytorch-Implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
214 lines (172 loc) · 7.58 KB
/
encoder.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'''Encode target locations and labels.'''
import torch
import math
import itertools
from config import cfg
class DataEncoder:
def __init__(self):
'''Compute default box sizes with scale and aspect transform.'''
scale = 300.
# steps = [s / scale for s in (8, 16, 32, 64, 100, 300)]
feature_map_sizes = (38, 19, 10, 5, 3, 1)
steps = [float(1) / s for s in feature_map_sizes]
sizes = [s / scale for s in (30, 60, 111, 162, 213, 264, 315)]
aspect_ratios = ((2,), (2,3), (2,3), (2,3), (2,), (2,))
# feature_map_sizes = (38, 19, 10, 5, 3, 1)
num_layers = len(feature_map_sizes)
boxes = []
for i in range(num_layers):
fmsize = feature_map_sizes[i]
for h,w in itertools.product(range(fmsize), repeat=2):
cx = (w + 0.5)*steps[i]
cy = (h + 0.5)*steps[i]
s = sizes[i]
boxes.append((cx, cy, s, s))
s = math.sqrt(sizes[i] * sizes[i+1])
boxes.append((cx, cy, s, s))
s = sizes[i]
for ar in aspect_ratios[i]:
boxes.append((cx, cy, s * math.sqrt(ar), s / math.sqrt(ar)))
boxes.append((cx, cy, s / math.sqrt(ar), s * math.sqrt(ar)))
self.default_boxes = torch.Tensor(boxes)
self.variances = [0.1, 0.2]
# self.variances = cfg.variances
def iou(self, box1, box2):
'''Compute the intersection over union of two set of boxes, each box is [x1,y1,x2,y2].
Args:
box1: (tensor) bounding boxes, sized [N,4].
box2: (tensor) bounding boxes, sized [M,4].
Return:
(tensor) iou, sized [N,M].
'''
N = box1.size(0)
M = box2.size(0)
lt = torch.max(
box1[:,:2].unsqueeze(1).expand(N,M,2), # [N,2] -> [N,1,2] -> [N,M,2]
box2[:,:2].unsqueeze(0).expand(N,M,2), # [M,2] -> [1,M,2] -> [N,M,2]
)
rb = torch.min(
box1[:,2:].unsqueeze(1).expand(N,M,2), # [N,2] -> [N,1,2] -> [N,M,2]
box2[:,2:].unsqueeze(0).expand(N,M,2), # [M,2] -> [1,M,2] -> [N,M,2]
)
wh = rb - lt # [N,M,2]
wh[wh<0] = 0 # clip at 0
inter = wh[:,:,0] * wh[:,:,1] # [N,M]
area1 = (box1[:,2]-box1[:,0]) * (box1[:,3]-box1[:,1]) # [N,]
area2 = (box2[:,2]-box2[:,0]) * (box2[:,3]-box2[:,1]) # [M,]
area1 = area1.unsqueeze(1).expand_as(inter) # [N,] -> [N,1] -> [N,M]
area2 = area2.unsqueeze(0).expand_as(inter) # [M,] -> [1,M] -> [N,M]
iou = inter / (area1 + area2 - inter)
return iou
def encode(self, boxes, classes, threshold=0.5):
'''Transform target bounding boxes and class labels to SSD boxes and classes.
Match each object box to all the default boxes, pick the ones with the
Jaccard-Index > 0.5:
Jaccard(A,B) = AB / (A+B-AB)
Args:
boxes: (tensor) object bounding boxes (xmin,ymin,xmax,ymax) of a image, sized [#obj, 4].
classes: (tensor) object class labels of a image, sized [#obj,].
threshold: (float) Jaccard index threshold
Returns:
boxes: (tensor) bounding boxes, sized [#obj, 8732, 4].
classes: (tensor) class labels, sized [8732,]
'''
default_boxes = self.default_boxes
num_default_boxes = default_boxes.size(0)
num_objs = boxes.size(0)
iou = self.iou( # [#obj,8732]
boxes,
torch.cat([default_boxes[:,:2] - default_boxes[:,2:]/2,
default_boxes[:,:2] + default_boxes[:,2:]/2], 1)
)
best_prior_iou, best_prior_idx = iou.max(1)
best_truth_iou, best_truth_idx = iou.max(0)
best_truth_idx.squeeze_(0)
best_truth_iou.squeeze_(0)
# print(best_prior_idx.size())
# print(best_truth_iou.size())
best_prior_idx.squeeze_(-1)
best_prior_iou.squeeze_(-1)
best_truth_iou.index_fill_(0, best_prior_idx, 2)
for j in range(best_prior_idx.size(0)):
best_truth_idx[best_prior_idx[j]] = j
# print best_truth_idx.size()
conf = 1 + classes[best_truth_idx]
conf[best_truth_iou<threshold] = 0
boxes = boxes[best_truth_idx]
# best_truth_iou, best_truth_idx = iou.max(0) # [1,8732]
# best_truth_idx.squeeze_(0) # [8732,]
# best_truth_iou.squeeze_(0) # [8732,]
# conf = 1 + classes[best_truth_idx] # [8732,], background class = 0
# conf[best_truth_iou<threshold] = 0 # background
# boxes = boxes[best_truth_idx] # [8732,4]
variances = self.variances
cxcy = (boxes[:,:2] + boxes[:,2:])/2 - default_boxes[:,:2] # [8732,2]
cxcy /= variances[0] * default_boxes[:,2:]
wh = (boxes[:,2:] - boxes[:,:2]) / default_boxes[:,2:] # [8732,2]
wh = torch.log(wh) / variances[1]
loc = torch.cat([cxcy, wh], 1) # [8732,4]
# print conf.size()
return loc, conf
def nms(self, bboxes, scores, threshold=0.5, mode='union'):
'''Non maximum suppression.
Args:
bboxes: (tensor) bounding boxes, sized [N,4].
scores: (tensor) bbox scores, sized [N,].
threshold: (float) overlap threshold.
mode: (str) 'union' or 'min'.
Returns:
keep: (tensor) selected indices.
Ref:
https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/nms/py_cpu_nms.py
'''
x1 = bboxes[:,0]
y1 = bboxes[:,1]
x2 = bboxes[:,2]
y2 = bboxes[:,3]
areas = (x2-x1) * (y2-y1)
_, order = scores.sort(0, descending=True)
keep = []
while order.numel() > 0:
i = order[0]
keep.append(i)
if order.numel() == 1:
break
xx1 = x1[order[1:]].clamp(min=x1[i])
yy1 = y1[order[1:]].clamp(min=y1[i])
xx2 = x2[order[1:]].clamp(max=x2[i])
yy2 = y2[order[1:]].clamp(max=y2[i])
w = (xx2-xx1).clamp(min=0)
h = (yy2-yy1).clamp(min=0)
inter = w*h
if mode == 'union':
ovr = inter / (areas[i] + areas[order[1:]] - inter)
elif mode == 'min':
ovr = inter / areas[order[1:]].clamp(max=areas[i])
else:
raise TypeError('Unknown nms mode: %s.' % mode)
ids = (ovr<=threshold).nonzero().squeeze()
if ids.numel() == 0:
break
order = order[ids+1]
return torch.LongTensor(keep)
def decode(self, loc, conf):
'''Transform predicted loc/conf back to real bbox locations and class labels.
Args:
loc: (tensor) predicted loc, sized [8732,4].
conf: (tensor) predicted conf, sized [8732,21].
Returns:
boxes: (tensor) bbox locations, sized [#obj, 4].
labels: (tensor) class labels, sized [#obj,1].
'''
variances = self.variances
wh = torch.exp(loc[:,2:]*variances[1]) * self.default_boxes[:,2:]
cxcy = loc[:,:2] * variances[0] * self.default_boxes[:,2:] + self.default_boxes[:,:2]
boxes = torch.cat([cxcy-wh/2, cxcy+wh/2], 1) # [8732,4]
max_conf, labels = conf.max(1) # [8732,1]
ids = labels.squeeze(1).nonzero()
if ids.numel() == 0:
return None, None, None
ids.squeeze_(1) # [#boxes,]
keep = self.nms(boxes[ids], max_conf[ids].squeeze(1), threshold=0.3)
return boxes[ids][keep], labels[ids][keep]-1, max_conf[ids][keep]