ディレクトリからイメージとラベルをロードするサンプル入力パイプラインスクリプト。この後、前処理(画像のサイズ変更など)を行うことができます。
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/task:0/cpu:0 "](WholeFileReaderV2、input_producer)]] – SilvioBarra
私はその画像を見つけることができないと思います。フォルダへのパスは正しいですか?少数のイメージで試してみてください。 – hars
'sess.run(tf.local_variables_initializer())'と 'sess.run(tf.global_variables_initializer())' –