初探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正则用法的简单实例
Jun 21 Python
python爬虫_微信公众号推送信息爬取的实例
Oct 23 Python
Tornado高并发处理方法实例代码
Jan 15 Python
Python交互环境下实现输入代码
Jun 22 Python
python实现K近邻回归,采用等权重和不等权重的方法
Jan 23 Python
python 将有序数组转换为二叉树的方法
Mar 26 Python
python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结
Jul 04 Python
python2 中 unicode 和 str 之间的转换及与python3 str 的区别
Jul 25 Python
浅谈Python中re.match()和re.search()的使用及区别
Apr 14 Python
如何解决cmd运行python提示不是内部命令
Jul 01 Python
Python字符串函数strip()原理及用法详解
Jul 23 Python
Python 实现进度条的六种方式
Jan 06 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
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
ThinkPHP框架获取最后一次执行SQL语句及变量调试简单操作示例
2018/06/13 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
基于jquery的jqDnR拖拽溢出的修改
2011/02/12 Javascript
javascript当中的代码嗅探扩展原生对象和原型(prototype)
2013/01/11 Javascript
JavaScript中的ArrayBuffer详细介绍
2014/12/08 Javascript
jquery实现列表上下移动功能
2016/02/25 Javascript
jQuery操作动态生成的内容的方法
2016/05/28 Javascript
JS控制层作圆周运动的方法
2016/06/20 Javascript
jQuery ajax方法传递中文时出现中文乱码的解决方法
2016/07/25 Javascript
js放到head中失效的原因与解决方法
2017/03/07 Javascript
react-native ListView下拉刷新上拉加载实现代码
2017/08/03 Javascript
JavaScript 中Date对象的格式化代码方法汇总
2017/09/06 Javascript
微信小程序中吸底按钮适配iPhone X方案
2017/11/29 Javascript
JS实现的DOM插入节点操作示例
2018/04/04 Javascript
JavaScript类型相关的常用操作总结
2019/02/14 Javascript
Layui 数据表格批量删除和多条件搜索的实例
2019/09/04 Javascript
Layui表格监听行单双击事件讲解
2019/11/14 Javascript
jquery实现抽奖功能
2020/10/22 jQuery
[55:04]海涛DOTA2死魂复燃6.82版本介绍
2014/09/28 DOTA
[10:18]2018DOTA2国际邀请赛寻真——Fnatic能否笑到最后?
2018/08/14 DOTA
python连接远程ftp服务器并列出目录下文件的方法
2015/04/01 Python
python学习入门细节知识点
2018/03/29 Python
对python-3-print重定向输出的几种方法总结
2018/05/11 Python
解决python3 Pycharm上连接数据库时报错的问题
2018/12/03 Python
PyTorch在Windows环境搭建的方法步骤
2020/05/12 Python
python中if及if-else如何使用
2020/06/02 Python
html5定位并在百度地图上显示的示例
2014/04/27 HTML / CSS
SkinCeuticals官网:美国药妆品牌
2018/04/19 全球购物
超市促销实习自我鉴定
2013/09/23 职场文书
外语学院毕业生的自我鉴定
2013/11/28 职场文书
自我反省检讨书
2014/01/23 职场文书
法定代表人授权委托书范本
2014/10/07 职场文书
医院见习报告范文
2014/11/03 职场文书
刘公岛导游词
2015/02/05 职场文书
抢劫罪辩护词
2015/05/21 职场文书