def generate_oof_masks(mask_file, img_dir="masks", oof_predictions_dir=None, img_size=640, masks_per_file=10): os.makedirs(img_dir, exist_ok=True) os.makedirs(os.path.join(img_dir, "original"), exist_ok=True) os.makedirs(os.path.join(img_dir, "solution"), exist_ok=True) mask_dir = os.path.join("/home/selim/datasets/spacenet/train_mask_binned") pred_dir = os.path.join(oof_predictions_dir, "full_oof") mask = cv2.imread( os.path.join(mask_dir, mask_file[:-4].replace("RGB", "MS") + ".tif"), cv2.IMREAD_GRAYSCALE) mask[mask > 0] = 255 solution_mask = cv2.imread(os.path.join(pred_dir, mask_file[:-4] + ".png"), cv2.IMREAD_GRAYSCALE) id = mask_file[:-4] for i in range(masks_per_file): h_start, w_start = random.random(), random.random() crop = random_crop(mask, img_size, img_size, h_start, w_start) if np.sum(crop) < 2000 * 255 and random.random() < 0.9: continue solution_crop = random_crop(solution_mask, img_size, img_size, h_start, w_start) cv2.imwrite( os.path.join(img_dir, "original", "{}_{}oof.png".format(id, i)), crop) cv2.imwrite( os.path.join(img_dir, "solution", "{}_{}oof.png".format(id, i)), solution_crop)
def test_random_crop_with_incorrectly_large_crop_size(): img = np.ones((4, 4), dtype=np.uint8) with pytest.raises(ValueError) as exc_info: F.random_crop(img, crop_height=8, crop_width=8, h_start=0, w_start=0) assert str( exc_info.value ) == 'Requested crop size (8, 8) is larger than the image size (4, 4)'
def albumentations(self, img): img = albumentations.random_crop(img, crop_height=64, crop_width=64, h_start=0, w_start=0) return albumentations.resize(img, height=512, width=512)
def albumentations(self, img): img = albumentations.random_crop(img, crop_height=64, crop_width=64, h_start=0, w_start=0) return np.ascontiguousarray(img)
def test_random_crop_float(target): img = np.array( [[0.01, 0.02, 0.03, 0.04], [0.05, 0.06, 0.07, 0.08], [0.09, 0.10, 0.11, 0.12], [0.13, 0.14, 0.15, 0.16]], dtype=np.float32, ) expected = np.array([[0.05, 0.06], [0.09, 0.10]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = F.random_crop(img, crop_height=2, crop_width=2, h_start=0.5, w_start=0) assert_array_almost_equal_nulp(cropped_img, expected)
def apply(self, img, crop_height=0, crop_width=0, h_start=0, w_start=0, interpolation=cv2.INTER_LINEAR, **params): crop = F.random_crop(img, crop_height, crop_width, h_start, w_start) return crop
def apply(self, img, height=1024, width=1024, h_start=0, w_start=0, interpolation=1, **params): img = F.random_crop(img, height, width, h_start, w_start) return img
def test_random_crop(target): img = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8) expected = np.array([[5, 6], [9, 10]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) cropped_img = F.random_crop(img, crop_height=2, crop_width=2, h_start=0.5, w_start=0) assert np.array_equal(cropped_img, expected)
def test_random_crop(target): img = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8) expected = np.array( [[5, 6], [9, 10]], dtype=np.uint8) if target == 'image': img = convert_2d_to_3d(img) expected = convert_2d_to_3d(expected) cropped_img = F.random_crop(img, crop_height=2, crop_width=2, h_start=0.5, w_start=0) assert np.array_equal(cropped_img, expected)
def paired_random_crop( images: Iterable[np.ndarray], crops_sizes: Iterable[Tuple[int, int]], ) -> Iterable[np.ndarray]: """Crop a random part of the input images. Args: images: Sequence of images. crops_sizes: Sequence of crop sizes ``(height, width)``. Returns: List of crops. """ h_start, w_start = random.random(), random.random() crops = [ F.random_crop(image, height, width, h_start, w_start) for image, (height, width) in zip(images, crops_sizes) ] return crops
def test_random_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.random_crop(img, crop_height=8, crop_width=8, h_start=0, w_start=0)
def albumentations(self, img): return albumentations.random_crop(img, crop_height=64, crop_width=64, h_start=0, w_start=0)
def apply(self, img, h_start=0, w_start=0, **params): h, w, _ = img.shape return F.random_crop(img, min(self.height, h), min(self.width, w), h_start, w_start)
def apply(self, img, crop_height=0, crop_width=0, h_start=0, w_start=0, interpolation=cv2.INTER_LINEAR, **params): h, w, _ = img.shape crop = F.random_crop(img, min(h, crop_height), min(w, crop_width), h_start, w_start) return F.resize(crop, self.height, self.width, interpolation)