Skip to content

tf.image.convert_image_dtype overflows with bfloat16/half and wide ints (bfloat16 1.0 -> uint8 gives 0) #129731

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261028 (git v1.12.1-148391-g87a97a3655f)

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Mobile device

No response

Python version

3.12.0

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

For float -> int, convert_image_dtype multiplies by dtype.max + 0.5 in the image's own float type (image_ops_impl.py#L2588-L2590). If that type can't represent dtype.max + 0.5, the constant rounds up to dtype.max + 1 or more, so an input of 1.0 lands just past the integer range and the cast wraps it:

  • bfloat16 -> uint8: the scale becomes 256, so a white pixel (1.0) converts to 0
  • half -> int16: the scale becomes 32768, so 1.0 converts to -32768
  • half -> uint16/int32/int64: the scale is inf, so every nonzero value comes out as max (and 0 * inf is nan)
  • float32 -> int32/uint32 and float64 -> int64: the scale rounds to 231 / 232 / 2**63, so 1.0 overflows the cast. That cast is undefined, so the result depends on the platform: it happens to saturate on my arm64 Mac, and tf.image.convert_image_dtype behaves incorrectly on Windows (but fine on MacOS) #60078 got 0 for convert_image_dtype(1., tf.uint32) on Windows.

saturate=True doesn't help for the half cases: the clamp runs after the inf scale, so half -> int32 maps both 0.5 and 1.0 to 65504.

The int -> half direction has the same root cause. uint16 and wider values don't fit in half, so converting a uint16 image (e.g. from a 16-bit PNG) to float16 turns 65535 into inf, and int32 input gives nan. #35450 reported the int32 case in 2019 and it was closed as intended, but the docstring says int -> float results are normalized to [0, 1].

The bfloat16 -> uint8 case is easy to hit with mixed precision: an image that ends up in bfloat16 and is converted back to uint8 for encode_png/saving loses all its fully white pixels.

Expected: 1.0 maps to (or just below) dtype.max, and the integer max maps to 1.0, for every dtype pair, the same way float32 -> uint8 already behaves.

Standalone code to reproduce the issue

import tensorflow as tf

c = tf.image.convert_image_dtype
x = [0.0, 0.5, 1.0]
print(c(tf.constant(x, tf.bfloat16), tf.uint8).numpy())
print(c(tf.constant(x, tf.float16), tf.int16).numpy())
print(c(tf.constant(x, tf.float16), tf.uint16).numpy())
print(c(tf.constant(x, tf.float16), tf.int32, saturate=True).numpy())
print(c(tf.constant([0, 32767, 65535], tf.uint16), tf.float16).numpy())
print(c(tf.constant([0, 2**30, 2**31 - 1], tf.int32), tf.float16).numpy())

Relevant log output

[  0 128   0]
[     0  16384 -32768]
[    0 65535 65535]
[    0 65504 65504]
[0.  0.5 inf]
[ 0. nan nan]

I have a fix ready and will open a PR.

Activity

  1. added
    2.21.0
    comp:ops
    awaiting PR merge
    on Oct 30, 2026
  2. Venkat6871 commented on Nov 2, 2026

    @Venkat6871
    Contributor

    Hi @bodapatisaikrishna ,
    Thank you for reporting this issue and raising the PR to address it. A fix has been proposed in PR, and the issue will be resolved once it is merged.
    We truly appreciate your initiative and effort in contributing this fix.

  3. google-ml-butler commented on Nov 2, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  4. added a commit that references this issue on Nov 2, 2026
    8776463
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions