/
detection.py
250 lines (187 loc) · 10.2 KB
/
detection.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import torch
import torch.nn.functional as F
import torch.distributed as dist
from ..box_utils import decode, jaccard, index2d
from utils import timer
from data import cfg, mask_type
import numpy as np
import pyximport
pyximport.install(setup_args={"include_dirs":np.get_include()}, reload_support=True)
if dist.is_initialized():
dist.barrier()
from utils.cython_nms import nms as cnms
class Detect(object):
"""At test time, Detect is the final layer of SSD. Decode location preds,
apply non-maximum suppression to location predictions based on conf
scores and threshold to a top_k number of output predictions for both
confidence score and locations, as the predicted masks.
"""
# TODO: Refactor this whole class away. It needs to go.
def __init__(self, num_classes, bkg_label, top_k, conf_thresh, nms_thresh):
self.num_classes = num_classes
self.background_label = bkg_label
self.top_k = top_k
# Parameters used in nms.
self.nms_thresh = nms_thresh
if nms_thresh <= 0:
raise ValueError('nms_threshold must be non negative.')
self.conf_thresh = conf_thresh
self.cross_class_nms = False
self.use_fast_nms = False
def __call__(self, predictions, extras=None):
"""
Args:
loc_data: (tensor) Loc preds from loc layers
Shape: [batch, num_priors, 4]
conf_data: (tensor) Shape: Conf preds from conf layers
Shape: [batch, num_priors, num_classes]
mask_data: (tensor) Mask preds from mask layers
Shape: [batch, num_priors, mask_dim]
prior_data: (tensor) Prior boxes and variances from priorbox layers
Shape: [num_priors, 4]
proto_data: (tensor) If using mask_type.lincomb, the prototype masks
Shape: [batch, mask_h, mask_w, mask_dim]
Returns:
output of shape (batch_size, top_k, 1 + 1 + 4 + mask_dim)
These outputs are in the order: class idx, confidence, bbox coords, and mask.
Note that the outputs are sorted only if cross_class_nms is False
"""
loc_data = predictions['loc']
conf_data = predictions['conf']
mask_data = predictions['mask']
prior_data = predictions['priors']
proto_data = predictions['proto'] if 'proto' in predictions else None
inst_data = predictions['inst'] if 'inst' in predictions else None
out = []
with timer.env('Detect'):
batch_size = loc_data.size(0)
num_priors = prior_data.size(0)
conf_preds = conf_data.view(batch_size, num_priors, self.num_classes).transpose(2, 1).contiguous()
for batch_idx in range(batch_size):
decoded_boxes = decode(loc_data[batch_idx], prior_data)
result = self.detect(batch_idx, conf_preds, decoded_boxes, mask_data, inst_data, extras)
if result is not None and proto_data is not None:
result['proto'] = proto_data[batch_idx]
out.append(result)
return out
def detect(self, batch_idx, conf_preds, decoded_boxes, mask_data, inst_data, extras=None):
""" Perform nms for only the max scoring class that isn't background (class 0) """
cur_scores = conf_preds[batch_idx, 1:, :]
conf_scores, _ = torch.max(cur_scores, dim=0)
keep = (conf_scores > self.conf_thresh)
scores = cur_scores[:, keep]
boxes = decoded_boxes[keep, :]
masks = mask_data[batch_idx, keep, :]
if inst_data is not None:
inst = inst_data[batch_idx, keep, :]
if scores.size(1) == 0:
return None
if self.use_fast_nms:
boxes, masks, classes, scores = self.fast_nms(boxes, masks, scores, self.nms_thresh, self.top_k)
else:
boxes, masks, classes, scores = self.traditional_nms(boxes, masks, scores, self.nms_thresh, self.conf_thresh)
return {'box': boxes, 'mask': masks, 'class': classes, 'score': scores}
def coefficient_nms(self, coeffs, scores, cos_threshold=0.9, top_k=400):
_, idx = scores.sort(0, descending=True)
idx = idx[:top_k]
coeffs_norm = F.normalize(coeffs[idx], dim=1)
# Compute the pairwise cosine similarity between the coefficients
cos_similarity = coeffs_norm @ coeffs_norm.t()
# Zero out the lower triangle of the cosine similarity matrix and diagonal
cos_similarity.triu_(diagonal=1)
# Now that everything in the diagonal and below is zeroed out, if we take the max
# of the cos similarity matrix along the columns, each column will represent the
# maximum cosine similarity between this element and every element with a higher
# score than this element.
cos_max, _ = torch.max(cos_similarity, dim=0)
# Now just filter out the ones higher than the threshold
idx_out = idx[cos_max <= cos_threshold]
# new_mask_norm = F.normalize(masks[idx_out], dim=1)
# print(new_mask_norm[:5] @ new_mask_norm[:5].t())
return idx_out, idx_out.size(0)
def fast_nms(self, boxes, masks, scores, iou_threshold:float=0.5, top_k:int=200, second_threshold:bool=False):
scores, idx = scores.sort(1, descending=True)
idx = idx[:, :top_k].contiguous()
scores = scores[:, :top_k]
num_classes, num_dets = idx.size()
boxes = boxes[idx.view(-1), :].view(num_classes, num_dets, 4)
masks = masks[idx.view(-1), :].view(num_classes, num_dets, -1)
iou = jaccard(boxes, boxes)
iou.triu_(diagonal=1)
iou_max, _ = iou.max(dim=1)
# Now just filter out the ones higher than the threshold
keep = (iou_max <= iou_threshold)
# We should also only keep detections over the confidence threshold, but at the cost of
# maxing out your detection count for every image, you can just not do that. Because we
# have such a minimal amount of computation per detection (matrix mulitplication only),
# this increase doesn't affect us much (+0.2 mAP for 34 -> 33 fps), so we leave it out.
# However, when you implement this in your method, you should do this second threshold.
if second_threshold:
keep *= (scores > self.conf_thresh)
# Assign each kept detection to its corresponding class
classes = torch.arange(num_classes, device=boxes.device)[:, None].expand_as(keep)
# This try-except block aims to fix the IndexError that we might encounter when we train on custom datasets and evaluate with TensorRT enabled. See https://github.com/haotian-liu/yolact_edge/issues/27.
try:
classes = classes[keep]
boxes = boxes[keep]
masks = masks[keep]
scores = scores[keep]
except IndexError:
from utils.logging_helper import log_once
log_once(self, "issue_27_flatten", name="yolact.layers.detect", message="Encountered IndexError as mentioned in https://github.com/haotian-liu/yolact_edge/issues/27. Flattening predictions to avoid error, please verify the outputs. If there are any problems you met related to this, please report an issue.")
classes = torch.flatten(classes, end_dim=1)
boxes = torch.flatten(boxes, end_dim=1)
masks = torch.flatten(masks, end_dim=1)
scores = torch.flatten(scores, end_dim=1)
keep = torch.flatten(keep, end_dim=1)
idx = torch.nonzero(keep, as_tuple=True)[0]
classes = torch.index_select(classes, 0, idx)
boxes = torch.index_select(boxes, 0, idx)
masks = torch.index_select(masks, 0, idx)
scores = torch.index_select(scores, 0, idx)
# Only keep the top cfg.max_num_detections highest scores across all classes
scores, idx = scores.sort(0, descending=True)
idx = idx[:cfg.max_num_detections]
scores = scores[:cfg.max_num_detections]
try:
classes = classes[idx]
boxes = boxes[idx]
masks = masks[idx]
except IndexError:
from utils.logging_helper import log_once
log_once(self, "issue_27_index_select", name="yolact.layers.detect", message="Encountered IndexError as mentioned in https://github.com/haotian-liu/yolact_edge/issues/27. Using `torch.index_select` to avoid error, please verify the outputs. If there are any problems you met related to this, please report an issue.")
classes = torch.index_select(classes, 0, idx)
boxes = torch.index_select(boxes, 0, idx)
masks = torch.index_select(masks, 0, idx)
return boxes, masks, classes, scores
def traditional_nms(self, boxes, masks, scores, iou_threshold=0.5, conf_thresh=0.05):
num_classes = scores.size(0)
idx_lst = []
cls_lst = []
scr_lst = []
# Multiplying by max_size is necessary because of how cnms computes its area and intersections
boxes = boxes * cfg.max_size
for _cls in range(num_classes):
cls_scores = scores[_cls, :]
conf_mask = cls_scores > conf_thresh
idx = torch.arange(cls_scores.size(0), device=boxes.device)
cls_scores = cls_scores[conf_mask]
idx = idx[conf_mask]
if cls_scores.size(0) == 0:
continue
preds = torch.cat([boxes[conf_mask], cls_scores[:, None]], dim=1).cpu().numpy()
keep = cnms(preds, iou_threshold)
keep = torch.Tensor(keep, device=boxes.device).long()
idx_lst.append(idx[keep])
cls_lst.append(keep * 0 + _cls)
scr_lst.append(cls_scores[keep])
idx = torch.cat(idx_lst, dim=0)
classes = torch.cat(cls_lst, dim=0)
scores = torch.cat(scr_lst, dim=0)
scores, idx2 = scores.sort(0, descending=True)
idx2 = idx2[:cfg.max_num_detections]
scores = scores[:cfg.max_num_detections]
idx = idx[idx2]
classes = classes[idx2]
# Undo the multiplication above
return boxes[idx] / cfg.max_size, masks[idx], classes, scores