Skip to content

Commit

Permalink
updated random reszied crop (#2236)
Browse files Browse the repository at this point in the history
  • Loading branch information
ternaus authored Jan 7, 2025
1 parent d58ed3e commit 6b18230
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
38 changes: 4 additions & 34 deletions albumentations/augmentations/crops/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
PositionType,
PxType,
ScaleFloatType,
ScaleIntType,
)

from . import functional as fcrops
Expand Down Expand Up @@ -886,7 +885,7 @@ class InitSchema(BaseTransformInitSchema):
mask_interpolation: InterpolationType
min_max_height: OnePlusIntRangeType
w2h_ratio: Annotated[float, Field(gt=0)]
size: tuple[int, int]
size: Annotated[tuple[int, int], AfterValidator(check_range_bounds(1, None))]

def __init__(
self,
Expand Down Expand Up @@ -999,50 +998,21 @@ class InitSchema(BaseTransformInitSchema):
AfterValidator(check_range_bounds(0, None)),
AfterValidator(nondecreasing),
]
width: int | None
height: int | None
size: ScaleIntType | None
size: Annotated[tuple[int, int], AfterValidator(check_range_bounds(1, None))]
interpolation: InterpolationType
mask_interpolation: InterpolationType

@model_validator(mode="after")
def process(self) -> Self:
if isinstance(self.size, int):
if isinstance(self.width, int):
warn(
"Initializing with 'size' as an integer and a separate 'width', `height` are deprecated. "
"Please use a tuple (height, width) for the 'size' argument.",
DeprecationWarning,
stacklevel=2,
)
self.size = (self.size, self.width)
else:
msg = "If size is an integer, width as integer must be specified."
raise TypeError(msg)

if self.size is None:
if self.height is None or self.width is None:
message = "If 'size' is not provided, both 'height' and 'width' must be specified."
raise ValueError(message)
self.size = (self.height, self.width)

return self

def __init__(
self,
# NOTE @zetyquickly: when (width, height) are deprecated, make 'size' non optional
size: ScaleIntType | None = None,
width: int | None = None,
height: int | None = None,
*,
size: tuple[int, int],
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,
p: float = 1.0,
):
super().__init__(
size=cast(tuple[int, int], size),
size=size,
interpolation=interpolation,
mask_interpolation=mask_interpolation,
p=p,
Expand Down
2 changes: 1 addition & 1 deletion tests/aug_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
[A.NoOp, {}],
[
A.RandomResizedCrop,
dict(height=20, width=30, scale=(0.5, 0.6), ratio=(0.8, 0.9)),
dict(size=(20, 30), scale=(0.5, 0.6), ratio=(0.8, 0.9)),
],
[A.FancyPCA, dict(alpha=0.3)],
[A.RandomRotate90, {}],
Expand Down

0 comments on commit 6b18230

Please sign in to comment.