Tensorflow学习教程------tfrecords数据格式生成与读取
首先是生成tfrecords格式的数据,具体代码如下:
#coding:utf-8 import os
import tensorflow as tf
from PIL import Image cwd = os.getcwd() '''
此处我加载的数据目录如下:
bt -- 14018.jpg
14019.jpg
14020.jpg nbt -- 1_ddd.jpg
1_dsdfs.jpg
1_dfd.jpg 这里的bt nbt 就是类别,也就是代码中的classes
''' writer = tf.python_io.TFRecordWriter("train.tfrecords")
classes = ['bt','nbt']
for index, name in enumerate(classes):
class_path = cwd + '/'+ name +'/' #每一类图片的目录地址
for img_name in os.listdir(class_path):
img_path = class_path + img_name #每一张图片的路径
img = Image.open(img_path)
img = img.resize((224,224))
img_raw = img.tobytes() #将图片转化为原生bytes
example = tf.train.Example(features = tf.train.Features(feature={
'label':tf.train.Feature(int64_list = tf.train.Int64List(value=[index])),
'img_raw':tf.train.Feature(bytes_list = tf.train.BytesList(value=[img_raw]))
}))
print "write" + ' ' + str(img_path) + "to train.tfrecords."
writer.write(example.SerializeToString()) #序列化为字符串
writer.close()
然后读取生成的tfrecords数据,并且将tfrecords里面的数据保存成jpg格式的图片。具体代码如下:
#coding:utf-8
import os
import tensorflow as tf
from PIL import Image
cwd = '/media/project/tfLearnning/dataread/pic/'
def read_and_decode(filename):
#根据文件名生成一个队列
filename_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #返回文件名和文件 features = tf.parse_single_example(serialized_example,
features={
'label':tf.FixedLenFeature([],tf.int64),
'img_raw':tf.FixedLenFeature([],tf.string),
})
img = tf.decode_raw(features['img_raw'],tf.uint8)
img = tf.reshape(img,[224,224,3])
#img = tf.cast(img,tf.float32) * (1./255) - 0.5 # 将图片变成tensor
#对图片进行归一化操作将【0,255】之间的像素归一化到【-0.5,0.5】,标准化处理可以使得不同的特征具有相同的尺度(Scale)。
#这样,在使用梯度下降法学习参数的时候,不同特征对参数的影响程度就一样了
label = tf.cast(features['label'], tf.int32) #将标签转化tensor
print img
print label
return img, label #read_and_decode('train.tfrecords')
img, label = read_and_decode('train.tfrecords')
#print img.shape, label
img_batch, label_batch = tf.train.shuffle_batch([img,label],batch_size=10,capacity=2000,min_after_dequeue=1000) #形成一个batch的数据,由于使用shuffle,因此每次取batch的时候
#都是随机取的,可以使样本尽可能被充分地训练,保证min_after值小于capacit值 init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# 创建一个协调器,管理线程
coord = tf.train.Coordinator()
# 启动QueueRunner, 此时文件名队列已经进队
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(10):
example, l = sess.run([img, label]) #从对列中一张一张读取图片和标签
#example, l = sess.run([img_batch,label_batch])
print(example.shape,l) img1=Image.fromarray(example, 'RGB') #将tensor转化成图片格式
img1.save(cwd+str(i)+'_'+'Label_'+str(l)+'.jpg')#save image
# 通知其他线程关闭
coord.request_stop()
# 其他所有线程关闭之后,这一函数才能返回
coord.join(threads)
Tensorflow学习教程------tfrecords数据格式生成与读取的更多相关文章
- Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例
紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...
- Tensorflow学习教程------过拟合
Tensorflow学习教程------过拟合 回归:过拟合情况 / 分类过拟合 防止过拟合的方法有三种: 1 增加数据集 2 添加正则项 3 Dropout,意思就是训练的时候隐层神经元每次随机 ...
- Tensorflow学习教程------代价函数
Tensorflow学习教程------代价函数 二次代价函数(quadratic cost): 其中,C表示代价函数,x表示样本,y表示实际值,a表示输出值,n表示样本的总数.为简单起见,使用一 ...
- tensorflow 学习教程
tensorflow 学习手册 tensorflow 学习手册1:https://cloud.tencent.com/developer/section/1475687 tensorflow 学习手册 ...
- Tensorflow学习笔记----模型的保存和读取(4)
一.模型的保存:tf.train.Saver类中的save TensorFlow提供了一个一个API来保存和还原一个模型,即tf.train.Saver类.以下代码为保存TensorFlow计算图的方 ...
- Tensorflow学习教程------lenet多标签分类
本文在上篇的基础上利用lenet进行多标签分类.五个分类标准,每个标准分两类.实际来说,本文所介绍的多标签分类属于多任务学习中的联合训练,具体代码如下. #coding:utf-8 import te ...
- Tensorflow学习教程------创建图启动图
Tensorflow作为目前最热门的机器学习框架之一,受到了工业界和学界的热门追捧.以下几章教程将记录本人学习tensorflow的一些过程. 在tensorflow这个框架里,可以讲是若数据类型,也 ...
- Tensorflow学习教程------非线性回归
自己搭建神经网络求解非线性回归系数 代码 #coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pypl ...
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类
#coding:utf-8 import tensorflow as tf from PIL import Image,ImageFilter from tensorflow.examples.tut ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-text-width
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Inheritance and the prototype chain 继承和 原型 链
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Inherita ...
- LeetCode#3 - 无重复字符的最长字串(滑动窗口)
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: abcabcbb 输出的结果应该是3,最长的无重复的字串是'abc' 果然无论做什么都要静下心来啊!昨晚上卡了一个多小 ...
- 《百面机器学习算法工程师带你去面试》高清PDF及epub+《美团机器学习实践》PDF及思维导图
http://blog.sina.com.cn/s/blog_ecd882db0102yuek.html <百面机器学习算法工程师带你去面试>高清PDF及epub+<美团机器学习实践 ...
- zabbix_server
http://www.linuxidc.com/Linux/2014-11/109909.htm [root@localhost zabbix]# service iptables stop 关闭i ...
- Codeforces 1299B/1300D - Aerodynamic
题目大意: 给定一个图形S,让这个图形任意平移,但是要保证原点(0,0)一直在它的内部或者边上 最后把它能移动到的所有位置进行拼合可以得到一个图形T 问图形S与图形T是否相似 点会按照逆时针顺序给出 ...
- Tensorflow Mask-RCNN(三)——实时 检测视频
参考:https://www.youtube.com/watch?v=lLM8oAsi32g import cv2 import numpy as np def ran ...
- WebView的学习
加载网页: 加载URL(网络或者本地assets文件下的html文件) 加载html代码 Native和JavaScript相互调用(利于混合开发) 1.加载网络URL webview.loadUrl ...
- JZOJPJ-C 8/21题解
原题大战D1 吐槽: T1 \(O(N^2)\; N \leq 26\) N大时还要写高精, 可以增加难度 T2 不给范围 T3 居然没有完全卡掉 不对应该赞美出题人 T4 PJ考个四边形不等式?? ...
- Java 类提供了自定义的构造方法,那么类的默认构造不会被调用
以下代码无法通过编译: public class Test1 { public static void main(String[] args) { //int a=6; Foo obj=new Foo ...