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

Behavior of solarize() depends on integer overflow #110

Open
EIFY opened this issue Jun 6, 2024 · 0 comments
Open

Behavior of solarize() depends on integer overflow #110

EIFY opened this issue Jun 6, 2024 · 0 comments

Comments

@EIFY
Copy link

EIFY commented Jun 6, 2024

I am not 100% sure about the intention but I do want to raise the alarm. The solarize() transform here

def solarize(image, threshold=128):
# For each pixel in the image, select the pixel
# if the value is less than the threshold.
# Otherwise, subtract 255 from the pixel.
return tf.where(image < threshold, image, 255 - image)

inverts the pixel when its value is greater or equal to the threshold, so one would think that higher augmentation magnitude needs lower threshold. However, the threshold increases linearly with magnitude:

'Solarize': lambda level: (int((level/_MAX_LEVEL) * 256),),

Counterintuitively, it still works as expected with magnitude=_MAX_LEVEL because of integer overflow. Given

t = tf.constant([[[0,0,0]]], dtype=tf.uint8)

t < i evaluates to tf.Tensor([[[False False False]]], shape=(1, 1, 3), dtype=bool) iff not (i % 256). In other words, magnitude=_MAX_LEVEL means int((level/_MAX_LEVEL) * 256) = 256, which is equivalent to 0 in tf.uint8. Given the following tf_gradient that goes from (0, 0, 0) to (255, 255, 255) in alternating directions

download (13)

Both solarize(tf_gradient, 256) and solarize(tf_gradient, 0) indeed fully invert the image:

download (14)

But if magnitude is 9, int((9/10) * 256) = 230, and solarize(tf_gradient, 230) "abruptly" only inverts a small portion of the image:

download (15)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant