Tensorflow使用tfrecord輸入數(shù)據(jù)格式
Tensorflow 提供了一種統(tǒng)一的格式來(lái)存儲(chǔ)數(shù)據(jù),這個(gè)格式就是TFRecord,上一篇文章中所提到的方法當(dāng)數(shù)據(jù)的來(lái)源更復(fù)雜,每個(gè)樣例中的信息更豐富的時(shí)候就很難有效的記錄輸入數(shù)據(jù)中的信息了,于是Tensorflow提供了TFRecord來(lái)統(tǒng)一存儲(chǔ)數(shù)據(jù),接下來(lái)我們就來(lái)介紹如何使用TFRecord來(lái)同意輸入數(shù)據(jù)的格式。
1. TFRecord格式介紹
TFRecord文件中的數(shù)據(jù)是通過(guò)tf.train.Example Protocol Buffer的格式存儲(chǔ)的,下面是tf.train.Example的定義
message Example {
Features features = 1;
};
message Features{
map<string,Feature> featrue = 1;
};
message Feature{
oneof kind{
BytesList bytes_list = 1;
FloatList float_list = 2;
Int64List int64_list = 3;
}
};
從上述代碼可以看到,ft.train.Example 的數(shù)據(jù)結(jié)構(gòu)相對(duì)簡(jiǎn)潔。tf.train.Example中包含了一個(gè)從屬性名稱到取值的字典,其中屬性名稱為一個(gè)字符串,屬性的取值可以為字符串(BytesList ),實(shí)數(shù)列表(FloatList )或整數(shù)列表(Int64List )。例如我們可以將解碼前的圖片作為字符串,圖像對(duì)應(yīng)的類別標(biāo)號(hào)作為整數(shù)列表。
2. 將自己的數(shù)據(jù)轉(zhuǎn)化為TFRecord格式
準(zhǔn)備數(shù)據(jù)
在上一篇中,我們?yōu)榱讼駛ゴ蟮腗NIST致敬,所以選擇圖像的前綴來(lái)進(jìn)行不同類別的分類依據(jù),但是大多數(shù)的情況下,在進(jìn)行分類任務(wù)的過(guò)程中,不同的類別都會(huì)放在不同的文件夾下,而且類別的個(gè)數(shù)往往浮動(dòng)性又很大,所以針對(duì)這樣的情況,我們現(xiàn)在利用不同類別在不同文件夾中的圖像來(lái)生成TFRecord.
我們?cè)贗ris&Contact這個(gè)文件夾下有兩個(gè)文件夾,分別為iris,contact。對(duì)于每個(gè)文件夾中存放的是對(duì)應(yīng)的圖片
轉(zhuǎn)換數(shù)據(jù)
數(shù)據(jù)準(zhǔn)備好以后,就開始準(zhǔn)備生成TFRecord,具體代碼如下:
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
cwd='/home/ruyiwei/Documents/Iris&Contact/'
classes={'iris','contact'}
writer= tf.python_io.TFRecordWriter("iris_contact.tfrecords")
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((512,80))
img_raw=img.tobytes()
#plt.imshow(img) # if you want to check you image,please delete '#'
#plt.show()
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]))
}))
writer.write(example.SerializeToString())
writer.close()
3. Tensorflow從TFRecord中讀取數(shù)據(jù)
def read_and_decode(filename): # read iris_contact.tfrecords
filename_queue = tf.train.string_input_producer([filename])# create a queue
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)#return file_name and file
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})#return image and label
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [512, 80, 3]) #reshape image to 512*80*3
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #throw img tensor
label = tf.cast(features['label'], tf.int32) #throw label tensor
return img, label
4. 將TFRecord中的數(shù)據(jù)保存為圖片
filename_queue = tf.train.string_input_producer(["iris_contact.tfrecords"])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #return file and file_name
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [512, 80, 3])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess:
init_op = tf.initialize_all_variables()
sess.run(init_op)
coord=tf.train.Coordinator()
threads= tf.train.start_queue_runners(coord=coord)
for i in range(20):
example, l = sess.run([image,label])#take out image and label
img=Image.fromarray(example, 'RGB')
img.save(cwd+str(i)+'_''Label_'+str(l)+'.jpg')#save image
print(example, l)
coord.request_stop()
coord.join(threads)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Tensorflow 實(shí)現(xiàn)將圖像與標(biāo)簽數(shù)據(jù)轉(zhuǎn)化為tfRecord文件
- 將自己的數(shù)據(jù)集制作成TFRecord格式教程
- TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例
- tensorflow入門:TFRecordDataset變長(zhǎng)數(shù)據(jù)的batch讀取詳解
- Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法
- Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法
- 使用TFRecord存取多個(gè)數(shù)據(jù)案例
相關(guān)文章
Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法
今天小編就為大家分享一篇Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
運(yùn)行django項(xiàng)目指定IP和端口的方法
今天小編就為大家分享一篇運(yùn)行django項(xiàng)目指定IP和端口的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
簡(jiǎn)單實(shí)現(xiàn)python進(jìn)度條腳本
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)python進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
PyCharm 無(wú)法 import pandas 程序卡住的解決方式
這篇文章主要介紹了PyCharm 無(wú)法 import pandas 程序卡住的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
python游戲測(cè)試工具自動(dòng)化遍歷游戲中所有關(guān)卡
這篇文章主要為大家介紹了python游戲測(cè)試工具自動(dòng)化遍歷游戲中所有關(guān)卡示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
python基礎(chǔ)之文件處理知識(shí)總結(jié)
今天帶大家了解python文件處理的相關(guān)知識(shí),文中介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
python使用SimpleXMLRPCServer實(shí)現(xiàn)簡(jiǎn)單的rpc過(guò)程
這篇文章主要介紹了python使用SimpleXMLRPCServer實(shí)現(xiàn)簡(jiǎn)單的rpc過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

