Home » Python » TensorFlow: training on my own image

TensorFlow: training on my own image

Posted by: admin April 4, 2018 Leave a comment

Questions:

I am new to TensorFlow. I am looking for the help on the image recognition where I can train my own image dataset.

Is there any example for training the new dataset?

Answers:

If you are interested in how to input your own data in TensorFlow, you can look at this tutorial.


New answer (with tf.data)

With the introduction of tf.data in r1.4, we can create a batch of images without placeholders and without queues. The steps are the following:

  1. Create a list containing the filenames of the images
  2. Create a tf.data.Dataset reading these filenames
  3. Preprocess the data
  4. Create an iterator from the tf.data.Dataset which will yield the next batch

The code is:

# step 1
filenames = ['im_01.jpg', 'im_02.jpg', 'im_03.jpg', 'im_04.jpg']

# step 2: create a dataset returning slices of `filenames`
dataset = tf.data.Dataset.from_tensor_slices(filenames)

# step 3: parse every image in the dataset using `map`
def _parse_function(filename):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_jpeg(image_string, channels=3)
    image = tf.cast(image_decoded, tf.float32)
    return image

dataset = dataset.map(_parse_function)
dataset = dataset.batch(2)

# step 4: create iterator and final input tensor
iterator = dataset.make_one_shot_iterator()
images = iterator.get_next()

Now we can run directly sess.run(images) without feeding any data through placeholders.


Old answer (with TensorFlow queues)

To sum it up you have multiple steps:

  1. Create a list of filenames (ex: the paths to your images)
  2. Create a TensorFlow filename queue
  3. Read and decode each image, resize them to a fixed size (necessary for batching)
  4. Output a batch of these images

The simplest code would be:

# step 1
filenames = ['im_01.jpg', 'im_02.jpg', 'im_03.jpg', 'im_04.jpg']

# step 2
filename_queue = tf.train.string_input_producer(filenames)

# step 3: read, decode and resize images
reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=3)
image = tf.cast(image, tf.float32)
resized_image = tf.image.resize_images(image, [224, 224])

# step 4: Batching
image_batch = tf.train.batch([resized_image], batch_size=8)