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>
You can collect some of this information using our environment capture
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 #tf.enable_eager_execution() np.random.seed(1) 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 = sess.run(encode_a) b_encoded = sess.run(encode_b) 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.