首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于从tfrecords记录读取数据时的形状

关于从tfrecords记录读取数据时的形状
EN

Stack Overflow用户
提问于 2017-11-05 10:29:37
回答 1查看 1.2K关注 0票数 0

我将阅读“图像”(2000)和“地标”(388)。

这是代码的一部分。

代码语言:javascript
复制
filename_queue = tf.train.string_input_producer([savepath])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features={'label': tf.FixedLenFeature([], tf.string), 'img_raw':tf.FixedLenFeature([], tf.string), })

image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [224, 224, 3])
image = tf.cast(image, tf.float32)

label = tf.decode_raw(features['label'], tf.float64) # problem is here
label = tf.cast(label, tf.float32)
label = tf.reshape(label, [388])

错误是

代码语言:javascript
复制
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 291 values, but the requested shape has 388.

当我将'float64‘改为’float64 32‘时:

代码语言:javascript
复制
 label = tf.decode_raw(features['label'], tf.float32) # problem is here

 #Error: InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 582 values, but the requested shape has 388

或者是“浮动16”:

代码语言:javascript
复制
label = tf.decode_raw(features['label'], tf.float16) # problem is here

#Error: InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1164 values, but the requested shape has 388

下面是我制作tfrecords的方法:(为了简单起见,我简化了一些代码)

代码语言:javascript
复制
writer = tf.python_io.TFRecordWriter(savepath)
for i in range(number_of_images):
    img = Image.open(ImagePath[i])  # load one image from path
    landmark = landmark_read_from_csv[i]  # shape of landmark_read_from_csv is (number_of_images, 388)
    example = tf.train.Example(features=tf.train.Features(feature={
    "label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[landmark.tobytes()])),
    'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tobytes()]))}))
    writer.write(example.SerializeToString())
writer.close()

我有三个问题:

  1. 为什么在更改数据类型后形状会发生变化?
  2. 如何选择合适的数据类型?(因为有时我可以用“tf.Float 64”成功地解码图像,但有时可以用不同的数据集解码“tf.uint8”)
  3. 创建of记录的代码有问题吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-22 23:22:37

我最近遇到了一个非常类似的问题,根据我个人的经验,我很有信心我能够推断出你的问题的答案,尽管我不能百分之百肯定。

  1. 由于不同的数据类型在编码为byte列表时具有不同的长度,并且由于float16的长度是float32的一半,所以相同的字节列表可以被读取为n个float32值的序列,或者是float16值的两倍。换句话说,当您更改数据类型时,您试图解码的byte列表不会改变,但是更改的是您对这个数组列表的分区。
  2. 您应该检查用于生成tfrecord文件的数据的数据类型,并在读取byte_list时使用相同的数据类型来解码它(可以使用.dtype属性检查numpy数组的数据类型)。
  3. 我看不见,但我可能错了。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47120509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档