Пример #1
0
def test_polygon_mask_rescale():
    # rescale with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    rescaled_masks = polygon_masks.rescale((56, 72))
    assert len(rescaled_masks) == 0
    assert rescaled_masks.height == 56
    assert rescaled_masks.width == 56
    assert rescaled_masks.to_ndarray().shape == (0, 56, 56)

    # rescale with polygon masks contain 3 instances
    raw_masks = [[np.array([1, 1, 3, 1, 4, 3, 2, 4, 1, 3], dtype=np.float)]]
    polygon_masks = PolygonMasks(raw_masks, 5, 5)
    rescaled_masks = polygon_masks.rescale((12, 10))
    assert len(rescaled_masks) == 1
    assert rescaled_masks.height == 10
    assert rescaled_masks.width == 10
    assert rescaled_masks.to_ndarray().shape == (1, 10, 10)
    truth = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
        np.uint8)
    assert (rescaled_masks.to_ndarray() == truth).all()
Пример #2
0
    def _load_masks(self, results):
        ann_info = results['ann_info']
        h, w = results['img_info']['height'], results['img_info']['width']
        gt_masks = ann_info['masks']
        if self.poly2mask:
            gt_masks = BitmapMasks(
                [self._poly2mask(mask, h, w) for mask in gt_masks], h, w)
        else:
            gt_masks = PolygonMasks(
                [self.process_polygons(polygons) for polygons in gt_masks], h,
                w)
        gt_masks_ignore = ann_info.get('masks_ignore', None)
        if gt_masks_ignore is not None:
            if self.poly2mask:
                gt_masks_ignore = BitmapMasks(
                    [self._poly2mask(mask, h, w) for mask in gt_masks_ignore],
                    h, w)
            else:
                gt_masks_ignore = PolygonMasks([
                    self.process_polygons(polygons)
                    for polygons in gt_masks_ignore
                ], h, w)
            results['gt_masks_ignore'] = gt_masks_ignore
            results['mask_fields'].append('gt_masks_ignore')

        results['gt_masks'] = gt_masks
        results['mask_fields'].append('gt_masks')
        return results
Пример #3
0
def test_polygon_mask_flip():
    # flip with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    flipped_masks = polygon_masks.flip(flip_direction='horizontal')
    assert len(flipped_masks) == 0
    assert flipped_masks.height == 28
    assert flipped_masks.width == 28
    assert flipped_masks.to_ndarray().shape == (0, 28, 28)

    # TODO: fixed flip correctness checking after v2.0_coord is merged
    # horizontally flip with polygon masks contain 3 instances
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    flipped_masks = polygon_masks.flip(flip_direction='horizontal')
    flipped_flipped_masks = flipped_masks.flip(flip_direction='horizontal')
    assert len(flipped_masks) == 3
    assert flipped_masks.height == 28
    assert flipped_masks.width == 28
    assert flipped_masks.to_ndarray().shape == (3, 28, 28)
    assert (polygon_masks.to_ndarray() == flipped_flipped_masks.to_ndarray()
            ).all()

    # vertically flip with polygon masks contain 3 instances
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    flipped_masks = polygon_masks.flip(flip_direction='vertical')
    flipped_flipped_masks = flipped_masks.flip(flip_direction='vertical')
    assert len(flipped_masks) == 3
    assert flipped_masks.height == 28
    assert flipped_masks.width == 28
    assert flipped_masks.to_ndarray().shape == (3, 28, 28)
    assert (polygon_masks.to_ndarray() == flipped_flipped_masks.to_ndarray()
            ).all()
Пример #4
0
def test_polygon_mask_init():
    # init with empty masks
    raw_masks = []
    polygon_masks = BitmapMasks(raw_masks, 28, 28)
    assert len(polygon_masks) == 0
    assert polygon_masks.height == 28
    assert polygon_masks.width == 28

    # init with masks contain 3 instances
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    assert isinstance(polygon_masks.masks, list)
    assert isinstance(polygon_masks.masks[0], list)
    assert isinstance(polygon_masks.masks[0][0], np.ndarray)
    assert len(polygon_masks) == 3
    assert polygon_masks.height == 28
    assert polygon_masks.width == 28
    assert polygon_masks.to_ndarray().shape == (3, 28, 28)

    # init with raw masks of unsupported type
    with pytest.raises(AssertionError):
        raw_masks = [[[]]]
        PolygonMasks(raw_masks, 28, 28)

        raw_masks = [dummy_raw_polygon_masks((3, 28, 28))]
        PolygonMasks(raw_masks, 28, 28)
Пример #5
0
def test_polygon_mask_crop():
    dummy_bbox = np.array([0, 10, 10, 27], dtype=np.int)
    # crop with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    cropped_masks = polygon_masks.crop(dummy_bbox)
    assert len(cropped_masks) == 0
    assert cropped_masks.height == 17
    assert cropped_masks.width == 10
    assert cropped_masks.to_ndarray().shape == (0, 17, 10)

    # crop with polygon masks contain 1 instances
    raw_masks = [[np.array([1., 3., 5., 1., 5., 6., 1, 6])]]
    polygon_masks = PolygonMasks(raw_masks, 7, 7)
    bbox = np.array([0, 0, 3, 4])
    cropped_masks = polygon_masks.crop(bbox)
    assert len(cropped_masks) == 1
    assert cropped_masks.height == 4
    assert cropped_masks.width == 3
    assert cropped_masks.to_ndarray().shape == (1, 4, 3)
    truth = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1], [0, 1, 1]])
    assert (cropped_masks.to_ndarray() == truth).all()

    # crop with invalid bbox
    with pytest.raises(AssertionError):
        dummy_bbox = dummy_bboxes(2, 28, 28)
        polygon_masks.crop(dummy_bbox)
Пример #6
0
def test_polygon_mask_resize():
    # resize with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    resized_masks = polygon_masks.resize((56, 72))
    assert len(resized_masks) == 0
    assert resized_masks.height == 56
    assert resized_masks.width == 72
    assert resized_masks.to_ndarray().shape == (0, 56, 72)

    # resize with polygon masks contain 1 instance 1 part
    raw_masks1 = [[np.array([1, 1, 3, 1, 4, 3, 2, 4, 1, 3], dtype=np.float)]]
    polygon_masks1 = PolygonMasks(raw_masks1, 5, 5)
    resized_masks1 = polygon_masks1.resize((10, 10))
    assert len(resized_masks1) == 1
    assert resized_masks1.height == 10
    assert resized_masks1.width == 10
    assert resized_masks1.to_ndarray().shape == (1, 10, 10)
    truth1 = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
        np.uint8)
    assert (resized_masks1.to_ndarray() == truth1).all()

    # resize with polygon masks contain 1 instance 2 part
    raw_masks2 = [[
        np.array([0., 0., 1., 0., 1., 1.]),
        np.array([1., 1., 2., 1., 2., 2., 1., 2.])
    ]]
    polygon_masks2 = PolygonMasks(raw_masks2, 3, 3)
    resized_masks2 = polygon_masks2.resize((6, 6))
    assert len(resized_masks2) == 1
    assert resized_masks2.height == 6
    assert resized_masks2.width == 6
    assert resized_masks2.to_ndarray().shape == (1, 6, 6)
    truth2 = np.array(
        [[0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0],
         [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]], np.uint8)
    assert (resized_masks2.to_ndarray() == truth2).all()

    # resize with polygon masks contain 2 instances
    raw_masks3 = [raw_masks1[0], raw_masks2[0]]
    polygon_masks3 = PolygonMasks(raw_masks3, 5, 5)
    resized_masks3 = polygon_masks3.resize((10, 10))
    assert len(resized_masks3) == 2
    assert resized_masks3.height == 10
    assert resized_masks3.width == 10
    assert resized_masks3.to_ndarray().shape == (2, 10, 10)
    truth3 = np.stack([truth1, np.pad(truth2, ((0, 4), (0, 4)), 'constant')])
    assert (resized_masks3.to_ndarray() == truth3).all()
Пример #7
0
def test_polygon_mask_to_ndarray():
    # empty polygon masks to ndarray
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    ndarray_masks = polygon_masks.to_ndarray()
    assert isinstance(ndarray_masks, np.ndarray)
    assert ndarray_masks.shape == (0, 28, 28)

    # polygon masks contain 3 instances to ndarray
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    ndarray_masks = polygon_masks.to_ndarray()
    assert isinstance(ndarray_masks, np.ndarray)
    assert ndarray_masks.shape == (3, 28, 28)
Пример #8
0
def test_polygon_mask_area():
    # area of empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    assert polygon_masks.areas.sum() == 0

    # area of polygon masks contain 1 instance
    # here we hack a case that the gap between the area of bitmap and polygon
    # is minor
    raw_masks = [[np.array([1, 1, 5, 1, 3, 4])]]
    polygon_masks = PolygonMasks(raw_masks, 6, 6)
    polygon_area = polygon_masks.areas
    bitmap_area = polygon_masks.to_bitmap().areas
    assert len(polygon_area) == 1
    assert np.isclose(polygon_area, bitmap_area).all()
Пример #9
0
def test_polygon_to_tensor():
    # empty polygon masks to tensor
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    tensor_masks = polygon_masks.to_tensor(dtype=torch.uint8, device='cpu')
    assert isinstance(tensor_masks, torch.Tensor)
    assert tensor_masks.shape == (0, 28, 28)

    # polygon masks contain 3 instances to tensor
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    tensor_masks = polygon_masks.to_tensor(dtype=torch.uint8, device='cpu')
    assert isinstance(tensor_masks, torch.Tensor)
    assert tensor_masks.shape == (3, 28, 28)
    assert (tensor_masks.numpy() == polygon_masks.to_ndarray()).all()
Пример #10
0
    def _load_masks(self, results):
        """Private function to load mask annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet.CustomDataset`.

        Returns:
            dict: The dict contains loaded mask annotations.
                If ``self.poly2mask`` is set ``True``, `gt_mask` will contain
                :obj:`PolygonMasks`. Otherwise, :obj:`BitmapMasks` is used.
        """

        h, w = results['img_info']['height'], results['img_info']['width']
        gt_masks = results['ann_info']['masks']
        if self.poly2mask:
            gt_masks = BitmapMasks(
                [self._poly2mask(mask, h, w) for mask in gt_masks], h, w)
        else:
            gt_bboxes = results['ann_info']['bboxes']
            gt_masks = PolygonMasks(
                [self.unify_polygons(polygons, gt_bboxes[i]) for i, polygons in enumerate(gt_masks)],
                h, w)
        results['gt_masks'] = gt_masks
        results['mask_fields'].append('gt_masks')
        return results
Пример #11
0
    def __call__(self, results):
        img = results['img']

        if np.random.random_sample() < self.pad_ratio:
            img, out_size = self.resize_img(img, keep_ratio=True)
            img, offset = self.square_pad(img)
        else:
            img, out_size = self.resize_img(img, keep_ratio=False)
            offset = (0, 0)

        results['img'] = img
        results['img_shape'] = img.shape

        for key in results.get('mask_fields', []):
            if len(results[key].masks) == 0:
                continue
            results[key] = results[key].resize(out_size)
            masks = results[key].masks
            processed_masks = []
            for mask in masks:
                square_pad_mask = self.square_pad_mask(mask[0], offset)
                processed_masks.append([square_pad_mask])

            results[key] = PolygonMasks(processed_masks, *(img.shape[:2]))

        return results
Пример #12
0
def rotate_polygonmask(masks, matrix, width, height):
    if len(masks) == 0:
        return masks

    points, sections, instances = [], [], []
    for i, polys_per_obj in enumerate(masks):
        for j, poly in enumerate(polys_per_obj):
            poly_points = poly.reshape(-1, 2)
            num_points = poly_points.shape[0]

            points.append(poly_points)
            sections.append(np.full((num_points, ), j))
            instances.append(np.full((num_points, ), i))
    points = np.concatenate(points, axis=0)
    sections = np.concatenate(sections, axis=0)
    instances = np.concatenate(instances, axis=0)

    points = cv2.transform(points[:, None, :], matrix)[:, 0, :]
    warpped_polygons = []
    for i in range(len(masks)):
        _points = points[instances == i]
        _sections = sections[instances == i]
        warpped_polygons.append([
            _points[_sections == j].reshape(-1) for j in np.unique(_sections)
        ])
    return PolygonMasks(warpped_polygons, height, width)
Пример #13
0
def test_square_resize_pad(mock_sample):
    results = {}
    img = np.zeros((15, 30, 3))
    polygon = np.array([10., 5., 20., 5., 20., 10., 10., 10.])
    poly_masks = PolygonMasks([[polygon]], 15, 30)
    results['img'] = img
    results['gt_masks'] = poly_masks
    results['mask_fields'] = ['gt_masks']
    srp = transforms.SquareResizePad(target_size=40, pad_ratio=0.5)

    # test resize with padding
    mock_sample.side_effect = [0.]
    output = srp(results)
    target = 4. / 3 * polygon
    target[1::2] += 10.
    assert np.allclose(output['gt_masks'].masks[0][0], target)
    assert output['img'].shape == (40, 40, 3)

    # test resize to square without padding
    results['img'] = img
    results['gt_masks'] = poly_masks
    mock_sample.side_effect = [1.]
    output = srp(results)
    target = polygon.copy()
    target[::2] *= 4. / 3
    target[1::2] *= 8. / 3
    assert np.allclose(output['gt_masks'].masks[0][0], target)
    assert output['img'].shape == (40, 40, 3)
Пример #14
0
    def __call__(self, results):
        if np.random.random_sample() < self.rotate_ratio:
            img = results['img']
            h, w = img.shape[:2]
            angle = self.sample_angle(self.max_angle)
            canvas_size = self.cal_canvas_size((h, w), angle)
            center_shift = (int(
                (canvas_size[1] - w) / 2), int((canvas_size[0] - h) / 2))

            # rotate image
            results['rotated_poly_angle'] = angle
            img = self.rotate_img(img, angle, canvas_size)
            results['img'] = img
            img_shape = img.shape
            results['img_shape'] = img_shape

            # rotate polygons
            for key in results.get('mask_fields', []):
                if len(results[key].masks) == 0:
                    continue
                masks = results[key].masks
                rotated_masks = []
                for mask in masks:
                    rotated_mask = self.rotate((w / 2, h / 2), mask[0], angle,
                                               center_shift)
                    rotated_masks.append([rotated_mask])

                results[key] = PolygonMasks(rotated_masks, *(img_shape[:2]))

        return results
Пример #15
0
def test_gen_pannet_targets(mock_show_feature):

    target_generator = textdet_targets.PANetTargets()
    assert target_generator.max_shrink == 20

    # test generate_kernels
    img_size = (3, 10)
    text_polys = [[np.array([0, 0, 1, 0, 1, 1, 0, 1])],
                  [np.array([2, 0, 3, 0, 3, 1, 2, 1])]]
    shrink_ratio = 1.0
    kernel = np.array([[1, 1, 2, 2, 0, 0, 0, 0, 0, 0],
                       [1, 1, 2, 2, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    output, _ = target_generator.generate_kernels(img_size, text_polys,
                                                  shrink_ratio)
    print(output)
    assert np.allclose(output, kernel)

    # test generate_effective_mask
    polys_ignore = text_polys
    output = target_generator.generate_effective_mask((3, 10), polys_ignore)
    target = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
                       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

    assert np.allclose(output, target)

    # test generate_targets
    results = {}
    results['img'] = np.zeros((3, 10, 3), np.uint8)
    results['gt_masks'] = PolygonMasks(text_polys, 3, 10)
    results['gt_masks_ignore'] = PolygonMasks([], 3, 10)
    results['img_shape'] = (3, 10, 3)
    results['mask_fields'] = []
    output = target_generator(results)
    assert len(output['gt_kernels']) == 2
    assert len(output['gt_mask']) == 1

    bundle = cf_bundle.CustomFormatBundle(keys=['gt_kernels', 'gt_mask'],
                                          visualize=dict(
                                              flag=True,
                                              boundary_key='gt_kernels'))
    bundle(output)
    assert 'gt_kernels' in output.keys()
    assert 'gt_mask' in output.keys()
    mock_show_feature.assert_called_once()
Пример #16
0
def test_random_crop_flip(mock_randint):
    img = np.ones((10, 10, 3), dtype=np.uint8)
    img[0, 0, :] = 0
    results = {'img': img, 'img_shape': img.shape}

    polygon = np.array([0., 0., 0., 10., 10., 10., 10., 0.])

    results['gt_masks'] = PolygonMasks([[polygon]], *(img.shape[:2]))
    results['gt_masks_ignore'] = PolygonMasks([], *(img.shape[:2]))
    results['mask_fields'] = ['gt_masks', 'gt_masks_ignore']

    crop_ratio = 1.1
    iter_num = 3
    random_crop_fliper = transforms.RandomCropFlip(crop_ratio=crop_ratio,
                                                   iter_num=iter_num)

    # test crop_target
    pad_ratio = 0.1
    h, w = img.shape[:2]
    pad_h = int(h * pad_ratio)
    pad_w = int(w * pad_ratio)
    all_polys = results['gt_masks'].masks
    h_axis, w_axis = random_crop_fliper.generate_crop_target(
        img, all_polys, pad_h, pad_w)

    assert np.allclose(h_axis, (0, 11))
    assert np.allclose(w_axis, (0, 11))

    # test __call__
    polygon = np.array([1., 1., 1., 9., 9., 9., 9., 1.])
    results['gt_masks'] = PolygonMasks([[polygon]], *(img.shape[:2]))
    results['gt_masks_ignore'] = PolygonMasks([[polygon]], *(img.shape[:2]))

    mock_randint.side_effect = [0, 1, 2]
    results = random_crop_fliper(results)

    out_img = results['img']
    out_poly = results['gt_masks'].masks[0][0]
    gt_img = img
    gt_poly = polygon

    assert np.allclose(out_img, gt_img)
    assert np.allclose(out_poly, gt_poly)
Пример #17
0
def test_polygon_mask_pad():
    # pad with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    padded_masks = polygon_masks.pad((56, 56))
    assert len(padded_masks) == 0
    assert padded_masks.height == 56
    assert padded_masks.width == 56
    assert padded_masks.to_ndarray().shape == (0, 56, 56)

    # pad with polygon masks contain 3 instances
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    padded_masks = polygon_masks.pad((56, 56))
    assert len(padded_masks) == 3
    assert padded_masks.height == 56
    assert padded_masks.width == 56
    assert padded_masks.to_ndarray().shape == (3, 56, 56)
    assert (padded_masks.to_ndarray()[:, 28:, 28:] == 0).all()
Пример #18
0
def test_dbnet_generate_targets():
    target_generator = textdet_targets.DBNetTargets()
    text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
                  [np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
    text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]

    results = {}
    results['mask_fields'] = []
    results['img_shape'] = (40, 40, 3)
    results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, 40, 40)
    results['gt_masks'] = PolygonMasks(text_polys, 40, 40)
    results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
    results['gt_labels'] = np.array([0, 1])

    target_generator.generate_targets(results)
    assert 'gt_shrink' in results['mask_fields']
    assert 'gt_shrink_mask' in results['mask_fields']
    assert 'gt_thr' in results['mask_fields']
    assert 'gt_thr_mask' in results['mask_fields']
Пример #19
0
def test_random_crop_poly_instances(mock_randint, mock_sample):
    results = {}
    img = np.zeros((30, 30, 3))
    poly_masks = PolygonMasks(
        [[np.array([5., 5., 25., 5., 25., 10., 5., 10.])],
         [np.array([5., 20., 25., 20., 25., 25., 5., 25.])]], 30, 30)
    results['img'] = img
    results['gt_masks'] = poly_masks
    results['gt_masks_ignore'] = PolygonMasks([], 30, 30)
    results['mask_fields'] = ['gt_masks', 'gt_masks_ignore']
    results['gt_labels'] = [1, 1]
    rcpi = transforms.RandomCropPolyInstances(instance_key='gt_masks',
                                              crop_ratio=1.0,
                                              min_side_ratio=0.3)

    # test sample_crop_box(img_size, results)
    mock_randint.side_effect = [0, 0, 0, 0, 30, 0, 0, 0, 15]
    crop_box = rcpi.sample_crop_box((30, 30), results)
    assert np.allclose(np.array(crop_box), np.array([0, 0, 30, 15]))

    # test __call__
    mock_randint.side_effect = [0, 0, 0, 0, 30, 0, 15, 0, 30]
    mock_sample.side_effect = [0.1]
    output = rcpi(results)
    target = np.array([5., 5., 25., 5., 25., 10., 5., 10.])
    assert len(output['gt_masks']) == 1
    assert len(output['gt_masks_ignore']) == 0
    assert np.allclose(output['gt_masks'].masks[0][0], target)
    assert output['img'].shape == (15, 30, 3)

    # test __call__ with blank instace_key masks
    mock_randint.side_effect = [0, 0, 0, 0, 30, 0, 15, 0, 30]
    mock_sample.side_effect = [0.1]
    rcpi = transforms.RandomCropPolyInstances(instance_key='gt_masks_ignore',
                                              crop_ratio=1.0,
                                              min_side_ratio=0.3)
    results['img'] = img
    results['gt_masks'] = poly_masks
    output = rcpi(results)
    assert len(output['gt_masks']) == 2
    assert np.allclose(output['gt_masks'].masks[0][0], poly_masks.masks[0][0])
    assert np.allclose(output['gt_masks'].masks[1][0], poly_masks.masks[1][0])
    assert output['img'].shape == (30, 30, 3)
Пример #20
0
def test_dbnet_ignore_texts():
    target_generator = textdet_targets.DBNetTargets()
    ignore_tags = [True, False]
    results = {}
    text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
                  [np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
    text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]

    results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, 40, 40)
    results['gt_masks'] = PolygonMasks(text_polys, 40, 40)
    results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
    results['gt_labels'] = np.array([0, 1])

    target_generator.ignore_texts(results, ignore_tags)

    assert np.allclose(results['gt_labels'], np.array([1]))
    assert len(results['gt_masks_ignore'].masks) == 2
    assert np.allclose(results['gt_masks_ignore'].masks[1][0],
                       text_polys[0][0])
    assert len(results['gt_masks'].masks) == 1
Пример #21
0
def test_polygon_mask_index():
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    # index by integer
    polygon_masks[0]
    # index by list
    polygon_masks[[0, 1]]
    # index by ndarray
    polygon_masks[np.asarray([0, 1])]
    with pytest.raises(ValueError):
        # invalid index
        polygon_masks[torch.Tensor([1, 2])]
Пример #22
0
 def _load_masks(self, results):
     h, w = results['img_info']['height'], results['img_info']['width']
     gt_masks = results['ann_info']['masks']
     if self.poly2mask:
         gt_masks = BitmapMasks(
             [self._poly2mask(mask, h, w) for mask in gt_masks], h, w)
     else:
         gt_masks = PolygonMasks(
             [self.process_polygons(polygons) for polygons in gt_masks], h,
             w)
     results['gt_masks'] = gt_masks
     results['mask_fields'].append('gt_masks')
     return results
Пример #23
0
    def _load_masks(self, results):
        """Private function to load mask annotations.

        Args:
            results (dict): Result dict from :obj:`mmdet.CustomDataset`.

        Returns:
            dict: The dict contains loaded mask annotations.
                If ``self.poly2mask`` is set ``True``, `gt_mask` will contain
                :obj:`PolygonMasks`. Otherwise, :obj:`BitmapMasks` is used.
        """

        h, w = results['img_info']['height'], results['img_info']['width']
        ph, pw = results['paste_image_info']['height'], results[
            'paste_image_info']['width']
        gt_masks = results['ann_info']['masks']
        paste_masks = results['paste_masks']
        if self.poly2mask:
            gt_masks = BitmapMasks(
                [self._poly2mask(mask, h, w) for mask in gt_masks], h, w)
            paste_masks = BitmapMasks(
                [self._poly2mask(mask, ph, pw) for mask in paste_masks], ph,
                pw)
        elif self.OC2mask:
            gt_masks = BitmapMasks(
                [self.ochuman2mask(mask) for mask in gt_masks], h, w)
        else:
            gt_masks = PolygonMasks(
                [self.process_polygons(polygons) for polygons in gt_masks], h,
                w)
            paste_masks = PolygonMasks(
                [self.process_polygons(polygons) for polygons in paste_masks],
                ph, pw)

        results['gt_masks'] = gt_masks
        results['paste_masks'] = paste_masks
        results['mask_fields'].append('gt_masks')
        #results['mask_fields'].append('paste_masks')
        return results
Пример #24
0
def test_fcenet_generate_targets():
    fourier_degree = 5
    target_generator = textdet_targets.FCENetTargets(
        fourier_degree=fourier_degree)

    h, w, c = (64, 64, 3)
    text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
                  [np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
    text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]

    results = {}
    results['mask_fields'] = []
    results['img_shape'] = (h, w, c)
    results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, h, w)
    results['gt_masks'] = PolygonMasks(text_polys, h, w)
    results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
    results['gt_labels'] = np.array([0, 1])

    target_generator.generate_targets(results)
    assert 'p3_maps' in results.keys()
    assert 'p4_maps' in results.keys()
    assert 'p5_maps' in results.keys()
Пример #25
0
def bbox2mask(bboxes, w, h, mask_type='polygon'):
    polys = bt.bbox2type(bboxes, 'poly')
    assert mask_type in ['polygon', 'bitmap']
    if mask_type == 'bitmap':
        masks = []
        for poly in polys:
            rles = maskUtils.frPyObjects([poly.tolist()], h, w)
            masks.append(maskUtils.decode(rles[0]))
        gt_masks = BitmapMasks(masks, h, w)

    else:
        gt_masks = PolygonMasks([[poly] for poly in polys], h, w)
    return gt_masks
Пример #26
0
def test_dbnet_targets_find_invalid():
    target_generator = textdet_targets.DBNetTargets()
    assert target_generator.shrink_ratio == 0.4
    assert target_generator.thr_min == 0.3
    assert target_generator.thr_max == 0.7

    results = {}
    text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
                  [np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
    results['gt_masks'] = PolygonMasks(text_polys, 40, 40)

    ignore_tags = target_generator.find_invalid(results)
    assert np.allclose(ignore_tags, [False, False])
Пример #27
0
def test_polygon_mask_crop_and_resize():
    dummy_bbox = dummy_bboxes(5, 28, 28)
    inds = np.random.randint(0, 3, (5, ))

    # crop and resize with empty polygon masks
    raw_masks = dummy_raw_polygon_masks((0, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    cropped_resized_masks = polygon_masks.crop_and_resize(
        dummy_bbox, (56, 56), inds)
    assert len(cropped_resized_masks) == 0
    assert cropped_resized_masks.height == 56
    assert cropped_resized_masks.width == 56
    assert cropped_resized_masks.to_ndarray().shape == (0, 56, 56)

    # crop and resize with polygon masks contain 3 instances
    raw_masks = dummy_raw_polygon_masks((3, 28, 28))
    polygon_masks = PolygonMasks(raw_masks, 28, 28)
    cropped_resized_masks = polygon_masks.crop_and_resize(
        dummy_bbox, (56, 56), inds)
    assert len(cropped_resized_masks) == 5
    assert cropped_resized_masks.height == 56
    assert cropped_resized_masks.width == 56
    assert cropped_resized_masks.to_ndarray().shape == (5, 56, 56)
Пример #28
0
    def _load_masks(self, results):
        ann_info = results['ann_info']
        h, w = results['img_info']['height'], results['img_info']['width']
        if self.use_img_shape:
            if results.get('ori_shape', None):
                h, w = results['ori_shape'][:2]
                results['img_info']['height'] = h
                results['img_info']['width'] = w
            else:
                warnings.warn('"ori_shape" not in results, use the shape '
                              'in "img_info" instead.')
        gt_masks = ann_info['masks']
        if self.poly2mask:
            gt_masks = BitmapMasks(
                [self._poly2mask(mask, h, w) for mask in gt_masks], h, w)
        else:
            gt_masks = PolygonMasks(
                [self.process_polygons(polygons) for polygons in gt_masks], h,
                w)
        gt_masks_ignore = ann_info.get('masks_ignore', None)
        if gt_masks_ignore is not None:
            if self.poly2mask:
                gt_masks_ignore = BitmapMasks(
                    [self._poly2mask(mask, h, w) for mask in gt_masks_ignore],
                    h, w)
            else:
                gt_masks_ignore = PolygonMasks([
                    self.process_polygons(polygons)
                    for polygons in gt_masks_ignore
                ], h, w)
            results['gt_masks_ignore'] = gt_masks_ignore
            results['mask_fields'].append('gt_masks_ignore')

        results['gt_masks'] = gt_masks
        results['mask_fields'].append('gt_masks')
        return results
Пример #29
0
 def dummy_masks(h, w, num_obj=3, mode='bitmap'):
     assert mode in ('polygon', 'bitmap')
     if mode == 'bitmap':
         masks = np.random.randint(0, 2, (num_obj, h, w), dtype=np.uint8)
         masks = BitmapMasks(masks, h, w)
     else:
         masks = []
         for i in range(num_obj):
             masks.append([])
             masks[-1].append(
                 np.random.uniform(0, min(h - 1, w - 1), (8 + 4 * i, )))
             masks[-1].append(
                 np.random.uniform(0, min(h - 1, w - 1), (10 + 4 * i, )))
         masks = PolygonMasks(masks, h, w)
     return masks
Пример #30
0
def test_random_rotate_poly_instances(mock_sample):
    results = {}
    img = np.zeros((30, 30, 3))
    poly_masks = PolygonMasks(
        [[np.array([10., 10., 20., 10., 20., 20., 10., 20.])]], 30, 30)
    results['img'] = img
    results['gt_masks'] = poly_masks
    results['mask_fields'] = ['gt_masks']
    rrpi = transforms.RandomRotatePolyInstances(rotate_ratio=1.0, max_angle=90)

    mock_sample.side_effect = [0., 1.]
    output = rrpi(results)
    assert np.allclose(output['gt_masks'].masks[0][0],
                       np.array([10., 20., 10., 10., 20., 10., 20., 20.]))
    assert output['img'].shape == (30, 30, 3)