0

I am trying to convert tensor of an image which is of shape(253,223) to numpy array of the same size so that I can plot the image. I have looked at the documentation and they suggested me to use the eval function as

sess = tf.Session()
with sess.as_default():
   print(type(tf.constant([img1]).eval()))

but it throws the error "List of Tensors when single Tensor expected".

Here type(img1) is <class 'tensorflow.python.framework.ops.Tensor'> and shape is (253, 223) . Using keras

tf.keras.backend.eval(x)

it throws

InvalidArgumentError: Input to DecodeRaw has length 56419 that is not a multiple of 4, the size of float [[{{node DecodeRaw}}]] error.

How can I convert given tensor to numpy array of the same dimension?

3
  • The problem is with x. How do you define it? Commented May 28, 2019 at 11:12
  • When I print(x) it returns Tensor("Reshape_12:0", shape=(253, 223), dtype=float32). Commented May 28, 2019 at 13:58
  • I found the answer, I was not doing the decode_raw properly, I cast it in float32 and then it worked Commented May 30, 2019 at 5:10

1 Answer 1

1

Any tensor returned by Session.run or eval is a NumPy array.

>>> print(type(tf.Session().run(tf.constant([1,2,3]))))

Or:

>>> sess = tf.InteractiveSession()
    print(type(tf.constant([1,2,3]).eval()))
    <class 'numpy.ndarray'>
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, It works fine but the same thing with above doesn't work. It is throwing an error "List of Tensors when single Tensor expected".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.