最新国产好看的视频,伊人天堂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://www.fzitv.net/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.fzitv.net/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)建輸入隊列 
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) #啟動線程運(yùn)行隊列 
#  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.fzitv.net/article/134532.htm
http://www.fzitv.net/article/134539.htm

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

相關(guān)文章

最新評論

保山市| 高清| 海阳市| 贵定县| 嘉荫县| 米泉市| 特克斯县| 甘孜| 凭祥市| 乌兰浩特市| 子洲县| 松潘县| 台中县| 获嘉县| 黄陵县| 收藏| 新津县| 页游| 朝阳县| 新泰市| 繁峙县| 海伦市| 七台河市| 瑞安市| 四平市| 五原县| 浙江省| 洪泽县| 遂川县| 民县| 唐山市| 海口市| 深圳市| 运城市| 固阳县| 额尔古纳市| 浪卡子县| 西贡区| 日喀则市| 武鸣县| 页游|