Ask questionstf.image.encode_png doesn't support 16 bit and inconsistent behavior in eager mode

<em>Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template</em>

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Win 10
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): tested with 1.9.0, 1.12.0 and 1.14.0
  • Python version: 3.7
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with: 1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)" 2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior

Creating a numpy array with uint16 datatype and passing it to tf.image.encode_png() yields different results in eager execution mode. The first time the array is passed it somehow gets transformed to a uint8 array and for the following encodings it works as expected.

Using a tf.session the uint16 input is always transformed to uint8

Describe the expected behavior

Just return a bytestring of a 16bit PNG

Code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem.

import numpy as np
import tensorflow as tf

A = np.random.randint(low=0, high=65535, size=100, dtype=np.uint16).reshape(10,10,1)
B = A.copy()
np.allclose(A,B) # is true

# Eager Execution
a_encoded = tf.image.encode_png(A).numpy()
b_encoded = tf.image.encode_png(B).numpy()

print(len(a_encoded),len(b_encoded)) # prints 178 and 278, 278 expected both times
assert(a_encoded == b_encoded) # Fails

# Session Mode
encode_a = tf.image.encode_png(A)
encode_b = tf.image.encode_png(B)

with tf.Session() as sess:
    a_encoded =
    b_encoded =

print(len(a_encoded),len(b_encoded)) # prints 178 and 178 but 278 expected 
assert(a_encoded == b_encoded) # True

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.


Answer questions akshaym

Sorry for the delayed response.

Eager internally has 2 paths (and you've found a place where they don't agree - a discrepancy I will remove soon).

Unfortunately, this means removing the behavior of being 278 is unfortunately going to go away in eager, and all the above cases will return 178.

As suggested by @yongtang, the best thing to do is to convert the numpy array to a tensor explicitly.


Related questions

ModuleNotFoundError: No module named 'tensorflow.contrib'
Error occurred when finalizing GeneratorDataset iterator
ModuleNotFoundError: No module named 'tensorflow.contrib'
When importing TensorFlow, error loading Hadoop hot 4
The flag 'log_dir' is defined twice. hot 3
[TF 2.0] Dataset has no attribute 'make_one_shot_iterator' hot 3
Lossy conversion from float32 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning. hot 3
TF2.0 AutoGraph issue hot 3
Error loading tensorflow hot 3
AttributeError: module 'tensorflow' has no attribute 'set_random_seed' hot 3
AttributeError: module &#39;tensorflow&#39; has no attribute &#39;Session&#39; hot 3
No tf.lite.experimental.nn.bidirectional_dynamic_rnn ops is finded hot 3
AttributeError: module 'tensorflow' has no attribute 'app' hot 3
Incorrect Error TypeError: padded_batch() missing 1 required positional argument: &#39;padded_shapes&#39; hot 3
tensorflow2.0 detected 'xla_gpu' , but 'gpu' expected hot 2
Github User Rank List