初探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实现猜数字游戏(无重复数字)示例分享
Mar 29 Python
python基础教程之基本数据类型和变量声明介绍
Aug 29 Python
在Linux中通过Python脚本访问mdb数据库的方法
May 06 Python
python从入门到精通(DAY 1)
Dec 20 Python
Python简单实现控制电脑的方法
Jan 22 Python
python爬虫基本知识
Mar 05 Python
python使用opencv驱动摄像头的方法
Aug 03 Python
Python中使用遍历在列表中添加字典遇到的坑
Feb 27 Python
python实现对服务器脚本敏感信息的加密解密功能
Aug 13 Python
简单瞅瞅Python vars()内置函数的实现
Sep 27 Python
python 在threading中如何处理主进程和子线程的关系
Apr 25 Python
完美解决pycharm 不显示代码提示问题
Jun 02 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记录日志的实现代码
2011/08/08 PHP
19个Android常用工具类汇总
2014/12/30 PHP
PHP实现删除字符串中任何字符的函数
2015/08/11 PHP
PHP自定义函数获取URL中一级域名的方法
2016/08/23 PHP
PHP读取CSV大文件导入数据库的实例
2017/07/24 PHP
json 入门基础教程 推荐
2009/10/31 Javascript
jQuery表单验证插件formValidator(改进版)
2012/02/03 Javascript
javascript-简单的计算器实现步骤分解(附图)
2013/05/30 Javascript
Jquery+asp.net后台数据传到前台js进行解析的方法
2014/05/11 Javascript
Jquery修改页面标题title其它JS失效的解决方法
2014/10/31 Javascript
javascript实现输出指定行数正方形图案的方法
2015/08/03 Javascript
js如何打印object对象
2015/10/16 Javascript
基于JavaScript如何实现私有成员的语法特征及私有成员的实现方式
2015/10/28 Javascript
JavaScript的removeChild()函数用法详解
2015/12/27 Javascript
jQuery序列化后的表单值转换成Json
2017/06/16 jQuery
VueJs使用Amaze ui调整列表和内容页面
2017/11/30 Javascript
vue 组件中添加样式不生效的解决方法
2018/07/06 Javascript
python 图片验证码代码
2008/12/07 Python
十条建议帮你提高Python编程效率
2016/02/16 Python
python实现斐波那契数列的方法示例
2017/01/12 Python
Python使用Matplotlib实现Logos设计代码
2017/12/25 Python
python opencv实现旋转矩形框裁减功能
2018/07/25 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2019/08/04 Python
python getpass模块用法及实例详解
2019/10/07 Python
Python识别html主要文本框过程解析
2020/02/18 Python
捷克家居装饰及图书音像购物网站:Velký košík
2018/04/16 全球购物
舞会礼服和舞会鞋:PromGirl
2019/04/22 全球购物
Moda Italia荷兰:意大利男士服装
2019/08/31 全球购物
七年级音乐教学反思
2014/01/26 职场文书
热爱祖国演讲稿
2014/05/04 职场文书
2014年师德师风学习材料
2014/05/16 职场文书
暑期社会实践证明书
2014/11/17 职场文书
新闻发布会新闻稿
2015/07/17 职场文书
员工工作心得体会
2019/05/07 职场文书
图文详解Nginx版本平滑升级方案
2021/09/15 Servers
Html5获取用户当前位置的几种方式
2022/01/18 HTML / CSS