Tensorflow:无法从tfrecord提取文件名

2024-04-16 06:01:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经将图像、标签和文件名写入了tfrecords文件。当我试图解码文件时,我无法将文件名转换为tf.字符串. 在

我编写的将其转换为tfrecords文件的代码:

num_batches = 6
batch_size = math.ceil(X_training.shape[0] / num_batches)

for i in range(num_batches):
    train_path = os.path.join("data","batch_" + str(i) + '.tfrecords')
    writer = tf.python_io.TFRecordWriter(train_path)
    start_row = i * batch_size
    end_row = start_row + batch_size - 1

    for idx in range(start_row, end_row):
        try:
            label = y_tr[idx]
            filename = train_filenames[idx].tostring()
            image = X_tr[idx]
            image_raw = image.tostring()
        except:
            continue

        example = tf.train.Example(
            features=tf.train.Features(
              feature={
                'label': _int64_feature(label),
                'filename': _bytes_feature(filename),
                'image': _bytes_feature(image_raw),
              }))

        serialized = example.SerializeToString()
        writer.write(serialized)

要读取和解码tfrecords文件,我有以下函数:

^{pr2}$

当我解码不同的批处理时,返回的文件名如下所示:

b'P\x00\x00\x00_\x00\x00\x000\x00\x00\x000\x00\x00\x001\x00\x00\x004\x00\x00\x008\x00\x00\x00_\x00\x00\x00R\x00\x00\x00I\x00\x00\x00G\x00\x00\x00H\x00\x00\x00T\x00\x00\x00_\x00\x00\x00M\x00\x00\x00L\x00\x00\x00O\x00\x00\x00.\x00\x00\x00j\x00\x00\x00p\x00\x00\x00g\x00\x00\x00'

我在解码一个tf.string?在


Tags: 文件imagesize文件名tftfrecordsbatchbatches