我正在尝试为我的语义分割数据集(rgb_image_in -> binary_raycast_out)创建 tfrecords。
以下是我将图像列表写入 train.tfrecord 的代码。
def _process_image_files(image_names, raycast_names): writer = tf.python_io.TFRecordWriter('train') # 我的解码 jpeg/png 图像的实现 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() # 为了保持一致性,将任何 PNG 转换为 JPEG。 if _is_png(filename): print('Converting PNG to JPEG for %s' % filename) image_data = coder.png_to_jpeg(image_data) # 解码 RGB JPEG。 image = coder.decode_jpeg(image_data) # 检查图像是否转换为 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, channelsdef _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_name
和 raycast_name
,那么你将从不同的运行中获取它们,它们不会是一对。
你可以通过同时评估它们来获取对,例如:
current_image_name, current_raycast_name = session.run([ image_name, raycast_name])
我还建议使用较新的 Dataset API 而不是队列。