简体   繁体   English

类型错误: <tf.tensor ... has type <class 'tensorflow.python.framework.ops.eagertensor'> ,但期望其中之一:numbers.Real</tf.tensor>

[英]TypeError: <tf.Tensor ... has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real

I am writing a function to save images to TFRecord files in order to then read then using the Data API of TensorFlow. However, when trying to create a TFRecord to save it, I receive the following error message:我正在编写一个 function 以将图像保存到 TFRecord 文件,然后使用 TensorFlow 的数据 API 进行读取。但是,当尝试创建一个 TFRecord 来保存它时,我收到以下错误消息:

TypeError: <tf.Tensor ...> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real

The function used to create the TFRecord is:用于创建 TFRecord 的 function 是:

def create_tfrecord(filepath, label):
    
    image = tf.io.read_file(filepath)
    image = tf.image.decode_jpeg(image, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [299, 299])
    
    tfrecord = Example(
        features = Features(
            feature = {
                'image' : Feature(float_list=FloatList(value=[image])),
                'label' : Feature(int64_list=Int64List(value=[label]))
    })).SerializeToString()
    
    return tfrecord

If you need additional information, please let me know.如果您需要更多信息,请告诉我。

The problem is that image is a tensor but you need a list of float values.问题是image是张量,但您需要一个浮点值列表。 Try something like this:尝试这样的事情:

import tensorflow as tf

def create_tfrecord(filepath, label):
    
    image = tf.io.read_file(filepath)
    image = tf.image.decode_jpeg(image, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [299, 299])
    
    tfrecord = tf.train.Example(
        features = tf.train.Features(
            feature = {
                'image' : tf.train.Feature(float_list=tf.train.FloatList(value=image.numpy().ravel().tolist())),
                'label' : tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
    })).SerializeToString()
    
    return tfrecord

create_tfrecord('/content/result_image.png', 1)

Dummy data was created like this:虚拟数据是这样创建的:

import numpy
from PIL import Image

imarray = numpy.random.rand(300,300,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
im.save('result_image.png')

If you want to reproduce this example.如果你想重现这个例子。 When loading the tf-record, you just have to reshape the image to its original size.加载 tf-record 时,只需将图像重塑为原始大小即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:<tf.tensor: shape ...> 有类型<class 'tensorflow.python.framework.ops.eagertensor'> ,但预期其中之一:(<class 'int'> ,)</class></class></tf.tensor:> - TypeError: <tf.Tensor: shape ... > has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,) 类型 &#39;tensorflow.python.framework.ops.EagerTensor&#39; 没有 len() - type 'tensorflow.python.framework.ops.EagerTensor' has no len() AttributeError: &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;to_tensor&#39; - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'to_tensor' &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;squeeze&#39; - 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'squeeze' 将“tensorflow.python.framework.ops.EagerTensor”转换为 tensorflow.Tensor 或 torch.Tensor? - convert "tensorflow.python.framework.ops.EagerTensor" to tensorflow.Tensor or torch.Tensor? 项目分配 Tensorflow 2.0 - TypeError: 'tensorflow.python.framework.ops.EagerTensor' object 不支持项目分配 - Item Assignment Tensorflow 2.0 - TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment TypeError: &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象不支持项目分配 - TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 'ravel' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'ravel' AttributeError: &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;decode&#39; - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'decode' 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 '_in_graph_mode' - 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_in_graph_mode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM