初探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.dhdzp.com/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.dhdzp.com/article/134547.htm (十圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制)
#path2 = tf.train.match_filenames_once(path)
file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #創(chuàng)建輸入隊(duì)列
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é)同啟動(dòng)的線程
# threads = tf.train.start_queue_runners(sess=sess, coord=coord) #啟動(dòng)線程運(yùn)行隊(duì)列
# 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.dhdzp.com/article/134532.htm
http://www.dhdzp.com/article/134539.htm
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 如何將兩個(gè)實(shí)數(shù)矩陣合并為一個(gè)復(fù)數(shù)矩陣
這篇文章主要介紹了使用python實(shí)現(xiàn)將兩個(gè)實(shí)數(shù)矩陣合并為一個(gè)復(fù)數(shù)矩陣的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-05-05
python3獲取當(dāng)前文件的上一級(jí)目錄實(shí)例
下面小編就為大家分享一篇python3獲取當(dāng)前文件的上一級(jí)目錄實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python純代碼通過神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合方式
這篇文章主要介紹了Python純代碼通過神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)線性回歸的擬合方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例
這篇文章主要介紹了Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下2020-10-10
Python中Django與Echarts的結(jié)合用法圖文詳解
ECharts是一個(gè)第三方控件,下面這篇文章主要給大家介紹了關(guān)于Python中Django與Echarts的結(jié)合用法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

