tf.train.slice_input_producer,tf.train.string_input_producer兩種隊列批量讀取方式

一.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.見下面代碼實例

  1. # -*- coding: utf-8 -*-
  2. ”“”
  3. Created on Mon Mar 26 22:02:22 2018
  4. @author: Administrator
  5. “”“
  6. import tensorflow as tf
  7. import glob
  8. import matplotlib.pyplot as plt
  9. import time
  10. datapath=r’/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/ORL/’
  11. imgpath = glob.glob(datapath+‘*.bmp’)
  12. # 將路徑轉化成張量形式
  13. imgpath = tf.convert_to_tensor(imgpath)
  14. # 產生一個隊列每次隨機產生一張圖片地址
  15. # 注意這裏要放在數組裏面
  16. image = tf.train.slice_input_producer([imgpath])
  17. # 得到一個batch的圖片地址
  18. # 由於tf.train.slice_input_producer()函數默認是隨機產生一個實例
  19. # 所以在這裏直接使用tf.train.batch()直接獲得一個batch的數據即可
  20. # 沒有必要再去使用tf.trian.shuffle_batch() 速度會慢
  21. img_batch = tf.train.batch([image],batch_size=20,capacity=100)
  22. with tf.Session() as sess:
  23. coord = tf.train.Coordinator()
  24. thread = tf.train.start_queue_runners(sess,coord)
  25. i = 0
  26. try:
  27. while not coord.should_stop():
  28. imgs = sess.run(img_batch)
  29. print(imgs)
  30. fig = plt.figure()
  31. for i,path in enumerate(imgs):
  32. img = plt.imread(path[0].decode(‘utf-8’))
  33. axes = fig.add_subplot(5,4,i+1)
  34. axes.imshow(img)
  35. axes.axis(‘off’)
  36. plt.ion()
  37. plt.show()
  38. time.sleep(1)
  39. plt.close()
  40. i+=1
  41. if i%10==0:
  42. break
  43. except tf.errors.OutOfRangeError:
  44. pass
  45. finally:
  46. coord.request_stop()
  47. coord.join(thread)

注意路徑此時被加載成二進制編碼格式了。

二.批量讀取圖片數據

    使用tf.train.slice_input_producer([path]),也可以批量讀取圖片,得到每個圖片的路徑後,我們可以加載圖片並解碼成三維數組的形式(圖像的深度必須是3通道或者4通道,筆者實驗灰度圖像,一直不成功)。當使用tf.train.slice_input_producer()時,加載圖片數據的reader使用tf.read_file(filename),直接讀取。注意圖片記得resize().見下面代碼:

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Mar 27 14:18:34 2018
  5. @author: wsw
  6. """
  7. # 用於通過讀取圖片的path,然後解碼成圖片數組的形式,最後返回batch個圖片數組
  8. import glob
  9. import tensorflow as tf
  10. import matplotlib.pyplot as plt
  11. path_list = r'/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/Yale2/'
  12. img_path = glob.glob(path_list+'*.bmp')
  13. img_path = tf.convert_to_tensor(img_path,dtype=tf.string)
  14. # 這裏img_path,不放在數組裏面
  15. # num_epochs = 1,表示將文件下所有的圖片都使用一次
  16. # num_epochs和tf.train.slice_input_producer()中是一樣的
  17. # 此參數可以用來設置訓練的 epochs
  18. image = tf.train.slice_input_producer([img_path],num_epochs=1)
  19. # load one image and decode img
  20. def load_img(path_queue):
  21. # 創建一個隊列讀取器,然後解碼成數組
  22. # reader = tf.WholeFileReader()
  23. # key,value = reader.read(path_queue)
  24. file_contents = tf.read_file(path_queue[0])
  25. img = tf.image.decode_bmp(file_contents,channels=1)
  26. # 這裏很有必要,否則會出錯
  27. # 感覺這個地方貌似只能解碼3通道以上的圖片
  28. img = tf.image.resize_images(img,size=(100,100))
  29. # img = tf.reshape(img,shape=(50,50,4))
  30. return img
  31. img = load_img(image)
  32. print(img.shape)
  33. image_batch = tf.train.batch([img],batch_size=20)
  34. with tf.Session() as sess:
  35. # initializer for num_epochs
  36. tf.local_variables_initializer().run()
  37. coord = tf.train.Coordinator()
  38. thread = tf.train.start_queue_runners(sess=sess,coord=coord)
  39. try:
  40. while not coord.should_stop():
  41. imgs = sess.run(image_batch)
  42. print(imgs.shape)
  43. except tf.errors.OutOfRangeError:
  44. print('done')
  45. finally:
  46. coord.request_stop()
  47. coord.join(thread)

三.使用tf.train.string_input_producer()

    tf.train.string_input_producer(path),傳入路徑時,不需要放入list中。然後加載圖片的reader是tf.WholeFileReader(),其他地方和tf.train.slice_input_producer()函數用法基本類似。見代碼:

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Mar 27 14:18:34 2018
  5. @author: wsw
  6. """
  7. # 用於通過讀取圖片的path,然後解碼成圖片數組的形式,最後返回batch個圖片數組
  8. import glob
  9. import tensorflow as tf
  10. path_list = r'/media/wsw/文檔/pythonfile_withpycharm/SVMLearning/faceLibrary/人臉庫/Yale2/'
  11. img_path = glob.glob(path_list+'*.bmp')
  12. img_path = tf.convert_to_tensor(img_path,dtype=tf.string)
  13. # 這裏img_path,不放在數組裏面
  14. # num_epochs = 1,表示將文件下所有的圖片都使用一次
  15. # num_epochs和tf.train.slice_input_producer()中是一樣的
  16. # 此參數可以用來設置訓練的 epochs
  17. image = tf.train.string_input_producer(img_path,num_epochs=1)
  18. # load one image and decode img
  19. def load_img(path_queue):
  20. # 創建一個隊列讀取器,然後解碼成數組
  21. reader = tf.WholeFileReader()
  22. key,value = reader.read(path_queue)
  23. img = tf.image.decode_bmp(value,channels=3)
  24. # 這裏很有必要,否則會出錯
  25. # 感覺這個地方貌似只能解碼3通道以上的圖片
  26. # img = tf.image.resize_images(img,size=(100,100))
  27. img = tf.reshape(img,shape=(224,224,3))
  28. return img
  29. img = load_img(image)
  30. print(img.shape)
  31. image_batch = tf.train.batch([img],batch_size=20)
  32. with tf.Session() as sess:
  33. # initializer for num_epochs
  34. tf.local_variables_initializer().run()
  35. coord = tf.train.Coordinator()
  36. thread = tf.train.start_queue_runners(sess=sess,coord=coord)
  37. try:
  38. while not coord.should_stop():
  39. imgs = sess.run(image_batch)
  40. print(imgs.shape)
  41. except tf.errors.OutOfRangeError:
  42. print('done')
  43. finally:
  44. coord.request_stop()
  45. coord.join(thread)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章