简体   繁体   中英

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'decode'

I am using tensorflow on python 3.7, Ubuntu 16.04. The code which throws the above mentioned error is written below. It is based on the following code . I am getting this error on both tensorflow 1.13 as well as 2.0.0-beta1

I have a dataset folder containing millions of data pair of the form (image,timeseries). The timeseries is in numpy format. I want to use np.load() function to load the data. But the filename is in string tensor format. The problem is np.load() does not accept 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)

Please use

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

EagerTensor can be converted to numpy array as:

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

So try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM