https://zhuanlan.zhihu.com/p/27238630

WholeFileReader

# 我们用一个具体的例子感受tensorflow中的数据读取。如图,
# 假设我们在当前文件夹中已经有A.jpg、B.jpg、C.jpg三张图片,
# 我们希望读取这三张图片5个epoch并且把读取的结果重新存到read文件夹中。 # 导入tensorflow
import tensorflow as tf # 新建一个Session
with tf.Session() as sess:
# 我们要读三幅图片A.jpg, B.jpg, C.jpg
filename = ['./data/A.png', './data/B.png', './data/C.png']
# string_input_producer会产生一个文件名队列
filename_queue = tf.train.string_input_producer(filename, shuffle=True, num_epochs=5)
# reader从文件名队列中读数据。对应的方法是reader.read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
# tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
tf.local_variables_initializer().run()
# 使用start_queue_runners之后,才会开始填充队列
threads = tf.train.start_queue_runners(sess=sess)
i = 0
while True:
i += 1
# 获取图片数据并保存
image_data = sess.run(value)
with open('data/test_%d.jpg' % i, 'wb') as f:
f.write(image_data)

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

import tensorflow as tf
import os
import matplotlib.pyplot as plt def file_name(file_dir): #来自http://blog.csdn.net/lsq2902101015/article/details/51305825
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] == '.png':
L.append(os.path.join(root, file))
return L file_name('data')
path = file_name2('data')
print(path) #以下参考http://blog.csdn.net/buptgshengod/article/details/72956846 (十图详解TensorFlow数据读取机制)
#以及http://blog.csdn.net/uestc_c2_403/article/details/74435286 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:
tf.local_variables_initializer().run()
threads = tf.train.start_queue_runners(sess=sess)
for _ in path+path:
plt.figure
plt.imshow(image.eval())
plt.show()

read_file

import tensorflow as tf
import os
import matplotlib.pyplot as pltimport numpy as np print(tf.__version__) image_value = tf.read_file('data/A.png')
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(img.eval())) # ndarray !!!
print(img.eval().shape)
print(img.eval().dtype)
plt.figure(1)
plt.imshow(img.eval())
plt.show()

gfile.FastGFile

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('data/A.png','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()

TensorFlow基础笔记(1) 数据读取与保存的更多相关文章

  1. 【Spark机器学习速成宝典】基础篇03数据读取与保存(Python版)

    目录 保存为文本文件:saveAsTextFile 保存为json:saveAsTextFile 保存为SequenceFile:saveAsSequenceFile 读取hive 保存为文本文件:s ...

  2. 【原】Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性

    本来应该上周更新的,结果碰上五一,懒癌发作,就推迟了 = =.以后还是要按时完成任务.废话不多说,第四章-第六章主要讲了三个内容:键值对.数据读取与保存与Spark的两个共享特性(累加器和广播变量). ...

  3. Spark学习之数据读取与保存总结(一)

    一.动机 我们已经学了很多在 Spark 中对已分发的数据执行的操作.到目前为止,所展示的示例都是从本地集合或者普通文件中进行数据读取和保存的.但有时候,数据量可能大到无法放在一台机器中,这时就需要探 ...

  4. TensorFlow基础笔记(0) 参考资源学习文档

    1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...

  5. TensorFlow基础笔记(3) cifar10 分类学习

    TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...

  6. Spark学习之数据读取与保存(4)

    Spark学习之数据读取与保存(4) 1. 文件格式 Spark对很多种文件格式的读取和保存方式都很简单. 如文本文件的非结构化的文件,如JSON的半结构化文件,如SequenceFile结构化文件. ...

  7. Spark学习笔记4:数据读取与保存

    Spark对很多种文件格式的读取和保存方式都很简单.Spark会根据文件扩展名选择对应的处理方式. Spark支持的一些常见文件格式如下: 文本文件 使用文件路径作为参数调用SparkContext中 ...

  8. Spark基础:(四)Spark 数据读取与保存

    1.文件格式 Spark对很多种文件格式的读取和保存方式都很简单. (1)文本文件 读取: 将一个文本文件读取为一个RDD时,输入的每一行都将成为RDD的一个元素. val input=sc.text ...

  9. matlab各格式数据读取与保存函数

    数据处理及matlab的初学者,可能最一开始接触的就是数据的读取与保存: %matlab数据保存与读入 function datepro clear all; %产生随机数据 mat = rand(, ...

随机推荐

  1. qs.js库 使用方法

    1.qs.js库说明 qs是一个url参数转化(parse和stringify)的js库. https://www.npmjs.com/package/qs 2.使用(以vue文件做示例) (1)基本 ...

  2. android中的byte数组转换(转)

    /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return convert result */ public static int unsign ...

  3. 【linux】查看内存和CPU使用情况

    1.内存命令:free 解释:以上数据单位KB. 所以,上面的mem物理内存共1G 下面是对这些数值的解释: total:总计物理内存的大小. used:已使用多大. free:可用有多少. Shar ...

  4. 执行 maven 命令 报错Unable to add module to the current project as it is not of packaging type 'pom'[转]

    今天学习在本地搭建Maven工程时,执行了mvn archetype:generate 命令,报错. Unable to create project from archetype [org.apac ...

  5. C# 项目发布到IIS后不能用log4net写日志

    在代码中正确配置了log4net后,IIS上仍然不能写日志的情况下,只需在写日志的目录添加 IIS_IUSRS 用户,并赋与读写权限即可.

  6. jquery-osx

    jQuery OSX https://github.com/jelly-liu/jquery-osx jquery-osx jquery, desktop, jquery desktop, jquer ...

  7. struts2 的国际化

    一.使用步骤 1)写资源文件,资源文件名命名规范和之前的讲的一致,有疑问请参考java开发中国际化 2)配置,在 struts.xml 中使用常量进行加载 struts.custom.i18n.res ...

  8. openWRT学习之LUCI之中的一个helloworld演示样例

    备注1:本文 讲述的是原生的openWRT环境下的LUCI 备注2:本文參考了诸多资料.感谢网友分享.參考资料: http://www.cnblogs.com/zmkeil/archive/2013/ ...

  9. win32环境下显示中文

    //编码转换 //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) // string title = "成绩"; // GBK2UTF8 ...

  10. UltraISO制作启动盘及提取U盘为ISO镜像

    我们先来说下UltraISO这个工具,中文名也叫软碟通,他是一个无需量产你的U盘就可以把U盘做成启动盘的工具,当然了,这么强大的工具肯定不是免费版的,对,他是共享的:但是你可以下载特别版嘛..网上到处 ...