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


Posted in Python onFebruary 06, 2018

本文记录一下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://3water.com/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://3water.com/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://3water.com/article/134532.htm
https://3water.com/article/134539.htm

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

Python 相关文章推荐
使用python实现knn算法
Dec 20 Python
答题辅助python代码实现
Jan 16 Python
python实现生命游戏的示例代码(Game of Life)
Jan 24 Python
Tensorflow使用支持向量机拟合线性回归
Sep 07 Python
详解Django的CSRF认证实现
Oct 09 Python
python 提取tuple类型值中json格式的key值方法
Dec 31 Python
Python设计模式之建造者模式实例详解
Jan 17 Python
使用Python 统计高频字数的方法
Jan 31 Python
python的依赖管理的实现
May 14 Python
tensorflow 分类损失函数使用小记
Feb 18 Python
python实现提取COCO,VOC数据集中特定的类
Mar 10 Python
python怎么调用自己的函数
Jul 01 Python
用十张图详解TensorFlow数据读取机制(附代码)
Feb 06 #Python
Python实现matplotlib显示中文的方法详解
Feb 06 #Python
Python实现自动上京东抢手机
Feb 06 #Python
Python获取指定文件夹下的文件名的方法
Feb 06 #Python
TensorFlow如何实现反向传播
Feb 06 #Python
tensorflow TFRecords文件的生成和读取的方法
Feb 06 #Python
TensorFlow实现创建分类器
Feb 06 #Python
You might like
这东西价格,可以买几台TECSUN S-2000
2021/03/02 无线电
apache rewrite_module模块使用教程
2008/01/10 PHP
php smarty 二级分类代码和模版循环例子
2011/06/01 PHP
深入解析php之sphinx
2013/05/15 PHP
PHP实现的文件上传类与用法详解
2017/07/05 PHP
jQuery实现的类flash菜单效果代码
2010/05/17 Javascript
浅析showModalDialog数据缓存问题(用禁止浏览器缓存解决)
2013/07/09 Javascript
javascript判断机器是否联网的2种方法
2013/08/09 Javascript
jQuery中:text选择器用法实例
2015/01/03 Javascript
javascript封装简单实现方法
2015/08/11 Javascript
js限制文本框的输入内容代码分享(3类)
2015/08/20 Javascript
jquery移动端TAB触屏切换实现效果
2020/12/22 Javascript
js实现input密码框提示信息的方法(附html5实现方法)
2016/01/14 Javascript
JS中递归函数
2016/06/17 Javascript
Angular.js中angular-ui-router的简单实践
2017/07/18 Javascript
js中int和string数据类型互相转化实例
2019/01/16 Javascript
一文看懂如何简单实现节流函数和防抖函数
2019/09/05 Javascript
jquery实现简单自动轮播图效果
2020/07/29 jQuery
python实现批量获取指定文件夹下的所有文件的厂商信息
2014/09/28 Python
Python入门篇之数字
2014/10/20 Python
利用python发送和接收邮件
2016/09/27 Python
python读取二进制mnist实例详解
2017/05/31 Python
Python用sndhdr模块识别音频格式详解
2018/01/11 Python
django从请求到响应的过程深入讲解
2018/08/01 Python
python中将zip压缩包转为gz.tar的方法
2018/10/18 Python
python占位符输入方式实例
2019/05/27 Python
pandas DataFrame索引行列的实现
2019/06/04 Python
Flask框架钩子函数功能与用法分析
2019/08/02 Python
基于python判断目录或者文件代码实例
2019/11/29 Python
时尚、社区、科技:SEVENSTORE
2019/04/26 全球购物
PHP经典面试题
2016/09/03 面试题
诚信考试倡议书
2014/04/15 职场文书
公司捐书倡议书
2015/04/27 职场文书
民事调解书范文
2015/05/20 职场文书
2015年药品销售工作总结范文
2015/05/25 职场文书
JavaScript流程控制(分支)
2021/12/06 Javascript