我假设您想获得一批具有数值标签的相同大小的图像。我们将使用
tf.decode_csv()
解析文本,
tf.read_file()
加载JPEG数据作为字符串,
tf.image.decode_jpeg()
将其解析为密集张量,最后使用
tf.train.batch()
构建解析数据成图像批处理。这些函数中有许多选项可供配置,请参阅文档以了解进一步的定制细节。请注意:保留HTML标记。
filename_producer = tf.string_input_producer(["train.txt"], ...)
line_reader = tf.TextLineReader()
next_line = line_reader.read(filename_producer)
image_filename, label = tf.decode_csv(
next_line, [tf.constant([], dtype=tf.string), tf.constant([], dtype=tf.int32)],
field_delim=" ")
image_bytes = tf.read_file(image_filename)
image_tensor = tf.image.decode_jpeg(image_bytes, ...)
HEIGHT = ...
WIDTH = ...
image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, HEIGHT, WIDTH)
BATCH_SIZE = 32
images, labels = tf.train.batch([image_tensor, label], BATCH_SIZE, ...)
这个例子广泛使用了TensorFlow预取来加载样例。TensorFlow文档中有一个如何使用预取功能的
教程,但最重要的是你必须在会话开始时调用
tf.train.start_queue_runners()
来开始预取:
sess = tf.Session()
tf.train.start_queue_runners(sess)