Beispiel #1
0
def test_center_crop_with_incorrectly_large_crop_size():
    img = np.ones((4, 4), dtype=np.uint8)
    with pytest.raises(ValueError) as exc_info:
        F.center_crop(img, 8, 8)
    assert str(
        exc_info.value
    ) == 'Requested crop size (8, 8) is larger than the image size (4, 4)'
def test_center_crop_with_incorrectly_large_crop_size():
    img = np.ones((4, 4), dtype=np.uint8)
    with pytest.raises(
            ValueError,
            message=
            'Requested crop size (8, 8) is larger than the image size (4, 4)'):
        F.center_crop(img, 8, 8)
Beispiel #3
0
def fixed_size_rotate_crop(
    img,
    angle,
    interpolation=cv2.INTER_LINEAR,
    border_mode=cv2.BORDER_REFLECT_101,
    value=None,
):
    original_height, original_width = img.shape[:2]
    rot_img = rotate_no_crop(img, angle, interpolation, border_mode, value)
    new_height, new_width = rotatedRectWithMaxArea(original_height,
                                                   original_width, angle)

    cropped_img = F.center_crop(rot_img, new_height, new_width)

    resize_height, resize_width = get_resized_max_ratio(
        original_height, original_width, new_height, new_width)

    resized_img = F.resize(
        cropped_img,
        height=resize_height,
        width=resize_width,
        interpolation=interpolation,
    )

    return F.center_crop(resized_img, original_height, original_width)
def predict(model, from_file_names, batch_size, to_path, img_transform):
    loader = DataLoader(dataset=SaltDataset(from_file_names,
                                            transform=img_transform,
                                            mode='predict'),
                        shuffle=False,
                        batch_size=batch_size,
                        num_workers=args.workers,
                        pin_memory=torch.cuda.is_available())

    with torch.no_grad():
        for _, (inputs, paths) in enumerate(tqdm(loader, desc='Predict')):
            inputs = utils.cuda(inputs)

            outputs = model(inputs)

            for i, _ in enumerate(paths):
                factor = prepare_data.binary_factor
                t_mask = (F.sigmoid(outputs[i, 0]).data.cpu().numpy() *
                          factor).astype(np.uint8)
                final_mask = center_crop(t_mask, 202, 202)
                final_mask = resize(final_mask,
                                    original_height,
                                    original_width,
                                    interpolation=cv2.INTER_AREA)

                to_path.mkdir(exist_ok=True, parents=True)

                cv2.imwrite(str(to_path / (Path(paths[i]).stem + '.png')),
                            final_mask)
Beispiel #5
0
def wrap_border(mask_src: np.ndarray) -> np.ndarray:
    mask = mask_src.copy()
    mask[14:27, :] = (mask[14:27, :] + vflip(mask[:13, :])) / 2
    mask[:, 14:27] = (mask[:, 14:27] + hflip(mask[:, :13])) / 2
    mask[99:113, :] = (mask[99:113, :] + vflip(mask[114:, :])) / 2
    mask[:, 99:113] = (mask[:, 99:113] + hflip(mask[:, 114:])) / 2
    return center_crop(mask, 101, 101)
Beispiel #6
0
def test_center_crop(target):
    img = np.array([[1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]],
                   dtype=np.uint8)
    expected = np.array([[1, 1], [0, 1]], dtype=np.uint8)
    img, expected = convert_2d_to_target_format([img, expected], target=target)
    cropped_img = F.center_crop(img, 2, 2)
    assert np.array_equal(cropped_img, expected)
Beispiel #7
0
def test_center_crop_float(target):
    img = np.array([[0.4, 0.4, 0.4, 0.4], [0.0, 0.4, 0.4, 0.4],
                    [0.0, 0.0, 0.4, 0.4], [0.0, 0.0, 0.0, 0.4]],
                   dtype=np.float32)
    expected = np.array([[0.4, 0.4], [0.0, 0.4]], dtype=np.float32)
    img, expected = convert_2d_to_target_format([img, expected], target=target)
    cropped_img = F.center_crop(img, 2, 2)
    assert_array_almost_equal_nulp(cropped_img, expected)
def test_center_crop(target):
    img = np.array([[1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]],
                   dtype=np.uint8)
    expected_output = np.array([[1, 1], [0, 1]], dtype=np.uint8)
    if target == 'image':
        img = convert_2d_to_3d(img)
        expected_output = convert_2d_to_3d(expected_output)
    cropped_img = F.center_crop(img, 2, 2)
    assert np.array_equal(cropped_img, expected_output)
    def apply(self, img, **params):
        w, h = img.shape[:2]
        crop_w, crop_h = self.width, self.height

        if self.crop_pos == 0:
            return F.crop(img, 0, 0, crop_w, crop_h)
        elif self.crop_pos == 1:
            return F.crop(img, w - crop_w, 0, w, crop_h)
        elif self.crop_pos == 2:
            return F.crop(img, 0, h - crop_h, crop_w, h)
        elif self.crop_pos == 3:
            return F.crop(img, w - crop_w, h - crop_h, w, h)
        else:
            return F.center_crop(img, crop_h, crop_w)
def test_center_crop(target):
    img = np.array(
        [[1, 1, 1, 1],
         [0, 1, 1, 1],
         [0, 0, 1, 1],
         [0, 0, 0, 1]], dtype=np.uint8)
    expected_output = np.array(
        [[1, 1],
         [0, 1]], dtype=np.uint8)
    if target == 'image':
        img = convert_2d_to_3d(img)
        expected_output = convert_2d_to_3d(expected_output)
    cropped_img = F.center_crop(img, 2, 2)
    assert np.array_equal(cropped_img, expected_output)
def avg_masks(mask_fns: List[str], fn: str, th: float):
    logger = configure_logger('prediction', logging.INFO, './logs')
    test = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'))
    masks = []
    for mask_fn in mask_fns:
        masks.append(np.load(mask_fn))
    masks = np.mean(masks, axis=0)

    np.save(os.path.join(SUBMISSION_DIR, 'avg_masks_fold_0256'), masks)
    masks = [(center_crop(mask, 101, 101) > th).astype(int) for mask in masks]
    rle_masks = [get_mask_rle(mask) for mask in masks]
    test['rle_mask'] = rle_masks
    submission_fn = os.path.join(SUBMISSION_DIR,
                                 f'{fn}_{get_current_datetime()}.csv')
    test.to_csv(submission_fn, index=None)
    logger.info(f'Saved submission to {submission_fn}')
def five_crop(
    img: numpy.ndarray, size: List[int]
) -> Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray,
           numpy.ndarray]:
    """Crop the given image into four corners and the central crop.
    Args:
        img (numpy.ndarray): Image to be cropped.
        size (sequence or int): Desired output size of the crop. If size is an
            int instead of sequence like (h, w), a square crop (size, size) is
            made. If provided a sequence of length 1, it will be interpreted
            as (size[0], size[0]).
    Returns:
       tuple: tuple (tl, tr, bl, br, center)
       Corresponding top left, top right, bottom left, bottom right and center crop.
    """

    image_height, image_width = img.shape[:2]
    crop_height, crop_width = size
    if crop_width > image_width or crop_height > image_height:
        msg = "Requested crop size {} is bigger than input size {}"
        raise ValueError(msg.format(size, (image_height, image_width)))

    tl = F.crop(img, 0, 0, crop_width, crop_height)
    tr = F.crop(img, image_width - crop_width, 0, image_width, crop_height)
    bl = F.crop(img, 0, image_height - crop_height, crop_width, image_height)
    br = F.crop(
        img,
        image_width - crop_width,
        image_height - crop_height,
        image_width,
        image_height,
    )

    center = F.center_crop(img, crop_height, crop_width)

    return tl, tr, bl, br, center
Beispiel #13
0
 def _prep_func(image: np.array):
     image_ = center_crop(image, self.input_size[1], self.input_size[0])
     image_ = imagenet_normalize(image_)
     return to_tensor(image_).squeeze().unsqueeze(0)
def predict_with_snapshot(snapshot_dir: str, arch: str, device: str, fn: str,
                          th: float, masks_fn: str):
    logger = configure_logger('prediction', logging.INFO, './logs')
    test = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'))

    if masks_fn is None:
        test_images = np.load(os.path.join(DATA_DIR, 'test_images.npy'))
        test_ids = test.index.tolist()
        model = architectures[arch]().to(device)
        test = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'))
        test_images = np.load(os.path.join(DATA_DIR, 'test_images.npy'))
        test_ids = test.index.tolist()
        tta_augs = [val_augmentations, flip_pad]
        tta_predictions = []
        for cycle_dir in os.listdir(snapshot_dir):
            snapshots = os.listdir(os.path.join(snapshot_dir, cycle_dir))
            best_snapshot = sorted(snapshots,
                                   key=lambda x: int(x.split('.')[-1]),
                                   reverse=True)[0]

            state_dict = torch.load(
                os.path.join(snapshot_dir, cycle_dir, best_snapshot))

            model.load_state_dict(state_dict)
            model.eval()
            logger.info(f'Loaded model from {best_snapshot}')

            for i, aug in enumerate(tta_augs):
                test_dataset = SaltTestDataset(test_ids, test_images, aug)
                test_loader = DataLoader(test_dataset, 30, shuffle=False)

                # actual prediction is made here
                masks = []
                with torch.no_grad():
                    for batch in tqdm.tqdm(test_loader):
                        image = batch['image'].to(device)
                        y_pred = torch.sigmoid(model(image)).cpu().numpy()
                        masks.append(y_pred)

                # postprocess masks (crop, threshold, rle)
                masks = np.concatenate(masks).reshape(
                    (len(test), NET_INPUT_SIZE, NET_INPUT_SIZE))
                # TODO: replace that with smth that makes more sens
                if i == 1:
                    masks = [hflip(mask) for mask in masks]
                tta_predictions.append(masks)

        masks = np.mean(tta_predictions, axis=0)
        np.save(os.path.join(SUBMISSION_DIR, 'raw_masks_fold1_scnd.npy'),
                masks)
    else:
        masks = np.load(os.path.join(SUBMISSION_DIR, masks_fn))

    masks = [(center_crop(mask, SRC_SIZE, SRC_SIZE) > th).astype(int)
             for mask in masks]
    rle_masks = [get_mask_rle(mask) for mask in masks]
    test['rle_mask'] = rle_masks
    # TODO: get some stats on empty masks etc. there and log it too
    submission_fn = os.path.join(SUBMISSION_DIR,
                                 f'{fn}_{get_current_datetime()}.csv')
    test.to_csv(submission_fn, index=None)
    logger.info(f'Saved submission to {submission_fn}')
def test_center_crop_with_incorrectly_large_crop_size():
    img = np.ones((4, 4), dtype=np.uint8)
    with pytest.raises(ValueError, message='Requested crop size (8, 8) is larger than the image size (4, 4)'):
        F.center_crop(img, 8, 8)
 def _load_images(path):
     image = np.array(Image.open(path))
     image = center_crop(image, height, width)
     image = torch_tools.imagenet_normalize(image)
     return to_tensor(image).squeeze().unsqueeze(0)
 def _load_mask(path):
     mask = np.array(Image.open(path))
     mask = center_crop(mask, height, width)
     mask = surface_types.convert_mask(mask, category_type)
     return mask
Beispiel #18
0
 def apply(self, img, **params):
     h, w, _ = img.shape
     return F.center_crop(img, min(self.height, h), min(self.width, w))
Beispiel #19
0
def crop_from_128(image: np.ndarray) -> np.ndarray:
    return center_crop(image, 101, 101)
Beispiel #20
0
def crop_resize_from_256(image: np.ndarray) -> np.ndarray:
    cropped = center_crop(image, 202, 202)
    return resize(cropped, 101, 101)