使用Tensorflow來讀取訓練自己的資料(一)
阿新 • • 發佈:2018-11-04
本文的程式碼以及思路都是參考別人的,現在只是整理一下思路,做一些解釋,畢竟是小白。
首先本文所使用的圖片資料都是https://www.kaggle.com/下載的,使用的是貓和狗的圖片集,https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data
程式碼分為三個部分,input_data.py處理原始資料,因為下載的資料圖片大小不一致等,model.py編寫網路的模型,使用了兩個卷積層,兩個池化層以及兩個全連線層,最後是training.py用來初始化並訓練模型,獲得結果。
import tensorflow as tf importnumpy as np import os # you need to change this to your data directory # train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/'
#存放訓練圖片的路徑 train_dir = '/Users/arcstone_mems_108/PycharmProjects/catsvsdogs/data/train/'
#傳入檔案的路徑,或者資料夾內所有圖片的資料以及標籤
def get_files(file_dir): ''' Args: file_dir: file directoryReturns: list of images and labels ''' cats = [] label_cats = [] dogs = [] label_dogs = []
#os.listdir為列出路徑內的所有檔案 for file in os.listdir(file_dir): name = file.split(sep='.') #將每一個檔名都進行分割,以.分割,
#這樣檔名就變為三部分 #name的形式為['dog', '9981', 'jpg']if name[0]=='cat':
cats.append(file_dir + '/' + file)
#在定義的cats列表內新增圖片路徑,由資料夾的路徑+檔名組成
label_cats.append(0)
#在貓的標籤列表中新增對應圖片的標籤,貓的標籤為0,狗為1 else: dogs.append(file_dir + '/' + file) label_dogs.append(1) print('There are %d cats\nThere are %d dogs' %(len(cats), len(dogs))) #打印出訓練資料中有多少張貓的圖片,多少張狗的圖片 image_list = np.hstack((cats, dogs)) #將貓和狗的列表合併為一個列表 label_list = np.hstack((label_cats, label_dogs)) #將貓和狗的標籤列表合併為一個列表 #將兩個列表構成一個數組 temp = np.array([image_list, label_list]) temp = temp.transpose() #將陣列矩陣轉置 np.random.shuffle(temp) #將資料打亂順序,不再按照前邊全是貓,後邊全是狗這樣排序 image_list = list(temp[:, 0]) #圖片列表為temp陣列的第一個元素 label_list = list(temp[:, 1]) #標籤列表為temp陣列的第二個元素 label_list = [int(i) for i in label_list] #轉換為int型別 #返回讀取結果,存放在image_list,和label_list中 return image_list, label_list #定義函式,將圖片資料分塊來處理 def get_batch(image, label, image_W, image_H, batch_size, capacity): ''' Args: image: list type label: list type image_W: image width image_H: image height batch_size: batch size capacity: the maximum elements in queue Returns: image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32 label_batch: 1D tensor [batch_size], dtype=tf.int32 ''' #資料轉換 image = tf.cast(image, tf.string) #將image資料轉換為string型別 label = tf.cast(label, tf.int32) #將label資料轉換為int型別 # make an input queue
#生成輸入的佇列,每次在資料集中產生一個切片 input_queue = tf.train.slice_input_producer([image, label]) #標籤為索引為1的位置 label = input_queue[1]
#圖片的內容為讀取索引為0的位置所得的內容 image_contents = tf.read_file(input_queue[0])
#解碼影象,解碼為一個張量 image = tf.image.decode_jpeg(image_contents, channels=3) ###################################### # data argumentation should go to here ###################################### #對影象的大小進行調整,調整大小為image_W,image_H image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H) # if you want to test the generated batches of images, you might want to comment the following line. # 如果想看到正常的圖片,請註釋掉111行(標準化)和 126行(image_batch = tf.cast(image_batch, tf.float32)) # 訓練時不要註釋掉!
#對影象進行標準化 image = tf.image.per_image_standardization(image) #使用train.batch函式來組合樣例,image和label代表訓練樣例和所對應的標籤,batch_size引數
#給出了每個batch中樣例的個數,capacity給出了佇列的最大容量,當佇列長度等於容量時,暫停入隊
#只是等待出隊 image_batch, label_batch = tf.train.batch([image, label], batch_size= batch_size, num_threads= 64, capacity = capacity)
#將label_batch轉換格式為[]
label_batch = tf.reshape(label_batch, [batch_size]) image_batch = tf.cast(image_batch, tf.float32) #將影象格式轉換為float32型別 return image_batch, label_batch
#最後返回所處理得到的影象batch和標籤batch