Search code examples
pythontensorflowtensorflow-datasets

How can I access the filenames gathered by tf.data.Dataset.list_files()?


I am using

file_data = tf.data.Dataset.list_files("../*.png")

to collect image files for training in TensorFlow, but would like to access the list of gathered filenames so I can perform a label lookup.

Calling sess.run([file_data]) has been unsuccessful:

TypeError: Fetch argument <TensorSliceDataset shapes: (), types: tf.string> has invalid type <class 'tensorflow.python.data.ops.dataset_ops.TensorSliceDataset'>, must be a string or Tensor. (Can not convert a TensorSliceDataset into a Tensor or Operation.)

Are there any other methods I can use?


Solution

  • With some additional experimenting, I found a way to solve this:

    First, turn the Dataset into an iterator:

    iterator_helper = file_data.make_one_shot_iterator()
    

    Then, iterate through the elements in a tf Session:

    with tf.Session() as sess:
        filename_temp = iterator_helper.get_next()
        print(sess.run[filename_temp])