1. 程式人生 > >21個專案玩轉深度學習 第二章 CIFAR10

21個專案玩轉深度學習 第二章 CIFAR10

  1. 首先介紹第一個檔案infar10_input.py,用途:在tensorflow中讀取人CIFAR-10訓練圖片。

這個IMAGE_SIZE=24並不是原始圖片的大小,而是接下來要裁剪成的大小

IMAGE_SIZE = 24

NUM_CLASSES = 10 NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 50000 NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 10000

接下來介紹第一個自定義函式

ARGS:      filename_queue:包含要讀取的檔名的字串佇列。

返回:       表示單個示例的物件,包含以下欄位:        height:結果中的行數(32)        width:結果中的列數(32)        depth:結果中的顏色通道數(3)        key:標量字串Tensor描述檔名和記錄號這個例子。        label:一個int32 Tensor,標籤範圍為0..9。        uint8image:一個 [高度,寬度,深度] uint8張量影象資料

給我一個要讀取的檔名佇列,返回result物件,包含了很多屬性

def read_cifar10(filename_queue):

首先建立一個繼承於object的基類,建立一個result的物件,物件屬性有height=32,width=32,depth=3,lable_bytes=1,

image_bytes=32*32*3,record_bytes = lable_bytes + image_bytes,建立一個分詞式reader,每次分詞record_bytes個,讀出的結果是result.key和value,value是字串型別,解碼value到tf.uint8型別,並賦值給recor_bytes,把record_bytes分離出一個位元組並轉換為tf.int32型別,賦值給result.lable,分離出第二部分,並且轉化為[result.depth,result.height,result.width],隨後轉換為【高,寬,深】,並賦值給result.unit8image,這個屬性是unit8格式的。最後返回result這個物件!

需要傳入上個函式所得到的image,lable,以及給定佇列中事例最少個數和batch_size,返回打包的的images,和打包的lable

images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size. labels: Labels. 1D tensor of [batch_size] size.

def _generate_image_and_label_batch(image, label, min_queue_examples,                                                                       batch_size, shuffle):

總之:建立一個打亂的示例的佇列,然後從示例佇列中讀取“batch_size”影象+標籤。

 num_preprocess_threads = 16    

如果打亂:

              images, label_batch = tf.train.shuffle_batch( [image, label],

                                                                                            batch_size=batch_size,                                                                                             num_threads=num_preprocess_threads,                                                                                             capacity=min_queue_examples + 3 * batch_size,                                                                                             min_after_dequeue=min_queue_examples)

如果沒有打亂:

                    images, label_batch = tf.train.batch( [image, label],                                                                                     batch_size=batch_size,                                                                                     num_threads=num_preprocess_threads,                                                                                     capacity=min_queue_examples + 3 * batch_size)

返回成批次的images,lable _batch,其中有一點不明白,tf.reshape(label_batch, [batch_size])???images 卻沒有reshape

def distorted_inputs(data_dir, batch_size):

 首先把data_batch1到data_batch5檔案列表生成佇列,呼叫read_cifar10(filename_queue),得到結果read_input,並把read_input的uint8image屬性轉換為tf.float32型別,賦值給reshaped_image,把reashped_image進行裁剪成24*24*3形狀,並進行左右翻轉,提升對比度等等一系列操作,  float_image.set_shape([height, width, 3],  read_input.label.set_shape([1]),呼叫上個函式,打包成batchimages和lable並返回

def inputs(eval_data, data_dir, batch_size):

跟上面一樣做同樣的操作,不同點1:上一個函式是打亂的,而這個是沒有打亂的。不同點2:這個函式有eval_data,這個一點目前並不太明白