Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于从TFR记录读取数据时的形状_Python_Tensorflow_Tfrecord - Fatal编程技术网

Python 关于从TFR记录读取数据时的形状

Python 关于从TFR记录读取数据时的形状,python,tensorflow,tfrecord,Python,Tensorflow,Tfrecord,我将阅读TFR记录中的“图像”(2000)和“地标”(388) 这是代码的一部分 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={'la

我将阅读TFR记录中的“图像”(2000)和“地标”(388)

这是代码的一部分

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])
错误是

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 291 values, but the requested shape has 388.
当我将“float64”更改为“float32”时:

 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”:

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的方法:(为了简单起见,我简化了一些代码)

我有3个问题:

  • 为什么数据类型更改后形状会更改
  • 如何选择合适的数据类型?(因为有时我可以使用'tf.float64'成功解码图像,但有时使用不同的数据集'tf.uint8'成功解码图像)
  • 创建TFR记录的代码有问题吗

  • 我最近遇到了一个非常类似的问题,根据我的个人经验,我非常有信心能够推断出你所问问题的答案,尽管我不是100%确定

  • 列表项形状会发生变化,因为当编码为
    byte
    列表时,不同的数据类型具有不同的长度,并且由于
    float16
    具有
    float32
    长度的一半,相同的字节列表可以被读取为n
    float32
    值的序列,或者是
    float16
    值的两倍。换句话说,当您更改数据类型时,您试图解码的
    字节列表不会更改,但更改的是您对该数组列表的分区

  • 您应该检查用于生成
    tfrecord
    文件的数据的数据类型,并在读取字节列表时使用相同的数据类型对其进行解码(您可以使用.dtype属性检查numpy数组的数据类型)

  • 我看不到,但我可能错了


  • 我最近遇到了一个非常类似的问题,根据我的个人经验,我非常有信心能够推断出你所问问题的答案,尽管我不是100%确定

  • 列表项形状会发生变化,因为当编码为
    byte
    列表时,不同的数据类型具有不同的长度,并且由于
    float16
    具有
    float32
    长度的一半,相同的字节列表可以被读取为n
    float32
    值的序列,或者是
    float16
    值的两倍。换句话说,当您更改数据类型时,您试图解码的
    字节列表不会更改,但更改的是您对该数组列表的分区

  • 您应该检查用于生成
    tfrecord
    文件的数据的数据类型,并在读取字节列表时使用相同的数据类型对其进行解码(您可以使用.dtype属性检查numpy数组的数据类型)

  • 我看不到,但我可能错了


  • 非常感谢。我确实忘了检查原始数据集的数据类型。我希望我能更进一步。这是一个需要调试的潜在错误。谢谢。我确实忘了检查原始数据集的数据类型。我希望我能更进一步。这是一个需要调试的潜在错误。
    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()