tensorflow TFRecords文件的生成和讀取的方法
TensorFlow提供了TFRecords的格式來統(tǒng)一存儲(chǔ)數(shù)據(jù),理論上,TFRecords可以存儲(chǔ)任何形式的數(shù)據(jù)。
TFRecords文件中的數(shù)據(jù)都是通過tf.train.Example Protocol Buffer的格式存儲(chǔ)的。以下的代碼給出了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 = 2;
Int64List int64_list = 3;
}
};
下面將介紹如何生成和讀取tfrecords文件:
首先介紹tfrecords文件的生成,直接上代碼:
from random import shuffle
import numpy as np
import glob
import tensorflow as tf
import cv2
import sys
import os
# 因?yàn)槲已b的是CPU版本的,運(yùn)行起來會(huì)有'warning',解決方法入下,眼不見為凈~
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
shuffle_data = True
image_path = '/path/to/image/*.jpg'
# 取得該路徑下所有圖片的路徑,type(addrs)= list
addrs = glob.glob(image_path)
# 標(biāo)簽數(shù)據(jù)的獲得具體情況具體分析,type(labels)= list
labels = ...
# 這里是打亂數(shù)據(jù)的順序
if shuffle_data:
c = list(zip(addrs, labels))
shuffle(c)
addrs, labels = zip(*c)
# 按需分割數(shù)據(jù)集
train_addrs = addrs[0:int(0.7*len(addrs))]
train_labels = labels[0:int(0.7*len(labels))]
val_addrs = addrs[int(0.7*len(addrs)):int(0.9*len(addrs))]
val_labels = labels[int(0.7*len(labels)):int(0.9*len(labels))]
test_addrs = addrs[int(0.9*len(addrs)):]
test_labels = labels[int(0.9*len(labels)):]
# 上面不是獲得了image的地址么,下面這個(gè)函數(shù)就是根據(jù)地址獲取圖片
def load_image(addr): # A function to Load image
img = cv2.imread(addr)
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 這里/255是為了將像素值歸一化到[0,1]
img = img / 255.
img = img.astype(np.float32)
return img
# 將數(shù)據(jù)轉(zhuǎn)化成對(duì)應(yīng)的屬性
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 _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
# 下面這段就開始把數(shù)據(jù)寫入TFRecods文件
train_filename = '/path/to/train.tfrecords' # 輸出文件地址
# 創(chuàng)建一個(gè)writer來寫 TFRecords 文件
writer = tf.python_io.TFRecordWriter(train_filename)
for i in range(len(train_addrs)):
# 這是寫入操作可視化處理
if not i % 1000:
print('Train data: {}/{}'.format(i, len(train_addrs)))
sys.stdout.flush()
# 加載圖片
img = load_image(train_addrs[i])
label = train_labels[i]
# 創(chuàng)建一個(gè)屬性(feature)
feature = {'train/label': _int64_feature(label),
'train/image': _bytes_feature(tf.compat.as_bytes(img.tostring()))}
# 創(chuàng)建一個(gè) example protocol buffer
example = tf.train.Example(features=tf.train.Features(feature=feature))
# 將上面的example protocol buffer寫入文件
writer.write(example.SerializeToString())
writer.close()
sys.stdout.flush()
上面只介紹了train.tfrecords文件的生成,其余的validation,test舉一反三吧。。
接下來介紹tfrecords文件的讀?。?br />
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
data_path = 'train.tfrecords' # tfrecords 文件的地址
with tf.Session() as sess:
# 先定義feature,這里要和之前創(chuàng)建的時(shí)候保持一致
feature = {
'train/image': tf.FixedLenFeature([], tf.string),
'train/label': tf.FixedLenFeature([], tf.int64)
}
# 創(chuàng)建一個(gè)隊(duì)列來維護(hù)輸入文件列表
filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
# 定義一個(gè) reader ,讀取下一個(gè) record
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
# 解析讀入的一個(gè)record
features = tf.parse_single_example(serialized_example, features=feature)
# 將字符串解析成圖像對(duì)應(yīng)的像素組
image = tf.decode_raw(features['train/image'], tf.float32)
# 將標(biāo)簽轉(zhuǎn)化成int32
label = tf.cast(features['train/label'], tf.int32)
# 這里將圖片還原成原來的維度
image = tf.reshape(image, [224, 224, 3])
# 你還可以進(jìn)行其他一些預(yù)處理....
# 這里是創(chuàng)建順序隨機(jī) batches(函數(shù)不懂的自行百度)
images, labels = tf.train.shuffle_batch([image, label], batch_size=10, capacity=30, min_after_dequeue=10)
# 初始化
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
# 啟動(dòng)多線程處理輸入數(shù)據(jù)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
....
#關(guān)閉線程
coord.request_stop()
coord.join(threads)
sess.close()
好了,就介紹到這里。。,有什么問題可以留言。。大家一起學(xué)習(xí)。。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決
這篇文章主要介紹了Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Django使用jinja2模板的實(shí)現(xiàn)
本文主要介紹了Django使用jinja2模板的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
django 基于中間件實(shí)現(xiàn)限制ip頻繁訪問過程詳解
這篇文章主要介紹了django 基于中間件實(shí)現(xiàn)限制ip頻繁訪問過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python3 Post登錄并且保存cookie登錄其他頁面的方法
今天小編就為大家分享一篇Python3 Post登錄并且保存cookie登錄其他頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

