cifar10_input.py
文件結構
讀取本地CIFAR-10的二進制文件共有四個函數
-
read_cifar10(filename_queue)
:讀取和解析圖片 -
_generate_image_and_label_batch(image, label, min_queue_examples, batch_size)
:建立一個圖片和標籤的batch隊列 -
distorted_inputs(data_dir, batch_size)
:爲訓練改變輸入的形狀 -
inputs(eval_data, data_dir, batch_size)
:爲評估構建輸入
具體如下
def read_cifar10(filename_queue):
"""Reads and parses examples from CIFAR10 data files.
Recommendation: if you want N-way read parallelism, call this function
N times. This will give you N independent Readers reading different
files & positions within those files, which will give better mixing of
examples.
Args:
filename_queue: A queue of strings with the filenames to read from.
Returns:
An object representing a single example, with the following fields:
height: number of rows in the result (32)
width: number of columns in the result (32)
depth: number of color channels in the result (3)
key: a scalar string Tensor describing the filename & record number
for this example.
label: an int32 Tensor with the label in the range 0..9.
uint8image: a [height, width, depth] uint8 Tensor with the image data
"""
return result
def _generate_image_and_label_batch(image, label, min_queue_examples,
batch_size):
"""Construct a queued batch of images and labels.
Args:
image: 3-D Tensor of [height, width, 3] of type.float32.
label: 1-D Tensor of type.int32
min_queue_examples: int32, minimum number of samples to retain
in the queue that provides of batches of examples.
batch_size: Number of images per batch.
Returns:
images: Images. 4D tensor of [batch_size, height, width, 3] size.
labels: Labels. 1D tensor of [batch_size] size.
"""
return images, tf.reshape(label_batch, [batch_size])
def distorted_inputs(data_dir, batch_size):
"""Construct distorted input for CIFAR training using the Reader ops.
Args:
data_dir: Path to the CIFAR-10 data directory.
batch_size: Number of images per batch.
Returns:
images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
labels: Labels. 1D tensor of [batch_size] size.
"""
return _generate_image_and_label_batch(float_image, read_input.label,
min_queue_examples, batch_size)
def inputs(eval_data, data_dir, batch_size):
"""Construct input for CIFAR evaluation using the Reader ops.
Args:
eval_data: bool, indicating if one should use the train or eval data set.
data_dir: Path to the CIFAR-10 data directory.
batch_size: Number of images per batch.
Returns:
images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
labels: Labels. 1D tensor of [batch_size] size.
"""
return _generate_image_and_label_batch(float_image, read_input.label,
min_queue_examples, batch_size)
函數詳解
read_cifar10(filename_queue)
最終返回一個result
(CIFAR10Record的實例),其中包含了
height、width:、depth
key:描述filename 和 record number的Tensor
label: 32位整型的 Tensor
uint8image: 圖片數據
class CIFAR10Record(object):
pass
result = CIFAR10Record()
result.height = 32
result.width = 32
result.depth = 3
這裏定義了一個類CIFAR10Record,pass
不做任何事情,一般用做佔位語句。該處的 pass 便是佔據一個位置,因爲如果定義一個空函數程序會報錯,當你沒有想好函數的內容是可以用 pass 填充,使程序可以正常運行。
後面定義一個CIFAR10Record的實例result,設置實例result的屬性值。他的優點是, 它是非常靈活的。類是在沒有任何成員的情況下定義的。任何函數都可以決定要添加哪些字段。不同的調用可以創建此類的對象, 並以不同的方式填充它們 (因此, 同時, 您可以擁有具有不同成員的同一個類的對象)。
這種靈活性也是一種弊端。這缺乏結構: 很難查看代碼並決定類將具有哪些成員。獲取這樣的對象並循環訪問成員也不那麼簡單。最後, 該類是一個沒有封裝的極端情況。
更好的做法請查看CIFAR10Record 函數的參考資料
tf.FixedLengthRecordReader
tf.FixedLengthRecordReader是讀取固定長度字節數信息(針對bin文件使用FixedLengthRecordReader讀取比較合適),下次調用時會接着上次讀取的位置繼續讀取文件,而不會從頭開始讀取。
參考資料tensorflow讀取數據-tfrecord格式Tensorflow中使用tfrecord方式讀取數據
tf.TFRecordReader.read(queue, name=None)
Returns the next record (key, value pair) produced by a reader.
返回一個閱讀器生成的下一個記錄(鍵值對)。
Will dequeue a work unit from queue if necessary (e.g. when the
Reader needs to start reading from a new file since it has
finished with the previous file).
如果有必要,將從隊列中對一個工作單元進行排序(例如,當讀者需要從一個新文件開始閱讀時,因爲它已經完成了前面的文件)。
Args:
queue: A Queue or a mutable string Tensor representing a handle
to a Queue, with string work items.
文件名隊列句柄
name: A name for the operation (optional).
Returns:
A tuple of Tensors (key, value).
key: A string scalar Tensor.
value: A string scalar Tensor.
返回鍵值對,其中值表示讀取的文件
distorted_inputs(data_dir, batch_size)
TF Boys (TensorFlow Boys ) 養成記(二): TensorFlow 數據讀取
_generate_image_and_label_batch(image, label, min_queue_examples, batch_size)
# Creates batches of 32 images and 32 labels.
image_batch, label_batch = tf.train.shuffle_batch(
[single_image, single_label],
batch_size=32,
num_threads=4,
capacity=50000,
min_after_dequeue=10000)
參考資料
這段代碼寫的是從[single_image, single_label]利用4個線程讀取32行
Args:
-
tensor_list
: 入隊的張量列表 -
batch_size
: 表示進行一次批處理的tensors數量. -
num_threads
: The number of threads enqueuingtensor_list
.
設置num_threads的值大於1,使用多個線程在tensor_list中讀取文件,這樣保證了同一時刻只在一個文件中進行讀取操作(但是讀取速度依然優於單線程),而不是之前的同時讀取多個文件,這種方案的優點是:
- 避免了兩個不同的線程從同一文件中讀取用一個樣本
- 避免了過多的磁盤操作
-
capacity
: 容量:一個整數,隊列中的最大的元素數.
這個參數一定要比min_after_dequeue參數的值大,並且決定了我們可以進行預處理操作元素的最大值. -
min_after_dequeue
: Minimum number elements in the queue after a
dequeue(出列), used to ensure a level of mixing of elements.
當一次出列操作完成後,隊列中元素的最小數量,往往用於定義元素的混合級別.
定義了隨機取樣的緩衝區大小,此參數越大表示更大級別的混合但是會導致啓動更加緩慢,並且會佔用更多的內存
Returns:
A list of tensors with the same number and types as tensor_list
.
默認返回一個和讀取tensor_list數據和類型一個tensor列表.