初探TensorFLow从文件读取图片的四种方式

本文记录一下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):  #来自//www.jb51.net/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')

#以下参考//www.jb51.net/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:

有空再看。

如果图片是根据分类放在不同的文件夹下,那么可以直接使用如下代码:
//www.jb51.net/article/134532.htm
//www.jb51.net/article/134539.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • tensorflow如何批量读取图片

    本文实例为大家分享了tensorflow如何批量读取图片的具体代码,供大家参考,具体内容如下 代码: import tensorflow as tf import os def picread(filelist): """ 读取狗的图片并转换成张量 :param filelist: 文件路f径+名字的列表 :return: 每张图片的张量 """ # 1.构造文件的队列 file_queue = tf.train.string_input_pro

  • tensorflow实现对图片的读取的示例代码

    tensorflow里面给出了一个函数用来读取图像,不过得到的结果是最原始的图像,是咩有经过解码的图像,这个函数为tf.gfile.FastGFile('path', 'r').read().如果要显示读入的图像,那就需要经过解码过程,tensorflow里面提供解码的函数有两个,tf.image.decode_jepg和tf.image.decode_png分别用于解码jpg格式和png格式的图像进行解码,得到图像的像素值,这个像素值可以用于显示图像.如果乜有解码,读取的图像是一个字符串,没法

  • python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结

    在处理图像的时候经常是读取图片以后把图片转换为灰度图.作为一个刚入坑的小白,我在这篇博客记录了四种处理的方法. 首先导入包: import numpy as np import cv2 import tensorflow as tf from PIL import Image 方法一:在使用OpenCV读取图片的同时将图片转换为灰度图: img = cv2.imread(imgfile, cv2.IMREAD_GRAYSCALE) print("cv2.imread(imgfile, cv2.I

  • 初探TensorFLow从文件读取图片的四种方式

    本文记录一下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 =

  • JSP向后台传递参数的四种方式总结

    Jsp页面传值的方法 一.通过Form表单提交传值 客户端通过Form表单提交到服务器端,服务器端通过 Java代码 request.getParameter(String xx); 来取得参数(xx)为参数名称. 通过get/post方式进行提交 二.通过隐藏域传值 通过在表单中加入一个隐藏域来提交到服务器端,这种方式的好处是可以在客户端加入一些自己想要加入的参数,以便得到相应的值. 客户端代码: Java代码 <input type="hidden" name="i

  • linux下实现web数据同步的四种方式(性能比较)

    实现web数据同步的四种方式 ======================================= 1.nfs实现web数据共享2.rsync +inotify实现web数据同步3.rsync+sersync更快更节约资源实现web数据同步4.unison+inotify实现web数据双向同步 ======================================= 一.nfs实现web数据共享 nfs能实现数据同步是通过NAS(网络附加存储),在服务器上共享一个文件,且服务器需

  • 横向对比分析Python解析XML的四种方式

    在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜索后发现,目前应用比较广泛,且效率相对较高的ElementTree也是一个比较多人推荐的算法,于是拿这个算法来实测对比,ElementTree也包括两种实现,一个是普通ElementTree(ET),一个是ElementTree.iterparse(ET_iter). 本文将对DOM.SAX.ET.ET_iter四种方式进行横向对比,通过处理相

  • 详细分析Javascript中创建对象的四种方式

    前言 使用Javascript创建对象的方式有很多,现在就来列举一下其中的四种方式,并且罗列出了每种方式的优缺点,可以让大家进行选择使用,下面来看看. 工厂模式 function createPerson(name, age){ var obj = new Object(); obj.name = name; obj.age = age; return obj; //一定要返回,否则打印undefined:undefined } var person1 = new createPerson('Y

  • Python函数中定义参数的四种方式

    Python中函数参数的定义主要有四种方式: 1. F(arg1,arg2,-) 这是最常见的定义方式,一个函数可以定义任意个参数,每个参数间用逗号分割,用这种方式定义的函数在调用的的时候也必须在函数名后的小括号里提供个数相等 的值(实际参数),而且顺序必须相同,也就是说在这种调用方式中,形参和实参的个数必须一致,而且必须一一对应,也就是说第一个形参对应这第一个实参.例如: 复制代码 代码如下: def a(x,y):print x,y 调用该函数,a(1,2)则x取1,y取2,形参与实参相对应

  • javascript中类的定义方式详解(四种方式)

    本文实例讲述了javascript中类的定义方式.分享给大家供大家参考,具体如下: 类的定义包括四种方式: 1.工厂方式 function createCar(name,color,price){ var tempcar=new Object; tempcar.name=name; tempcar.color=color; tempcar.price=price; tempcar.getName=function(){ document.write(this.name+"-----"+

  • String字符串截取的四种方式总结

    如下所示: import java.util.StringTokenizer; import java.util.regex.Pattern; import org.junit.Test; public class TestStringToken { @Test public void subSting() { String str = "java,javac,javae"; String s1 = str.substring(2);//"va,javac,javae&quo

  • Python实现运行其他程序的四种方式实例分析

    本文实例讲述了Python实现运行其他程序的四种方式.分享给大家供大家参考,具体如下: 在Python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本或程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32process模块中的函数,如果想进一步控制进程,则可以使用ctype模块,直接调用kernel32.dll中的函数. [方式一]使用os.system()函数运行其他程序 os模块中的system()函数可以方便地运行

  • js学习总结_基于数据类型检测的四种方式(必看)

    1.typeof 用来检测数据类型的运算符 console.log(typeof 12)//Number 使用typeof检测数据类型,首先返回的都是字符串 ,其次字符串中包含了对应的数据类型 例如:"number"."string"."boolean"."undefined"."function"."object" console.log(typeof typeof function(

随机推荐