使用TFRecord存取多個數(shù)據(jù)案例
TensorFlow提供了一種統(tǒng)一的格式來存儲數(shù)據(jù),就是TFRecord,它可以統(tǒng)一不同的原始數(shù)據(jù)格式,并且更加有效地管理不同的屬性。
TFRecord格式
TFRecord文件中的數(shù)據(jù)都是用tf.train.Example Protocol Buffer的格式來存儲的,tf.train.Example可以被定義為:
message Example{
Features features = 1
}
message Features{
map<string, Feature> feature = 1
}
message Feature{
oneof kind{
BytesList bytes_list = 1
FloatList float_list = 1
Int64List int64_list = 1
}
}
可以看出Example是一個嵌套的數(shù)據(jù)結(jié)構(gòu),其中屬性名稱可以為一個字符串,其取值可以是字符串BytesList、實(shí)數(shù)列表FloatList或整數(shù)列表Int64List。
將數(shù)據(jù)轉(zhuǎn)化為TFRecord格式
以下代碼是將MNIST輸入數(shù)據(jù)轉(zhuǎn)化為TFRecord格式:
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
# 生成整數(shù)型的屬性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
# 生成浮點(diǎn)型的屬性
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
#若想保存為數(shù)組,則要改成value=value即可
# 生成字符串型的屬性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
mnist = input_data.read_data_sets("/tensorflow_google", dtype=tf.uint8, one_hot=True)
images = mnist.train.images
# 訓(xùn)練數(shù)據(jù)所對應(yīng)的正確答案,可以作為一個屬性保存在TFRecord中
labels = mnist.train.labels
# 訓(xùn)練數(shù)據(jù)的圖像分辨率,這可以作為Example中的一個屬性
pixels = images.shape[1]
num_examples = mnist.train.num_examples
# 輸出TFRecord文件的地址
filename = "/tensorflow_google/mnist_output.tfrecords"
# 創(chuàng)建一個writer來寫TFRecord文件
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
# 將圖像矩陣轉(zhuǎn)換成一個字符串
image_raw = images[index].tostring()
# 將一個樣例轉(zhuǎn)化為Example Protocol Buffer, 并將所有的信息寫入這個數(shù)據(jù)結(jié)構(gòu)
example = tf.train.Example(features=tf.train.Features(feature={
'pixels': _int64_feature(pixels),
'label': _int64_feature(np.argmax(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
# 將一個Example寫入TFRecord文件
writer.write(example.SerializeToString())
writer.close()
本程序?qū)NIST數(shù)據(jù)集中所有的訓(xùn)練數(shù)據(jù)存儲到了一個TFRecord文件中,若數(shù)據(jù)量較大,也可以存入多個文件。
從TFRecord文件中讀取數(shù)據(jù)
以下代碼可以從上面代碼中的TFRecord中讀取單個或多個訓(xùn)練數(shù)據(jù):
# -*- coding: utf-8 -*-
import tensorflow as tf
# 創(chuàng)建一個reader來讀取TFRecord文件中的樣例
reader = tf.TFRecordReader()
# 創(chuàng)建一個隊(duì)列來維護(hù)輸入文件列表
filename_queue = tf.train.string_input_producer(["/Users/gaoyue/文檔/Program/tensorflow_google/chapter7"
"/mnist_output.tfrecords"])
# 從文件中讀出一個樣例,也可以使用read_up_to函數(shù)一次性讀取多個樣例
# _, serialized_example = reader.read(filename_queue)
_, serialized_example = reader.read_up_to(filename_queue, 6) #讀取6個樣例
# 解析讀入的一個樣例,如果需要解析多個樣例,可以用parse_example函數(shù)
# features = tf.parse_single_example(serialized_example, features={
# 解析多個樣例
features = tf.parse_example(serialized_example, features={
# TensorFlow提供兩種不同的屬性解析方法
# 第一種是tf.FixedLenFeature,得到的解析結(jié)果為Tensor
# 第二種是tf.VarLenFeature,得到的解析結(jié)果為SparseTensor,用于處理稀疏數(shù)據(jù)
# 解析數(shù)據(jù)的格式需要與寫入數(shù)據(jù)的格式一致
'image_raw': tf.FixedLenFeature([], tf.string),
'pixels': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
})
# tf.decode_raw可以將字符串解析成圖像對應(yīng)的像素?cái)?shù)組
images = tf.decode_raw(features['image_raw'], tf.uint8)
labels = tf.cast(features['label'], tf.int32)
pixels = tf.cast(features['pixels'], tf.int32)
sess = tf.Session()
# 啟動多線程處理輸入數(shù)據(jù)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# 每次運(yùn)行可以讀取TFRecord中的一個樣例,當(dāng)所有樣例都讀完之后,會重頭讀取
# for i in range(10):
# image, label, pixel = sess.run([images, labels, pixels])
# # print(image, label, pixel)
# print(label, pixel)
# 讀取TFRecord中的前6個樣例,若加入循環(huán),則會每次從上次輸出的地方繼續(xù)順序讀6個樣例
image, label, pixel = sess.run([images, labels, pixels])
print(label, pixel)
sess.close()
>> [7 3 4 6 1 8] [784 784 784 784 784 784]
輸出結(jié)果顯示,從TFRecord文件中順序讀出前6個樣例。
以上這篇使用TFRecord存取多個數(shù)據(jù)案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
django項(xiàng)目環(huán)境搭建及在虛擬機(jī)本地創(chuàng)建django項(xiàng)目的教程
這篇文章主要介紹了django項(xiàng)目環(huán)境搭建及在虛擬機(jī)本地創(chuàng)建django項(xiàng)目的教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Pygame實(shí)戰(zhàn)之實(shí)現(xiàn)經(jīng)典外星人游戲
這篇文章主要介紹了通過Pygame實(shí)現(xiàn)經(jīng)典的外星人游戲的示例代碼,文中的代碼講解詳細(xì),對我們了解Pygame有一定的幫助,感興趣的同學(xué)可以試一試2022-01-01
詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法
這篇文章主要介紹了詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
windows中python實(shí)現(xiàn)自動化部署
本文主要介紹了windows中python實(shí)現(xiàn)自動化部署,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Python實(shí)現(xiàn)的根據(jù)IP地址計(jì)算子網(wǎng)掩碼位數(shù)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的根據(jù)IP地址計(jì)算子網(wǎng)掩碼位數(shù)功能,涉及Python數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

