TensorFlow基础笔记(1) 数据读取与保存
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) 数据读取与保存的更多相关文章
- 【Spark机器学习速成宝典】基础篇03数据读取与保存(Python版)
目录 保存为文本文件:saveAsTextFile 保存为json:saveAsTextFile 保存为SequenceFile:saveAsSequenceFile 读取hive 保存为文本文件:s ...
- 【原】Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性
本来应该上周更新的,结果碰上五一,懒癌发作,就推迟了 = =.以后还是要按时完成任务.废话不多说,第四章-第六章主要讲了三个内容:键值对.数据读取与保存与Spark的两个共享特性(累加器和广播变量). ...
- Spark学习之数据读取与保存总结(一)
一.动机 我们已经学了很多在 Spark 中对已分发的数据执行的操作.到目前为止,所展示的示例都是从本地集合或者普通文件中进行数据读取和保存的.但有时候,数据量可能大到无法放在一台机器中,这时就需要探 ...
- TensorFlow基础笔记(0) 参考资源学习文档
1 官方文档 https://www.tensorflow.org/api_docs/ 2 极客学院中文文档 http://www.tensorfly.cn/tfdoc/api_docs/python ...
- TensorFlow基础笔记(3) cifar10 分类学习
TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...
- Spark学习之数据读取与保存(4)
Spark学习之数据读取与保存(4) 1. 文件格式 Spark对很多种文件格式的读取和保存方式都很简单. 如文本文件的非结构化的文件,如JSON的半结构化文件,如SequenceFile结构化文件. ...
- Spark学习笔记4:数据读取与保存
Spark对很多种文件格式的读取和保存方式都很简单.Spark会根据文件扩展名选择对应的处理方式. Spark支持的一些常见文件格式如下: 文本文件 使用文件路径作为参数调用SparkContext中 ...
- Spark基础:(四)Spark 数据读取与保存
1.文件格式 Spark对很多种文件格式的读取和保存方式都很简单. (1)文本文件 读取: 将一个文本文件读取为一个RDD时,输入的每一行都将成为RDD的一个元素. val input=sc.text ...
- matlab各格式数据读取与保存函数
数据处理及matlab的初学者,可能最一开始接触的就是数据的读取与保存: %matlab数据保存与读入 function datepro clear all; %产生随机数据 mat = rand(, ...
随机推荐
- java实现链队列
java实现链队列的类代码: package linkqueue; public class LinkQueue { class Element { Object elem; Element next ...
- android中listview点击事件失效的灵异事件
首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...
- OpenMeetings(4)----新用户注册
用户登录与注册的主要代码都在WebContent\src\base\auth\checkLoginData.lzx文件中 <simpleLabelButton labelid=" ...
- 算法笔记_151:算法提高 01背包(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 给定N个物品,每个物品有一个重量W和一个价值V.你有一个能装M重量的背包.问怎么装使得所装价值最大.每个物品只有一个. 输入格式 输入的第 ...
- java程序设计题库
选择题 答题要求:单选题,每题只有一个正确答案,选择正确给分,不正确不给分. 1. 下面( A )数据类型可用于main()方法中传递的参数 A.String B.Integer C ...
- java防止sql注入
public final static String filterSQLInjection(String s) { if (s == null || "".equals(s)) { ...
- MySQL-安全对调两个表名
我们想要的是同时完成表名对调,如果是先后的对掉,用RENAME的话可能会导致有些数据写入失败,那怎么办? 其实也不难,从MySQL手册里就能找到方法,那就是:同时锁定2个表,不允许写入,然后对调表名. ...
- PHP-Windows下搭建Nginx+PHP环境
项目中光用Nginx了, 由于有运维人员, 很少搭建Nginx服务器, 开发也就用用Apache, 搭过几次Nginx也忘的快, 每次都去翻别人博客, 今天重搭特此记录, 装前最好了解下FastCGI ...
- PHP-php.ini中文版
今天细看了下配置文件 有很多没用过的 就从网上搜了一篇 常看看 ;;;;;;;;;;;;;;;; 简介 ;;;;;;;;;;;;;;;;; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众 ...
- 【转载】Redis在windows下安装过程
一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...