2017-07-07 38 views
0

我想將我的圖像數據從我的TFRecord文件填充到tf.train.shuffle_batch()。我有一個load_img_file()函數,它讀取TFRecord文件,進行預處理,並以[[圖像陣列,np.uint8格式],[標籤數組,np.uint8格式]]的格式返回圖像和單熱標籤。我做了這個操作tf.train.shuffle_batch()ValueError:無法推斷張量的等級:張量(「PyFunc:0」,dtype = uint8)

load_img_file_op = tf.py_func(self.load_img_file, [], [np.uint8, np.uint8]) 

它將該函數轉換爲操作。我已經驗證了該操作在做

data = tf.Session().run(load_img_file_op) 
for n in range(50): #go through images 
    print data[1][n] #print one-hot label 
    self.image_set.display_img(data[0][n]) #display image 

它成功地打印單熱標籤並顯示相應的圖像。

然而,當我嘗試做一些像

self.batch = tf.train.shuffle_batch(load_img_file_op, batch_size=self.batch_size, capacity=q_capacity, min_after_dequeue=10000) 

我得到的錯誤

raise ValueError("Cannot infer Tensor's rank: %s" % tl[i]) ValueError: Cannot infer Tensor's rank: Tensor("PyFunc:0", dtype=uint8)"

我已經嘗試了許多變化,試圖以匹配的導向作用:

  • 而不是self.batch =,我試過example_batch,label_batch =(試圖獲得兩個值而不是一個) imageslabels
  • 有我load_image_file()功能和load_img_file_op返回兩個不同的值設置enqueue_many爲True
  • 。然後輸入他們像tf.train.shuffle_batch([images, labels],...)
  • 返回/在同一時間輸入只有一個圖像和標籤到tf.train.shuffle_batch()
  • 使用tf.train.shuffle_batch_join()

似乎沒有任何工作,但我覺得我下面的guide的格式和我見過的各種其他教程。我究竟做錯了什麼?我很抱歉,如果我的錯誤是愚蠢的或微不足道的(搜索這個錯誤似乎沒有返回任何與我有關的東西)。感謝您的幫助和時間!

+2

你可能想看看這個答案:https://stackoverflow.com/questions/42590431/output-from-tensorflow-py-func-has-unknown-rank-shape – npf

回答

0

The link在評論中幫了很多忙;謝謝! (答案是你必須在使用py_func時給出形狀)。由於我必須弄清楚一點,我會發布完整的解決方案:

我不得不讓我的函數返回兩個單獨的值,以使它們將是兩個不同的張量,並且可以分別形:

return images, labels 

然後,進入如上面的問題,但整形:

load_img_file_op = tf.py_func(self.load_img_file, [], [np.uint8, np.uint8]) # turn the function into an op 
images, labels = load_img_file_op 
images.set_shape([imgs_per_file, height * width]) 
labels.set_shape([imgs_per_file, num_classes]) 
self.batch = tf.train.shuffle_batch([images, labels], batch_size=self.batch_size, capacity=q_capacity, min_after_dequeue=1000, enqueue_many = True) 

enqueue_many是重要的,使得圖像將單獨進入隊列雙重。

相關問題