初探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
投稿

猜你喜欢

  • PyCharm使用Docker镜像搭建Python开发环境

    2021-09-24 21:41:16
  • mysql中workbench实例详解

    2024-01-15 01:45:03
  • Python安装Imaging报错:The _imaging C module is not installed问题解决方法

    2021-05-19 06:09:39
  • 详解Python的Django框架中manage命令的使用与扩展

    2021-01-04 05:09:27
  • vue设计一个倒计时秒杀的组件详解

    2024-05-09 10:41:55
  • Advanced SQL Injection with MySQL

    2024-01-24 18:09:24
  • 如何在pycharm中配置pyqt5设计GUI操作教程

    2021-09-28 17:42:21
  • 如何提升JavaScript的运行速度(DOM篇)[译]

    2009-02-25 12:24:00
  • Python3爬虫中关于Ajax分析方法的总结

    2021-04-07 17:28:47
  • response.setHeader()方法设置http文件头的值

    2010-03-11 22:43:00
  • Python生成短uuid的方法实例详解

    2021-06-30 14:12:03
  • SQL Server简单实现数据的日报和月报功能

    2024-01-18 15:19:17
  • 用python画了个圣诞树给女朋友

    2022-06-03 00:54:44
  • Vscode上使用SQL的方法

    2024-01-14 07:02:19
  • Mac上安装MySQL过程分享

    2024-01-22 16:03:34
  • Pytorch保存模型用于测试和用于继续训练的区别详解

    2021-12-02 08:49:39
  • 如何利用Python开发一个简单的猜数字游戏

    2022-05-21 20:38:08
  • python实现b站直播自动发送弹幕功能

    2023-07-13 06:58:15
  • python 为什么说eval要慎用

    2022-05-01 17:38:49
  • python 两种方法修改文件的创建时间、修改时间、访问时间

    2023-08-10 03:06:52
  • asp之家 网络编程 m.aspxhome.com