初探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
0
投稿
猜你喜欢
- 启用Ad Hoc Distributed Queries:exec sp_configure 'show advanced opti
- 凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果注意:密文是大写字母,在变换加密之前把明文字母都替换为大写字母def casar(mes
- 哲学家就餐问题:哲学家就餐问题是典型的同步问题,该问题描述的是五个哲学家共用一张圆桌,分别坐在五张椅子上,在圆桌上有五个盘子和五个叉子(如下
- 用法示例:import random# 1)随机小数print(random.random()) # 获取大于0且小于1 之间的小数 &nb
- 在例子视图中返回文本的方式有点特别,即HTML被直接硬编码在Python代码之中。def current_datetime(request)
- 本文实例讲述了Python文件与文件夹常见基本操作。分享给大家供大家参考,具体如下:1、判断文件(夹)是否存在。os.path.exists
- 利用GDAL库对tif影像进行读取 示例代码默认波段为[B、G、R、NIR的顺序,且为四个波段]import gdaldef readTif
- 在HTML中,我们设置border=”1″ 时,表格边框实际大小是2px,那如果我们要做成1px的细线表格要怎么办?以前在做1px的表格的时
- 如果你想让你的IIS支持wml,做个wap网站,只需作小小的改变就行了.虽然目前支持wml的虚拟主机极少,但是自己在本机上玩玩也好的.首先在
- 运维平台导入数据这一功能实在是太重要了,我敢说在没有建自己的cmdb平台前,大多数公司管理服务器信息肯定是表格,用表格最麻烦的就是有点更新就
- 下面的示例看看这三个函数的具体的区别,其中var_dump和var_export比较少用,但他们两者又很相似。所以可以看看:<?php
- <input type=button value=刷新 onclick="hist
- 我很想自己写一个这样的程序,不过wordpress是php的,我blog是ASP的,只好用ASP写代码了。经过一番折腾测试,终于让我的梦想变
- 斐波那契记忆优化法:<script type="text/javascript">var fibonacci
- 看了cragle的《有没有必要将网站Div+Css重构?》的文章,有一些想法不说不快,我也在文章的评论里提到曾经开除过两个执着使用div技术
- 表单验证是WEB开发中经常遇到的问题,我们以前常见的做法是:在客户端对表单域进行内容的检查,看是否是满足一定的要求或满足一定的结构,比如:是
- Python实现对变位词的判断,供大家参考,具体内容如下什么是变位词呢?即两个单词都是由相同的字母组成,而各自的字母顺序不同,譬如pytho
- 终于完成了偶的拖动窗口,花了近15个小时,庆祝一下(*^__^*);以前写了IE下的功能,于是又写了firefox下的功能,在firefox
- 本文实例为大家分享了python语音整点报时的具体代码,供大家参考,具体内容如下主要的技术特殊点在于PyS60的定时器最多只能定2147秒。
- 1.1.1 摘要 Join是关系型数据库系统的重要操作之一,SQL Server中包含的常用Join:内联接、外联接和交叉联接等。如果我们想