一.tf.train.slice_input_producer()
tf.train.slice_input_producer([image,label],num_epochs=10),隨機產生一個圖片和標籤,num_epochs=10,則表示把所有的數據過10遍,使用完所有的圖片數據爲一個epoch,這是重複使用10次。上面的用法表示你的數據集和標籤已經全部加載到內存中了,如果數據集非常龐大,我們通過這個函數也可以只加載圖片的路徑,放入圖片的path,注意path必須是一個list或者tensorlist.見下面代碼實例
- # -*- coding: utf-8 -*-
- ”“”
- Created on Mon Mar 26 22:02:22 2018
- @author: Administrator
- “”“
- import tensorflow as tf
- import glob
- import matplotlib.pyplot as plt
- import time
-
- datapath=r’/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/ORL/’
- imgpath = glob.glob(datapath+‘*.bmp’)
- # 將路徑轉化成張量形式
- imgpath = tf.convert_to_tensor(imgpath)
-
- # 產生一個隊列每次隨機產生一張圖片地址
- # 注意這裏要放在數組裏面
-
- image = tf.train.slice_input_producer([imgpath])
- # 得到一個batch的圖片地址
- # 由於tf.train.slice_input_producer()函數默認是隨機產生一個實例
- # 所以在這裏直接使用tf.train.batch()直接獲得一個batch的數據即可
- # 沒有必要再去使用tf.trian.shuffle_batch() 速度會慢
- img_batch = tf.train.batch([image],batch_size=20,capacity=100)
-
- with tf.Session() as sess:
- coord = tf.train.Coordinator()
- thread = tf.train.start_queue_runners(sess,coord)
- i = 0
- try:
- while not coord.should_stop():
- imgs = sess.run(img_batch)
- print(imgs)
- fig = plt.figure()
- for i,path in enumerate(imgs):
- img = plt.imread(path[0].decode(‘utf-8’))
- axes = fig.add_subplot(5,4,i+1)
- axes.imshow(img)
- axes.axis(‘off’)
- plt.ion()
- plt.show()
- time.sleep(1)
- plt.close()
- i+=1
- if i%10==0:
- break
- except tf.errors.OutOfRangeError:
- pass
- finally:
- coord.request_stop()
- coord.join(thread)
-
-
-
注意路徑此時被加載成二進制編碼格式了。
二.批量讀取圖片數據
使用tf.train.slice_input_producer([path]),也可以批量讀取圖片,得到每個圖片的路徑後,我們可以加載圖片並解碼成三維數組的形式(圖像的深度必須是3通道或者4通道,筆者實驗灰度圖像,一直不成功)。當使用tf.train.slice_input_producer()時,加載圖片數據的reader使用tf.read_file(filename),直接讀取。注意圖片記得resize().見下面代碼:
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Tue Mar 27 14:18:34 2018
- @author: wsw
- """
-
- # 用於通過讀取圖片的path,然後解碼成圖片數組的形式,最後返回batch個圖片數組
- import glob
- import tensorflow as tf
- import matplotlib.pyplot as plt
-
- path_list = r'/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/Yale2/'
- img_path = glob.glob(path_list+'*.bmp')
- img_path = tf.convert_to_tensor(img_path,dtype=tf.string)
-
- # 這裏img_path,不放在數組裏面
- # num_epochs = 1,表示將文件下所有的圖片都使用一次
- # num_epochs和tf.train.slice_input_producer()中是一樣的
- # 此參數可以用來設置訓練的 epochs
- image = tf.train.slice_input_producer([img_path],num_epochs=1)
-
-
- # load one image and decode img
- def load_img(path_queue):
- # 創建一個隊列讀取器,然後解碼成數組
- # reader = tf.WholeFileReader()
- # key,value = reader.read(path_queue)
- file_contents = tf.read_file(path_queue[0])
- img = tf.image.decode_bmp(file_contents,channels=1)
- # 這裏很有必要,否則會出錯
- # 感覺這個地方貌似只能解碼3通道以上的圖片
- img = tf.image.resize_images(img,size=(100,100))
- # img = tf.reshape(img,shape=(50,50,4))
- return img
-
- img = load_img(image)
- print(img.shape)
- image_batch = tf.train.batch([img],batch_size=20)
-
- with tf.Session() as sess:
-
- # initializer for num_epochs
- tf.local_variables_initializer().run()
- coord = tf.train.Coordinator()
- thread = tf.train.start_queue_runners(sess=sess,coord=coord)
- try:
- while not coord.should_stop():
- imgs = sess.run(image_batch)
- print(imgs.shape)
- except tf.errors.OutOfRangeError:
- print('done')
- finally:
- coord.request_stop()
- coord.join(thread)
三.使用tf.train.string_input_producer()
tf.train.string_input_producer(path),傳入路徑時,不需要放入list中。然後加載圖片的reader是tf.WholeFileReader(),其他地方和tf.train.slice_input_producer()函數用法基本類似。見代碼:
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Tue Mar 27 14:18:34 2018
- @author: wsw
- """
-
- # 用於通過讀取圖片的path,然後解碼成圖片數組的形式,最後返回batch個圖片數組
- import glob
- import tensorflow as tf
-
- path_list = r'/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/Yale2/'
- img_path = glob.glob(path_list+'*.bmp')
- img_path = tf.convert_to_tensor(img_path,dtype=tf.string)
-
- # 這裏img_path,不放在數組裏面
- # num_epochs = 1,表示將文件下所有的圖片都使用一次
- # num_epochs和tf.train.slice_input_producer()中是一樣的
- # 此參數可以用來設置訓練的 epochs
- image = tf.train.string_input_producer(img_path,num_epochs=1)
-
-
- # load one image and decode img
- def load_img(path_queue):
- # 創建一個隊列讀取器,然後解碼成數組
- reader = tf.WholeFileReader()
- key,value = reader.read(path_queue)
- img = tf.image.decode_bmp(value,channels=3)
- # 這裏很有必要,否則會出錯
- # 感覺這個地方貌似只能解碼3通道以上的圖片
- # img = tf.image.resize_images(img,size=(100,100))
- img = tf.reshape(img,shape=(224,224,3))
- return img
-
- img = load_img(image)
- print(img.shape)
- image_batch = tf.train.batch([img],batch_size=20)
-
- with tf.Session() as sess:
-
- # initializer for num_epochs
- tf.local_variables_initializer().run()
- coord = tf.train.Coordinator()
- thread = tf.train.start_queue_runners(sess=sess,coord=coord)
- try:
- while not coord.should_stop():
- imgs = sess.run(image_batch)
- print(imgs.shape)
- except tf.errors.OutOfRangeError:
- print('done')
- finally:
- coord.request_stop()
- coord.join(thread)