欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

初探TensorFLow從文件讀取圖片的四種方式

 更新時間:2018年02月06日 14:30:35   作者:Wayne2019  
本篇文章主要介紹了初探TensorFLow從文件讀取圖片的四種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文記錄一下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禁用鍵鼠與提權(quán)代碼實例

    python禁用鍵鼠與提權(quán)代碼實例

    這篇文章主要介紹了python禁用鍵鼠與提權(quán)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • python循環(huán)之彩色圓環(huán)實現(xiàn)示例

    python循環(huán)之彩色圓環(huán)實現(xiàn)示例

    這篇文章主要為大家介紹了python循環(huán)之彩色圓環(huán)實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Django頁面數(shù)據(jù)的緩存與使用的具體方法

    Django頁面數(shù)據(jù)的緩存與使用的具體方法

    這篇文章主要介紹了Django頁面數(shù)據(jù)的緩存與使用的具體方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • python實現(xiàn)基于SVM手寫數(shù)字識別功能

    python實現(xiàn)基于SVM手寫數(shù)字識別功能

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)基于SVM手寫數(shù)字識別功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • python中session的使用案例詳解

    python中session的使用案例詳解

    這篇文章主要介紹了python?session使用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • python 簡單的繪圖工具turtle使用詳解

    python 簡單的繪圖工具turtle使用詳解

    這篇文章主要介紹了python 簡單的繪圖工具turtle使用詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換

    基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換

    這篇文章主要介紹了基于python實現(xiàn)地址和經(jīng)緯度轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • python基于物品協(xié)同過濾算法實現(xiàn)代碼

    python基于物品協(xié)同過濾算法實現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了python基于物品協(xié)同過濾算法實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 基于pandas向csv添加新的行和列

    基于pandas向csv添加新的行和列

    這篇文章主要介紹了基于pandas向csv添加新的行和列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Numpy中的ravel_multi_index函數(shù)用法說明

    Numpy中的ravel_multi_index函數(shù)用法說明

    這篇文章主要介紹了Numpy中的ravel_multi_index函數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05

最新評論