1 # --------------------------------------------------------
3 # Copyright (c) 2015 Microsoft
4 # Licensed under The MIT License [see LICENSE for details]
5 # Written by Sergey Karayev
6 # --------------------------------------------------------
13 ctypedef np.float_t DTYPE_t
15 def bbox_overlaps_float(
16 np.ndarray[DTYPE_t, ndim=2] boxes,
17 np.ndarray[DTYPE_t, ndim=2] query_boxes):
21 boxes: (N, 4) ndarray of float
22 query_boxes: (K, 4) ndarray of float
25 overlaps: (N, K) ndarray of overlap between boxes and query_boxes
27 cdef unsigned int N = boxes.shape[0]
28 cdef unsigned int K = query_boxes.shape[0]
29 cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
30 cdef DTYPE_t iw, ih, box_area
32 cdef unsigned int k, n
35 (query_boxes[k, 2] - query_boxes[k, 0]) *
36 (query_boxes[k, 3] - query_boxes[k, 1])
40 min(boxes[n, 2], query_boxes[k, 2]) -
41 max(boxes[n, 0], query_boxes[k, 0])
45 min(boxes[n, 3], query_boxes[k, 3]) -
46 max(boxes[n, 1], query_boxes[k, 1])
50 (boxes[n, 2] - boxes[n, 0]) *
51 (boxes[n, 3] - boxes[n, 1]) +
54 # if query_boxes[k, 4] == -1:
55 # ua = float((boxes[n, 2] - boxes[n, 0])
56 # *(boxes[n, 3] - boxes[n, 1]))
59 # (boxes[n, 2] - boxes[n, 0]) *
60 # (boxes[n, 3] - boxes[n, 1]) +
63 overlaps[n, k] = iw * ih / ua
67 np.ndarray[DTYPE_t, ndim=2] boxes,
68 np.ndarray[DTYPE_t, ndim=2] query_boxes):
72 boxes: (N, 4) ndarray of float
73 query_boxes: (K, 4) ndarray of float
76 overlaps: (N, K) ndarray of overlap between boxes and query_boxes
78 cdef unsigned int N = boxes.shape[0]
79 cdef unsigned int K = query_boxes.shape[0]
80 cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
81 cdef DTYPE_t iw, ih, box_area
83 cdef unsigned int k, n
86 (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
87 (query_boxes[k, 3] - query_boxes[k, 1] + 1)
91 min(boxes[n, 2], query_boxes[k, 2]) -
92 max(boxes[n, 0], query_boxes[k, 0]) + 1
96 min(boxes[n, 3], query_boxes[k, 3]) -
97 max(boxes[n, 1], query_boxes[k, 1]) + 1
101 (boxes[n, 2] - boxes[n, 0] + 1) *
102 (boxes[n, 3] - boxes[n, 1] + 1) +
105 overlaps[n, k] = iw * ih / ua
108 def bbox_overlaps_self(
109 np.ndarray[DTYPE_t, ndim=2] boxes,
110 np.ndarray[DTYPE_t, ndim=2] query_boxes):
114 boxes: (N, 4) ndarray of float
115 query_boxes: (K, 4) ndarray of float
118 overlaps: (N, K) ndarray of overlap between boxes and query_boxes
120 cdef unsigned int N = boxes.shape[0]
121 cdef unsigned int K = query_boxes.shape[0]
122 cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
123 cdef DTYPE_t iw, ih, box_area
125 cdef unsigned int k, n
128 (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
129 (query_boxes[k, 3] - query_boxes[k, 1] + 1)
133 min(boxes[n, 2], query_boxes[k, 2]) -
134 max(boxes[n, 0], query_boxes[k, 0]) + 1
138 min(boxes[n, 3], query_boxes[k, 3]) -
139 max(boxes[n, 1], query_boxes[k, 1]) + 1
143 overlaps[n, k] = iw * ih / ua
147 def bbox_overlaps_ignore(
148 np.ndarray[DTYPE_t, ndim=2] boxes,
149 np.ndarray[DTYPE_t, ndim=2] query_boxes):
153 boxes: (N, 4) ndarray of float
154 query_boxes: (K, 4) ndarray of float
157 overlaps: (N, K) ndarray of overlap between boxes and query_boxes
159 cdef unsigned int N = boxes.shape[0]
160 cdef unsigned int K = query_boxes.shape[0]
161 cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)
162 cdef DTYPE_t iw, ih, box_area
164 cdef unsigned int k, n
167 (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
168 (query_boxes[k, 3] - query_boxes[k, 1] + 1)
172 min(boxes[n, 2], query_boxes[k, 2]) -
173 max(boxes[n, 0], query_boxes[k, 0]) + 1
177 min(boxes[n, 3], query_boxes[k, 3]) -
178 max(boxes[n, 1], query_boxes[k, 1]) + 1
181 if query_boxes[k, 4] == -1:
182 ua = float((boxes[n, 2] - boxes[n, 0] + 1)
183 *(boxes[n, 3] - boxes[n, 1] + 1))
186 (boxes[n, 2] - boxes[n, 0] + 1) *
187 (boxes[n, 3] - boxes[n, 1] + 1) +
190 overlaps[n, k] = iw * ih / ua
194 def get_assignment_overlaps(
195 np.ndarray[DTYPE_t, ndim=2] boxes,
196 np.ndarray[DTYPE_t, ndim=2] query_boxes,
198 """ Used for proposal_target_layer_ignore
201 boxes: (N, 4) ndarray of float
202 query_boxes: (K, 4) ndarray of float
205 overlaps: (N, K) ndarray of overlap between boxes and query_boxes
207 cdef unsigned int N = boxes.shape[0]
208 cdef unsigned int K = query_boxes.shape[0]
209 cdef np.ndarray[long, ndim=1] gt_assignment = np.zeros((N,), dtype=np.int)
210 cdef np.ndarray[DTYPE_t, ndim=1] max_overlaps = np.zeros((N,), dtype=DTYPE)
211 cdef DTYPE_t iw, ih, box_area
213 cdef unsigned int k, n
216 (query_boxes[k, 2] - query_boxes[k, 0] + 1) *
217 (query_boxes[k, 3] - query_boxes[k, 1] + 1)
221 min(boxes[n, 2], query_boxes[k, 2]) -
222 max(boxes[n, 0], query_boxes[k, 0]) + 1
226 min(boxes[n, 3], query_boxes[k, 3]) -
227 max(boxes[n, 1], query_boxes[k, 1]) + 1
230 if query_boxes[k, 4] == -1:
231 ua = float((boxes[n, 2] - boxes[n, 0] + 1)
232 *(boxes[n, 3] - boxes[n, 1] + 1))
233 overlap = iw * ih / ua
234 if overlap > max_overlaps[n]:
235 if query_boxes[gt_assignment[n], 4] == -1 or max_overlaps[n] < FG_THRESH:
236 max_overlaps[n] = overlap
240 (boxes[n, 2] - boxes[n, 0] + 1) *
241 (boxes[n, 3] - boxes[n, 1] + 1) +
244 overlap = iw * ih / ua
245 if overlap > max_overlaps[n]:
246 max_overlaps[n] = overlap
248 #overlaps[n, k] = overlap
249 return gt_assignment, max_overlaps