Accessing filename from file queue in Tensor Flow

前端 未结 5 1464
梦如初夏
梦如初夏 2021-02-20 10:11

I have a directory of images, and a separate file matching image filenames to labels. So the directory of images has files like \'train/001.jpg\' and the labeling file looks lik

5条回答
  •  伪装坚强ぢ
    2021-02-20 10:34

    Here's what I was able to do.

    I first shuffled the filenames and matched the labels to them in Python:

    np.random.shuffle(filenames)
    labels = [label_dict[f] for f in filenames]
    

    Then created a string_input_producer for the filenames with shuffle off, and a FIFO for labels:

    lv = tf.constant(labels)
    label_fifo = tf.FIFOQueue(len(filenames),tf.int32, shapes=[[]])
    file_fifo = tf.train.string_input_producer(filenames, shuffle=False, capacity=len(filenames))
    label_enqueue = label_fifo.enqueue_many([lv])
    

    Then to read the image I could use a WholeFileReader and to get the label I could dequeue the fifo:

    reader = tf.WholeFileReader()
    image = tf.image.decode_jpeg(value, channels=3)
    image.set_shape([128,128,3])
    result.uint8image = image
    result.label = label_fifo.dequeue()
    

    And generate the batches as follows:

    min_fraction_of_examples_in_queue = 0.4
    min_queue_examples = int(num_examples_per_epoch *
                             min_fraction_of_examples_in_queue)
    num_preprocess_threads = 16
    images, label_batch = tf.train.shuffle_batch(
      [result.uint8image, result.label],
      batch_size=FLAGS.batch_size,
      num_threads=num_preprocess_threads,
      capacity=min_queue_examples + 3 * FLAGS.batch_size,
      min_after_dequeue=min_queue_examples)
    

提交回复
热议问题