【发布时间】: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