官網的mnist和cifar10數據以後,筆者嘗試着製做本身的數據集,並保存,讀入,顯示。 TensorFlow能夠支持cifar10的數據格式, 也提供了標準的TFRecord 格式,而關於 tensorflow 讀取數據, 官網提供了3中方法
1 Feeding: 在tensorflow程序運行的每一步, 用Python代碼在線提供數據
2 Reader : 在一個計算圖(tf.graph)的開始前,將文件讀入到流(queue)中
3 在聲明tf.variable變量或numpy數組時保存數據。受限於內存大小,適用於數據較小的狀況python
在本文,主要介紹第二種方法,利用tf.record標準接口來讀入文件數組
筆者找了2類狗的圖片, 哈士奇和吉娃娃, 所有 resize成128 * 128大小
以下圖, 保存地址爲/home/molys/Python/data/dog
每類中有10張圖片
網絡
如今利用這2 類 20張圖片製做TFRecord文件session
1 先聊一下tfrecord, 這是一種將圖像數據和標籤放在一塊兒的二進制文件,能更好的利用內存,在tensorflow中快速的複製,移動,讀取,存儲 等等..ui
這裏注意,tfrecord會根據你選擇輸入文件的類,自動給每一類打上一樣的標籤
如在本例中,只有0,1 兩類.net
2 先上「製做TFRecord文件」的代碼,註釋附詳解code
import os import tensorflow as tf from PIL import Image #注意Image,後面會用到 import matplotlib.pyplot as plt import numpy as np cwd='/home/molys/Python/data/' classes={'husky','chihuahua'} #人爲 設定 2 類 writer= tf.python_io.TFRecordWriter("dog_train.tfrecords") #要生成的文件 for index,name in enumerate(classes): class_path=cwd+name+'/' for img_name in os.listdir(class_path): img_path=class_path+img_name #每個圖片的地址 img=Image.open(img_path) img= img.resize((128,128)) img_raw=img.tobytes()#將圖片轉化爲二進制格式 example = tf.train.Example(features=tf.train.Features(feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) })) #example對象對label和image數據進行封裝 writer.write(example.SerializeToString()) #序列化爲字符串 writer.close()
運行完這段代碼後,會生成dog_train.tfrecords 文件,以下圖 對象
tf.train.Example 協議內存塊包含了Features字段,經過feature將圖片的二進制數據和label進行統一封裝, 而後將example協議內存塊轉化爲字符串, tf.python_io.TFRecordWriter 寫入到TFRecords文件中。接口
在製做完tfrecord文件後, 將該文件讀入到數據流中。
代碼以下隊列
def read_and_decode(filename): # 讀入dog_train.tfrecords filename_queue = tf.train.string_input_producer([filename])#生成一個queue隊列 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue)#返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw' : tf.FixedLenFeature([], tf.string), })#將image數據和label取出來 img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [128, 128, 3]) #reshape爲128*128的3通道圖片 img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中拋出img張量 label = tf.cast(features['label'], tf.int32) #在流中拋出label張量 return img, label
注意,feature的屬性「label」和「img_raw」名稱要和製做時統一 ,返回的img數據和label數據一一對應。返回的img和label是2個 tf 張量,print出來 以下圖
有些時候咱們但願檢查分類是否有誤,或者在以後的網絡訓練過程當中能夠監視,輸出圖片,來觀察分類等操做的結果,那麼咱們就能夠session回話中,將tfrecord的圖片從流中讀取出來,再保存。 緊跟着一開始的代碼寫:
filename_queue = tf.train.string_input_producer(["dog_train.tfrecords"]) #讀入流中 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) #返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw' : tf.FixedLenFeature([], tf.string), }) #取出包含image和label的feature對象 image = tf.decode_raw(features['img_raw'], tf.uint8) image = tf.reshape(image, [128, 128, 3]) label = tf.cast(features['label'], tf.int32) with tf.Session() as sess: #開始一個會話 init_op = tf.initialize_all_variables() sess.run(init_op) coord=tf.train.Coordinator() threads= tf.train.start_queue_runners(coord=coord) for i in range(20): example, l = sess.run([image,label])#在會話中取出image和label img=Image.fromarray(example, 'RGB')#這裏Image是以前提到的 img.save(cwd+str(i)+'_''Label_'+str(l)+'.jpg')#存下圖片 print(example, l) coord.request_stop() coord.join(threads)
代碼運行完後, 從tfrecord中取出的文件被保存了。以下圖:
在這裏咱們能夠看到,圖片文件名的第一個數字表示在流中的順序(筆者這裏沒有用shuffle), 第二個數字則是 每一個圖片的label,吉娃娃都爲0,哈士奇都爲1。 因而可知,咱們一開始製做tfrecord文件時,圖片分類正確。