初探TensorFLow从文件读取图片的四种方式
作者:Wayne2019 发布时间:2021-08-06 06:04:34
标签: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): #来自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


猜你喜欢
- 先上需要用到的全部代码片段(截取) MenuControl.prototype.boxDisplay = false;//是否显示图层选择菜
- js延时提示框效果演示: 实现方法 移入显示,移出隐藏 移除延时隐藏,可以实现从第一个div移入第二个div,仍然可以显示<!DOCT
- 学习编写简练、优化的CSS需要大量的实践和一种不自觉的强迫性清洁的渴望。然而让你的CSS保持整洁并不仅仅是你对清洁的疯狂的心理需求,尤其对于
- (1)、导库import pandas as pdfrom pandas import Series(2)、读取csv文件的两种方式#读取c
- 1.循环删除 #这个是我选中其中的一个分支进行右键清空操作时进行的处理for i in range(self.tree.currentIte
- 1. 带默认值的参数在了解带星号(*)的参数之前,先看下带有默认值的参数,函数定义如下:>> def defaultValueA
- subprocess.Popen用来创建子进程。1)Popen启动新的进程与父进程并行执行,默认父进程不等待新进程结束。def TestPo
- TensorFlow官网给的cifar-10教程,是卷积神经网络入门的好例子,有时想直接拿这个模型来跑自己的数据,却发现他的数据类型不是常见
- 前言首先说一下: 错误指的是可能出现问题的地方出现了问题。如打开件失败,这种情况在意料之中 。异常指的是不应该出现问题的地方出现了
- python 中datetime中strptime用法,具体代码如下所示:import datetimeday20 = datetime.d
- 在tensorflow中,有三种方式输入数据1. 利用feed_dict送入numpy数组2. 利用队列从文件中直接读取数据3. 预加载数据
- 代理模式的优点代理模式可以保护原对象,控制对原对象的访问;代理模式可以增强原对象的功能,通过代理对象来添加一些额外的功能;代理模式可以提高系
- 现在我们常见到有些网站常有这样个功能:有个按钮(如工作职位或省份或地区的选择等等)你一点就跳出一个图层(对话框)让你选择之类的。关闭它就点右
- 目录1.1 题目1.2 思路1.2.1 发送请求1.2.2 解析网页1.2.3 获取结点1.2.4 数据保存 (单线程)1.2.4 数据保存
- 最近无意看到网上有人使用Python编写几十行代码生成图像验证码,感觉很是繁琐,这里为各位朋友推荐两种方法,使用4行Python代码即可生成
- 比如 1--1 2--1  
- 处理前文件内容代码处理后的# 读取代码fr = open('three.txt', 'r')dic = {}
- 问题描述因为项目强制关闭,但是服务还在运行,导致重新运行项目时候 提示地址已经使用(端口被占用)/usr/bin/python3.5 pyt
- 一、文件操作1.打开r+ 打开存在文件 文件不存在 报错file = open("user.txt","r+&
- 一、读写文件在 Python 中,我们可以使用 open() 函数打开文件,with 语句可以自动关闭文件。读取文件内容:with open