Tensorflow 错误解析记录

Tensorflow parses the record incorrectly

我正在尝试为我的语义分割数据集创建 tfrecords (rgb_image_in -> binary_raycast_out)。

下面是我将图像列表写入 train.tfrecord 的代码。

    def _process_image_files(image_names, raycast_names):

        writer = tf.python_io.TFRecordWriter('train')

        #My implementation of decoding jpeg/png image
        coder = ImageCoder()

        for i in range(len(image_names)):
            print('{}\n{}\n\n'.format(image_names[i], raycast_names[i]))

            image_buffer, im_height, im_width, im_channels = _process_image(image_names[i], coder)

            raycast_buffer, rc_height, rc_width, rc_channels = _process_image(raycast_names[i], coder)

            example = _convert_to_example(image_names[i], raycast_names[i], image_buffer, raycast_buffer, \
                                          im_height, im_width, im_channels)

            writer.write(example.SerializeToString())
        writer.close()
        sys.stdout.flush() 

def _process_image(filename, coder):
    with tf.gfile.FastGFile(filename, 'rb') as f:
        image_data = f.read()

    # Convert any PNG to JPEG's for consistency.
    if _is_png(filename):
        print('Converting PNG to JPEG for %s' % filename)
        image_data = coder.png_to_jpeg(image_data)

    # Decode the RGB JPEG.
    image = coder.decode_jpeg(image_data)

    # Check that image converted to RGB
    assert len(image.shape) == 3
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2]
    assert channels == 3

    return image_data, height, width, channels


def _convert_to_example(image_name, raycast_name, image_buffer, raycast_buffer, sample_height, sample_width, sample_channels):

    example = tf.train.Example(features=tf.train.Features(feature={
        'height': _int64_feature(sample_height),
        'width': _int64_feature(sample_width),
        'channels': _int64_feature(sample_channels),
        'image/filename': _bytes_feature(tf.compat.as_bytes(image_name)),
        'image/encoded': _bytes_feature(tf.compat.as_bytes(image_buffer)),
        'raycast/filename': _bytes_feature(tf.compat.as_bytes(raycast_name)),
        'raycast/encoded': _bytes_feature(tf.compat.as_bytes(raycast_buffer))}))

    return example

以上代码在创建 tfrecord 文件时运行良好。我在 _convert_to_example 方法中放置了一些打印语句,以确保相应的文件名 (image_file & raycast_file) 被写入一个示例中。

但是,当我从 tfrecord 读取示例并打印图像名称时,image_file 和 raycast_file 名称似乎不对应。 tfRecordReader()读取的图片对是错误的。

下面是我读取记录的代码:

def parse_example_proto(example_serialized):

    feature_map = {
                    'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
                    'raycast/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
                    'height': tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1),
                    'width': tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1),
                    'channels': tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1),
                    'image/filename': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
                    'raycast/filename': tf.FixedLenFeature([], dtype=tf.string, default_value='')
                    }

    features = tf.parse_single_example(example_serialized, feature_map)

    return features['image/encoded'], features['raycast/encoded'], \
           features['height'], features['width'], features['channels'],\
           features['image/filename'], features['raycast/filename']



def retrieve_samples():

    with tf.name_scope('batch_processing'):
        data_files = ['train']

        filename_queue = tf.train.string_input_producer(data_files, shuffle=False)

        reader = tf.TFRecordReader()

        _, example_serialized = reader.read(filename_queue)

        image_buffer, raycast_buffer, height, width, channels, image_name, raycast_name = parse_example_proto(example_serialized)            

        orig_image = tf.image.resize_images(tf.image.decode_jpeg(image_buffer, channels=3), 
                                            [480, 856])
        orig_raycast = tf.image.resize_images(tf.image.decode_jpeg(raycast_buffer, channels=3), 
                                              [480, 856])

        return image_name, raycast_name

下面是我打印一对文件名的代码

image_name, raycast_name = retrieve_samples()
with tf.Session() as sess:    
    for i in range(1):
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        print(sess.run(image_name))
        print(sess.run(raycast_name))
        coord.request_stop()
        coord.join(threads)

我在这上面花了几天时间。我无法确定为什么我无法检索到正确的一对。正在检索的示例应该与正在创建的示例具有相同的数据吗?为什么我在读写时看到不同的名称对?

如有任何帮助,我们将不胜感激

例子越小越好。

每个 session.run 将计算张量和 运行 图形。这意味着如果您分别评估 image_nameraycast_name,那么您将从不同的 运行 中获得它们,并且它们不会是一对。

您可以通过同时评估两者来获得配对,例如:

current_image_name, current_raycast_name = session.run([
    image_name, raycast_name
])

我还建议在队列中使用较新的 Dataset API