예제 #1
0
    def __getitem__(self, index):
        id = self.sample_ids[index]
        image = read_train_image(id, self.data_dir)

        data = {
            INDEX_KEY: index,
            ID_KEY: id,
            IMAGE_KEY: image,
        }

        if self.groundtruth is not None:
            instance_mask = self.decode_mask(id)
            data[MASK_KEY] = instance_mask_to_fg_and_edge(instance_mask)

        data = self.transform(**data)
        data = self.normalize(**data)

        data[IMAGE_KEY] = tensor_from_rgb_image(data[IMAGE_KEY])

        image = data[IMAGE_KEY]

        if self.groundtruth is not None:
            data[MASK_KEY] = tensor_from_rgb_image(data[MASK_KEY]).float()

        y_true = {i: data[i] for i in data if i != IMAGE_KEY}
        return image, y_true
    def __getitem__(self, index):
        id = self.sample_ids[index]
        image = read_train_image(id, self.data_dir)

        data = {
            INDEX_KEY: index,
            ID_KEY: id,
            IMAGE_KEY: image,
        }

        if self.groundtruth is not None:
            data[MASK_KEY] = self.decode_mask(id)

        if self.transform is not None:
            data = self.transform(**data)

        data = self.normalize(**data)
        data[IMAGE_KEY] = tensor_from_rgb_image(data[IMAGE_KEY])

        image = data[IMAGE_KEY]

        if self.groundtruth is not None:
            data[SHIP_PRESENSE_KEY] = np.array([(data[MASK_KEY] > 0).any()],
                                               dtype=np.float32)
            data[MASK_KEY] = data[MASK_KEY].astype(int)
            y_true = {i: data[i] for i in data if i != IMAGE_KEY}
            return image, y_true
        else:
            return image
예제 #3
0
    def __getitem__(self, index):

        id = self.sample_ids[index]

        data = {
            INDEX_KEY: index,
            ID_KEY: id,
            IMAGE_KEY: read_train_image(id, self.data_dir),
            MASK_KEY: self.decode_mask(id)
        }
        # print(data[IMAGE_KEY].shape, data[MASK_KEY].shape)

        if self.transform is not None:
            data = self.transform(**data)
        data = self.normalize(**data)

        # After augmentation let's apply oversampling by cropping ships
        if data[MASK_KEY].max() > 0:  # If we have ships
            ship_index = random.randint(1, data[MASK_KEY].max())
            ship_mask = (data[MASK_KEY] == ship_index).astype(np.uint8)
            rect = cv2.boundingRect(cv2.findNonZero(ship_mask))

            min_x = rect[0]
            min_y = rect[1]
            max_x = rect[2] + min_x
            max_y = rect[3] + min_y
        else:
            min_x = 0
            min_y = 0
            max_x = data[MASK_KEY].shape[1]
            max_y = data[MASK_KEY].shape[0]

        i = random.randint(min_y, max_y)
        j = random.randint(min_x, max_x)

        ic = np.clip(i - self.patch_size // 2, 0,
                     data[MASK_KEY].shape[0] - self.patch_size - 1)
        jc = np.clip(j - self.patch_size // 2, 0,
                     data[MASK_KEY].shape[1] - self.patch_size - 1)

        image = data[IMAGE_KEY][ic:ic + self.patch_size,
                                jc:jc + self.patch_size]
        mask = data[MASK_KEY][ic:ic + self.patch_size, jc:jc + self.patch_size]

        if image.shape[0] == 0 or image.shape[1] == 0:
            breakpoint()

        image = tensor_from_rgb_image(image)
        data[IMAGE_KEY] = image

        mask = instance_mask_to_fg_and_edge(mask)
        mask = tensor_from_rgb_image(mask).float()
        data[MASK_KEY] = mask

        y_true = {i: data[i] for i in data if i != IMAGE_KEY}
        return image, y_true
예제 #4
0
def test_rssd_encoding():
    id = 'dc6adaf6b.jpg'
    groundtruth = pd.read_csv(
        os.path.join(data_dir, 'train_ship_segmentations_v2.csv'))
    image = read_train_image(id, data_dir)
    rle = get_rle_from_groundtruth(groundtruth, id)
    label_image = rle2instancemask(rle)

    # Test what happens if we rotate
    # image = np.rot90(image).copy()
    # label_image = np.rot90(label_image).copy()

    rbboxes = instance_mask_to_rbboxes(label_image)
    print('Instances', rbboxes)

    labels = np.zeros(len(rbboxes), dtype=np.intp)

    box_coder = RSSDBoxCoder(768, 778)

    loc_targets, cls_targets = box_coder.encode(rbboxes, labels)
    print(loc_targets.shape, cls_targets.shape)

    cls_targets_one_hot = np.eye(2)[cls_targets]
    print(cls_targets_one_hot.shape)

    dec_boxes, dec_labels, dec_scores = box_coder.decode(
        loc_targets, cls_targets_one_hot)
    print(dec_boxes)

    for bbox in dec_boxes:
        visualize_rbbox(image, bbox, (255, 0, 0), thickness=3)

    for bbox in rbboxes:
        visualize_rbbox(image, bbox, (0, 255, 0), thickness=1)

    cv2.imshow('a', image)
    cv2.waitKey(-1)
예제 #5
0
    def __getitem__(self, index):
        id = self.sample_ids[index]
        if self.test:
            image = read_test_image(id, self.data_dir)
        else:
            image = read_train_image(id, self.data_dir)

        data = {
            INDEX_KEY: index,
            ID_KEY: id,
            IMAGE_KEY: image,
        }

        if self.groundtruth is not None:
            data[MASK_KEY] = self.decode_mask(id)

        if self.transform is not None:
            data = self.transform(**data)

        data = self.normalize(**data)

        image = tensor_from_rgb_image(data[IMAGE_KEY])

        # Extract bboxes & convert them to RSSD format
        if self.groundtruth is not None:
            rbboxes = instance_mask_to_rbboxes(data[MASK_KEY])
            del data[MASK_KEY]

            labels = np.zeros(
                len(rbboxes), dtype=np.long
            )  # We have only one class -'ship' which is class 0
            rbboxes, labels = self.box_coder.encode(rbboxes, labels)
            data[SSD_BBOXES_KEY], data[SSD_LABELS_KEY] = torch.from_numpy(
                rbboxes), torch.from_numpy(labels).long()

        y_true = {i: data[i] for i in data if i != IMAGE_KEY}
        return image, y_true
예제 #6
0
def test_mask():
    # id = 'aed55df18.jpg'
    id = 'dc6adaf6b.jpg'
    groundtruth = pd.read_csv(
        os.path.join(data_dir, 'train_ship_segmentations_v2.csv'))
    image = read_train_image(id, data_dir)
    rle = get_rle_from_groundtruth(groundtruth, id)
    label_image = rle2instancemask(rle)

    mask = instance_mask_to_fg_and_edge(label_image)

    dummy = np.zeros((label_image.shape[0], label_image.shape[1], 1),
                     dtype=np.uint8)
    mask = np.concatenate([mask * 255, dummy], axis=2)
    cv2.imshow('mask (no edge)', (label_image > 0).astype(np.uint8) * 255)
    cv2.imshow('mask', mask)
    # cv2.waitKey(-1)

    rbboxes = instance_mask_to_rbboxes(label_image)
    for bbox in rbboxes:
        visualize_rbbox(image, bbox, (0, 255, 0))

    cv2.imshow('a', image)
    cv2.waitKey(-1)