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

作者:Wayne2019 时间:2021-08-06 06:04:34 

本文记录一下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://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')

#以下参考https://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:

有空再看。

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

来源:http://blog.csdn.net/wayne2019/article/details/77884478

标签:TensorFLow,读取,图片
0
投稿

猜你喜欢

  • Python图像分割之均匀性度量法分析

    2021-02-11 11:45:24
  • Python 文档解析lxml库的使用详解

    2022-01-24 10:42:38
  • 浅谈用户注册表单

    2008-11-13 12:27:00
  • ASP如何跳出本次进入下一次循环

    2008-10-23 13:46:00
  • ASP无组件汉字验证码

    2008-05-08 13:19:00
  • js编写的语法高亮引擎工具

    2008-05-25 13:27:00
  • MySQL 集群配置

    2009-04-20 14:15:00
  • 交互设计师应该具备哪些素质

    2009-03-12 12:21:00
  • Script块放在另一个Script 块内方法

    2009-02-04 15:43:00
  • php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法

    2023-07-16 11:36:01
  • Django form表单与请求的生命周期步骤详解

    2023-06-20 06:29:15
  • CentOS 5.5使用yum来安装LAMP(php运行环境)

    2023-11-14 12:15:52
  • numpy给array增加维度np.newaxis的实例

    2023-06-30 06:41:34
  • 互联网产品交互事件分析

    2009-10-06 15:23:00
  • asp 使用正则表达式替换word中的标签,转为纯文本

    2011-02-28 10:49:00
  • SQL Server数据库备份出错及应对措施

    2009-04-20 17:02:00
  • 详解ES6之async+await 同步/异步方案

    2023-08-24 11:10:54
  • GO的range具体使用

    2023-07-21 03:27:27
  • 细品Dreamweaver MX 内建FW技术

    2008-06-04 09:41:00
  • 理解 SQL Server 中系统表Sysobjects

    2009-01-20 15:13:00
  • asp之家 网络编程 m.aspxhome.com