TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例
Tensor Flow官方網(wǎng)站上提供三種讀取數(shù)據(jù)的方法
1. 預(yù)加載數(shù)據(jù):在Tensor Flow圖中定義常量或變量來保存所有數(shù)據(jù),將數(shù)據(jù)直接嵌到數(shù)據(jù)圖中,當(dāng)訓(xùn)練數(shù)據(jù)較大時(shí),很消耗內(nèi)存。
如
x1=tf.constant([0,1]) x2=tf.constant([1,0]) y=tf.add(x1,x2)
2.填充數(shù)據(jù):使用sess.run()的feed_dict參數(shù),將Python產(chǎn)生的數(shù)據(jù)填充到后端,之前的MNIST數(shù)據(jù)集就是通過這種方法。也有消耗內(nèi)存,數(shù)據(jù)類型轉(zhuǎn)換耗時(shí)的缺點(diǎn)。
3. 從文件讀取數(shù)據(jù):從文件中直接讀取,讓隊(duì)列管理器從文件中讀取數(shù)據(jù)。分為兩步
先把樣本數(shù)據(jù)寫入TFRecords二進(jìn)制文件
再從隊(duì)列中讀取
TFRecord是TensorFlow提供的一種統(tǒng)一存儲(chǔ)數(shù)據(jù)的二進(jìn)制文件,能更好的利用內(nèi)存,更方便的復(fù)制和移動(dòng),并且不需要單獨(dú)的標(biāo)記文件。下面通過代碼來將MNIST轉(zhuǎn)換成TFRecord的數(shù)據(jù)格式,其他數(shù)據(jù)集也類似。
#生成整數(shù)型的屬性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
#生成字符串型的屬性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def convert_to(data_set,name):
'''
將數(shù)據(jù)填入到tf.train.Example的協(xié)議緩沖區(qū)(protocol buffer)中,將協(xié)議緩沖區(qū)序列
化為一個(gè)字符串,通過tf.python_io.TFRecordWriter寫入TFRecords文件
'''
images=data_set.images
labels=data_set.labels
num_examples=data_set.num_examples
if images.shape[0]!=num_examples:
raise ValueError ('Imagessize %d does not match label size %d.'\
%(images.shape[0],num_examples))
rows=images.shape[1] #28
cols=images.shape[2] #28
depth=images.shape[3] #1 是黑白圖像
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
#使用下面語句就會(huì)將三個(gè)文件存儲(chǔ)為一個(gè)TFRecord文件,當(dāng)數(shù)據(jù)量較大時(shí),最好將數(shù)據(jù)寫入多個(gè)文件
#filename="C:/Users/dbsdz/Desktop/TF練習(xí)/TFRecord"
print('Writing',filename)
writer=tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw=images[index].tostring() #將圖像矩陣化為一個(gè)字符串
#寫入?yún)f(xié)議緩沖區(qū),height、width、depth、label編碼成int 64類型,image——raw編碼成二進(jìn)制
example=tf.train.Example(features=tf.train.Features(feature={
'height':_int64_feature(rows),
'width':_int64_feature(cols),
'depth':_int64_feature(depth),
'label':_int64_feature(int(labels[index])),
'image_raw':_bytes_feature(image_raw)}))
writer.write(example.SerializeToString()) #序列化字符串
writer.close()
上面程序可以將MNIST數(shù)據(jù)集中所有的訓(xùn)練數(shù)據(jù)存儲(chǔ)到三個(gè)TFRecord文件中。結(jié)果如下圖

從隊(duì)列中TFRecord文件,過程分三步
1. 創(chuàng)建張量,從二進(jìn)制文件中讀取一個(gè)樣本
2. 創(chuàng)建張量,從二進(jìn)制文件中隨機(jī)讀取一個(gè)mini-batch
3. 把每一批張量傳入網(wǎng)絡(luò)作為輸入節(jié)點(diǎn)
具體代碼如下
def read_and_decode(filename_queue): #輸入文件名隊(duì)列
reader=tf.TFRecordReader()
_,serialized_example=reader.read(filename_queue)
#解析一個(gè)example,如果需要解析多個(gè)樣例,使用parse_example函數(shù)
features=tf.parse_single_example(
serialized_example,
#必須寫明feature里面的key的名稱
features={
#TensorFlow提供兩種不同的屬性解析方法,一種方法是tf.FixedLenFeature,
#這種方法解析的結(jié)果為一個(gè)Tensor。另一個(gè)方法是tf.VarLenFeature,
#這種方法得到的解析結(jié)果為SparseTensor,用于處理稀疏數(shù)據(jù)。
#這里解析數(shù)據(jù)的格式需要和上面程序?qū)懭霐?shù)據(jù)的格式一致
'image_raw':tf.FixedLenFeature([],tf.string),#圖片是string類型
'label':tf.FixedLenFeature([],tf.int64), #標(biāo)記是int64類型
})
#對(duì)于BytesList,要重新進(jìn)行編碼,把string類型的0維Tensor變成uint8類型的一維Tensor
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([IMAGE_PIXELS])
#tensor("input/DecodeRaw:0",shape=(784,),dtype=uint8)
#image張量的形狀為:tensor("input/sub:0",shape=(784,),dtype=float32)
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
#把標(biāo)記從uint8類型轉(zhuǎn)換為int32類性
#label張量的形狀為tensor(“input/cast_1:0",shape=(),dtype=int32)
label = tf.cast(features['label'], tf.int32)
return image,label
def inputs(train,batch_size,num_epochs):
#輸入?yún)?shù):
#train:選擇輸入訓(xùn)練數(shù)據(jù)/驗(yàn)證數(shù)據(jù)
#batch_size:訓(xùn)練的每一批有多少個(gè)樣本
#num_epochs:過幾遍數(shù)據(jù),設(shè)置為0/None表示永遠(yuǎn)訓(xùn)練下去
'''
返回結(jié)果: A tuple (images,labels)
*images:類型為float,形狀為【batch_size,mnist.IMAGE_PIXELS],范圍【-0.5,0.5】。
*label:類型為int32,形狀為【batch_size],范圍【0,mnist.NUM_CLASSES]
注意tf.train.QueueRunner必須用tf.train.start_queue_runners()來啟動(dòng)線程
'''
if not num_epochs:num_epochs=None
#獲取文件路徑,即./MNIST_data/train.tfrecords,./MNIST_data/validation.records
filename=os.path.join(FLAGS.train_dir,TRAIN_FILE if train else VALIDATION_FILE)
with tf.name_scope('input'):
#tf.train.string_input_producer返回一個(gè)QueueRunner,里面有一個(gè)FIFOQueue
filename_queue=tf.train.string_input_producer(#如果樣本量很大,可以分成若干文件,把文件名列表傳入
[filename],num_epochs=num_epochs)
image,label=read_and_decode(filename_queue)
#隨機(jī)化example,并把它們整合成batch_size大小
#tf.train.shuffle_batch生成了RandomShuffleQueue,并開啟兩個(gè)線程
images,sparse_labels=tf.train.shuffle_batch(
[image,label],batch_size=batch_size,num_threads=2,
capacity=1000+3*batch_size,
min_after_dequeue=1000) #留下一部分隊(duì)列,來保證每次有足夠的數(shù)據(jù)做隨機(jī)打亂
return images,sparse_labels
最后,構(gòu)建一個(gè)三層的神經(jīng)網(wǎng)絡(luò),包含兩層卷積層以及一層使用SoftMax層,附上完整代碼如下
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 8 11:06:16 2018
@author: dbsdz
https://blog.csdn.net/xy2953396112/article/details/54929073
"""
import tensorflow as tf
import os
import time
import math
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Basic model parameters as external flags.
flags = tf.app.flags
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'Mnist_data/', 'Directory to put the training data.')
flags.DEFINE_string('directory', './MNIST_data',
'Directory to download data files and write the '
'converted result')
flags.DEFINE_integer('validation_size', 5000,
'Number of examples to separate from the training '
'data for the validation set.')
flags.DEFINE_integer('num_epochs',10,'num_epochs set')
FLAGS = tf.app.flags.FLAGS
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE #圖片像素728
TRAIN_FILE = "train.tfrecords"
VALIDATION_FILE="validation.tfrecords"
#生成整數(shù)型的屬性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
#生成字符串型的屬性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def convert_to(data_set,name):
'''
將數(shù)據(jù)填入到tf.train.Example的協(xié)議緩沖區(qū)(protocol buffer)中,將協(xié)議緩沖區(qū)序列
化為一個(gè)字符串,通過tf.python_io.TFRecordWriter寫入TFRecords文件
'''
images=data_set.images
labels=data_set.labels
num_examples=data_set.num_examples
if images.shape[0]!=num_examples:
raise ValueError ('Imagessize %d does not match label size %d.'\
%(images.shape[0],num_examples))
rows=images.shape[1] #28
cols=images.shape[2] #28
depth=images.shape[3] #1 是黑白圖像
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
#使用下面語句就會(huì)將三個(gè)文件存儲(chǔ)為一個(gè)TFRecord文件,當(dāng)數(shù)據(jù)量較大時(shí),最好將數(shù)據(jù)寫入多個(gè)文件
#filename="C:/Users/dbsdz/Desktop/TF練習(xí)/TFRecord"
print('Writing',filename)
writer=tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw=images[index].tostring() #將圖像矩陣化為一個(gè)字符串
#寫入?yún)f(xié)議緩沖區(qū),height、width、depth、label編碼成int 64類型,image——raw編碼成二進(jìn)制
example=tf.train.Example(features=tf.train.Features(feature={
'height':_int64_feature(rows),
'width':_int64_feature(cols),
'depth':_int64_feature(depth),
'label':_int64_feature(int(labels[index])),
'image_raw':_bytes_feature(image_raw)}))
writer.write(example.SerializeToString()) #序列化字符串
writer.close()
def inference(images, hidden1_units, hidden2_units):
with tf.name_scope('hidden1'):
weights = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),name='weights')
biases = tf.Variable(tf.zeros([hidden1_units]),name='biases')
hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
with tf.name_scope('hidden2'):
weights = tf.Variable(
tf.truncated_normal([hidden1_units, hidden2_units],
stddev=1.0 / math.sqrt(float(hidden1_units))),
name='weights')
biases = tf.Variable(tf.zeros([hidden2_units]),
name='biases')
hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
with tf.name_scope('softmax_linear'):
weights = tf.Variable(
tf.truncated_normal([hidden2_units,FLAGS.num_epochs],
stddev=1.0 / math.sqrt(float(hidden2_units))),name='weights')
biases = tf.Variable(tf.zeros([FLAGS.num_epochs]),name='biases')
logits = tf.matmul(hidden2, weights) + biases
return logits
def lossFunction(logits, labels):
labels = tf.to_int64(labels)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=labels, name='xentropy')
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
return loss
def training(loss, learning_rate):
tf.summary.scalar(loss.op.name, loss)
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
def read_and_decode(filename_queue): #輸入文件名隊(duì)列
reader=tf.TFRecordReader()
_,serialized_example=reader.read(filename_queue)
#解析一個(gè)example,如果需要解析多個(gè)樣例,使用parse_example函數(shù)
features=tf.parse_single_example(
serialized_example,
#必須寫明feature里面的key的名稱
features={
#TensorFlow提供兩種不同的屬性解析方法,一種方法是tf.FixedLenFeature,
#這種方法解析的結(jié)果為一個(gè)Tensor。另一個(gè)方法是tf.VarLenFeature,
#這種方法得到的解析結(jié)果為SparseTensor,用于處理稀疏數(shù)據(jù)。
#這里解析數(shù)據(jù)的格式需要和上面程序?qū)懭霐?shù)據(jù)的格式一致
'image_raw':tf.FixedLenFeature([],tf.string),#圖片是string類型
'label':tf.FixedLenFeature([],tf.int64), #標(biāo)記是int64類型
})
#對(duì)于BytesList,要重新進(jìn)行編碼,把string類型的0維Tensor變成uint8類型的一維Tensor
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape([IMAGE_PIXELS])
#tensor("input/DecodeRaw:0",shape=(784,),dtype=uint8)
#image張量的形狀為:tensor("input/sub:0",shape=(784,),dtype=float32)
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
#把標(biāo)記從uint8類型轉(zhuǎn)換為int32類性
#label張量的形狀為tensor(“input/cast_1:0",shape=(),dtype=int32)
label = tf.cast(features['label'], tf.int32)
return image,label
def inputs(train,batch_size,num_epochs):
#輸入?yún)?shù):
#train:選擇輸入訓(xùn)練數(shù)據(jù)/驗(yàn)證數(shù)據(jù)
#batch_size:訓(xùn)練的每一批有多少個(gè)樣本
#num_epochs:過幾遍數(shù)據(jù),設(shè)置為0/None表示永遠(yuǎn)訓(xùn)練下去
'''
返回結(jié)果: A tuple (images,labels)
*images:類型為float,形狀為【batch_size,mnist.IMAGE_PIXELS],范圍【-0.5,0.5】。
*label:類型為int32,形狀為【batch_size],范圍【0,mnist.NUM_CLASSES]
注意tf.train.QueueRunner必須用tf.train.start_queue_runners()來啟動(dòng)線程
'''
if not num_epochs:num_epochs=None
#獲取文件路徑,即./MNIST_data/train.tfrecords,./MNIST_data/validation.records
filename=os.path.join(FLAGS.train_dir,TRAIN_FILE if train else VALIDATION_FILE)
with tf.name_scope('input'):
#tf.train.string_input_producer返回一個(gè)QueueRunner,里面有一個(gè)FIFOQueue
filename_queue=tf.train.string_input_producer(#如果樣本量很大,可以分成若干文件,把文件名列表傳入
[filename],num_epochs=num_epochs)
image,label=read_and_decode(filename_queue)
#隨機(jī)化example,并把它們整合成batch_size大小
#tf.train.shuffle_batch生成了RandomShuffleQueue,并開啟兩個(gè)線程
images,sparse_labels=tf.train.shuffle_batch(
[image,label],batch_size=batch_size,num_threads=2,
capacity=1000+3*batch_size,
min_after_dequeue=1000) #留下一部分隊(duì)列,來保證每次有足夠的數(shù)據(jù)做隨機(jī)打亂
return images,sparse_labels
def run_training():
with tf.Graph().as_default():
#輸入images和labels
images,labels=inputs(train=True,batch_size=FLAGS.batch_size,
num_epochs=3) #num_epochs就是訓(xùn)練的輪數(shù)
#構(gòu)建一個(gè)從推理模型來預(yù)測(cè)數(shù)據(jù)的圖
logits=inference(images,FLAGS.hidden1,FLAGS.hidden2)
loss=lossFunction(logits,labels) #定義損失函數(shù)
#Add to the Graph operations that train the model
train_op=training(loss,FLAGS.learning_rate)
#初始化參數(shù),特別注意:string——input_producer內(nèi)部創(chuàng)建了一個(gè)epoch計(jì)數(shù)變量
#歸入tf.graphkey.local_variables集合中,必須單獨(dú)用initialize_local_variables()初始化
init_op=tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
sess=tf.Session()
sess.run(init_op)
#Start input enqueue threads
coord =tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
try:
step=0
while not coord.should_stop(): #進(jìn)入永久循環(huán)
start_time=time.time()
_,loss_value=sess.run([train_op,loss])
#每100次訓(xùn)練輸出一次結(jié)果
if step % 100 ==0:
duration=time.time()-start_time
print('Step %d: loss=%.2f (%.3f sec)'%(step,loss_value,duration))
step+=1
except tf.errors.OutOfRangeError:
print('Done training for %d epochs,%d steps.'%(FLAGS.num_epochs,step))
finally:
coord.request_stop()#通知其他線程關(guān)閉
coord.join(threads)
sess.close()
def main(unused_argv):
#獲取數(shù)據(jù)
data_sets=input_data.read_data_sets(FLAGS.directory,dtype=tf.uint8,reshape=False,
validation_size=FLAGS.validation_size)
#將數(shù)據(jù)轉(zhuǎn)換成tf.train.Example類型,并寫入TFRecords文件
convert_to(data_sets.train,'train')
convert_to(data_sets.validation,'validation')
convert_to(data_sets.test,'test')
print('convert finished')
run_training()
if __name__ == '__main__':
tf.app.run()
運(yùn)行結(jié)果如圖

以上這篇TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解
這篇文章主要介紹了感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
django框架實(shí)現(xiàn)一次性上傳多個(gè)文件功能示例【批量上傳】
這篇文章主要介紹了django框架實(shí)現(xiàn)一次性上傳多個(gè)文件功能,結(jié)合實(shí)例形式分析了Django框架批量上傳相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-06-06
pytorch::Dataloader中的迭代器和生成器應(yīng)用詳解
這篇文章主要介紹了pytorch::Dataloader中的迭代器和生成器應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

