Accessing filename from file queue in Tensor Flow

前端 未结 5 1503
梦如初夏
梦如初夏 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:52

    There is tf.py_func() you could utilize to implement a mapping from file path to label.

    files = gfile.Glob(data_pattern)
    filename_queue = tf.train.string_input_producer(
    files, num_epochs=num_epochs, shuffle=True) #  list of files to read
    
    def extract_label(s):
        # path to label logic for cat&dog dataset
        return 0 if os.path.basename(str(s)).startswith('cat') else 1
    
    def read(filename_queue):
      key, value = reader.read(filename_queue)
      image = tf.image.decode_jpeg(value, channels=3)
      image = tf.cast(image, tf.float32)
      image = tf.image.resize_image_with_crop_or_pad(image, width, height)
      label = tf.cast(tf.py_func(extract_label, [key], tf.int64), tf.int32)
      label = tf.reshape(label, [])
    
    training_data = [read(filename_queue) for _ in range(num_readers)]
    
    ...
    
    tf.train.shuffle_batch_join(training_data, ...)
    

提交回复
热议问题