2016-08-12 11 views
7

Jestem stosunkowo nowy w ML i bardzo nowy w TensorfFlow. Spędziłem sporo czasu w samouczku TensorFlow MINST, a także https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/how_tos/reading_data, aby spróbować dowiedzieć się, jak czytać własne dane, ale jestem trochę zdezorientowany.Ładowanie obrazów w katalogu jako zestaw danych Tensorflow

Mam kilka obrazów (.png) w katalogu/images/0_Non /. Próbuję wprowadzić je w zestawie danych TensorFlow, więc mogę zasadniczo uruchomić rzeczy z mini-tutoriala na ten temat jako pierwszy przebieg.

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) 

Mam trochę problemów ze zrozumieniem, co tu się dzieje. Więc wydaje się, że image to tensor, a image_tensor to tablica numpy?

Jak mogę zamieścić moje obrazy w zbiorze danych? Próbowałem także podążać za przykładem Iris, który jest dla pliku CSV, który doprowadził mnie tutaj: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/base.py, ale nie byłem pewien, jak to zrobić, aby działał w moim przypadku, w którym mam png.

Dzięki!

+0

Możesz użyć typu (zdjęcia), aby dowiedzieć się typu. Czym różni się format/organizacja zbioru danych od przykładów MNIST? Czy możesz ponownie użyć tego samego kodu, który przykład MNIST ładuje dane? –

+0

Hmm. Przykład MNIST wygląda na to, że dane przychodzą jako format .tar.gz? Czy to działałoby, gdybym po prostu zrobił mój katalog png jako format .tar.gz? – Vincent

Odpowiedz

1

Niedawno dodane tf.data API ułatwia to zrobić:

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 

Zauważ, że na szkolenie trzeba będzie uzyskać etykiety skądś. Transformacja Dataset.zip() jest możliwym połączeniem ze sobą zestawu danych etykiet z innego źródła.

Powiązane problemy