Exemplo n.º 1
0
def viz_coco_example(img, imginfo, annotations, classnames):
    height, width = imginfo['height'], imginfo['width']
    assert (img.shape[0] == height)
    assert (img.shape[1] == width)
    masks = SegmentationMask([ann['segmentation'] for ann in annotations],
                             size=(width, height),
                             mode='poly')
    bboxes = []
    classids = []
    for ann in annotations:
        x, y, width, height = ann['bbox']
        x1, y1, x2, y2 = x, y, x + width, y + height
        bboxes.append([y1, x1, y2, x2])
        classids.append(ann['category_id'] -
                        1)  # category_ids are 1-indexed in COCO datasets
    classids = np.array(list(map(int, classids)))
    bboxes = np.stack(bboxes, axis=0)
    masks = masks.get_mask_tensor().numpy()
    if masks.ndim == 2:
        masks = masks[np.newaxis, :, :]
    masks = np.transpose(masks, (1, 2, 0))
    display_instances(img,
                      boxes=bboxes,
                      masks=masks,
                      class_ids=classids,
                      class_names=classnames)
Exemplo n.º 2
0
class TestSegmentationMask(unittest.TestCase):
    def __init__(self, method_name='runTest'):
        super(TestSegmentationMask, self).__init__(method_name)
        poly = [[
            [
                423.0, 306.5, 406.5, 277.0, 400.0, 271.5, 389.5, 277.0, 387.5,
                292.0, 384.5, 295.0, 374.5, 220.0, 378.5, 210.0, 391.0, 200.5,
                404.0, 199.5, 414.0, 203.5, 425.5, 221.0, 438.5, 297.0, 423.0,
                306.5
            ],
            [100, 100, 200, 100, 200, 200, 100, 200],
        ]]
        # poly[0][0] = [round(x) for x in poly[0][0]]
        width = 640
        height = 480
        size = width, height

        self.P = SegmentationMask(poly, size, 'poly')
        self.M = SegmentationMask(poly, size, 'poly').convert('mask')

    def L1(self, A, B):
        diff = A.get_mask_tensor() - B.get_mask_tensor()
        diff = torch.sum(torch.abs(diff.float())).item()
        return diff

    def test_convert(self):
        M_hat = self.M.convert('poly').convert('mask')
        P_hat = self.P.convert('mask').convert('poly')

        diff_mask = self.L1(self.M, M_hat)
        diff_poly = self.L1(self.P, P_hat)
        self.assertTrue(diff_mask == diff_poly)
        print(diff_mask, diff_poly)
        # print(self.P.instances.polygons[0].polygons)
        # print(M_hat.instances)
        # print(P_hat.instances)
        # print(P_hat.instances.polygons[0].polygons)
        self.assertTrue(diff_mask <= 8169.)
        self.assertTrue(diff_poly <= 8169.)

    def test_crop(self):
        box = [400, 250, 500, 300]  # xyxy
        diff = self.L1(self.M.crop(box), self.P.crop(box))
        print(diff)
        self.assertTrue(diff <= 1.)

    def test_resize(self):
        new_size = 50, 25
        M_hat = self.M.resize(new_size)
        P_hat = self.P.resize(new_size)
        diff = M_hat.get_mask_tensor() - P_hat.get_mask_tensor()
        print(diff.shape, diff.min(), diff.max())
        print(diff.sum())
        diff = self.L1(M_hat, P_hat)
        print(M_hat.size)
        print(P_hat.size)
        print(self.M.get_mask_tensor().sum())
        print(self.P.get_mask_tensor().sum())
        print(M_hat.get_mask_tensor().sum())
        print(P_hat.get_mask_tensor().sum())
        print(P_hat.instances.polygons[0].polygons)
        torch.set_printoptions(profile="full")
        print(M_hat.instances.masks)
        print(P_hat.get_mask_tensor())
        torch.set_printoptions(profile="default")

        self.assertTrue(self.M.size == self.P.size)
        self.assertTrue(M_hat.size == P_hat.size)
        self.assertTrue(self.M.size != M_hat.size)
        print(diff)
        self.assertTrue(diff <= 255.)

    def test_transpose(self):
        FLIP_LEFT_RIGHT = 0
        FLIP_TOP_BOTTOM = 1
        diff_hor = self.L1(self.M.transpose(FLIP_LEFT_RIGHT),
                           self.P.transpose(FLIP_LEFT_RIGHT))

        diff_ver = self.L1(self.M.transpose(FLIP_TOP_BOTTOM),
                           self.P.transpose(FLIP_TOP_BOTTOM))

        print(diff_hor, diff_ver)
        self.assertTrue(diff_hor <= 53250.)
        self.assertTrue(diff_ver <= 42494.)