Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ternaus committed Oct 25, 2024
1 parent ecaaf05 commit 19b274c
Show file tree
Hide file tree
Showing 26 changed files with 645 additions and 737 deletions.
7 changes: 3 additions & 4 deletions albumentations/augmentations/blur/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pydantic import Field, ValidationInfo, field_validator, model_validator
from typing_extensions import Self

from albumentations import random_utils
from albumentations.augmentations import functional as fmain
from albumentations.augmentations.utils import check_range
from albumentations.core.pydantic import (
Expand Down Expand Up @@ -103,7 +102,7 @@ def apply(self, img: np.ndarray, kernel: int, **params: Any) -> np.ndarray:
return fblur.blur(img, kernel)

def get_params(self) -> dict[str, Any]:
return {"kernel": random_utils.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2)))}
return {"kernel": self.random_generator.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2)))}

def get_transform_init_args_names(self) -> tuple[str, ...]:
return ("blur_limit",)
Expand Down Expand Up @@ -497,7 +496,7 @@ def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, A
width_pixels = height - self.max_delta * 2
height_pixels = width - self.max_delta * 2
total_pixels = int(width_pixels * height_pixels)
dxy = random_utils.randint(-self.max_delta, self.max_delta, size=(total_pixels, self.iterations, 2))
dxy = self.random_generator.integers(-self.max_delta, self.max_delta, size=(total_pixels, self.iterations, 2))

return {"dxy": dxy}

Expand Down Expand Up @@ -660,7 +659,7 @@ def get_params(self) -> dict[str, np.ndarray]:
random.uniform(self.beta_limit[0], 1) if random.random() < HALF else random.uniform(1, self.beta_limit[1])
)

noise_matrix = random_utils.uniform(*self.noise_limit, size=(ksize, ksize))
noise_matrix = self.random_generator.uniform(*self.noise_limit, size=(ksize, ksize))

# Generate mesh grid centered at zero.
ax = np.arange(-ksize // 2 + 1.0, ksize // 2 + 1.0)
Expand Down
17 changes: 8 additions & 9 deletions albumentations/augmentations/dropout/coarse_dropout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pydantic import AfterValidator, Field, model_validator
from typing_extensions import Literal, Self

from albumentations import random_utils
from albumentations.augmentations.dropout.transforms import BaseDropout
from albumentations.core.pydantic import check_1plus, nondecreasing
from albumentations.core.types import ColorType, NumericType, ScalarType
Expand Down Expand Up @@ -156,8 +155,8 @@ def __init__(
self.hole_height_range = hole_height_range
self.hole_width_range = hole_width_range

@staticmethod
def calculate_hole_dimensions(
self,
image_shape: tuple[int, int],
height_range: tuple[ScalarType, ScalarType],
width_range: tuple[ScalarType, ScalarType],
Expand All @@ -173,12 +172,12 @@ def calculate_hole_dimensions(
min_width = width_range[0]
max_width = min(width_range[1], width)

hole_heights = random_utils.randint(int(min_height), int(max_height + 1), size=size)
hole_widths = random_utils.randint(int(min_width), int(max_width + 1), size=size)
hole_heights = self.random_generator.integers(int(min_height), int(max_height + 1), size=size)
hole_widths = self.random_generator.integers(int(min_width), int(max_width + 1), size=size)

else: # Assume float
hole_heights = (height * random_utils.uniform(*height_range, size=size)).astype(int)
hole_widths = (width * random_utils.uniform(*width_range, size=size)).astype(int)
hole_heights = (height * self.random_generator.uniform(*height_range, size=size)).astype(int)
hole_widths = (width * self.random_generator.uniform(*width_range, size=size)).astype(int)

return hole_heights, hole_widths

Expand All @@ -196,14 +195,14 @@ def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, A

height, width = image_shape[:2]

y_min = random_utils.randint(0, height - hole_heights + 1, size=num_holes)
x_min = random_utils.randint(0, width - hole_widths + 1, size=num_holes)
y_min = self.random_generator.integers(0, height - hole_heights + 1, size=num_holes)
x_min = self.random_generator.integers(0, width - hole_widths + 1, size=num_holes)
y_max = y_min + hole_heights
x_max = x_min + hole_widths

holes = np.stack([x_min, y_min, x_max, y_max], axis=-1)

return {"holes": holes}
return {"holes": holes, "seed": self.random_generator.integers(0, 2**32 - 1)}

def get_transform_init_args_names(self) -> tuple[str, ...]:
return (*super().get_transform_init_args_names(), "num_holes_range", "hole_height_range", "hole_width_range")
29 changes: 15 additions & 14 deletions albumentations/augmentations/dropout/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from albucore import MAX_VALUES_BY_DTYPE, is_grayscale_image, preserve_channel_dim
from typing_extensions import Literal

from albumentations import random_utils
from albumentations.augmentations.geometric.functional import split_uniform_grid
from albumentations.augmentations.utils import handle_empty_array
from albumentations.core.types import MONO_CHANNEL_DIMENSIONS, ColorType
Expand Down Expand Up @@ -39,7 +38,7 @@ def channel_dropout(
def generate_random_fill(
dtype: np.dtype,
shape: tuple[int, ...],
random_generator: np.random.Generator | None = None,
random_generator: np.random.Generator,
) -> np.ndarray:
"""Generate a random fill array based on the given dtype and target shape.
Expand All @@ -50,7 +49,7 @@ def generate_random_fill(
Args:
dtype (np.dtype): The data type of the array to be generated.
shape (tuple[int, ...]): The shape of the array to be generated.
random_generator (np.random.Generator | None): The random generator to use for generating values.
random_generator (np.random.Generator): The random generator to use for generating values.
If None, the default numpy random generator is used.
Returns:
Expand All @@ -69,17 +68,17 @@ def generate_random_fill(
"""
max_value = MAX_VALUES_BY_DTYPE[dtype]
if np.issubdtype(dtype, np.integer):
return random_utils.randint(0, max_value + 1, size=shape, dtype=dtype, random_generator=random_generator)
return random_generator.integers(0, max_value + 1, size=shape, dtype=dtype)
if np.issubdtype(dtype, np.floating):
return random_utils.uniform(0, max_value, size=shape, random_generator=random_generator).astype(dtype)
return random_generator.uniform(0, max_value, size=shape).astype(dtype)
raise ValueError(f"Unsupported dtype: {dtype}")


def cutout(
img: np.ndarray,
holes: np.ndarray,
fill_value: ColorType | Literal["random"],
random_generator: np.random.Generator | None = None,
random_generator: np.random.Generator,
) -> np.ndarray:
"""Apply cutout augmentation to the image by cutting out holes and filling them
with either a given value or random noise.
Expand All @@ -88,10 +87,10 @@ def cutout(
img (np.ndarray): The image to augment. Can be a 2D (grayscale) or 3D (color) array.
holes (np.ndarray): An array of holes with shape (num_holes, 4).
Each hole is represented as [x1, y1, x2, y2].
fill_value (Union[ColorType, Literal["random"]], optional): The fill value to use for the holes.
fill_value (ColorType | Literal["random"]): The fill value to use for the holes.
Can be a single integer, a tuple or list of numbers for multichannel images,
or the string "random" to fill with random noise.
random_generator (np.random.Generator | None, optional): The random generator to use for generating
random_generator (np.random.Generator): The random generator to use for generating
random fill values. If None, a new random generator will be used. Defaults to None.
Returns:
Expand Down Expand Up @@ -218,6 +217,7 @@ def calculate_grid_dimensions(
image_shape: tuple[int, int],
unit_size_range: tuple[int, int] | None,
holes_number_xy: tuple[int, int] | None,
random_generator: np.random.Generator,
) -> tuple[int, int]:
"""Calculate the dimensions of grid units for GridDropout.
Expand All @@ -233,6 +233,7 @@ def calculate_grid_dimensions(
If provided, a random size within this range will be chosen for both height and width.
holes_number_xy (tuple[int, int] | None, optional): The number of holes in the x and y directions.
If provided, the grid dimensions will be calculated to fit this number of holes.
random_generator (np.random.Generator): The random generator to use for generating random values.
Returns:
tuple[int, int]: The calculated grid unit dimensions as (unit_height, unit_width).
Expand Down Expand Up @@ -262,7 +263,7 @@ def calculate_grid_dimensions(
if unit_size_range is not None:
if unit_size_range[1] > min(image_shape[:2]):
raise ValueError("Grid size limits must be within the shortest image edge.")
unit_size = random_utils.randint(*unit_size_range)
unit_size = random_generator.integers(*unit_size_range)
return unit_size, unit_size

if holes_number_xy:
Expand All @@ -283,7 +284,7 @@ def generate_grid_holes(
ratio: float,
random_offset: bool,
shift_xy: tuple[int, int],
random_generator: np.random.Generator | None = None,
random_generator: np.random.Generator,
) -> np.ndarray:
"""Generate a list of holes for GridDropout using a uniform grid.
Expand All @@ -300,7 +301,7 @@ def generate_grid_holes(
If False, uses the global shift specified by shift_xy.
shift_xy (tuple[int, int]): The global shift to apply to all holes as (shift_x, shift_y).
Only used when random_offset is False.
random_generator (np.random.Generator | None): The random generator for generating random offsets
random_generator (np.random.Generator): The random generator for generating random offsets
and shuffling. If None, a new Generator will be created.
Returns:
Expand Down Expand Up @@ -343,10 +344,10 @@ def generate_grid_holes(
max_offset_y = cell_heights - hole_heights
max_offset_x = cell_widths - hole_widths

if random_offset and random_generator is not None:
if random_offset:
# Generate random offsets for each hole
offset_y = random_utils.randint(0, max_offset_y + 1, random_generator=random_generator)
offset_x = random_utils.randint(0, max_offset_x + 1, random_generator=random_generator)
offset_y = random_generator.integers(0, max_offset_y + 1)
offset_x = random_generator.integers(0, max_offset_x + 1)
else:
# Use global shift
offset_y = np.full_like(max_offset_y, shift_xy[1])
Expand Down
4 changes: 3 additions & 1 deletion albumentations/augmentations/dropout/grid_dropout.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, A
image_shape,
self.unit_size_range,
self.holes_number_xy,
self.random_generator,
)
grid = (image_shape[0] // unit_height, image_shape[1] // unit_width)

Expand All @@ -158,8 +159,9 @@ def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, A
self.ratio,
self.random_offset,
self.shift_xy,
self.random_generator,
)
return {"holes": holes}
return {"holes": holes, "seed": self.random_generator.integers(0, 2**32 - 1)}

def get_transform_init_args_names(self) -> tuple[str, ...]:
return (
Expand Down
8 changes: 4 additions & 4 deletions albumentations/augmentations/dropout/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def __init__(
self.fill_value = fill_value
self.mask_fill_value = mask_fill_value

def apply(self, img: np.ndarray, holes: np.ndarray, **params: Any) -> np.ndarray:
return cutout(img, holes, self.fill_value)
def apply(self, img: np.ndarray, holes: np.ndarray, seed: int, **params: Any) -> np.ndarray:
return cutout(img, holes, self.fill_value, np.random.default_rng(seed))

def apply_to_mask(self, mask: np.ndarray, holes: np.ndarray, **params: Any) -> np.ndarray:
def apply_to_mask(self, mask: np.ndarray, holes: np.ndarray, seed: int, **params: Any) -> np.ndarray:
if self.mask_fill_value is None:
return mask
return cutout(mask, holes, self.mask_fill_value)
return cutout(mask, holes, self.mask_fill_value, np.random.default_rng(seed))

def apply_to_bboxes(
self,
Expand Down
2 changes: 1 addition & 1 deletion albumentations/augmentations/dropout/xy_masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_params_dependent_on_data(
masks_y = self.generate_masks(self.num_masks_y, image_shape, self.mask_y_length, axis="y")

holes = np.array(masks_x + masks_y)
return {"holes": holes}
return {"holes": holes, "seed": self.random_generator.integers(0, 2**32 - 1)}

@staticmethod
def generate_mask_size(mask_length: tuple[int, int]) -> int:
Expand Down
Loading

0 comments on commit 19b274c

Please sign in to comment.