How to convert "tensor" to "numpy" array in tensorflow?



I am trying to convert a tensor to numpy in the tesnorflow2.0 version. Since tf2.0 have eager execution enabled then it should work by default and working too in normal runtime. While I execute code in API then it gives an error

“AttributeError: ‘Tensor’ object has no attribute ‘numpy'”

I have tried “.numpy()” after tensorflow variable and for “.eval()” I am unable to get default session.

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
# tf.executing_eagerly()
import os
import time
import matplotlib.pyplot as plt
from IPython.display import clear_output
from model.utils import  get_noise
import cv2

def random_noise(input_image):
  img_out = get_noise(input_image)
  return img_out

def load_denoising(image_file):
  image =
  image = tf.image.decode_png(image)
  real_image = image
  input_image = random_noise(image.numpy())
  input_image = tf.cast(input_image, tf.float32)
  real_image = tf.cast(real_image, tf.float32)
  return input_image, real_image

def load_image_train(image_file):
  input_image, real_image = load_denoising(image_file)
  return input_image, real_image

This works fine

inp, re = load_denoising('/data/images/train/18.png')
# Check for correct run
print(re.shape,"  ", inp.shape)

And this produces mentioned error

train_dataset ='/data/images/train/*.png')
train_dataset =,

Note: random_noise have cv2 and sklearn functions


You can’t use the .numpy method on a tensor, if this tensor is going to be used in a call.

The object under the hood works by creating a static graph: this means that you can’t use .numpy() because the tf.Tensor object when in a static-graph context do not have this attribute.

Therefore, the line input_image = random_noise(image.numpy()) should be input_image = random_noise(image).

But the code is likely to fail again since random_noise calls get_noise from the model.utils package.
If the get_noise function is written using Tensorflow, then everything will work. Otherwise, it won’t work.

The solution? Write the code using only the Tensorflow primitives.

For instance, if your function get_noise just creates random noise with the shee of your input image, you can define it like:

def get_noise(image):
    return tf.random.normal(shape=tf.shape(image))

using only the Tensorflow primitives, and it will work.

Hope this overview helps!

P.S: you could be interested in having a look at the articles “Analyzing tf.function to discover AutoGraph strengths and subtleties” – they cover this aspect (perhaps part 3 is the one related to your scenario): part 1 part 2 part 3

Answered By – nessuno

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More