Skip to content

Commit

Permalink
Fix blur (#1700)
Browse files Browse the repository at this point in the history
* Added heavy refactoring

* Added heavy refactoring

* Updated interface for Downscale

* Updated Downscale transform

* Fix in CI
  • Loading branch information
ternaus authored May 4, 2024
1 parent 66273bf commit cd50269
Show file tree
Hide file tree
Showing 25 changed files with 664 additions and 550 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install requirements
run: |
python -m pip install --upgrade pip
pip install .
- name: Install dev requirements
run: pip install -r requirements-dev.txt
- name: Run checks
Expand Down
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: requirements-txt-fixer
- repo: local
hooks:
- id: check-defaults-in-apply
name: Check defaults in apply method of BasicTransform subclasses
entry: python -m tools.check_defaults
language: system
files: '\.py$'
- repo: local
hooks:
- id: check-docstrings
Expand Down Expand Up @@ -66,7 +73,7 @@ repos:
- id: codespell
additional_dependencies: ["tomli"]
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.39.0
rev: v0.40.0
hooks:
- id: markdownlint
- repo: https://github.com/tox-dev/pyproject-fmt
Expand Down
26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ Even if a parameter defined as `Tuple`, the transform should work correctly with

To maintain determinism and reproducibility, handle all probability calculations within the `get_params` or `get_params_dependent_on_targets` methods. These calculations should not occur in the `apply_xxx` or `__init__` methods, as it is crucial to separate configuration from execution in our codebase.

### Specific Guidelines for Method Definitions

#### Handling `apply_xxx` Methods

When contributing code related to transformation methods, specifically methods that start with `apply_` (e.g., `apply_to_mask`, `apply_to_bbox`), please adhere to the following guidelines:

**No Default Arguments**: Do not use default arguments in `apply_xxx` methods. Every parameter should be explicitly required, promoting clarity and reducing hidden behaviors that can arise from default values.

### Examples

Here are a few examples to illustrate these guidelines:

**Incorrect** method definition:

```python
def apply_to_mask(self, mask, fill_value=0): # Default value not allowed
# implementation
```

**Correct** method definition:

```python
def apply_to_mask(self, mask, fill_value): # No default values
# implementation
```

## Guidelines for Modifying Existing Code

Maintaining the stability and usability of Albumentations for all users is a priority. When contributing, it's important to follow these guidelines for modifying existing code:
Expand Down
22 changes: 9 additions & 13 deletions albumentations/augmentations/blur/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ def __init__(self, blur_limit: ScaleIntType = 7, always_apply: bool = False, p:
super().__init__(always_apply, p)
self.blur_limit = cast(Tuple[int, int], blur_limit)

def apply(self, img: np.ndarray, kernel: int = 3, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, kernel: int, **params: Any) -> np.ndarray:
return F.blur(img, kernel)

def get_params(self) -> Dict[str, Any]:
return {"ksize": int(random.choice(list(range(self.blur_limit[0], self.blur_limit[1] + 1, 2))))}
return {"kernel": random_utils.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 @@ -133,7 +133,7 @@ def __init__(
def get_transform_init_args_names(self) -> Tuple[str, ...]:
return (*super().get_transform_init_args_names(), "allow_shifted")

def apply(self, img: np.ndarray, kernel: Optional[np.ndarray] = None, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, kernel: np.ndarray, **params: Any) -> np.ndarray:
return FMain.convolve(img, kernel=kernel)

def get_params(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -194,7 +194,7 @@ class MedianBlur(Blur):
def __init__(self, blur_limit: ScaleIntType = 7, always_apply: bool = False, p: float = 0.5):
super().__init__(blur_limit, always_apply, p)

def apply(self, img: np.ndarray, kernel: int = 3, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, kernel: int, **params: Any) -> np.ndarray:
return F.median_blur(img, kernel)


Expand Down Expand Up @@ -260,7 +260,7 @@ def __init__(
self.blur_limit = cast(Tuple[int, int], blur_limit)
self.sigma_limit = cast(Tuple[float, float], sigma_limit)

def apply(self, img: np.ndarray, ksize: int = 3, sigma: float = 0, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, ksize: int, sigma: float, **params: Any) -> np.ndarray:
return F.gaussian_blur(img, ksize, sigma=sigma)

def get_params(self) -> Dict[str, float]:
Expand Down Expand Up @@ -318,7 +318,7 @@ def __init__(
self.iterations = iterations
self.mode = mode

def apply(self, img: np.ndarray, *args: Any, dxy: np.ndarray = None, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, *args: Any, dxy: np.ndarray, **params: Any) -> np.ndarray:
if dxy is None:
msg = "dxy is None"
raise ValueError(msg)
Expand Down Expand Up @@ -450,7 +450,7 @@ def __init__(
self.beta_limit = cast(Tuple[float, float], beta_limit)
self.noise_limit = cast(Tuple[float, float], noise_limit)

def apply(self, img: np.ndarray, kernel: Optional[np.ndarray] = None, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, kernel: np.ndarray, **params: Any) -> np.ndarray:
return FMain.convolve(img, kernel=kernel)

def get_params(self) -> Dict[str, np.ndarray]:
Expand Down Expand Up @@ -532,7 +532,7 @@ def __init__(
self.radius = cast(Tuple[int, int], radius)
self.alias_blur = cast(Tuple[float, float], alias_blur)

def apply(self, img: np.ndarray, radius: int = 3, alias_blur: float = 0.5, **params: Any) -> np.ndarray:
def apply(self, img: np.ndarray, radius: int, alias_blur: float, **params: Any) -> np.ndarray:
return F.defocus(img, radius, alias_blur)

def get_params(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -582,11 +582,7 @@ def __init__(
self.max_factor = cast(Tuple[float, float], max_factor)
self.step_factor = cast(Tuple[float, float], step_factor)

def apply(self, img: np.ndarray, zoom_factors: Optional[np.ndarray] = None, **params: Any) -> np.ndarray:
if zoom_factors is None:
msg = "zoom_factors is None"
raise ValueError(msg)

def apply(self, img: np.ndarray, zoom_factors: np.ndarray, **params: Any) -> np.ndarray:
return F.zoom_blur(img, zoom_factors)

def get_params(self) -> Dict[str, Any]:
Expand Down
Loading

0 comments on commit cd50269

Please sign in to comment.