TensorFlow中图片转TfRecord的基本流程

writer = tf.python_io.TFRecordWriter(output_file) image_buffer, height, width = _process_image(filename, coder) example = _convert_to_example(filename, image_buffer, label,text, height, width) writer.write(example.SerializeToString()) writer.close()

class ImageCoder(object): """Helper class that provides TensorFlow image coding utilities."""def __init__(self): # Create a single Session to run all image coding calls. self._sess = tf.Session()# Initializes function that converts PNG to JPEG data. self._png_data = https://www.it610.com/article/tf.placeholder(dtype=tf.string) image = tf.image.decode_png(self._png_data, channels=3) self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)# Initializes function that decodes RGB JPEG data. self._decode_jpeg_data = tf.placeholder(dtype=tf.string) self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)def png_to_jpeg(self, image_data): return self._sess.run(self._png_to_jpeg, feed_dict={self._png_data: image_data})def decode_jpeg(self, image_data): image = self._sess.run(self._decode_jpeg, feed_dict={self._decode_jpeg_data: image_data}) assert len(image.shape) == 3 assert image.shape[2] == 3 return image

def _process_image(filename, coder): """Process a single image file. Args: filename: string, path to an image file e.g., '/path/to/example.JPG'. coder: instance of ImageCoder to provide TensorFlow image coding utils. Returns: image_buffer: string, JPEG encoding of RGB image. height: integer, image height in pixels. width: integer, image width in pixels. """ # Read the image file. with open(filename, 'r') as f: image_data = https://www.it610.com/article/f.read()# Convert any PNG to JPEG's for consistency. if _is_png(filename): logging.info('Converting PNG to JPEG for %s' % filename) image_data = https://www.it610.com/article/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] assert image.shape[2] == 3return image_data, height, width

def _convert_to_example(filename, image_buffer, label, text, height, width): """Build an Example proto for an example. Args: filename: string, path to an image file, e.g., '/path/to/example.JPG' image_buffer: string, JPEG encoding of RGB image label: integer, identifier for the ground truth for the network text: string, unique human-readable, e.g. 'dog' height: integer, image height in pixels width: integer, image width in pixels Returns: Example proto """colorspace = 'RGB' channels = 3 image_format = 'JPEG'example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': _int64_feature(height), 'image/width': _int64_feature(width), 'image/colorspace': _bytes_feature(colorspace), 'image/channels': _int64_feature(channels), 'image/class/label': _int64_feature(label), 'image/class/text': _bytes_feature(text), 'image/format': _bytes_feature(image_format), 'image/filename': _bytes_feature(os.path.basename(filename)), 'image/encoded': _bytes_feature(image_buffer)})) return example

    推荐阅读