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

Add random horizontal flip #2126

Merged
merged 2 commits into from
Nov 9, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ Spatial-level transforms will simultaneously change both an input image as well
| [RandomCrop](https://explore.albumentations.ai/transform/RandomCrop) | ✓ | ✓ | ✓ | ✓ |
| [RandomCropFromBorders](https://explore.albumentations.ai/transform/RandomCropFromBorders) | ✓ | ✓ | ✓ | ✓ |
| [RandomGridShuffle](https://explore.albumentations.ai/transform/RandomGridShuffle) | ✓ | ✓ | ✓ | ✓ |
| [RandomHorizontalFlip](https://explore.albumentations.ai/transform/RandomHorizontalFlip) | ✓ | ✓ | ✓ | ✓ |
| [RandomResizedCrop](https://explore.albumentations.ai/transform/RandomResizedCrop) | ✓ | ✓ | ✓ | ✓ |
| [RandomRotate90](https://explore.albumentations.ai/transform/RandomRotate90) | ✓ | ✓ | ✓ | ✓ |
| [RandomScale](https://explore.albumentations.ai/transform/RandomScale) | ✓ | ✓ | ✓ | ✓ |
Expand Down
59 changes: 58 additions & 1 deletion albumentations/augmentations/tk/transform.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from __future__ import annotations

from typing import Annotated
from warnings import warn

from pydantic import AfterValidator

from albumentations.augmentations.geometric.transforms import HorizontalFlip
from albumentations.augmentations.transforms import ImageCompression
from albumentations.core.pydantic import check_0plus, nondecreasing
from albumentations.core.transforms_interface import BaseTransformInitSchema
from albumentations.core.types import Targets

__all__ = ["RandomJPEG"]
__all__ = ["RandomJPEG", "RandomHorizontalFlip"]


class RandomJPEG(ImageCompression):
Expand Down Expand Up @@ -69,3 +73,56 @@ def __init__(

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


class RandomHorizontalFlip(HorizontalFlip):
"""Horizontally flip the input randomly with a given probability.

This transform is an alias for HorizontalFlip, provided for compatibility with
torchvision and Kornia APIs. For new code, it is recommended to use
albumentations.HorizontalFlip directly.

Args:
p (float): probability of applying the transform. Default: 0.5.

Targets:
image, mask, bboxes, keypoints

Image types:
uint8, float32

Number of channels:
Any

Note:
This is a direct alias for albumentations.HorizontalFlip transform.
It is provided to make migration from torchvision and Kornia easier by
maintaining API compatibility.

Example:
>>> transform = A.RandomHorizontalFlip(p=0.5)
>>> # Consider using instead:
>>> transform = A.HorizontalFlip(p=0.5)

References:
- torchvision: https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.RandomHorizontalFlip
- Kornia: https://kornia.readthedocs.io/en/latest/augmentation.html#kornia.augmentation.RandomHorizontalFlip
"""

_targets = (Targets.IMAGE, Targets.MASK, Targets.BBOXES, Targets.KEYPOINTS)

class InitSchema(BaseTransformInitSchema):
pass

def __init__(
self,
p: float = 0.5,
always_apply: bool | None = None,
):
warn(
"RandomHorizontalFlip is an alias for HorizontalFlip transform. "
"Consider using HorizontalFlip directly from albumentations.HorizontalFlip. ",
UserWarning,
stacklevel=2,
)
super().__init__(p=p, always_apply=always_apply)
1 change: 1 addition & 0 deletions tests/aug_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,5 @@
[A.TimeMasking, {"time_mask_param": 10}],
[A.FrequencyMasking, {"freq_mask_param": 10}],
[A.RandomJPEG, {"jpeg_quality": (50, 50)}],
[A.RandomHorizontalFlip, {}],
]
1 change: 1 addition & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,7 @@ def test_mask_dropout_bboxes(remove_invisible, expected_keypoints):
A.Perspective,
A.RandomGridShuffle,
A.TimeReverse,
A.RandomHorizontalFlip,
ternaus marked this conversation as resolved.
Show resolved Hide resolved
},
),
)
Expand Down