初探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全局变量操作详解
Apr 14 Python
基于Python_脚本CGI、特点、应用、开发环境(详解)
May 23 Python
python3.6的venv模块使用详解
Aug 01 Python
对Pyhon实现静态变量全局变量的方法详解
Jan 11 Python
实例讲解Python3中abs()函数
Feb 19 Python
一行python实现树形结构的方法
Aug 09 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
Aug 27 Python
快速解决Django关闭Debug模式无法加载media图片与static静态文件
Apr 07 Python
如何在python中判断变量的类型
Jul 29 Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
Nov 28 Python
python3字符串输出常见面试题总结
Dec 01 Python
python 发送邮件的四种方法汇总
Dec 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
PHPMyadmin 配置文件详解(配置)
2009/12/03 PHP
PHP XML数据解析代码
2010/05/26 PHP
zf框架的zend_cache缓存使用方法(zend框架)
2014/03/14 PHP
Yii基于CActiveForm的Ajax数据验证用法示例
2016/07/14 PHP
PHP网站自动化配置的实现方法(必看)
2017/05/27 PHP
PHP数据库操作二:memcache用法分析
2017/08/16 PHP
php解压缩zip和rar压缩包文件的方法
2019/07/10 PHP
PHP扩展安装方法步骤解析
2020/11/24 PHP
javascript IFrame 强制刷新代码
2009/07/23 Javascript
《JavaScript高级程序设计》阅读笔记(三) ECMAScript中的引用类型
2012/02/27 Javascript
Jquery操作下拉框(DropDownList)实现取值赋值
2013/08/13 Javascript
js根据鼠标移动速度背景图片自动旋转的方法
2015/02/28 Javascript
jQuery选择器源码解读(四):tokenize方法的Expr.preFilter
2015/03/31 Javascript
js实现网页多级级联菜单代码
2015/08/20 Javascript
深入剖析javascript中的exec与match方法
2016/05/18 Javascript
WebView启动支付宝客户端支付失败的问题小结
2017/01/11 Javascript
解决Vue 通过下表修改数组,页面不渲染的问题
2018/03/08 Javascript
Vue结合Video.js播放m3u8视频流的方法示例
2018/05/04 Javascript
详解Nodejs内存治理
2018/05/13 NodeJs
详解vue2.0监听属性的使用心得及搭配计算属性的使用
2018/07/18 Javascript
VUE基于NUXT的SSR 服务端渲染
2018/11/30 Javascript
Javascript组合继承方法代码实例解析
2020/04/02 Javascript
在Django的URLconf中进行函数导入的方法
2015/07/18 Python
解决tensorflow测试模型时NotFoundError错误的问题
2018/07/26 Python
python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
2019/04/03 Python
python删除文件夹下相同文件和无法打开的图片
2019/07/16 Python
python多继承(钻石继承)问题和解决方法简单示例
2019/10/21 Python
TENSORFLOW变量作用域(VARIABLE SCOPE)
2020/01/10 Python
css3+jq创作含苞待放的荷花
2014/02/20 HTML / CSS
大学生职业生涯规划书模版
2013/12/30 职场文书
高一地理教学反思
2014/01/18 职场文书
中英文求职信范文
2014/01/27 职场文书
房屋财产继承协议书范本
2014/11/03 职场文书
2014年个人年终总结
2015/03/09 职场文书
解决xampp安装后Apache无法启动
2022/03/21 Servers
python双向链表实例详解
2022/05/25 Python