Tensorflow错误地解析记录

时间:2018-10-02 18:53:52

标签: tensorflow machine-learning serialization protocol-buffers tfrecord

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

下面是我的代码,用于将图像列表写入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 retirieve_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 = retirieve_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)

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

任何帮助都将得到

1 个答案:

答案 0 :(得分:1)

一个较小的示例会更好。

每个image_name将评估张量并运行图。这意味着,如果分别评估raycast_namecurrent_image_name, current_raycast_name = session.run([ image_name, raycast_name ]) ,则会从不同的运行次数中获得它们,而它们将不是一对。

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

alt

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