TensorFlow: training on my own image



  • 1 Answer(s)

        Using tf.data in r1.4, create more images without placeholders and queues. Do the below steps:

    • First create a list hold the filenames of the images and their list of labels
    • Then create a tf.data.Dataset reading these filenames and labels
    • Preprocess the data
    • From this tf.data.Dataset create an iterator to yield the next batch

    Run this code:

    # step 1
    filenames = tf.constant(['im_01.jpg', 'im_02.jpg', 'im_03.jpg', 'im_04.jpg'])
    labels = tf.constant([0, 1, 0, 1])
     
    # step 2: create a dataset returning slices of `filenames`
    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
     
    # step 3: parse every image in the dataset using `map`
    def _parse_function(filename, label):
        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, label
     
    dataset = dataset.map(_parse_function)
    dataset = dataset.batch(2)
     
    # step 4: create iterator and final input tensor
    iterator = dataset.make_one_shot_iterator()
    images, labels = iterator.get_next()
    

    Then Run sess.run([images, labels]) without feeding any data through placeholders.

    Answered on December 19, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.