![](/img/trans.png)
[英]TypeError: <tf.Tensor: shape ... > has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
[英]TypeError: <tf.Tensor ... has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real
我正在編寫一個 function 以將圖像保存到 TFRecord 文件,然后使用 TensorFlow 的數據 API 進行讀取。但是,當嘗試創建一個 TFRecord 來保存它時,我收到以下錯誤消息:
TypeError: <tf.Tensor ...> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real
用於創建 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
如果您需要更多信息,請告訴我。
問題是image
是張量,但您需要一個浮點值列表。 嘗試這樣的事情:
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)
虛擬數據是這樣創建的:
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')
如果你想重現這個例子。 加載 tf-record 時,只需將圖像重塑為原始大小即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.