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

Force mask interpolation #2003

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
67 changes: 54 additions & 13 deletions albumentations/augmentations/crops/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,28 +424,39 @@ class _BaseRandomSizedCrop(DualTransform):
# Base class for RandomSizedCrop and RandomResizedCrop

class InitSchema(BaseRandomSizedCropInitSchema):
interpolation: InterpolationType = cv2.INTER_LINEAR
interpolation: InterpolationType
mask_interpolation: InterpolationType

def __init__(
self,
size: tuple[int, int],
interpolation: int = cv2.INTER_LINEAR,
mask_interpolation: int = cv2.INTER_NEAREST,
always_apply: bool | None = None,
p: float = 1.0,
):
super().__init__(p, always_apply)
super().__init__(p=p, always_apply=always_apply)
self.size = size
self.interpolation = interpolation
self.mask_interpolation = mask_interpolation

def apply(
self,
img: np.ndarray,
crop_coords: tuple[int, int, int, int],
interpolation: int,
**params: Any,
) -> np.ndarray:
crop = fcrops.crop(img, *crop_coords)
return fgeometric.resize(crop, self.size, interpolation)
return fgeometric.resize(crop, self.size, self.interpolation)

def apply_to_mask(
self,
mask: np.ndarray,
crop_coords: tuple[int, int, int, int],
**params: Any,
) -> np.ndarray:
crop = fcrops.crop(mask, *crop_coords)
return fgeometric.resize(crop, self.size, self.mask_interpolation)

def apply_to_bboxes(
self,
Expand Down Expand Up @@ -476,7 +487,7 @@ def apply_to_keypoints(
return fgeometric.keypoints_scale(cropped_keypoints, scale_x, scale_y)

def get_transform_init_args_names(self) -> tuple[str, ...]:
return "size", "interpolation"
return "size", "interpolation", "mask_interpolation"


class RandomSizedCrop(_BaseRandomSizedCrop):
Expand Down Expand Up @@ -538,6 +549,7 @@ class RandomSizedCrop(_BaseRandomSizedCrop):

class InitSchema(BaseTransformInitSchema):
interpolation: InterpolationType
mask_interpolation: InterpolationType
min_max_height: OnePlusIntRangeType
w2h_ratio: Annotated[float, Field(gt=0)]
width: int | None = Field(
Expand Down Expand Up @@ -582,10 +594,17 @@ def __init__(
*,
w2h_ratio: float = 1.0,
interpolation: int = cv2.INTER_LINEAR,
mask_interpolation: int = cv2.INTER_NEAREST,
always_apply: bool | None = None,
p: float = 1.0,
):
super().__init__(size=cast(tuple[int, int], size), interpolation=interpolation, p=p, always_apply=always_apply)
super().__init__(
size=cast(tuple[int, int], size),
interpolation=interpolation,
mask_interpolation=mask_interpolation,
p=p,
always_apply=always_apply,
)
self.min_max_height = min_max_height
self.w2h_ratio = w2h_ratio

Expand Down Expand Up @@ -685,6 +704,7 @@ class InitSchema(BaseTransformInitSchema):
)
size: ScaleIntType | None
interpolation: InterpolationType
mask_interpolation: InterpolationType

@model_validator(mode="after")
def process(self) -> Self:
Expand Down Expand Up @@ -713,10 +733,17 @@ def __init__(
scale: tuple[float, float] = (0.08, 1.0),
ratio: tuple[float, float] = (0.75, 1.3333333333333333),
interpolation: int = cv2.INTER_LINEAR,
mask_interpolation: int = cv2.INTER_NEAREST,
always_apply: bool | None = None,
p: float = 1.0,
):
super().__init__(size=cast(tuple[int, int], size), interpolation=interpolation, p=p, always_apply=always_apply)
super().__init__(
size=cast(tuple[int, int], size),
interpolation=interpolation,
mask_interpolation=mask_interpolation,
p=p,
always_apply=always_apply,
)
self.scale = scale
self.ratio = ratio

Expand Down Expand Up @@ -776,7 +803,7 @@ def get_params_dependent_on_data(
return {"crop_coords": crop_coords}

def get_transform_init_args_names(self) -> tuple[str, ...]:
return "size", "scale", "ratio", "interpolation"
return "size", "scale", "ratio", "interpolation", "mask_interpolation"


class RandomCropNearBBox(BaseCrop):
Expand Down Expand Up @@ -1042,20 +1069,23 @@ class InitSchema(CropInitSchema):
le=1.0,
)
interpolation: InterpolationType
mask_interpolation: InterpolationType

def __init__(
self,
height: int,
width: int,
erosion_rate: float = 0.0,
interpolation: int = cv2.INTER_LINEAR,
mask_interpolation: int = cv2.INTER_NEAREST,
always_apply: bool | None = None,
p: float = 1.0,
):
super().__init__(erosion_rate=erosion_rate, p=p, always_apply=always_apply)
self.height = height
self.width = width
self.interpolation = interpolation
self.mask_interpolation = mask_interpolation

def apply(
self,
Expand All @@ -1066,6 +1096,15 @@ def apply(
crop = fcrops.crop(img, *crop_coords)
return fgeometric.resize(crop, (self.height, self.width), self.interpolation)

def apply_to_mask(
self,
mask: np.ndarray,
crop_coords: tuple[int, int, int, int],
**params: Any,
) -> np.ndarray:
crop = fcrops.crop(mask, *crop_coords)
return fgeometric.resize(crop, (self.height, self.width), self.mask_interpolation)

def apply_to_keypoint(
self,
keypoints: np.ndarray,
Expand All @@ -1082,7 +1121,7 @@ def apply_to_keypoint(
return fgeometric.keypoints_scale(keypoints, scale_x=scale_x, scale_y=scale_y)

def get_transform_init_args_names(self) -> tuple[str, ...]:
return (*super().get_transform_init_args_names(), "height", "width", "interpolation")
return (*super().get_transform_init_args_names(), "height", "width", "interpolation", "mask_interpolation")


class CropAndPad(DualTransform):
Expand Down Expand Up @@ -1175,6 +1214,7 @@ class InitSchema(BaseTransformInitSchema):
keep_size: bool
sample_independently: bool
interpolation: InterpolationType
mask_interpolation: InterpolationType

@model_validator(mode="after")
def check_px_percent(self) -> Self:
Expand All @@ -1196,6 +1236,7 @@ def __init__(
keep_size: bool = True,
sample_independently: bool = True,
interpolation: int = cv2.INTER_LINEAR,
mask_interpolation: int = cv2.INTER_NEAREST,
always_apply: bool | None = None,
p: float = 1.0,
):
Expand All @@ -1212,14 +1253,14 @@ def __init__(
self.sample_independently = sample_independently

self.interpolation = interpolation
self.mask_interpolation = mask_interpolation

def apply(
self,
img: np.ndarray,
crop_params: Sequence[int],
pad_params: Sequence[int],
pad_value: ColorType,
interpolation: int,
**params: Any,
) -> np.ndarray:
return fcrops.crop_and_pad(
Expand All @@ -1228,7 +1269,7 @@ def apply(
pad_params,
pad_value,
params["shape"][:2],
interpolation,
self.interpolation,
self.pad_mode,
self.keep_size,
)
Expand All @@ -1239,7 +1280,6 @@ def apply_to_mask(
crop_params: Sequence[int],
pad_params: Sequence[int],
pad_value_mask: float,
interpolation: int,
**params: Any,
) -> np.ndarray:
return fcrops.crop_and_pad(
Expand All @@ -1248,7 +1288,7 @@ def apply_to_mask(
pad_params,
pad_value_mask,
params["shape"][:2],
interpolation,
self.mask_interpolation,
self.pad_mode,
self.keep_size,
)
Expand Down Expand Up @@ -1424,6 +1464,7 @@ def get_transform_init_args_names(self) -> tuple[str, ...]:
"keep_size",
"sample_independently",
"interpolation",
"mask_interpolation",
)


Expand Down
Loading
Loading