樣本輸入管道腳本從目錄加載圖像和標籤。在此之後,您可以進行預處理(調整圖像大小等)。
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
首先,感謝您的快速回復。 我試過你的代碼片段,它引發了以下錯誤。 tensorflow.python.framework.errors_impl.OutOfRangeError:FIFOQueue '_0_input_producer' 被關閉,沒有足夠的元件(1請求,當前大小0) \t [[節點:ReaderReadV2 = ReaderReadV2 [_device =「/作業:本地主機/複製品: 0 /任務:0/CPU:0「](WholeFileReaderV2,input_producer)]] – SilvioBarra
我認爲它無法找到圖像。文件夾的路徑是否正確?嘗試幾張圖片。 – hars
我能夠通過以下兩行代碼修復不足的元素錯誤:'sess.run(tf.local_variables_initializer())'和'sess.run(tf.global_variables_initializer())' –