https://zhuanlan.zhihu.com/p/27238630
WholeFileReader
# 我們用一個具體的例子感受tensorflow中的數據讀取。如圖, # 假設我們在當前文件夾中已經有A.jpg、B.jpg、C.jpg三張圖片, # 我們希望讀取這三張圖片5個epoch並且把讀取的結果重新存到read文件夾中。 # 導入tensorflow import tensorflow as tf # 新建一個Session with tf.Session() as sess: # 我們要讀三幅圖片A.jpg, B.jpg, C.jpg filename = ['./data/A.png', './data/B.png', './data/C.png'] # string_input_producer會產生一個文件名隊列 filename_queue = tf.train.string_input_producer(filename, shuffle=True, num_epochs=5) # reader從文件名隊列中讀數據。對應的方法是reader.read reader = tf.WholeFileReader() key, value = reader.read(filename_queue) # tf.train.string_input_producer定義了一個epoch變量,要對它進行初始化 tf.local_variables_initializer().run() # 使用start_queue_runners之后,才會開始填充隊列 threads = tf.train.start_queue_runners(sess=sess) i = 0 while True: i += 1 # 獲取圖片數據並保存 image_data = sess.run(value) with open('data/test_%d.jpg' % i, 'wb') as f: f.write(image_data)
http://blog.csdn.net/wayne2019/article/details/77884478
import tensorflow as tf import os import matplotlib.pyplot as plt def file_name(file_dir): #來自http://blog.csdn.net/lsq2902101015/article/details/51305825 for root, dirs, files in os.walk(file_dir): #模塊os中的walk()函數遍歷文件夾下所有的文件 print(root) #當前目錄路徑 print(dirs) #當前路徑下所有子目錄 print(files) #當前路徑下所有非目錄子文件 def file_name2(file_dir): #特定類型的文件 L=[] for root, dirs, files in os.walk(file_dir): for file in files: if os.path.splitext(file)[1] == '.png': L.append(os.path.join(root, file)) return L file_name('data') path = file_name2('data') print(path) #以下參考http://blog.csdn.net/buptgshengod/article/details/72956846 (十圖詳解TensorFlow數據讀取機制) #以及http://blog.csdn.net/uestc_c2_403/article/details/74435286 file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #創建輸入隊列 image_reader = tf.WholeFileReader() key, image = image_reader.read(file_queue) image = tf.image.decode_jpeg(image) with tf.Session() as sess: tf.local_variables_initializer().run() threads = tf.train.start_queue_runners(sess=sess) for _ in path+path: plt.figure plt.imshow(image.eval()) plt.show()
read_file
import tensorflow as tf import os import matplotlib.pyplot as pltimport numpy as np print(tf.__version__) image_value = tf.read_file('data/A.png') img = tf.image.decode_jpeg(image_value, channels=3) with tf.Session() as sess: print(type(image_value)) # bytes print(type(img)) # Tensor print(type(img.eval())) # ndarray !!! print(img.eval().shape) print(img.eval().dtype) plt.figure(1) plt.imshow(img.eval()) plt.show()
gfile.FastGFile
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('data/A.png','rb').read() #bytes img = tf.image.decode_jpeg(image_raw) #Tensor #img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8) with tf.Session() as sess: print(type(image_raw)) # bytes print(type(img)) # Tensor #print(type(img2)) print(type(img.eval())) # ndarray !!! print(img.eval().shape) print(img.eval().dtype) # print(type(img2.eval())) # print(img2.eval().shape) # print(img2.eval().dtype) plt.figure(1) plt.imshow(img.eval()) plt.show()