2017-05-31 29 views
0

我有3個隊列,一個是由string_input_producer提供的FileReader,兩個是分別由int32向量和int32向量提供的slice_input_producers。它們都是這樣排列的,當順序閱讀時,它們提供了一個圖像,問題和答案,構成一個例子。保留Tensorflow中隊列的一致性

我想要做的是洗牌,同時保留它們之間的關係。

我試過使用shuffle_batch,但是這並不保留關係 - 使它無用。

我當前的代碼(相關位):

def load_images(self,images,q_name): 
    filename_queue = tf.train.string_input_producer(images,shuffle=False,name=q_name) 
    reader = tf.WholeFileReader() 
    key, value = reader.read(filename_queue) 
    imagedata = tf.image.decode_png(value) 
    imagedata = tf.cast(tf.image.resize_images(imagedata,[224,224],tf.image.ResizeMethod.NEAREST_NEIGHBOR),tf.float32) 
    imagedata = tf.div(imagedata,tf.reduce_max(tf.abs(imagedata))) 
    imagedata.set_shape([224,224,3]) 
    return key,imagedata 

keys[testfile],imagedata[testfile] = self.load_images(imagefiles[testfile],'test') 
keys[trainfile],imagedata[trainfile] = self.load_images(imagefiles[trainfile],'train') 

s_train_answer_batch,s_train_question_batch,s_train_image_batch = tf.train.batch([tf.train.slice_input_producer([answers[trainfile]],shuffle=False)[0],tf.train.slice_input_producer([questions[trainfile]],shuffle=False)[0],imagedata[trainfile]],batch_size=batch_size,capacity=batch_size*2,enqueue_many=False) 

feed_dict = {self.x_image:s_train_image_batch.eval(), self.x_question: s_train_question_batch.eval(), self.y_:s_train_answer_batch.eval(),self.keep_prob:keep_prob} 

_,ce, summary, image_summary, accuracy= sess.run([self.train_step,self.cross_entropy, self.summary_op, self.image_summary_op, self.accuracy],feed_dict=feed_dict) 

所以,必須絕對清楚:如果圖像,問題和回答矩陣,其中的只是數字一到十的載體,我想飼料字典的樣子:

q:[4,1,8,2,3,9,6,5,7],a:[4,1,8,2,3,9,6,5,7],i:[4,1,8,2,3,9,6,5,7] 

,但目前他們會看起來像:

q:[4,1,8,2,3,9,6,5,7],a:[7,3,1,5,6,2,4,9,8],i:[9,8,3,5,4,6,7,1,2] 

回答

0

我解決了它!不要使用.eval()來獲取輸出,請調用sess.run([image_batch,question_batch,answer_batch])。這保留了排序並進行洗牌。我不知道爲什麼。

+0

應該有更好的方法來做到這一點。你不應該sess.run()來獲得你的例子。除了低效率外,使用feed_dict還會降低閱讀器的功能,因爲您將事情從閱讀器中導出,然後添加到feed字典中,向圖中添加更多節點並多次複製數據。您是否嘗試讓三個獨立的閱讀器返回一個圖像/問題/答案,並調用'tf.train.shuffle_batch([image,question,answer])'?這個函數不會在重新排序的地方發生,它應該來自更早的地方。 – jpm

+0

我想我可能會誤會我做了什麼。要清楚,我的代碼現在看起來像: ''b_image,_question,b_answer = sess.run(s_train_answer_batch,s_train_question_batch,s_train_image_batch)' 'feed_dict = {self.x_image:b_image,self.x_question:b_question,self.y_: b_answe,self.keep_prob:keep_prob}' – Amylizzle

+0

沒有道理,但效率不高。您應該使用feed_dict或讀取器管道,而不是兩者。您將浪費時間將數據從批處理功能(調用sess.run時)複製到每個批處理的feed_dict中。批處理函數提供輸入張量,代入feed_dict。還可以嘗試使用'a_q = tf.train.slice_input_producer([answers [trainfile],questions [trainfile]],shuffle = False)'。答案a_q [0]和問題a_q [1]應該是正確的順序,如果張量'回答[trainfile]'和'questions [trainfile]'也是相互依次的。 – jpm

0

爲什麼不使用單個隊列(如tf.train.range_input_producershuffle)來提取用於訪問不同數據的整數?

換句話說,您使用單個隊列來提取您用於索引所有三個數據結構的整數。

+0

我無法將圖像加載到內存中,並以數組的形式訪問它們,因爲它們太多而且太大。我可能會延遲加載圖像直到最後一分鐘,並將字符串數組編入索引,但這會顯着變慢,因爲我無法首先預加載批處理。 – Amylizzle

相關問題