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

Fix ChannelShuffle #418

Closed
wants to merge 4 commits into from
Closed
Changes from 2 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
12 changes: 8 additions & 4 deletions keras_cv/layers/preprocessing/channel_shuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import tensorflow as tf

import keras_cv


@tf.keras.utils.register_keras_serializable(package="keras_cv")
class ChannelShuffle(tf.keras.__internal__.layers.BaseImageAugmentationLayer):
Expand Down Expand Up @@ -65,10 +67,12 @@ def augment_image(self, image, transformation=None):
)

channels_per_group = num_channels // self.groups
image = tf.reshape(image, [height, width, self.groups, channels_per_group])
image = tf.transpose(image, perm=[2, 0, 1, 3])
image = tf.random.shuffle(image, seed=self.seed)
image = tf.transpose(image, perm=[1, 2, 3, 0])

rand_uniform = keras_cv.UniformFactorSampler(lower=0, upper=1, seed=self.seed)
rand_indices = tf.argsort(rand_uniform(shape=[self.groups]))

image = tf.reshape(image, [height, width, channels_per_group, self.groups])
image = tf.gather(image, rand_indices, axis=-1)
image = tf.reshape(image, [height, width, num_channels])

return image
Expand Down