简体   繁体   English

Tensorflow 无法解码 tfrecords 中的 jpeg 字节

[英]Tensorflow failed to decode jpeg bytes in tfrecords

I tried to write some images to a tfrecord file, but I found it is too large.我试图将一些图像写入 tfrecord 文件,但我发现它太大了。 Then I tried to write the origin jpeg bytes into The tfrecord file.然后我尝试将原始 jpeg 字节写入 tfrecord 文件。 but when I try to read it, there is exception: ValueError: Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [32].但是当我尝试阅读它时,出现异常:ValueError:Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [32].

Followed is my code以下是我的代码

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)

How can I read and decode the jpeg bytes properly?如何正确读取和解码 jpeg 字节? Thanks!谢谢!

You need to decode images before batching the dataset.您需要在批处理数据集之前解码图像。 In other words, in your inputs() function the 'correct' order would be:换句话说,在您的 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 considered a scalar) while if you batch the dataset object first the tf.io.decode_image receives a list (or a batch) of images (represented as a list of batch_size times 0 dimensional strings). 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 维字符串的列表)。 It then complains that it expected 0-dimensional array while received an array with the shape of [32] (which is the batch size in your case).然后它抱怨它在收到一个形状为 [32] 的数组时期望 0 维数组(在您的情况下是批量大小)。

I have no idea about how we could optimize input pipeline for batch-processing other than inefficiently do batching after processing.我不知道我们如何优化批处理的输入管道,而不是在处理后低效地进行批处理。 As usual, there is nothing about it in docs on tf 2.0.像往常一样,在 tf 2.0 的文档中没有任何内容。

暂无
暂无

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

相关问题 Tensorflow 预测错误,invalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP] - Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] InvalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP] - InvalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] “预测失败:模型执行期间出错:无法将字节解码为JPEG,PNG,GIF或BMP” - “Prediction failed: Error during model execution: Unable to decode bytes as JPEG, PNG, GIF, or BMP” 解码字节失败-IronPython - Failed to decode bytes - IronPython 将tfrecords中的原始字节解码为tf.feature_column.numeric_column功能 - Decode raw bytes in tfrecords into tf.feature_column.numeric_column feature 将 TFRecords 转换回 JPEG 图像 - Converting TFRecords back into JPEG Images 我可以使用Tensorflow转换/解码二进制的JPEG文件吗? - Can I convert/decode a JPEG file that is in binary with Tensorflow? 如何在 Tensorflow 中 decode_jpeg 后获取图像的形状? - How to get the shape of an image after decode_jpeg in Tensorflow? 使用 tf.image.decode_image 时出错“b'无法将字节解码为 JPEG、PNG、GIF 或 BMP'” - Error using tf.image.decode_image " b'Unable to decode bytes as JPEG, PNG, GIF, or BMP'" Tensorflow 1.10 TFRecordDataset-恢复TFRecords - Tensorflow 1.10 TFRecordDataset - recovering TFRecords
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM