初探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实现用户登录系统
May 21 Python
python装饰器初探(推荐)
Jul 21 Python
Python实现Linux的find命令实例分享
Jun 04 Python
Python编程之基于概率论的分类方法:朴素贝叶斯
Nov 11 Python
Python enumerate索引迭代代码解析
Jan 19 Python
python,Django实现的淘宝客登录功能示例
Jun 12 Python
Python笔记之facade模式
Nov 20 Python
如何在django中添加日志功能
Feb 06 Python
Python3实现飞机大战游戏
Apr 24 Python
Django ORM filter() 的运用详解
May 14 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
May 15 Python
Python2与Python3关于字符串编码处理的差别总结
Sep 07 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
Windows下IIS6/Apache2.2.4+MySQL5.2+PHP5.2.1安装配置方法
2007/05/03 PHP
PHP 获取客户端真实IP地址多种方法小结
2010/05/15 PHP
thinkPHP模板引擎用法示例
2016/12/08 PHP
PHP实现对数组分页处理实例详解
2017/02/07 PHP
thinkphp3.2实现跨控制器调用其他模块的方法
2017/03/14 PHP
docker-compose部署php项目实例详解
2019/07/30 PHP
解决Laravel blade模板转义html标签的问题
2019/09/03 PHP
php设计模式之职责链模式定义与用法经典示例
2019/09/19 PHP
可以把编码转换成 gb2312编码lib.UTF8toGB2312.js
2007/08/21 Javascript
js事件冒泡实例分享(已测试)
2013/04/23 Javascript
js判断undefined类型示例代码
2014/02/10 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
jquery实现简单的无缝滚动
2015/04/15 Javascript
js中 javascript:void(0) 用法详解
2015/08/11 Javascript
JavaScript 浏览器兼容性总结及常用浏览器兼容性分析
2016/03/30 Javascript
即将发布的jQuery 3 有哪些新特性
2016/04/14 Javascript
jQuery 全选 全部选 反选 实现代码
2016/08/17 Javascript
详解Angular 4.x NgIf 的用法
2017/05/22 Javascript
jQuery 实现图片的依次加载图片功能
2017/07/06 jQuery
EasyUI在Panel上动态添加LinkButton按钮
2017/08/11 Javascript
Vue 中文本内容超出规定行数后展开收起的处理的实现方法
2019/04/28 Javascript
windows如何把已安装的nodejs高版本降级为低版本(图文教程)
2020/12/14 NodeJs
[02:12]2015国际邀请赛 SHOWOPEN
2015/08/05 DOTA
python中的闭包函数
2018/02/09 Python
使用 Python 写一个简易的抽奖程序
2019/12/08 Python
Python日志logging模块功能与用法详解
2020/04/09 Python
详解anaconda离线安装pytorchGPU版
2020/09/08 Python
关于html字符串正则判断和匹配的具体使用
2019/12/12 HTML / CSS
TIME时代杂志台湾总代理:台时亚洲
2018/10/22 全球购物
一套SQL笔试题
2016/08/14 面试题
linux系统都有哪些运行级别
2012/04/15 面试题
实习护士自我鉴定
2013/10/13 职场文书
单位成立周年感言
2014/01/26 职场文书
校园环保建议书
2014/05/14 职场文书
小学班主任培训心得体会
2016/01/07 职场文书
Python编写冷笑话生成器
2022/04/20 Python