Skip to content
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
9 changes: 8 additions & 1 deletion dm_pix/_src/augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ def random_flip_left_right(
image: chex.Array,
*,
probability: chex.Numeric = 0.5,
channel_axis: int = -1,
) -> chex.Array:
"""Applies `flip_left_right` with a given probability.

Expand All @@ -781,14 +782,20 @@ def random_flip_left_right(
...HWC or ...CHW.
probability: the probability of applying flip_left_right transform. Must be
a value in [0, 1].
channel_axis: the index of the channel axis.

Returns:
A left-right flipped image if condition is met, otherwise original image.
"""
# DO NOT REMOVE - Logging usage.

should_transform = jax.random.bernoulli(key=key, p=probability)
return jax.lax.cond(should_transform, flip_left_right, lambda x: x, image)
return jax.lax.cond(
should_transform,
lambda x: flip_left_right(x, channel_axis=channel_axis),
lambda x: x,
image,
)


def random_flip_up_down(
Expand Down
6 changes: 6 additions & 0 deletions dm_pix/_src/augment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ def test_flip(self, images_list):
reference_fn=None,
probability=(0., 1.))

def test_random_flip_left_right_channel_axis(self):
image = jnp.arange(2 * 3 * 4, dtype=jnp.float32).reshape((3, 2, 4))
result = augment.random_flip_left_right(
jax.random.PRNGKey(0), image, probability=1., channel_axis=0)
np.testing.assert_array_equal(result, jnp.flip(image, axis=2))

# Due to a bug in scipy we cannot test all available modes, refer to these
# issues for more information: https://github.com/jax-ml/jax/issues/11097,
# https://github.com/jax-ml/jax/issues/11097
Expand Down