Tensorflow檔案讀取

2021-10-01 16:37:31 字數 3804 閱讀 4208

tensorflow讀取檔案的特點

讀取檔案資料量特別大

需要在樣本集中隨機讀取n個樣本,每批次讀取的物件不一樣(隨機,分批次)

需要讀取的快(使用tensorflow的執行緒,不使用python的執行緒(全域性直譯器鎖))

tensorflow檔案讀取的步驟

將要讀取的檔案放入檔名佇列中(因為用執行緒來讀需要使用queue)

讀取(執行緒讀取)檔案內容,並實行解碼

批處理,按照指定數量構建乙個批次取出

假設樣本檔案資料都放在當前目錄下的test_data目錄

import tensorflow as tf

import os

#批量讀取文字資料,返回一組樣本資料和標籤資料

defcsv_read

(file_list)

:#1. 將file_list構建成佇列,該api由tensorflow提供

file_queue = tf.train.string_input_producer(file_list)

#2. 讀取檔案內容,並解碼

#建立檔案讀取器

reader = tf.textlinereader(

)#從file_queue中讀取檔案,返回k(檔名稱),v(值)

#這是乙個典型的多執行緒模型,需要使用queue避免執行緒間打架

k,v = reader.reade(file_queue)

#解碼 records =[[

'none'],

['none']]

#給出預設值,用於解碼解不出來

example,label = tf.decode_csv(v,

#值 record_defaults=records)

#如果解碼不出來(出問題了)的預設值

#3. 批處理,將資料分成指定大小的批次

exam_bat,label_bat = tf.train.batch(

[example,label]

,#資料

batch_size=9,

#每批次讀取的數量

num_threads=1,

)#執行緒數

return exa,_bat,label_bat #返回批量讀取出來的資料

if __name__ ==

'__main__'

:#1.構建檔案佇列

dir_name =

'./test_data/'

file_names = os.listdir(dir_name)

#列出所有檔案

file_list =

#python的變數

for f in file_names:

#將完整的檔案路徑新增到file_list中

#呼叫csv_read(),批量讀取file_list中檔案,返回一組樣本資料和標籤資料

example,label = csv_read(file_list)

#執行with tf.session(

)as sess:

#定義乙個執行緒協調器來管理執行緒:等待**執行緒**資源

coord = tf.train.coordinator(

)#建立一組執行緒幹活

threads = tf.train.start_queue_runners(sess,

coord=coord)

#指點執行緒協調器

#執行讀取檔案的op(這兒才真正執行)

print

(sess.run(

[example,label]))

#**執行緒

coord.request_stop(

)#請求執行緒停止

coord.join(threads)

#等待執行緒結束**資源

假設樣本檔案資料都放在當前目錄下的test_data_img目錄

import tensorflow as tf

import os

import matplotlib.pyplot as plt

#讀取def

img_read

(file_list)

:#1. 構建檔案佇列

file

-queue = tf.train.string_input_producer(file_list)

#2.讀取資料

#定義讀取器,直接讀取整個檔案(csv是按行讀)

reader = tf.wholefilereader(

)#讀檔案,返回k(name)和v(值)

k, v = reader.read(file_queue)

#解碼,呼叫tf.image.decode_jped()

img = tf.image.decode_jpeg(v)

#調整大小

img_resized = tf.image.resize(img,

[200

,200])

#設定大小200*200

#固定樣本資料的格式(批處理對格式有要求)

img_resized.set_shape(

[200

,200,3

])#3.批處理

img_bat = tf.train.batch(

[img_resized]

,#資料

batch_size=10,

#每批次處理資料數

num_threads=1)

#執行緒數

return img_bat #返回批處理資料

if __name__ ==

'__main__'

:#1. 構建檔案佇列

dir_name =

'../data/test_img/'

file_names = os.listdir(dir_name)

#列出所有檔案

file_list =

for f in file_names:

#拼出完整檔案路徑,並新增到列表

)#批量讀取file_list中指定檔案,返回樣本資料

imgs = img_read(file_list)

with tf.session(

)as sess:

coord = tf.train.coordinator(

) threads = tf.train.start_queue_runners(sess,coord=coord)

#在當前session下執行op,並用imgs接收資料

imgs = imgs.

eval()

coord.request_stop(

) coord.join(threads)

#視覺化

plt.figure(

'imgshow'

,facecolor=

'lightgray'

)#建立子圖,顯示

for i in

range(10

):plt.subplot(2,

5,i+1)

#建立2行5列共10個子圖,用來顯示,當樣本資料中不夠10張時再次讀取資料,顯示時顯示重複的

plt.xticks(

) plt.yticks(

) plt.imshow(imgs[i]

.astype(

'int32'))

#imgs資料為float型,使用astype轉為int

plt.tight_layout(

)#緊湊顯示

plt.show(

)

TensorFlow筆記 檔案讀取

這些只用於可以完全載入到記憶體中的小型資料集 1,儲存在常數中 2,儲存在變數中,初始化後,永遠不改變它的值 使用常量 training data training labels with tf.session input data tf.constant training data input l...

學習筆記 tensorflow檔案讀取

目錄先看下檔案讀取以及讀取資料處理成張量結果的過程 一般資料檔案格式有文字 excel和資料。那麼tensorflow都有對應的解析函式,除了這幾種。還有tensorflow指定的檔案格式。tensorflow還提供了一種內建檔案格式tfrecord,二進位制資料和訓練類別標籤資料儲存在同一檔案。模...

TensorFlow 讀取CSV檔案例項

import tensorflow as tf import osdef csvread filelist 讀取csv檔案 param filename 路徑 檔名的列表 return 讀取內容 1.構造檔案的佇列 file queue tf.train.string input producer ...