初探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 01 Python
python删除列表内容
Aug 04 Python
Python tkinter模块弹出窗口及传值回到主窗口操作详解
Jul 28 Python
Python3实现的简单验证码识别功能示例
May 02 Python
Python 一句话生成字母表的方法
Jan 02 Python
利用Python半自动化生成Nessus报告的方法
Mar 19 Python
对Python _取log的几种方式小结
Jul 25 Python
Python图像处理模块ndimage用法实例分析
Sep 05 Python
使用python实现画AR模型时序图
Nov 20 Python
Pytorch 中retain_graph的用法详解
Jan 07 Python
使用TensorFlow搭建一个全连接神经网络教程
Feb 06 Python
Python2手动安装更新pip过程实例解析
Jul 16 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开发GUI
2006/10/09 PHP
PHP ajax 分页类代码
2008/11/13 PHP
PHP数字和字符串ID互转函数(类似优酷ID)
2014/06/30 PHP
Zend Framework常用校验器详解
2016/12/09 PHP
网上应用的一个不错common.js脚本
2007/08/08 Javascript
Extjs中常用表单介绍与应用
2010/06/07 Javascript
jQuery :nth-child前有无空格的区别分析
2011/07/11 Javascript
Javascript面向对象设计一 工厂模式
2011/12/20 Javascript
JS+CSS实现简单滑动门(滑动菜单)效果
2015/09/19 Javascript
Javascript如何判断数据类型和数组类型
2016/06/22 Javascript
全屏滚动插件fullPage.js使用实例解析
2016/10/21 Javascript
TableSort.js表格排序插件使用方法详解
2017/02/10 Javascript
详解VueJs前后端分离跨域问题
2017/05/24 Javascript
vue中v-for加载本地静态图片方法
2018/03/03 Javascript
详解创建自定义的Angular Schematics
2018/06/06 Javascript
jQuery 同时获取多个标签的指定内容并储存为数组
2018/11/20 jQuery
Vue中CSS动画原理的实现
2019/02/13 Javascript
使用 vue 实例更好的监听事件及vue实例的方法
2019/04/22 Javascript
vue实现多级菜单效果
2019/10/19 Javascript
uni-app实现获取验证码倒计时功能
2020/11/01 Javascript
[02:04]2014DOTA2国际邀请赛 DK一个时代的落幕
2014/07/21 DOTA
[01:16:12]完美世界DOTA2联赛PWL S2 FTD vs Inki 第一场 11.21
2020/11/23 DOTA
Python中的生成器和yield详细介绍
2015/01/09 Python
python实现判断数组是否包含指定元素的方法
2015/07/15 Python
学习Python selenium自动化网页抓取器
2018/01/20 Python
通过Py2exe将自己的python程序打包成.exe/.app的方法
2018/05/26 Python
详解pandas删除缺失数据(pd.dropna()方法)
2019/06/25 Python
Python Opencv提取图片中某种颜色组成的图形的方法
2019/09/19 Python
查看已安装tensorflow版本的方法示例
2020/04/19 Python
设计部经理的岗位职责
2013/11/16 职场文书
2014年庆元旦活动方案
2014/02/15 职场文书
红头文件任命书范本
2014/06/05 职场文书
防汛通知
2015/04/25 职场文书
创业计划书介绍
2019/04/24 职场文书
Python编程中内置的NotImplemented类型的用法
2022/03/23 Python
JS实现数组去重的11种方法总结
2022/04/04 Javascript