繁体   English   中英

Tensorflow 无法解码 tfrecords 中的 jpeg 字节

[英]Tensorflow failed to decode jpeg bytes in tfrecords

我试图将一些图像写入 tfrecord 文件,但我发现它太大了。 然后我尝试将原始 jpeg 字节写入 tfrecord 文件。 但是当我尝试阅读它时,出现异常:ValueError:Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [32].

以下是我的代码

import tensorflow as tf
import os


def write_features(example_image_paths, tf_records_path):
    with tf.python_io.TFRecordWriter(tf_records_path) as writer:
        for image_path in example_image_paths:
            with open(image_path, 'rb') as f:
                image_bytes = f.read()
            feautres = tf.train.Features(
                feautres={
                    'images':
                    tf.train.Feature(bytes_list=tf.train.BytesList(
                        value=image_bytes))
                })
            example = tf.train.Example(feautres)
            writer.write(example.SerializeToString())


def extract_features_batch(serialized_batch):
    """

    :param serialized_batch:
    :return:
    """
    features = tf.parse_example(
        serialized_batch,
        features={'images': tf.FixedLenFeature([], tf.string)})
    bs = features['images'].shape[0]
    images = tf.image.decode_image(features['images'], 3)
    w, h = (280, 32)
    images = tf.cast(x=images, dtype=tf.float32)
    images = tf.reshape(images, [bs, h, w, 3])

    return images


def inputs(tfrecords_path, batch_size, num_epochs, num_threads=4):
    """

    :param tfrecords_path:
    :param batch_size:
    :param num_epochs:
    :param num_threads:
    :return: input_images, input_labels, input_image_names
    """

    if not num_epochs:
        num_epochs = None

    dataset = tf.data.TFRecordDataset(tfrecords_path)

    dataset = dataset.batch(batch_size, drop_remainder=True)

    # The map transformation takes a function and applies it to every element
    # of the dataset.
    dataset = dataset.map(map_func=extract_features_batch,
                          num_parallel_calls=num_threads)
    dataset = dataset.shuffle(buffer_size=1000)
    dataset = dataset.repeat()

    iterator = dataset.make_one_shot_iterator()

    return iterator.get_next(name='IteratorGetNext')


if __name__ == '__main__':
    pass
    # img_names = os.listdir('./images')
    # img_paths = []
    # for img_name in img_paths:
    #     img_paths.append(os.path.join('./images', img_name))
    # write_features(img_paths, 'test.tfrecords')

    images = inputs('./test.tfrecords', 32, None)

如何正确读取和解码 jpeg 字节? 谢谢!

您需要在批处理数据集之前解码图像。 换句话说,在您的 inputs() function 中,“正确”的顺序是:

dataset = dataset.map(map_func=extract_features_batch,
                      num_parallel_calls=num_threads) 

dataset = dataset.batch(batch_size, drop_remainder=True)

The documentation says ( https://www.tensorflow.org/api_docs/python/tf/io/decode_image ) that tf.io.decode_image expects an image in a form of a scalar or 0-dimensional string (0-D string is被认为是一个标量),而如果您首先对数据集 object 进行批处理,则 tf.io.decode_image 接收图像列表(或批次)(表示为 batch_size 乘以 0 维字符串的列表)。 然后它抱怨它在收到一个形状为 [32] 的数组时期望 0 维数组(在您的情况下是批量大小)。

我不知道我们如何优化批处理的输入管道,而不是在处理后低效地进行批处理。 像往常一样,在 tf 2.0 的文档中没有任何内容。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM