【问题标题】:How to convert "tensor" to "numpy" array in tensorflow?如何在张量流中将“张量”转换为“numpy”数组?
【发布时间】:2019-05-10 09:50:52
【问题描述】:

我正在尝试在 tesnorflow2.0 版本中将张量转换为 numpy。由于 tf2.0 启用了急切执行,因此它应该默认工作并且在正常运行时也可以工作。当我在 tf.data.Dataset API 中执行代码时,它给出了一个错误

"AttributeError: 'Tensor' 对象没有属性 'numpy'"

我在 tensorflow 变量之后尝试了“.numpy()”,对于“.eval()”,我无法获得默认会话。

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

这很好用

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

这会产生提到的错误

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)

注意:random_noise 有 cv2 和 sklearn 函数

【问题讨论】:

  • 我无法阅读您的代码。您是否总是删除所有空格,即使在函数定义之间也是如此?而且您的代码几乎都与您的问题无关。请尝试制定MCVE
  • 嗨 Nils,我已经格式化了代码。代码完全使用 tensorflow2.0 并产生相同的输出和错误。请帮我解决这个错误。
  • 提供完整的错误信息
  • @AkshayNevrekar 当我使用 tensorname.numpy() 它产生"AttributeError: 'Tensor' object has no attribute 'numpy'

标签: python tensorflow tensorflow-datasets tensorflow2.0


【解决方案1】:

如果要在 tf.data.Dataset.map 调用中使用此张量,则不能对张量使用 .numpy 方法。

底层的tf.data.Dataset 对象通过创建静态图来工作:这意味着您不能使用.numpy(),因为在静态图上下文中tf.Tensor 对象没有此属性。

因此,input_image = random_noise(image.numpy()) 应该是 input_image = random_noise(image)

但由于random_noisemodel.utils 包中调用get_noise,代码很可能再次失败。 如果get_noise 函数是使用 Tensorflow 编写的,那么一切都会正常工作。否则,它将不起作用。

解决方案?仅使用 Tensorflow 原语编写代码。

例如,如果您的函数 get_noise 只是在输入图像的薄片上产生随机噪声,您可以将其定义为:

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

只使用 Tensorflow 原语,它就可以工作。

希望此概述对您有所帮助!

P.S:您可能有兴趣查看文章“分析 tf.function 以发现 AutoGraph 的优势和微妙之处”——它们涵盖了这一方面(也许第 3 部分与您的场景相关):part 1@ 987654322@part 3

【讨论】:

  • 'def get_noise(image)' 函数不是用 tensorflow 编写的。该函数的实现有 OpenCV、sklearn 等库。您能建议我在 tensorflow 执行中调用 get_noise 函数的任何方法吗?
  • 如果要给图像添加随机噪声,我之前写的函数(get_noise`可以在管道中使用为:image = image + get_noise(image)
  • 是的,或者更好的是,您可以将增强步骤包装到 py_func 调用中,并在数据处理管道中使用它。 py_func 的所有内容都不会转换为图形代码(因为 tensorflow 不知道如何将这些操作转换为节点),但是您的管道会很好。我建议将数据扩充步骤放入 py_func,然后调用 tf.data.Dataset 的 .cache() 方法,以便将扩充结果缓存到实际张量中,从而消除任何 python 瓶颈
  • 非常全面的回复,非常感谢。欣赏它。
  • 你错了@mcExchange。 tf.data.Dataset 对象在图形模式下工作,而不是在急切模式下工作。这就是存在此问题的原因。您不能在将要进行图形转换的方法内的张量上调用 .numpy() 方法。如果您在使用 tf.function 修饰的函数内部使用的张量上调用 .numpy() 方法,也会发生同样的情况。
【解决方案2】:

在 TF2.x 版本中,使用tf.config.run_functions_eagerly(True)

【讨论】:

    猜你喜欢
    • 2020-12-21
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2021-10-17
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多