Split image into a grid and randomly permute cells; same shuffle for all targets. Grid size from grid (e.g. (3, 3)). Breaks global layout, keeps local content.
gridSize of the grid for splitting the image into cells. Each cell is shuffled randomly. For example, (3, 3) will divide the image into a 3x3 grid, resulting in 9 cells to be shuffled. Default: (3, 3)
pProbability that the transform will be applied. Should be in the range [0, 1]. Default: 0.5
>>> import numpy as np
>>> import albumentations as A
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> bboxes = np.array([[10, 10, 50, 50], [40, 40, 80, 80]], dtype=np.float32)
>>> bbox_labels = [1, 2]
>>> keypoints = np.array([[20, 30], [60, 70]], dtype=np.float32)
>>> keypoint_labels = [0, 1]
>>>
>>> # Define transform with grid as a tuple
>>> transform = A.Compose([
... A.RandomGridShuffle(grid=(3, 3), p=1.0),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
... keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = transformed['image'] # Grid-shuffled image
>>> transformed_mask = transformed['mask'] # Grid-shuffled mask
>>> transformed_bboxes = transformed['bboxes'] # Grid-shuffled bounding boxes
>>> transformed_keypoints = transformed['keypoints'] # Grid-shuffled keypoints
>>>
>>> # Visualization example with a simpler grid
>>> simple_image = np.array([
... [1, 1, 1, 2, 2, 2],
... [1, 1, 1, 2, 2, 2],
... [1, 1, 1, 2, 2, 2],
... [3, 3, 3, 4, 4, 4],
... [3, 3, 3, 4, 4, 4],
... [3, 3, 3, 4, 4, 4]
... ])
>>> simple_transform = A.RandomGridShuffle(grid=(2, 2), p=1.0)
>>> simple_result = simple_transform(image=simple_image)
>>> simple_transformed = simple_result['image']
>>> # The result could look like:
>>> # array([[4, 4, 4, 2, 2, 2],
>>> # [4, 4, 4, 2, 2, 2],
>>> # [4, 4, 4, 2, 2, 2],
>>> # [3, 3, 3, 1, 1, 1],
>>> # [3, 3, 3, 1, 1, 1],
>>> # [3, 3, 3, 1, 1, 1]])