2017-06-07 132 views
1

我是tensorflow的新手,但我已經遵循並執行了他們推廣的教程以及遍佈網絡的許多其他人。 我在MNIST圖像上做了一個小的卷積神經網絡。沒什麼特別的,但我想測試我自己的圖像。 現在我的問題來了:我創建了幾個文件夾;每個文件夾的名稱是圖像所屬的類(標籤)。加載tensorflow中的圖像文件夾

圖像有不同的形狀;我的意思是他們沒有固定的大小。

我如何加載它們以使用Tensorflow?

我遵循了許多教程,並在StackOverflow和其他Q/A站點上都回答了這兩個問題。但是,我仍然沒有弄清楚如何做到這一點。

非常感謝。 Silvio

回答

1

樣本輸入管道腳本從目錄加載圖像和標籤。在此之後,您可以進行預處理(調整圖像大小等)。

import tensorflow as tf 
filename_queue = tf.train.string_input_producer(
tf.train.match_filenames_once("/home/xxx/Desktop/stackoverflow/images/*/*.png")) 

image_reader = tf.WholeFileReader() 
key, image_file = image_reader.read(filename_queue) 
S = tf.string_split([key],'/') 
length = tf.cast(S.dense_shape[1],tf.int32) 
# adjust constant value corresponding to your paths if you face issues. It should work for above format. 
label = S.values[length-tf.constant(2,dtype=tf.int32)] 
label = tf.string_to_number(label,out_type=tf.int32) 
image = tf.image.decode_png(image_file) 

# Start a new session to show example output. 
with tf.Session() as sess: 
    # Required to get the filename matching to run. 
    tf.initialize_all_variables().run() 

    # Coordinate the loading of image files. 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    for i in xrange(6): 
     # Get an image tensor and print its value. 
     key_val,label_val,image_tensor = sess.run([key,label,image]) 
     print(image_tensor.shape) 
     print(key_val) 
     print(label_val) 


    # Finish off the filename queue coordinator. 
    coord.request_stop() 
    coord.join(threads) 

文件目錄

./images/1/1.png 
./images/1/2.png 
./images/3/1.png 
./images/3/2.png 
./images/2/1.png 
./images/2/2.png 

輸出:

所有的
(881, 2079, 3) 
/home/xxxx/Desktop/stackoverflow/images/3/1.png 
3 
(155, 2552, 3) 
/home/xxxx/Desktop/stackoverflow/images/2/1.png 
2 
(562, 1978, 3) 
/home/xxxx/Desktop/stackoverflow/images/3/2.png 
3 
(291, 2558, 3) 
/home/xxxx/Desktop/stackoverflow/images/1/1.png 
1 
(157, 2554, 3) 
/home/xxxx/Desktop/stackoverflow/images/1/2.png 
1 
(866, 936, 3) 
/home/xxxx/Desktop/stackoverflow/images/2/2.png 
2 
+0

首先,感謝您的快速回復。 我試過你的代碼片段,它引發了以下錯誤。 tensorflow.python.framework.errors_impl.OutOfRangeError:FIFOQueue '_0_input_producer' 被關閉,沒有足夠的元件(1請求,當前大小0) \t [[節點:ReaderReadV2 = ReaderReadV2 [_device =「/作業:本地主機/複製品: 0 /任務:0/CPU:0「](WholeFileReaderV2,input_producer)]] – SilvioBarra

+0

我認爲它無法找到圖像。文件夾的路徑是否正確?嘗試幾張圖片。 – hars

+1

我能夠通過以下兩行代碼修復不足的元素錯誤:'sess.run(tf.local_variables_initializer())'和'sess.run(tf.global_variables_initializer())' –

相關問題