如何转换“张量"到“numpy"张量流中的数组? [英] How to convert "tensor" to "numpy" array in tensorflow?
问题描述
我试图在 tesnorflow2.0 版本中将张量转换为 numpy.由于 tf2.0 启用了急切执行,因此它应该默认工作并且在正常运行时也工作.当我在 tf.data.Dataset API 中执行代码时,它给出了一个错误
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' 对象没有属性 'numpy'"
"AttributeError: 'Tensor' object has no attribute 'numpy'"
我在 tensorflow 变量之后尝试了.numpy()",而对于.eval()",我无法获得默认会话.
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
这很好用
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 函数
Note: random_noise have cv2 and sklearn functions
推荐答案
你不能在张量上使用 .numpy
方法,如果这个张量要在 中使用tf.data.Dataset.map
调用.
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.
引擎盖下的 tf.data.Dataset
对象通过创建静态图来工作:这意味着您不能使用 .numpy()
因为 tf.Tensor
对象在静态图上下文中没有此属性.
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.
因此,行 input_image = random_noise(image.numpy())
应该是 input_image = random_noise(image)
.
Therefore, the line input_image = random_noise(image.numpy())
should be input_image = random_noise(image)
.
但是由于random_noise
调用了model.utils
包中的get_noise
,代码很可能再次失败.如果 get_noise
函数是使用 Tensorflow 编写的,那么一切都会正常进行.否则,它将无法工作.
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.
解决方案?仅使用 Tensorflow 原语编写代码.
The solution? Write the code using only the Tensorflow primitives.
例如,如果您的函数 get_noise
只是使用输入图像的薄片创建随机噪声,您可以将其定义为:
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))
只使用 Tensorflow 原语,它会起作用.
using only the Tensorflow primitives, and it will work.
希望这篇概述能有所帮助!
Hope this overview helps!
PS:您可能有兴趣查看文章分析 tf.function 以发现 AutoGraph 的优势和微妙之处"-它们涵盖了这方面(也许第 3 部分与您的场景相关):第 1 部分 第 2 部分 第 3 部分
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
这篇关于如何转换“张量"到“numpy"张量流中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!