shmibloo
shmibloo

Reputation: 123

Training on my own image dataset using Tensorflow

I have some code that I basically cobbled up from different places online and i'm honestly not sure exactly whats going on.

import tensorflow as tf


filename_queue = tf.train.string_input_producer(
    tf.train.match_filenames_once("/testimages/*.jpg"), name="filename_queue")

image_reader = tf.WholeFileReader(name="image_reader")

image_name, image_file = image_reader.read(filename_queue)

image = tf.image.decode_jpeg(image_file, channels=3)
batch_size=10

#resize the image
image = tf.image.resize_image_with_crop_or_pad(image, 128, 128)
image.set_shape((128, 128, 3))

# Generate batch
num_preprocess_threads = 1
min_queue_examples = 10
images = tf.train.shuffle_batch(
    [image],
    batch_size=batch_size,
    num_threads=num_preprocess_threads,
    capacity=min_queue_examples + 3 * batch_size,
    min_after_dequeue=min_queue_examples,
    name="images")
print_images = tf.Print(images.get_shape(),[images])
print(images.get_shape())

# Start a new session to show example output.
with tf.Session() as sess:
    summary_writer = tf.summary.FileWriter('/logdir', sess.graph)
    # Required to get the filename matching to run.
    tf.global_variables_initializer().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.
    for x in range(20):
        print(x)
        image_tensor= sess.run(images)
        sess.run(print_images)

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

My question is what is happening in the for loop when I call sess.run(images)? Does it queue a batch of 10 images? And what happens when I do it 20 times? I only have 100 images in the directory but it still seems to work? Should I even be using a for loop here? Also, how can I now train a model using these images? I apologize for sounding so confused. I've really been trying to figure this out by looking at other questions and tutorials.

Thanks!

Upvotes: 0

Views: 845

Answers (1)

chris
chris

Reputation: 1894

what is happening in the for loop when I call sess.run(images)?

When you're running a bunch of TensorFlow functions, nothing is actually getting computed at first. You're really setting up a computation graph. When you pass something to sess.run, that's when your stuff actually gets computed. Maybe you can think of TensorFlow functions as its own language, and sess.run as its interpreter.

Does it queue a batch of 10 images? And what happens when I do it 20 times? I only have 100 images in the directory but it still seems to work? Should I even be using a for loop here?

None of that code is really necessary to train anything - it's setup stuff that you can do yourself. Loading images myself has worked great so far.

Also, how can I now train a model using these images?

That depends what you're trying to train it to do. Generally, you would do something like this: define some computation graph with variables and a loss function, use something like tf.train.AdamOptimizer to automatically adjust the values of the variables so your loss function (a measure of how well you're doing) goes down.

I apologize for sounding so confused. I've really been trying to figure this out by looking at other questions and tutorials.

No worries! You may want to find some introductory machine learning course or book to figure this out before starting with TensorFlow. This tutorial might be a good place to learn about TensorFlow.

Upvotes: 1

Related Questions