2016-08-12 10 views
7

私はMLには比較的新しく、TensorfFlowは非常に新しいです。私はTensorFlow MINSTチュートリアルや、https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_dataでもかなり時間をかけて自分のデータを読み込む方法を考えましたが、ちょっと混乱しています。Tensorflowとしてディレクトリに画像を読み込むデータセット

私はディレクトリ/ images/0_Non /にたくさんの画像(.png)を持っています。私はこれらをTensorFlowデータセットにしようとしていますので、基本的にMINSTチュートリアルから最初のパスとして実行できます。

import tensorflow as tf 

# Make a queue of file names including all the JPEG images files in the relative image directory. 
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("../images/0_Non/*.png")) 

image_reader = tf.WholeFileReader() 

# Read a whole file from the queue, the first returned value in the tuple is the filename which we are ignoring. 
_, image_file = image_reader.read(filename_queue) 

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) 

    # Get an image tensor and print its value. 
    image_tensor = sess.run([image]) 
    print(image_tensor) 

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

ここで何が起こっているのかを理解するのに少し問題があります。だから、imageはテンソルで、image_tensorはnumpyの配列ですか?

イメージをデータセットに挿入するにはどうすればよいですか?私はまた、ここに私をもたらしたCSVのためのアイリスの例に沿って試してみました:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/base.pyしかし、私はpngの束を持っている私の場合にこれを動作させる方法を確かではありませんでした。

ありがとうございます!

import tensorflow as tf 

# Make a Dataset of file names including all the PNG images files in 
# the relative image directory. 
filename_dataset = tf.data.Dataset.list_files("../images/0_Non/*.png") 

# Make a Dataset of image tensors by reading and decoding the files. 
image_dataset = filename_dataset.map(lambda x: tf.decode_png(tf.read_file(x))) 

# NOTE: You can add additional transformations, like 
# `image_dataset.batch(BATCH_SIZE)` or `image_dataset.repeat(NUM_EPOCHS)` 
# in here. 

iterator = image_dataset.make_one_shot_iterator() 
next_image = iterator.get_next() 

# Start a new session to show example output. 
with tf.Session() as sess: 

    try: 

    while True: 
     # Get an image tensor and print its value. 
     image_array = sess.run([next_image]) 
     print(image_tensor) 

    except tf.errors.OutOfRangeError: 
    # We have reached the end of `image_dataset`. 
    pass 

注訓練のためにあなたがどこかからラベルを取得する必要があること:

+0

タイプ(画像)を使用してタイプを調べることができます。あなたのデータセットフォーマット/組織は、MNISTの例とどのように違うのですか?あなたは、MNISTの例がデータをロードするのと同じコードを再利用できますか? –

+0

Hmm。データが.tar.gz形式になっているようなMNISTの例はありますか? .tar.gz形式のpngディレクトリを作成したばかりの場合、これはうまくいきますか? – Vincent

答えて

1

は最近tf.data APIが、それは簡単にこれを実行することができました。 Dataset.zip()変換は、image_datasetと、別のソースのラベルのデータセットを組み合わせる方法です。

関連する問題