Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matrix=None case for piecewise affine #1495

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions albumentations/augmentations/geometric/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,13 @@ def keypoint_safe_rotate(
@clipped
def piecewise_affine(
img: np.ndarray,
matrix: skimage.transform.PiecewiseAffineTransform,
matrix: Optional[skimage.transform.PiecewiseAffineTransform],
interpolation: int,
mode: str,
cval: float,
) -> np.ndarray:
if matrix is None:
return img
return skimage.transform.warp(
img, matrix, order=interpolation, mode=mode, cval=cval, preserve_range=True, output_shape=img.shape
)
Expand Down Expand Up @@ -823,11 +825,13 @@ def from_distance_maps(

def keypoint_piecewise_affine(
keypoint: KeypointInternalType,
matrix: skimage.transform.PiecewiseAffineTransform,
matrix: Optional[skimage.transform.PiecewiseAffineTransform],
h: int,
w: int,
keypoints_threshold: float,
) -> KeypointInternalType:
if matrix is None:
return keypoint
x, y, a, s = keypoint[:4]
dist_maps = to_distance_maps([(x, y)], h, w, True)
dist_maps = piecewise_affine(dist_maps, matrix, 0, "constant", 0)
Expand All @@ -837,11 +841,13 @@ def keypoint_piecewise_affine(

def bbox_piecewise_affine(
bbox: BoxInternalType,
matrix: skimage.transform.PiecewiseAffineTransform,
matrix: Optional[skimage.transform.PiecewiseAffineTransform],
h: int,
w: int,
keypoints_threshold: float,
) -> BoxInternalType:
if matrix is None:
return bbox
x1, y1, x2, y2 = denormalize_bbox(bbox, h, w)[:4]
keypoints = [
(x1, y1),
Expand Down
17 changes: 12 additions & 5 deletions albumentations/augmentations/geometric/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,12 @@ def get_params_dependent_on_targets(self, params) -> dict:

jitter: np.ndarray = random_utils.normal(0, scale, (nb_cells, 2))
if not np.any(jitter > 0):
return {"matrix": None}
for i in range(10): # See: https://github.com/albumentations-team/albumentations/issues/1442
jitter = random_utils.normal(0, scale, (nb_cells, 2))
if np.any(jitter > 0):
break
if not np.any(jitter > 0):
return {"matrix": None}

y = np.linspace(0, h, nb_rows)
x = np.linspace(0, w, nb_cols)
Expand Down Expand Up @@ -944,11 +949,13 @@ def get_params_dependent_on_targets(self, params) -> dict:
"matrix": matrix,
}

def apply(self, img: np.ndarray, matrix: skimage.transform.PiecewiseAffineTransform = None, **params) -> np.ndarray:
def apply(
self, img: np.ndarray, matrix: Optional[skimage.transform.PiecewiseAffineTransform] = None, **params
) -> np.ndarray:
return F.piecewise_affine(img, matrix, self.interpolation, self.mode, self.cval)

def apply_to_mask(
self, img: np.ndarray, matrix: skimage.transform.PiecewiseAffineTransform = None, **params
self, img: np.ndarray, matrix: Optional[skimage.transform.PiecewiseAffineTransform] = None, **params
) -> np.ndarray:
return F.piecewise_affine(img, matrix, self.mask_interpolation, self.mode, self.cval_mask)

Expand All @@ -957,7 +964,7 @@ def apply_to_bbox(
bbox: BoxInternalType,
rows: int = 0,
cols: int = 0,
matrix: skimage.transform.PiecewiseAffineTransform = None,
matrix: Optional[skimage.transform.PiecewiseAffineTransform] = None,
**params
) -> BoxInternalType:
return F.bbox_piecewise_affine(bbox, matrix, rows, cols, self.keypoints_threshold)
Expand All @@ -967,7 +974,7 @@ def apply_to_keypoint(
keypoint: KeypointInternalType,
rows: int = 0,
cols: int = 0,
matrix: skimage.transform.PiecewiseAffineTransform = None,
matrix: Optional[skimage.transform.PiecewiseAffineTransform] = None,
**params
):
return F.keypoint_piecewise_affine(keypoint, matrix, rows, cols, self.keypoints_threshold)
Expand Down
Loading