Tensorflow load images for training

High level (with Estimator & input_fn) and low level (with feed_dict):

def input_fn():
    image_list = []
    label_list = []
    for f_name in glob('/Users/shawn/Documents/*.png'):
        image_list.append(f_name)
        label = int(re.match(r'.*_(\d+).png', f_name).group(1))
        label_list.append(label)
    imagest = tf.convert_to_tensor(image_list, dtype=tf.string)
    labelst = tf.convert_to_tensor(label_list, dtype=tf.int32)

    input_queue = tf.train.slice_input_producer([imagest, labelst],
                                                num_epochs=1,
                                                shuffle=True)

    filenamesq = tf.convert_to_tensor(input_queue[0], dtype=tf.string)
    file_content = tf.read_file(filenamesq)
    images = tf.image.decode_png(file_content, channels=3)
    images = tf.cast(images, tf.float32)
    images = tf.image.rgb_to_grayscale(images)
    resized_images = tf.image.resize_images(images, [80, 60])

    dataset_dict = dict(images=resized_images, labels=input_queue[1], files=imagest)
    batch_dict = tf.train.batch(dataset_dict, 100,
                                num_threads=1, capacity=100 * 2,
                                enqueue_many=False, shapes=None, dynamic_pad=False,
                                allow_smaller_final_batch=False,
                                shared_name=None, name=None)

    batch_labels = batch_dict.pop('labels')
    batch_images = batch_dict.pop('images')
    return batch_images, batch_labels

def main(unused_argv):

    classifier.fit(
      input_fn=input_fn,
      steps=100,
      monitors=[logging_hook])

image_paths = []
labels = []
for f_name in glob('/Users/shawn/Documents/*.png'):
    image_paths.append(f_name)
    label = int(re.match(r'.*_(\d+).png', f_name).group(1))
    labels.append(label)

image_paths_tf = tf.convert_to_tensor(image_paths, dtype=tf.string, name="image_paths_tf")
labels_tf = tf.convert_to_tensor(labels, dtype=tf.int32, name="labels_tf")

image_path_tf, label_tf = tf.train.slice_input_producer([image_paths_tf, labels_tf], shuffle=False)

image_buffer_tf = tf.read_file(image_path_tf, name="image_buffer")
image_tf = tf.image.decode_jpeg(image_buffer_tf, channels=3, name="image")
image_tf = preprocess_image_tensor(image_tf)  //see above processing

# creating a batch of images and labels
batch_size = 100
num_threads = 4
images_batch_tf, labels_batch_tf = tf.train.batch([image_tf, label_tf], batch_size=batch_size,
                                                  num_threads=num_threads)
# define train_step here

with tf.Session() as sess:
    sess.run(init)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(20):
        images, labels = sess.run([images_batch_tf, labels_batch_tf])
        _, loss_val = sess.run([train_step, loss], feed_dict={X: images, Y: labels})

    coord.request_stop() 
    coord.join(threads)
This entry was posted in tensorflow and tagged , , , . Bookmark the permalink.

Leave a comment