Example #1
0
    def test_transform_crop_random_crop_fixed_aspect_ratio_scale_offset(self):
        aug = tf_crop.RandomCropFixedAspectRatio(
            [1.0 / 2], scale_range=[0.5, 0.5], offset_scale_range=[-0.5, -0.5])
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)
        sem_seg[5, 4] = 1
        sem_seg[10, 13] = 1
        sem_seg[5:11, 4:14] = 1
        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertArrayEqual(mask_xywh, torch.Tensor([4, 5, 10, 6]))

        trans = aug.get_transform(img, sem_seg)
        self.assertArrayEqual(trans.src_rect,
                              torch.Tensor([1.5, 0.0, 6.5, 10.0]))
        self.assertArrayEqual(trans.output_size, torch.Tensor([10, 5]))

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, torch.Tensor([10, 5, 3]))
        self.assertEqual(np.unique(out_img), 1)

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([10, 5]))
        self.assertEqual(np.unique(out_mask[6:, 3:]), 1)
Example #2
0
    def test_transform_crop_random_crop_fixed_aspect_ratio(self):
        aug = tf_crop.RandomCropFixedAspectRatio([1.0 / 2])
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)
        sem_seg[5, 4] = 1
        sem_seg[10, 13] = 1
        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertArrayEqual(mask_xywh, torch.Tensor([4, 5, 10, 6]))

        trans = aug.get_transform(img, sem_seg)
        self.assertArrayEqual(trans.src_rect, torch.Tensor([4, -2, 14, 18]))
        self.assertArrayEqual(trans.output_size, torch.Tensor([20, 10]))

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, torch.Tensor([20, 10, 3]))

        self.assertArrayEqual(np.unique(out_img[2:13, :, :]),
                              torch.Tensor([1]))
        self.assertArrayEqual(np.unique(out_img[0:2, :, :]), torch.Tensor([0]))
        self.assertArrayEqual(np.unique(out_img[13:, :, :]), torch.Tensor([0]))

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([20, 10]))
        self.assertEqual(out_mask[7, 0], 1)
        self.assertEqual(out_mask[12, -1], 1)
Example #3
0
 def test_get_box_from_mask(self):
     img_w, img_h = 8, 6
     mask = np.zeros([img_h, img_w])
     self.assertEqual(mask.shape, (img_h, img_w))
     mask[2:4, 3:6] = 1
     box = bu.get_box_from_mask(mask)
     self.assertEqual(box, (3, 2, 3, 2))
Example #4
0
 def test_get_box_from_mask_union(self):
     img_w, img_h = 8, 6
     mask = np.zeros([img_h, img_w])
     self.assertEqual(mask.shape, (img_h, img_w))
     mask[2:4, 1:4] = 1
     mask[5:6, 4:8] = 1
     box = bu.get_box_from_mask(mask)
     self.assertEqual(box, (1, 2, 7, 4))
Example #5
0
    def test_transform_crop_random_crop_fixed_aspect_ratio_empty_mask(self):
        """The sem_mask is empty (the whole image is background)"""
        aug = tf_crop.RandomCropFixedAspectRatio([1.0 / 2])
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)

        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertEqual(mask_xywh, None)

        trans = aug.get_transform(img, sem_seg)
        self.assertIsInstance(trans, tf_crop.NoOpTransform)

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, img.shape)

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, sem_seg.shape)
Example #6
0
def _get_boxes_from_image(image, scale_xy=None):
    """Extract boxes from image created by `_get_image_with_box()`"""
    cur_img_int = ((image / 10.0 + 0.5).int().float() * 10.0).int()
    values = torch.unique(cur_img_int)
    gt_values = [x * 10 for x in range(len(values))]
    assert set(values.tolist()) == set(gt_values)
    boxes = []
    for idx in range(cur_img_int.shape[0]):
        val = torch.unique(cur_img_int[idx]).tolist()
        val = max(val)
        if val == 0:
            continue
        # mask = (cur_img_int[idx, :, :] == val).int()
        mask = (cur_img_int[idx, :, :] > 0).int()
        box_xywh = bu.get_box_from_mask(mask.numpy())
        boxes.append(bu.to_boxes_from_xywh(box_xywh))
    ret = Boxes.cat(boxes)
    if scale_xy is not None:
        ret.scale(*scale_xy)
    return ret
Example #7
0
 def test_get_box_from_mask_empty(self):
     img_w, img_h = 8, 6
     mask = np.zeros([img_h, img_w])
     box = bu.get_box_from_mask(mask)
     self.assertIsNone(box)