AttributeError:“ tensorflow.python.framework.ops.EagerTensor”对象没有属性“ decode”

时间:2019-06-19 18:36:04

标签: python numpy tensorflow tensorflow-datasets tensorflow2.0

我正在python 3.7,Ubuntu 16.04上使用tensorflow。引发上述错误的代码如下所示。它基于以下code。我在tensorflow 1.13和2.0.0-beta1

上都收到此错误

我有一个数据集文件夹,其中包含数百万个格式(图像,时间序列)的数据对。时间序列为numpy格式。我想使用np.load()函数加载数据。但是文件名是字符串张量格式。问题是np.load()不接受tensorflow.python.framework.ops.EagerTensor

import tensorflow as tf
import numpy as np
import imageio

#tf.enable_eager_execution()    # use this line if using tensorflow 1.13

imageio.imwrite('data.jpg', np.random.rand(256,256,3))
np.save('data.npy',np.ones(1024))

def load(image_file,timeseries_file):
    image = tf.io.read_file(image_file)
    image = tf.image.decode_jpeg(image)
    timeseries = np.load(timeseries_file.decode())
    timeseries = tf.convert_to_tensor(timeseries, np.float32)
    image = tf.cast(image, tf.float32)
    timeseries = tf.cast(timeseries, tf.float32)
    return image, timeseries

image_files = ['data.jpg']
timeseries_files = ['data.npy']
train_dataset = tf.data.Dataset.from_tensor_slices((image_files, timeseries_files))
train_dataset = train_dataset.map(
lambda image_file, timeseries_file: tuple(tf.py_function(
    load, [image_file, timeseries_file], [tf.float32, tf.float32])))
for x in train_dataset.take(1):
    print(x)

2 个答案:

答案 0 :(得分:0)

请使用

import tensorflow.compat.v1 as tf 
tf.disable_v2_behavior()

答案 1 :(得分:0)

EagerTensor 可以转换为 numpy 数组:

tensor.numpy()
# or
np.array(tensor)

那就试试吧:

timeseries = np.load(timeseries_file.numpy().decode())