1. 程式人生 > >TensorFLow 讀取圖片1:初探四種從檔案讀取的方式

TensorFLow 讀取圖片1:初探四種從檔案讀取的方式

本文記錄一下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://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] == '.jpg': L.append(os.path.join(root, file)) return L path = file_name2('test') #以下參考http://blog.csdn.net/buptgshengod/article/details/72956846 (十圖詳解TensorFlow資料讀取機制) #以及http://blog.csdn.net/uestc_c2_403/article/details/74435286 #path2 = tf.train.match_filenames_once(path) 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: # coord = tf.train.Coordinator() #協同啟動的執行緒 # 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
顯示圖片(略)