예제 #1
0
def segm_results(cls_boxes, masks, ref_boxes, im_h, im_w):
    if 'deeplab' in cfg.MRCNN.ROI_MASK_HEAD:
        return segm_results_deeplab(cls_boxes, masks, ref_boxes, im_h, im_w)
    num_classes = cfg.MODEL.NUM_CLASSES
    cls_segms = [[] for _ in range(num_classes)]
    mask_ind = 0
    # To work around an issue with cv2.resize (it seems to automatically pad
    # with repeated border values), we manually zero-pad the masks by 1 pixel
    # prior to resizing back to the original image resolution. This prevents
    # "top hat" artifacts. We therefore need to expand the reference boxes by an
    # appropriate factor.
    M = cfg.MRCNN.RESOLUTION
    scale = (M + 2.0) / M
    ref_boxes = box_utils.expand_boxes(ref_boxes, scale)
    ref_boxes = ref_boxes.astype(np.int32)
    padded_mask = np.zeros((M + 2, M + 2), dtype=np.float32)

    # skip j = 0, because it's the background class
    for j in range(1, num_classes):
        segms = []
        for _ in range(cls_boxes[j].shape[0]):
            if cfg.MRCNN.CLS_SPECIFIC_MASK:
                padded_mask[1:-1, 1:-1] = masks[mask_ind, j, :, :]
            else:
                padded_mask[1:-1, 1:-1] = masks[mask_ind, 0, :, :]

            ref_box = ref_boxes[mask_ind, :]
            w = ref_box[2] - ref_box[0] + 1
            h = ref_box[3] - ref_box[1] + 1
            w = np.maximum(w, 1)
            h = np.maximum(h, 1)

            mask = cv2.resize(padded_mask, (w, h))
            mask = np.array(mask > cfg.MRCNN.THRESH_BINARIZE, dtype=np.uint8)
            im_mask = np.zeros((im_h, im_w), dtype=np.uint8)

            x_0 = max(ref_box[0], 0)
            x_1 = min(ref_box[2] + 1, im_w)
            y_0 = max(ref_box[1], 0)
            y_1 = min(ref_box[3] + 1, im_h)

            im_mask[y_0:y_1, x_0:x_1] = mask[
                (y_0 - ref_box[1]):(y_1 - ref_box[1]),
                (x_0 - ref_box[0]):(x_1 - ref_box[0])
            ]

            # Get RLE encoding used by the COCO evaluation API
            rle = mask_util.encode(
                np.array(im_mask[:, :, np.newaxis], order='F')
            )[0]
            segms.append(rle)

            mask_ind += 1

        cls_segms[j] = segms

    assert mask_ind == masks.shape[0]
    return cls_segms
예제 #2
0
파일: test.py 프로젝트: Jakaria08/Detectron
def segm_results(cls_boxes, masks, ref_boxes, im_h, im_w):
    num_classes = cfg.MODEL.NUM_CLASSES
    cls_segms = [[] for _ in range(num_classes)]
    mask_ind = 0
    # To work around an issue with cv2.resize (it seems to automatically pad
    # with repeated border values), we manually zero-pad the masks by 1 pixel
    # prior to resizing back to the original image resolution. This prevents
    # "top hat" artifacts. We therefore need to expand the reference boxes by an
    # appropriate factor.
    M = cfg.MRCNN.RESOLUTION
    scale = (M + 2.0) / M
    ref_boxes = box_utils.expand_boxes(ref_boxes, scale)
    ref_boxes = ref_boxes.astype(np.int32)
    padded_mask = np.zeros((M + 2, M + 2), dtype=np.float32)

    # skip j = 0, because it's the background class
    for j in range(1, num_classes):
        segms = []
        for _ in range(cls_boxes[j].shape[0]):
            if cfg.MRCNN.CLS_SPECIFIC_MASK:
                padded_mask[1:-1, 1:-1] = masks[mask_ind, j, :, :]
            else:
                padded_mask[1:-1, 1:-1] = masks[mask_ind, 0, :, :]

            ref_box = ref_boxes[mask_ind, :]
            w = ref_box[2] - ref_box[0] + 1
            h = ref_box[3] - ref_box[1] + 1
            w = np.maximum(w, 1)
            h = np.maximum(h, 1)

            mask = cv2.resize(padded_mask, (w, h))
            mask = np.array(mask > cfg.MRCNN.THRESH_BINARIZE, dtype=np.uint8)
            im_mask = np.zeros((im_h, im_w), dtype=np.uint8)

            x_0 = max(ref_box[0], 0)
            x_1 = min(ref_box[2] + 1, im_w)
            y_0 = max(ref_box[1], 0)
            y_1 = min(ref_box[3] + 1, im_h)

            im_mask[y_0:y_1, x_0:x_1] = mask[
                (y_0 - ref_box[1]):(y_1 - ref_box[1]),
                (x_0 - ref_box[0]):(x_1 - ref_box[0])
            ]

            # Get RLE encoding used by the COCO evaluation API
            rle = mask_util.encode(
                np.array(im_mask[:, :, np.newaxis], order='F')
            )[0]
            segms.append(rle)

            mask_ind += 1

        cls_segms[j] = segms

    assert mask_ind == masks.shape[0]
    return cls_segms
예제 #3
0
파일: test.py 프로젝트: gbegkas/Detectron
def segm_results(cls_boxes, masks, ref_boxes, im_h, im_w, timers):
    num_classes = cfg.MODEL.NUM_CLASSES
    cls_segms = [[] for _ in range(num_classes)]
    ind = []
    mask_ind = 0
    # To work around an issue with cv2.resize (it seems to automatically pad
    # with repeated border values), we manually zero-pad the masks by 1 pixel
    # prior to resizing back to the original image resolution. This prevents
    # "top hat" artifacts. We therefore need to expand the reference boxes by an
    # appropriate factor.
    M = cfg.MRCNN.RESOLUTION
    scale = (M + 2.0) / M
    ref_boxes = box_utils.expand_boxes(ref_boxes, scale)
    ref_boxes = ref_boxes.astype(np.int32)
    padded_mask = np.zeros((M + 2, M + 2), dtype=np.float32)

    # skip j = 0, because it's the background class
    for j in range(1, num_classes):
        segms = []
        num_threads = multiprocessing.cpu_count() - 1
        for cls_segm, indicator in Parallel(n_jobs=num_threads)(delayed(test)(k, im_w, im_h, ref_boxes, padded_mask, masks, j) for k in range(cls_boxes[j].shape[0])):
            cls_segms[j].append(cls_segm)
            ind.append(indicator)
        mask_ind = mask_ind + max(ind) + 1

        # for _ in range(cls_boxes[j].shape[0]):
        #     if cfg.MRCNN.CLS_SPECIFIC_MASK:
        #         padded_mask[1:-1, 1:-1] = masks[mask_ind, j, :, :]
        #     else:
        #         padded_mask[1:-1, 1:-1] = masks[mask_ind, 0, :, :]
        #
        #     ref_box = ref_boxes[mask_ind, :]
        #     w = ref_box[2] - ref_box[0] + 1
        #     h = ref_box[3] - ref_box[1] + 1
        #     w = np.maximum(w, 1)
        #     h = np.maximum(h, 1)
        #
        #     mask = cv2.resize(padded_mask, (w, h))
        #     mask = np.array(mask > cfg.MRCNN.THRESH_BINARIZE, dtype=np.uint8)
        #     im_mask = np.zeros((im_h, im_w), dtype=np.uint8)
        #
        #     x_0 = max(ref_box[0], 0)
        #     x_1 = min(ref_box[2] + 1, im_w)
        #     y_0 = max(ref_box[1], 0)
        #     y_1 = min(ref_box[3] + 1, im_h)
        #
        #     im_mask[y_0:y_1, x_0:x_1] = mask[
        #                                 (y_0 - ref_box[1]):(y_1 - ref_box[1]),
        #                                 (x_0 - ref_box[0]):(x_1 - ref_box[0])
        #                                 ]
        #
        #     # Get RLE encoding used by the COCO evaluation API
        #     rle = mask_util.encode(
        #         np.array(im_mask[:, :, np.newaxis], order='F')
        #     )[0]
        #     segms.append(rle)
        #
        #     mask_ind += 1
        #
        # cls_segms[j] = segms

    assert mask_ind == masks.shape[0]
    return cls_segms