我假設你想獲得一批具有數字標籤的相同大小的圖像。我們將使用tf.decode_csv()
分析文本,tf.read_file()
加載JPEG數據作爲一個字符串,tf.image.decode_jpeg()
解析它變成一個緻密的張量,最後tf.train.batch()
的解析數據建成一批圖像。許多這些功能都有很多選項可供配置,因此請參閱文檔以獲取更多定製細節。
# Set options here for whether to repeat, etc.
filename_producer = tf.string_input_producer(["train.txt"], ...)
# Read lines from the file, one at a time.
line_reader = tf.TextLineReader()
next_line = line_reader.read(filename_producer)
# Parse line into a filename and an integer label.
image_filename, label = tf.decode_csv(
next_line, [tf.constant([], dtype=tf.string), tf.constant([], dtype=tf.int32)],
field_delim=" ")
# Read the image as a string.
image_bytes = tf.read_file(image_filename)
# Convert the image into a 3-D tensor (height x width x channels).
image_tensor = tf.image.decode_jpeg(image_bytes, ...)
# OPTIONAL: Resize your image to a standard size if they are not already.
HEIGHT = ...
WIDTH = ...
image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, HEIGHT, WIDTH)
# Create a batch of images.
BATCH_SIZE = 32
images, labels = tf.train.batch([image_tensor, label], BATCH_SIZE, ...)
# [...build the rest of your model...]
本示例大量使用TensorFlow預取來加載示例。該TensorFlow文檔有一個how-to that explains how to use the prefetching feature,但是要注意的最重要的事情是,你必須在你的會話開始打電話tf.train.start_queue_runners()
開始預取:
sess = tf.Session()
# You must execute this statement to begin prefetching data.
tf.train.start_queue_runners(sess)
感謝您詳細的解答。這非常有幫助。在你的代碼,您使用的圖像,標籤= tf.train.batch([image_tensor,標號],BATCH_SIZE,...)。我不知道如果我需要得到的數據,並從下一批次的標籤,我應該只是使用相同的參數再次調用tf.train.batch或實現自己的get_next_batch功能。謝謝!! –