SquareSymmetry

Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32

Applies one of the eight possible square symmetry transformations to a square-shaped input. This is an alias for D4 transform with a more intuitive name for those not familiar with group theory.

The square symmetry transformations include:

  • Identity: No transformation is applied
  • 90° rotation: Rotate 90 degrees counterclockwise
  • 180° rotation: Rotate 180 degrees
  • 270° rotation: Rotate 270 degrees counterclockwise
  • Vertical flip: Mirror across vertical axis
  • Anti-diagonal flip: Mirror across anti-diagonal
  • Horizontal flip: Mirror across horizontal axis
  • Main diagonal flip: Mirror across main diagonal

When group_element is specified, the transform is deterministic—useful for TTA (Test Time Augmentation) where you need to apply each of the 8 symmetries explicitly and invert predictions. Call inverse() on a deterministic instance to get a new transform that undoes the operation.

Arguments
p
float
1

Probability of applying the transform. Default: 1.0.

group_element
e | r90 | r180 | r270 | v | hvt | h | t |

If set, always apply this specific D4 group element instead of sampling randomly. Use for TTA. Default: None (random choice).

Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.Compose([
...     A.SquareSymmetry(p=1.0),
... ])
>>> transformed = transform(image=image)
>>> transformed_image = transformed['image']
# The resulting image will be one of the 8 possible square symmetry transformations of the input

>>> # TTA: apply each symmetry, run inference, then undo the transform on the prediction
>>> from albumentations.core.type_definitions import d4_group_elements
>>> predictions = []
>>> for element in d4_group_elements:
...     aug = A.SquareSymmetry(p=1.0, group_element=element)
...     aug_image = aug(image=image)["image"]
...     pred_mask = np.zeros((100, 100, 1), dtype=np.uint8)  # placeholder for model output
...     restored = aug.inverse()(image=pred_mask)["image"]
...     predictions.append(restored)
Notes
  • This transform is particularly useful for augmenting data that does not have a clear orientation, such as top-view satellite or drone imagery, or certain types of medical images.
  • The input image should be square-shaped for optimal results. Non-square inputs may lead to unexpected behavior or distortions.
  • When applied to bounding boxes or keypoints, their coordinates will be adjusted according to the selected transformation.
  • This transform preserves the aspect ratio and size of the input.
  • inverse() requires group_element to be set explicitly; raises ValueError otherwise.