初探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 相关文章推荐
一则python3的简单爬虫代码
May 26 Python
Python re模块介绍
Nov 30 Python
Python的装饰器模式与面向切面编程详解
Jun 21 Python
python 计算两个日期相差多少个月实例代码
May 24 Python
python实现屏保计时器的示例代码
Aug 08 Python
pytorch对可变长度序列的处理方法详解
Dec 08 Python
详解Selenium+PhantomJS+python简单实现爬虫的功能
Jul 14 Python
Django Python 获取请求头信息Content-Range的方法
Aug 06 Python
扩展Django admin的list_filter()可使用范围方法
Aug 21 Python
在tensorflow中实现屏蔽输出的log信息
Feb 04 Python
Python过滤掉numpy.array中非nan数据实例
Jun 08 Python
使用Python解决图表与画布的间距问题
Apr 11 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
DOTA2 1月28日更新:监管系统降临刀塔世界
2021/01/28 DOTA
5种PHP创建数组的实例代码分享
2014/01/17 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
2014/08/08 PHP
js CSS操作方法集合
2008/10/31 Javascript
jquery.tmpl JQuery模板插件
2011/10/10 Javascript
javascript 全选与全取消功能的实现代码
2012/12/23 Javascript
JavaScript中的逻辑判断符&amp;&amp;、||与!介绍
2014/12/31 Javascript
简单介绍JavaScript的变量和数据类型
2015/06/03 Javascript
jQuery中的100个技巧汇总
2016/12/15 Javascript
JavaScript运动框架 多物体任意值运动(三)
2017/05/17 Javascript
Node.JS 循环递归复制文件夹目录及其子文件夹下的所有文件
2017/09/18 Javascript
vue v-for 使用问题整理小结
2019/08/04 Javascript
Vue的transition-group与Virtual Dom Diff算法的使用
2019/12/09 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
基于JS+HTML实现弹窗提示是否确认提交功能
2020/06/17 Javascript
[01:14]辉夜杯战队访谈宣传片—NEWBEE.Y
2015/12/26 DOTA
使用Python读取安卓手机的屏幕分辨率方法
2018/03/31 Python
Python 处理图片像素点的实例
2019/01/08 Python
pybind11在Windows下的使用教程
2019/07/04 Python
Django xadmin开启搜索功能的实现
2019/11/15 Python
linux环境下安装python虚拟环境及注意事项
2020/01/07 Python
美国诺德斯特龙百货官网:Nordstrom
2016/08/23 全球购物
上海中网科技笔试题
2012/02/19 面试题
Shell编程面试题
2016/05/29 面试题
入党自我评价范文
2014/02/02 职场文书
美术专业自荐信
2014/07/07 职场文书
明星员工获奖感言
2014/08/14 职场文书
2014年政府采购工作总结
2014/12/09 职场文书
党员进社区活动总结
2015/05/07 职场文书
会议营销主持词
2015/07/03 职场文书
CSS预处理框架——Stylus
2021/04/21 HTML / CSS
python中如何对多变量连续赋值
2021/06/03 Python
python3+PyQt5+Qt Designer实现界面可视化
2021/06/10 Python
MySQL数据库10秒内插入百万条数据的实现
2021/11/01 MySQL
使用Java去实现超市会员管理系统
2022/03/18 Java/Android
在虚拟机中安装windows server 2008的图文教程
2022/06/28 Servers