2016-07-25 49 views
0

作爲我正在處理的CNN的輸入,我想要使用一系列圖像(在conv層中使用3D卷積)。如何讀取作爲3D張量的圖像序列?

但是,我已經在作爲可用於計算的3D張量的圖像中讀取失敗。

這裏是我的原始嘗試:

def get_sequence_as_tensor(folder): 
    images = [folder + "/depth-%i.png" for i in range(15)] 

    tensor = tf.zeros(shape=(480, 640, 15), dtype=tf.float32) 
    for i, image in enumerate(images): 
     img = tf.image.decode_png(image) 
     img_float = tf.cast(img, tf.float32) 
     img_float = tf.reshape(img_float, (480, 640)) 
     tensor[:, :, i] = img_float 

    return tensor 

,因爲我不能使用張量的指數符號,同時我也會numpy的陣列想到這已經失敗。

TypeError: 'Tensor' object does not support item assignment 

以3D張量的形式讀取圖像序列的正確方法是什麼?

回答

0

我建議將圖像合併到tensorflow之外的numpy數組中,然後將它們傳遞給佔位符。

這樣的事情應該工作。

filename = tf.placeholder("string") 
png_string = tf.read_file(filename) 
img = tf.image.decode_png(png_string) 
img_float = tf.cast(img, tf.float32) 
img_float = tf.reshape(img_float, (480, 640)) 

images = tf.placeholder("float", (480, 640, 15)) 
output = some_operation(images) 

sess = tf.Session() 

images_array = np.zeros((480, 640, 15), np.float32) 
for i, image in enumerate(filenames): 
    images_array[:,:,i] = sess.run(img_float, feed_dict{filename: image}) 

out = sess.run(output, feed_dict={images: images_array}) 

我希望這會有所幫助。

+0

所以,訣竅是定義一個float佔位符,並簡單地將一個numpy數組傳遞到該佔位符中,以便它自動轉換爲張量,是嗎? – florianletsch

+0

謝謝,我得到它的工作。針對您的代碼示例進行了一些修復(以便與後來的人一起工作變得更容易): 1.'Sesssion' - >'Session'。 2.枚舉(圖像) - >'枚舉(文件名)'或類似的,遍歷文件名列表。 3.'feed_dict {file_name:image}' - >'feed_dict = {filename:image}' – florianletsch

+0

很高興能幫到你。對錯誤抱歉。 – jasekp