初探TensorFLow从文件读取图片的四种方式

 更新时间:2018年02月06日 14:30:35   作者:Wayne2019  
本篇文章主要介绍了初探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):  #来自https://www.jb51.net/article/134543.htm
  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')


#以下参考https://www.jb51.net/article/134547.htm (十图详解TensorFlow数据读取机制)
#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
显示图片(略)

4.TFRecords:

有空再看。

如果图片是根据分类放在不同的文件夹下,那么可以直接使用如下代码:
https://www.jb51.net/article/134532.htm
https://www.jb51.net/article/134539.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python轻松管理与操作文件的技巧分享

    Python轻松管理与操作文件的技巧分享

    在日常开发中,我们经常会遇到需要对文件进行操作的场景,如读写文件、文件夹操作等。本文将为大家介绍一些 Python 中处理文件的实用技巧,让你的工作更高效
    2023-05-05
  • Python中八种数据导入方法总结

    Python中八种数据导入方法总结

    数据分析过程中,需要对获取到的数据进行分析,往往第一步就是导入数据。导入数据有很多方式,不同的数据文件需要用到不同的导入方式,相同的文件也会有几种不同的导入方式。下面总结几种常用的文件导入方法
    2022-11-11
  • 详解numpy矩阵的创建与数据类型

    详解numpy矩阵的创建与数据类型

    这篇文章主要介绍了详解numpy矩阵的创建与数据类型,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Python实现时钟显示效果思路详解

    Python实现时钟显示效果思路详解

    这篇文章主要介绍了Python实现时钟显示,需要的朋友可以参考下
    2018-04-04
  • python查看包版本、更新单个包、卸载单个包的操作方法

    python查看包版本、更新单个包、卸载单个包的操作方法

    这篇文章主要介绍了python查看包版本、更新单个包、卸载单个包,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • 如何利用python读取图片属性信息

    如何利用python读取图片属性信息

    这篇文章主要介绍了如何利用python读取图片属性信息,文章围绕python读取信息相关资料展开全文,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • pd.drop_duplicates删除重复行的方法实现

    pd.drop_duplicates删除重复行的方法实现

    drop_duplicates 方法实现对数据框 DataFrame 去除特定列的重复行,本文主要介绍了pd.drop_duplicates删除重复行的方法实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Python argparse模块应用实例解析

    Python argparse模块应用实例解析

    这篇文章主要介绍了Python argparse模块应用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 对python中dict和json的区别详解

    对python中dict和json的区别详解

    今天小编就为大家分享一篇对python中dict和json的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • opencv实现矿石图片检测矿石数量

    opencv实现矿石图片检测矿石数量

    这篇文章主要为大家详细介绍了opencv实现矿石图片检测矿石数量,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08

最新评论