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

Speedup in ChannelShuffle #2291

Merged
merged 2 commits into from
Jan 24, 2025
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
7 changes: 6 additions & 1 deletion albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,12 @@ def invert(img: np.ndarray) -> np.ndarray:


def channel_shuffle(img: np.ndarray, channels_shuffled: np.ndarray) -> np.ndarray:
return img[..., channels_shuffled]
img = img.copy()
from_to = []
for i, j in enumerate(channels_shuffled):
from_to.extend([i, j])
cv2.mixChannels([img], [img], from_to)
return img


def gamma_transform(img: np.ndarray, gamma: float) -> np.ndarray:
Expand Down
14 changes: 11 additions & 3 deletions albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,9 @@
Targets:
image

Number of channels:
Any

Image types:
uint8, float32

Expand All @@ -2497,18 +2500,23 @@
def apply(
self,
img: np.ndarray,
channels_shuffled: tuple[int, ...],
channels_shuffled: tuple[int, ...] | None,
**params: Any,
) -> np.ndarray:
if channels_shuffled is None:
return img

Check warning on line 2507 in albumentations/augmentations/transforms.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/transforms.py#L2507

Added line #L2507 was not covered by tests
return fmain.channel_shuffle(img, channels_shuffled)

def get_params_dependent_on_data(
self,
params: dict[str, Any],
data: dict[str, Any],
) -> dict[str, Any]:
ch_arr = list(range(params["shape"][2]))
self.random_generator.shuffle(ch_arr)
shape = params["shape"]
if len(shape) == 2 or shape[-1] == 1:
return {"channels_shuffled": None}

Check warning on line 2517 in albumentations/augmentations/transforms.py

View check run for this annotation

Codecov / codecov/patch

albumentations/augmentations/transforms.py#L2517

Added line #L2517 was not covered by tests
ch_arr = list(range(shape[-1]))
self.py_random.shuffle(ch_arr)
return {"channels_shuffled": ch_arr}

def get_transform_init_args_names(self) -> tuple[()]:
Expand Down
Loading