def test_init_invalid_dims(self): with pytest.raises(ValueError): BBox2DList(np.random.rand(10, 3)) with pytest.raises(ValueError): BBox2DList(np.random.rand(10, 5)) with pytest.raises(ValueError): BBox2DList(np.random.rand(10, 1, 4))
def test_blank_nms(): bbl = BBox2DList([]) scores = np.random.rand(40) thresh = 0.0001 keep = nms(bbl, scores, thresh) naive_keep = naive_nms(bbl.numpy(), thresh) assert np.all(keep == naive_keep)
def test_aspect_ratio(): box = BBox2D([0, 0, 15, 15], mode=XYXY) box_ar = aspect_ratio(box, [0.5, 1, 2]) gt_box_ar = BBox2DList(np.array([[-3.5, 2., 18.5, 13.], [0., 0., 15., 15.], [2.5, -3, 12.5, 18.]]), mode=XYXY) assert box_ar == gt_box_ar
def test_multi_jaccard_index_2d(): # bounding boxes of the form (x, y, w, h) bboxes_1 = [[39, 63, 203, 112], [49, 75, 203, 125], [31, 69, 201, 125], [50, 72, 197, 121], [35, 51, 196, 110]] bboxes_2 = [[54, 66, 198, 114], [42, 78, 186, 126], [18, 63, 235, 135], [54, 72, 198, 120], [36, 60, 180, 108]] # generate the BBox2DLists a = BBox2DList(bboxes_1) b = BBox2DList(bboxes_2) # Our method iou = multi_jaccard_index_2d(a, b) # generate IoU matrix using naive implementation gt_iou = np.zeros((len(bboxes_1), len(bboxes_2))) for i, x in enumerate(bboxes_1): for j, y in enumerate(bboxes_2): bx = [x[0], x[1], x[2] + x[0] - 1, x[3] + x[1] - 1] by = [y[0], y[1], y[2] + y[0] - 1, y[3] + y[1] - 1] gt_iou[i, j] = naive_intersection_over_union(bx, by) assert gt_iou.shape == iou.shape assert np.array_equal(gt_iou, iou)
def test_multi_jaccard_index_2d_performance(): """ Test the performance of `multi_jaccard_index_2d` on 5,000 randomly sampled bounding boxes. """ # sample bounding boxes and create BBox2DList bboxes = np.random.randint(low=0, high=500, size=(5000, 4)) bbl = BBox2DList(bboxes) # time the performance start = pendulum.now() _ = multi_jaccard_index_2d(bbl, bbl) dt = pendulum.now() - start # our runtime should be less than 3 seconds for 5k boxes assert dt.microseconds < 1e6 assert dt.seconds < 3
def aspect_ratio(bbox: BBox2D, ratios): """ Enumerate box for each aspect ratio. """ cx, cy = bbox.center() w, h = bbox.w, bbox.h size = w * h size_ratios = size / ratios ws = np.round(np.sqrt(size_ratios)) hs = np.round(ws * ratios) stack = np.vstack((cx - 0.5 * (ws - 1), cy - 0.5 * (hs - 1), cx + 0.5 * (ws - 1), cy + 0.5 * (hs - 1))) boxes = BBox2DList(stack.T, two_point=True) return boxes
def aspect_ratio(bbox: BBox2D, ratios): """ Enumerate box for each aspect ratio. """ cx, cy = bbox.center() w, h = bbox.w, bbox.h size = w * h ratios = np.asarray(ratios, dtype=np.float) size_ratios = size / ratios ws = np.round(np.sqrt(size_ratios)) hs = np.round(ws * ratios) stack = np.vstack((cx - 0.5 * (ws - 1), cy - 0.5 * (hs - 1), cx + 0.5 * (ws - 1), cy + 0.5 * (hs - 1))) boxes = BBox2DList(stack.T, mode=XYXY) return boxes
def test_nms(): # bbl, scores, thresh np.random.seed(529) x1 = np.random.randint(0, 50, size=40) y1 = np.random.randint(0, 50, size=40) w = np.random.randint(0, 50, size=40) h = np.random.randint(0, 50, size=40) x2 = x1 + w - 1 y2 = y1 + h - 1 bboxes_list = np.stack((x1, y1, x2, y2), axis=1) scores = np.random.rand(40) thresh = 0.0001 dets = np.hstack((bboxes_list, scores[:, np.newaxis])) naive_keep = naive_nms(dets, thresh) # see how easy it is with `bbox` bblist = np.stack((x1, y1, w, h), axis=1) bbl = BBox2DList(bblist) keep = nms(bbl, scores, thresh) assert np.all(keep == naive_keep)
def test_box_shapes(self): n = 10 l = [BBox2D(np.random.randint(0, 1024, size=4)) for _ in range(n)] bbl = BBox2DList(l) assert bbl.shape == (n, 4) lx1 = np.array([b.x1 for b in l]) lx2 = np.array([b.x2 for b in l]) ly1 = np.array([b.y1 for b in l]) ly2 = np.array([b.y2 for b in l]) assert lx1.shape == bbl.x1.shape assert ly1.shape == bbl.y1.shape assert lx2.shape == bbl.x2.shape assert ly2.shape == bbl.y2.shape assert np.array_equal(lx1, bbl.x1) assert np.array_equal(lx2, bbl.x2) assert np.array_equal(ly1, bbl.y1) assert np.array_equal(ly2, bbl.y2) assert bbl.x1.shape == (n, )
def aspect_ratio(bbox, ratios): """ Enumerate box for each aspect ratio. Args: bbox (:py:class:`BBox2D`): 2D bounding box. ratios (:py:class:`list`): list of int/float values. """ cx, cy = bbox.center() w, h = bbox.w, bbox.h size = w * h ratios = np.asarray(ratios, dtype=np.float) size_ratios = size / ratios ws = np.round(np.sqrt(size_ratios)) hs = np.round(ws * ratios) stack = np.vstack((cx - 0.5*(ws-1), cy - 0.5*(hs-1), cx + 0.5*(ws-1), cy + 0.5*(hs-1))) boxes = BBox2DList(stack.T, mode=XYXY) return boxes
def test_mul(self): bbl = BBox2DList(np.ones((7, 4))) bbl_scaled = bbl * 11 assert np.all(bbl_scaled.bboxes == 11) bbl_scaled = 11 * bbl assert np.all(bbl_scaled.bboxes == 11)
def test_equality_invalid(self): bblist = BBox2DList(self.l) assert bblist != repr(self.bbl)
def setup_class(cls): cls.n = 10 cls.l = [ BBox2D(np.random.randint(0, 1024, size=4)) for _ in range(cls.n) ] cls.bbl = BBox2DList(cls.l)
def test_inequality(self): bbl = BBox2DList([ BBox2D(np.random.randint(0, 1024, size=4)) for _ in range(self.n) ]) assert bbl != self.bbl
def test_init(self): bbl = BBox2DList(self.bbl) assert np.array_equal(bbl.numpy(), self.bbl.numpy())
def test_invalid_mul(self): bbl = BBox2DList(np.ones((7, 4))) with pytest.raises(ValueError): bbl * "11"
dic2 = {} dic2["bbx2d"] = BBox2D(x=(bbox2[0], bbox2[1], bbox2[2], bbox2[3]), mode=1) dic2["score"] = score2 dic2["line"] = l2 dics2.append(dic2) dics_all = dics + dics2 bbx2ds = [] confidences = [] for dic in dics_all: bbx2ds.append(dic["bbx2d"]) confidences.append(dic["score"]) bbx2dlist = BBox2DList(bbx2ds, mode=1) new_boxes = nms(bbx2dlist, confidences, thresh=iou_thresh) list_final = [] for i in new_boxes: list_final.append(dics_all[i]) out_file = open("submit/fusion/{}".format(cpt.split('/')[-1]), "w") while len(list_final) > 0: asd = list_final.pop()["line"] asd = asd.split(" ") asd[-1] = asd[-1][:-1] typ = asd[0] truncated = float(asd[1]) occluded = int(asd[2])
def test_init_empty_ndarray(self): bbl = BBox2DList(np.empty((0, 4))) assert bbl.bboxes.shape == (0, 4)
def test_init_invalid_element_type(self): with pytest.raises(TypeError): BBox2DList(["1, 2, 3, 4", [1, 2, 3, 4]])
def test_repr(self): bbl = BBox2DList([[0, 0, 1, 1], [5, 5, 5, 5]]) assert repr( bbl) == "array([[0., 0., 1., 1.],\n [5., 5., 5., 5.]])"
def test_str(self): bbl = BBox2DList([[0, 0, 1, 1], [5, 5, 5, 5]]) assert str(bbl) == "[[0. 0. 1. 1.]\n [5. 5. 5. 5.]]"
def test_init_invalid(self): with pytest.raises(TypeError): BBox2DList("1, 2, 3, 4")
def test_null(self): bbl = BBox2DList([]) assert bbl.shape == (0, 4)
def test_equality(self): bblist = BBox2DList(self.l) assert bblist == self.bbl
def test_init_vector(self): bbl = BBox2DList(np.asarray([0, 1, 2, 4])) assert bbl.bboxes.shape == (1, 4)
def test_append_bboxlist(self): x = BBox2DList([[3, 7, 10, 16]], mode=XYXY) bbl = self.bbl.append(x) assert np.array_equal(bbl.bboxes, np.vstack((self.bbl.bboxes, [3, 7, 10, 16])))