示例#1
0
    def __getitem__(self, index):
        ann = self.coco[index]

        # bbox transform.
        bbox = np.array([ann["bbox"]])  # xmin, ymin, w, h
        bbox = BoxMode.convert(bbox, BoxMode.XYWH_ABS,
                               BoxMode.XYXY_ABS)  # x1y1x2y2
        bbox = Boxes(bbox)

        # mask transform.
        mask = PolygonMasks([ann["segmentation"]])
        mask = mask.crop_and_resize(bbox.tensor, self.size).float()

        return mask
    def __getitem__(self, index):
        ann = self.all_annotations[index]

        # bbox transform.
        bbox = np.array([ann["bbox"]])  # xmin, ymin, xmax, ymax
        bbox = Boxes(bbox)

        # mask transform.
        # print(bbox)
        # print(ann["segmentation"])
        mask = PolygonMasks([ann["segmentation"]])
        mask = mask.crop_and_resize(bbox.tensor, self.size).float()

        return mask
示例#3
0
def valid_dct_source(coco, dct_dim, mask_size):
    dct_mask_encoding = DctMaskEncoding(dct_dim, mask_size)
    mIoU = []
    Number = 0
    for ann in coco:
        Number += 1
        bbox = np.array([ann["bbox"]])  # xmin, ymin, w, h
        w, h = bbox[0][2], bbox[0][3]
        w, h = round(w), round(h)
        bbox = BoxMode.convert(bbox, BoxMode.XYWH_ABS,
                               BoxMode.XYXY_ABS)  # x1y1x2y2
        bbox = Boxes(bbox)

        # mask transform.
        mask = PolygonMasks([ann["segmentation"]])
        mask_source = rasterize_polygons_within_box_for_arbitrary_shape(
            mask.polygons[0], bbox.tensor[0].numpy(), h, w)
        mask_source = mask_source.numpy()  # numpy [h,w] binary

        mask_k = mask.crop_and_resize(
            bbox.tensor, mask_size).float()  # tensor [1,28,28],all 0 or 1
        mask_k = mask_k.view([mask_size, mask_size])
        dct_code = dct_mask_encoding.encode(mask_k)
        mask_re = dct_mask_encoding.decode(dct_code).numpy().squeeze()
        res = cv2.resize(mask_re.astype('float'),
                         dsize=(mask_source.shape[1], mask_source.shape[0]),
                         interpolation=cv2.INTER_LINEAR)

        res = np.where(res >= 0.5, 1, 0)
        res = np.reshape(res, [1, -1])
        mask_source = np.reshape(mask_source, [1, -1])
        res = res.astype(int)

        IoUevaluate = IOUMetric(2)
        IoUevaluate.add_batch(res, mask_source)

        _, _, _, mean_iu, _ = IoUevaluate.evaluate()
        mIoU.append(mean_iu)
        if Number % 1000 == 1:
            print(np.mean(mIoU))
    return np.mean(mIoU)
示例#4
0
    def __getitem__(self, index):
        ann = self.coco[index]

        # bbox transform.
        bbox = np.array([ann["bbox"]])  # xmin, ymin, w, h
        bbox = BoxMode.convert(bbox, BoxMode.XYWH_ABS,
                               BoxMode.XYXY_ABS)  # x1y1x2y2
        bbox = Boxes(bbox)

        # mask transform.
        mask = PolygonMasks([ann["segmentation"]])
        mask = mask.crop_and_resize(bbox.tensor, self.size).float()
        if self.transform:
            if torch.rand(1) < 0.5:
                mask = mask.flip(2)

        # introduce several noise.
        noise_matrix = VALUE_NOISE * torch.rand(mask.shape)
        mask = torch.where(mask > noise_matrix, mask - noise_matrix,
                           noise_matrix)

        return mask