最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

初探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://m.fzitv.net/article/134543.htm
  for root, dirs, files in os.walk(file_dir): #模塊os中的walk()函數(shù)遍歷文件夾下所有的文件
    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://m.fzitv.net/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://m.fzitv.net/article/134532.htm
http://m.fzitv.net/article/134539.htm

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

介休市| 宁远县| 宜州市| 义乌市| 于田县| 西宁市| 阿拉善右旗| 和硕县| 多伦县| 平泉县| 福贡县| 吐鲁番市| 石嘴山市| 台江县| 宁远县| 灵石县| 林州市| 林西县| 阳江市| 沿河| 湖南省| 云林县| 南乐县| 大宁县| 小金县| 姜堰市| 曲麻莱县| 扬中市| 南溪县| 岳普湖县| 福鼎市| 民乐县| 江阴市| 淅川县| 康马县| 娄烦县| 汪清县| 垦利县| 且末县| 称多县| 综艺|