初探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实现登录接口的示例代码
Jul 21 Python
详解Python中 sys.argv[]的用法简明解释
Dec 20 Python
python实现简单神经网络算法
Mar 10 Python
将Dataframe数据转化为ndarry数据的方法
Jun 28 Python
解决Python一行输出不显示的问题
Dec 03 Python
Django如何将URL映射到视图
Jul 29 Python
Python 根据日志级别打印不同颜色的日志的方法示例
Aug 08 Python
在 Python 中接管键盘中断信号的实现方法
Feb 04 Python
Matplotlib使用Cursor实现UI定位的示例代码
Mar 12 Python
python 利用 PIL 将数组值转成图片的实现
Apr 12 Python
Django 实现jwt认证的示例
Apr 30 Python
Django实现翻页的示例代码
May 24 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
第一个无线电台是由谁发明的
2021/03/01 无线电
php PDO中文乱码解决办法
2009/07/20 PHP
Discuz!下Memcache缓存实现方法
2010/05/28 PHP
PHP求最大子序列和的算法实现
2011/06/24 PHP
限制ckeditor上传图片文件大小的方法
2013/11/15 PHP
PHP curl 抓取AJAX异步内容示例
2014/09/09 PHP
php获取开始与结束日期之间所有日期的方法
2016/11/29 PHP
PHP 访问数据库配置通用方法(json)
2018/05/20 PHP
JS中字符问题(二进制/十进制/十六进制及ASCII码之间的转换)
2008/11/03 Javascript
当前页禁止复制粘贴截屏代码小集
2013/07/24 Javascript
手写的一个兼容各种浏览器的javascript getStyle函数(获取元素的样式)
2014/06/06 Javascript
javascript获取dom的下一个节点方法
2014/09/05 Javascript
JavaScript中pop()方法的使用教程
2015/06/09 Javascript
jquery插件uploadify多图上传功能实现代码
2016/08/12 Javascript
JavaScript数据结构之广义表的定义与表示方法详解
2017/04/12 Javascript
nodejs制作爬虫实现批量下载图片
2017/05/19 NodeJs
JS数组求和的常用方法总结【5种方法】
2019/01/14 Javascript
基于JavaScript判断两个对象内容是否相等
2020/01/10 Javascript
详细介绍Python语言中的按位运算符
2013/11/26 Python
为Python程序添加图形化界面的教程
2015/04/29 Python
Python中使用ElementTree解析XML示例
2015/06/02 Python
Python实现简单拆分PDF文件的方法
2015/07/30 Python
Python实现导出数据生成excel报表的方法示例
2017/07/12 Python
世界上最全面的汽车零部件和配件集合:JC Whitney
2016/09/04 全球购物
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
KARATOV珠宝在线商店:俄罗斯珠宝品牌
2019/03/13 全球购物
俄罗斯大型在线书店:Читай-город
2019/10/10 全球购物
创业计划书——互联网商机
2014/01/12 职场文书
听课评语大全
2014/04/30 职场文书
重阳节演讲稿:尊敬帮助老人 弘扬传统美德
2014/09/25 职场文书
工作保证书怎么写
2015/02/28 职场文书
教学督导岗位职责
2015/04/10 职场文书
2015秋季幼儿园开学通知
2015/07/16 职场文书
Python实现拼音转换
2021/06/07 Python
Python实现学生管理系统(面向对象版)
2021/06/24 Python
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
2022/05/11 Servers