初探TensorFLow從文件讀取圖片的四種方式
本文記錄一下TensorFLow的幾種圖片讀取方法,官方文檔有較為全面的介紹。
1.使用gfile讀圖片,decode輸出是Tensor,eval后是ndarray
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('test/a.jpg','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()
輸出為:
1.3.0
<class 'bytes'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
圖片顯示(略)
2.使用WholeFileReader輸入queue,decode輸出是Tensor,eval后是ndarray
import tensorflow as tf import os import matplotlib.pyplot as plt def file_name(file_dir): #來自http://www.dbjr.com.cn/article/134543.htm for root, dirs, files in os.walk(file_dir): #模塊os中的walk()函數(shù)遍歷文件夾下所有的文件 print(root) #當(dāng)前目錄路徑 print(dirs) #當(dāng)前路徑下所有子目錄 print(files) #當(dāng)前路徑下所有非目錄子文件 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] == '.jpg': L.append(os.path.join(root, file)) return L path = file_name2('test') #以下參考http://www.dbjr.com.cn/article/134547.htm (十圖詳解TensorFlow數(shù)據(jù)讀取機制) #path2 = tf.train.match_filenames_once(path) file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #創(chuàng)建輸入隊列 image_reader = tf.WholeFileReader() key, image = image_reader.read(file_queue) image = tf.image.decode_jpeg(image) with tf.Session() as sess: # coord = tf.train.Coordinator() #協(xié)同啟動的線程 # threads = tf.train.start_queue_runners(sess=sess, coord=coord) #啟動線程運行隊列 # coord.request_stop() #停止所有的線程 # coord.join(threads) tf.local_variables_initializer().run() threads = tf.train.start_queue_runners(sess=sess) #print (type(image)) #print (type(image.eval())) #print(image.eval().shape) for _ in path+path: plt.figure plt.imshow(image.eval()) plt.show()
3.使用read_file,decode輸出是Tensor,eval后是ndarray
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_value = tf.read_file('test/a.jpg') 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(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()
輸出是:
1.3.0
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
顯示圖片(略)
4.TFRecords:
有空再看。
如果圖片是根據(jù)分類放在不同的文件夾下,那么可以直接使用如下代碼:
http://www.dbjr.com.cn/article/134532.htm
http://www.dbjr.com.cn/article/134539.htm
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python循環(huán)之彩色圓環(huán)實現(xiàn)示例
這篇文章主要為大家介紹了python循環(huán)之彩色圓環(huán)實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Django頁面數(shù)據(jù)的緩存與使用的具體方法
這篇文章主要介紹了Django頁面數(shù)據(jù)的緩存與使用的具體方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04python實現(xiàn)基于SVM手寫數(shù)字識別功能
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)基于SVM手寫數(shù)字識別功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換
這篇文章主要介紹了基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05python基于物品協(xié)同過濾算法實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了python基于物品協(xié)同過濾算法實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Numpy中的ravel_multi_index函數(shù)用法說明
這篇文章主要介紹了Numpy中的ravel_multi_index函數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05