How to convert “tensor” to “numpy” array in tensorflow?

徘徊边缘 提交于 2019-12-11 00:21:55

问题


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 tf.data.Dataset 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 = tf.io.read_file(image_file)
  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
plt.figure()
plt.imshow(inp)
print(re.shape,"  ", inp.shape)

And this produces mentioned error

train_dataset = tf.data.Dataset.list_files('/data/images/train/*.png')
train_dataset = train_dataset.map(load_image_train,num_parallel_calls=tf.data.experimental.AUTOTUNE)

Note: random_noise have cv2 and sklearn functions


回答1:


You can't use the .numpy method on a tensor, if this tensor is going to be used in a tf.data.Dataset.map call.

The tf.data.Dataset 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



来源:https://stackoverflow.com/questions/56075037/how-to-convert-tensor-to-numpy-array-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!