pytorch
353 строки · 12.0 Кб
1
2
3
4
5
6from caffe2.python import core
7from hypothesis import given, settings
8import caffe2.python.hypothesis_test_util as hu
9import caffe2.python.serialized_test.serialized_test_util as serial
10import hypothesis.strategies as st
11import numpy as np
12
13
14# Reference implementation from detectron/lib/utils/boxes.py
15def bbox_transform(boxes, deltas, weights=(1.0, 1.0, 1.0, 1.0)):
16"""Forward transform that maps proposal boxes to predicted ground-truth
17boxes using bounding-box regression deltas. See bbox_transform_inv for a
18description of the weights argument.
19"""
20if boxes.shape[0] == 0:
21return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)
22
23boxes = boxes.astype(deltas.dtype, copy=False)
24
25widths = boxes[:, 2] - boxes[:, 0] + 1.0
26heights = boxes[:, 3] - boxes[:, 1] + 1.0
27ctr_x = boxes[:, 0] + 0.5 * widths
28ctr_y = boxes[:, 1] + 0.5 * heights
29
30wx, wy, ww, wh = weights
31dx = deltas[:, 0::4] / wx
32dy = deltas[:, 1::4] / wy
33dw = deltas[:, 2::4] / ww
34dh = deltas[:, 3::4] / wh
35
36# Prevent sending too large values into np.exp()
37BBOX_XFORM_CLIP = np.log(1000. / 16.)
38dw = np.minimum(dw, BBOX_XFORM_CLIP)
39dh = np.minimum(dh, BBOX_XFORM_CLIP)
40
41pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]
42pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]
43pred_w = np.exp(dw) * widths[:, np.newaxis]
44pred_h = np.exp(dh) * heights[:, np.newaxis]
45
46pred_boxes = np.zeros(deltas.shape, dtype=deltas.dtype)
47# x1
48pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w
49# y1
50pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h
51# x2 (note: "- 1" is correct; don't be fooled by the asymmetry)
52pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w - 1
53# y2 (note: "- 1" is correct; don't be fooled by the asymmetry)
54pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h - 1
55
56return pred_boxes
57
58
59# Reference implementation from detectron/lib/utils/boxes.py
60def clip_tiled_boxes(boxes, im_shape):
61"""Clip boxes to image boundaries. im_shape is [height, width] and boxes
62has shape (N, 4 * num_tiled_boxes)."""
63assert (
64boxes.shape[1] % 4 == 0
65), "boxes.shape[1] is {:d}, but must be divisible by 4.".format(
66boxes.shape[1]
67)
68# x1 >= 0
69boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0)
70# y1 >= 0
71boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0)
72# x2 < im_shape[1]
73boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0)
74# y2 < im_shape[0]
75boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0)
76return boxes
77
78
79def generate_rois(roi_counts, im_dims):
80assert len(roi_counts) == len(im_dims)
81all_rois = []
82for i, num_rois in enumerate(roi_counts):
83if num_rois == 0:
84continue
85# [batch_idx, x1, y1, x2, y2]
86rois = np.random.uniform(0, im_dims[i], size=(roi_counts[i], 5)).astype(
87np.float32
88)
89rois[:, 0] = i # batch_idx
90# Swap (x1, x2) if x1 > x2
91rois[:, 1], rois[:, 3] = (
92np.minimum(rois[:, 1], rois[:, 3]),
93np.maximum(rois[:, 1], rois[:, 3]),
94)
95# Swap (y1, y2) if y1 > y2
96rois[:, 2], rois[:, 4] = (
97np.minimum(rois[:, 2], rois[:, 4]),
98np.maximum(rois[:, 2], rois[:, 4]),
99)
100all_rois.append(rois)
101if len(all_rois) > 0:
102return np.vstack(all_rois)
103return np.empty((0, 5)).astype(np.float32)
104
105
106def bbox_transform_rotated(
107boxes,
108deltas,
109weights=(1.0, 1.0, 1.0, 1.0),
110angle_bound_on=True,
111angle_bound_lo=-90,
112angle_bound_hi=90,
113):
114"""
115Similar to bbox_transform but for rotated boxes with angle info.
116"""
117if boxes.shape[0] == 0:
118return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)
119
120boxes = boxes.astype(deltas.dtype, copy=False)
121
122ctr_x = boxes[:, 0]
123ctr_y = boxes[:, 1]
124widths = boxes[:, 2]
125heights = boxes[:, 3]
126angles = boxes[:, 4]
127
128wx, wy, ww, wh = weights
129dx = deltas[:, 0::5] / wx
130dy = deltas[:, 1::5] / wy
131dw = deltas[:, 2::5] / ww
132dh = deltas[:, 3::5] / wh
133da = deltas[:, 4::5] * 180.0 / np.pi
134
135# Prevent sending too large values into np.exp()
136BBOX_XFORM_CLIP = np.log(1000. / 16.)
137dw = np.minimum(dw, BBOX_XFORM_CLIP)
138dh = np.minimum(dh, BBOX_XFORM_CLIP)
139
140pred_boxes = np.zeros(deltas.shape, dtype=deltas.dtype)
141pred_boxes[:, 0::5] = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]
142pred_boxes[:, 1::5] = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]
143pred_boxes[:, 2::5] = np.exp(dw) * widths[:, np.newaxis]
144pred_boxes[:, 3::5] = np.exp(dh) * heights[:, np.newaxis]
145
146pred_angle = da + angles[:, np.newaxis]
147if angle_bound_on:
148period = angle_bound_hi - angle_bound_lo
149assert period % 180 == 0
150pred_angle[np.where(pred_angle < angle_bound_lo)] += period
151pred_angle[np.where(pred_angle > angle_bound_hi)] -= period
152pred_boxes[:, 4::5] = pred_angle
153
154return pred_boxes
155
156
157def clip_tiled_boxes_rotated(boxes, im_shape, angle_thresh=1.0):
158"""
159Similar to clip_tiled_boxes but for rotated boxes with angle info.
160Only clips almost horizontal boxes within angle_thresh. The rest are
161left unchanged.
162"""
163assert (
164boxes.shape[1] % 5 == 0
165), "boxes.shape[1] is {:d}, but must be divisible by 5.".format(
166boxes.shape[1]
167)
168
169(H, W) = im_shape[:2]
170
171# Filter boxes that are almost upright within angle_thresh tolerance
172idx = np.where(np.abs(boxes[:, 4::5]) <= angle_thresh)
173idx5 = idx[1] * 5
174# convert to (x1, y1, x2, y2)
175x1 = boxes[idx[0], idx5] - (boxes[idx[0], idx5 + 2] - 1) / 2.0
176y1 = boxes[idx[0], idx5 + 1] - (boxes[idx[0], idx5 + 3] - 1) / 2.0
177x2 = boxes[idx[0], idx5] + (boxes[idx[0], idx5 + 2] - 1) / 2.0
178y2 = boxes[idx[0], idx5 + 1] + (boxes[idx[0], idx5 + 3] - 1) / 2.0
179# clip
180x1 = np.maximum(np.minimum(x1, W - 1), 0)
181y1 = np.maximum(np.minimum(y1, H - 1), 0)
182x2 = np.maximum(np.minimum(x2, W - 1), 0)
183y2 = np.maximum(np.minimum(y2, H - 1), 0)
184# convert back to (xc, yc, w, h)
185boxes[idx[0], idx5] = (x1 + x2) / 2.0
186boxes[idx[0], idx5 + 1] = (y1 + y2) / 2.0
187boxes[idx[0], idx5 + 2] = x2 - x1 + 1
188boxes[idx[0], idx5 + 3] = y2 - y1 + 1
189
190return boxes
191
192
193def generate_rois_rotated(roi_counts, im_dims):
194rois = generate_rois(roi_counts, im_dims)
195# [batch_id, ctr_x, ctr_y, w, h, angle]
196rotated_rois = np.empty((rois.shape[0], 6)).astype(np.float32)
197rotated_rois[:, 0] = rois[:, 0] # batch_id
198rotated_rois[:, 1] = (rois[:, 1] + rois[:, 3]) / 2. # ctr_x = (x1 + x2) / 2
199rotated_rois[:, 2] = (rois[:, 2] + rois[:, 4]) / 2. # ctr_y = (y1 + y2) / 2
200rotated_rois[:, 3] = rois[:, 3] - rois[:, 1] + 1.0 # w = x2 - x1 + 1
201rotated_rois[:, 4] = rois[:, 4] - rois[:, 2] + 1.0 # h = y2 - y1 + 1
202rotated_rois[:, 5] = np.random.uniform(-90.0, 90.0) # angle in degrees
203return rotated_rois
204
205
206class TestBBoxTransformOp(serial.SerializedTestCase):
207@given(
208num_rois=st.integers(1, 10),
209num_classes=st.integers(1, 10),
210im_dim=st.integers(100, 600),
211skip_batch_id=st.booleans(),
212rotated=st.booleans(),
213angle_bound_on=st.booleans(),
214clip_angle_thresh=st.sampled_from([-1.0, 1.0]),
215**hu.gcs_cpu_only
216)
217@settings(deadline=10000)
218def test_bbox_transform(
219self,
220num_rois,
221num_classes,
222im_dim,
223skip_batch_id,
224rotated,
225angle_bound_on,
226clip_angle_thresh,
227gc,
228dc,
229):
230"""
231Test with all rois belonging to a single image per run.
232"""
233rois = (
234generate_rois_rotated([num_rois], [im_dim])
235if rotated
236else generate_rois([num_rois], [im_dim])
237)
238box_dim = 5 if rotated else 4
239if skip_batch_id:
240rois = rois[:, 1:]
241deltas = np.random.randn(num_rois, box_dim * num_classes).astype(np.float32)
242im_info = np.array([im_dim, im_dim, 1.0]).astype(np.float32).reshape(1, 3)
243
244def bbox_transform_ref(rois, deltas, im_info):
245boxes = rois if rois.shape[1] == box_dim else rois[:, 1:]
246im_shape = im_info[0, 0:2]
247if rotated:
248box_out = bbox_transform_rotated(
249boxes, deltas, angle_bound_on=angle_bound_on
250)
251box_out = clip_tiled_boxes_rotated(
252box_out, im_shape, angle_thresh=clip_angle_thresh
253)
254else:
255box_out = bbox_transform(boxes, deltas)
256box_out = clip_tiled_boxes(box_out, im_shape)
257return [box_out]
258
259op = core.CreateOperator(
260"BBoxTransform",
261["rois", "deltas", "im_info"],
262["box_out"],
263apply_scale=False,
264correct_transform_coords=True,
265rotated=rotated,
266angle_bound_on=angle_bound_on,
267clip_angle_thresh=clip_angle_thresh,
268)
269
270self.assertReferenceChecks(
271device_option=gc,
272op=op,
273inputs=[rois, deltas, im_info],
274reference=bbox_transform_ref,
275)
276
277@given(
278roi_counts=st.lists(st.integers(0, 5), min_size=1, max_size=10),
279num_classes=st.integers(1, 10),
280rotated=st.booleans(),
281angle_bound_on=st.booleans(),
282clip_angle_thresh=st.sampled_from([-1.0, 1.0]),
283**hu.gcs_cpu_only
284)
285@settings(deadline=10000)
286def test_bbox_transform_batch(
287self,
288roi_counts,
289num_classes,
290rotated,
291angle_bound_on,
292clip_angle_thresh,
293gc,
294dc,
295):
296"""
297Test with rois for multiple images in a batch
298"""
299batch_size = len(roi_counts)
300total_rois = sum(roi_counts)
301im_dims = np.random.randint(100, 600, batch_size)
302rois = (
303generate_rois_rotated(roi_counts, im_dims)
304if rotated
305else generate_rois(roi_counts, im_dims)
306)
307box_dim = 5 if rotated else 4
308deltas = np.random.randn(total_rois, box_dim * num_classes).astype(np.float32)
309im_info = np.zeros((batch_size, 3)).astype(np.float32)
310im_info[:, 0] = im_dims
311im_info[:, 1] = im_dims
312im_info[:, 2] = 1.0
313
314def bbox_transform_ref(rois, deltas, im_info):
315box_out = []
316offset = 0
317for i, num_rois in enumerate(roi_counts):
318if num_rois == 0:
319continue
320cur_boxes = rois[offset : offset + num_rois, 1:]
321cur_deltas = deltas[offset : offset + num_rois]
322im_shape = im_info[i, 0:2]
323if rotated:
324cur_box_out = bbox_transform_rotated(
325cur_boxes, cur_deltas, angle_bound_on=angle_bound_on
326)
327cur_box_out = clip_tiled_boxes_rotated(
328cur_box_out, im_shape, angle_thresh=clip_angle_thresh
329)
330else:
331cur_box_out = bbox_transform(cur_boxes, cur_deltas)
332cur_box_out = clip_tiled_boxes(cur_box_out, im_shape)
333box_out.append(cur_box_out)
334offset += num_rois
335
336if len(box_out) > 0:
337box_out = np.vstack(box_out)
338else:
339box_out = np.empty(deltas.shape).astype(np.float32)
340return [box_out, roi_counts]
341
342op = core.CreateOperator(
343"BBoxTransform",
344["rois", "deltas", "im_info"],
345["box_out", "roi_batch_splits"],
346apply_scale=False,
347correct_transform_coords=True,
348rotated=rotated,
349angle_bound_on=angle_bound_on,
350clip_angle_thresh=clip_angle_thresh,
351)
352
353self.assertReferenceChecks(
354device_option=gc,
355op=op,
356inputs=[rois, deltas, im_info],
357reference=bbox_transform_ref,
358)
359