0

I want to make a generic input images with labels in tensorflow. I checked the question Tensorflow read images with labels and write my test code, but there are some errors.

    import tensorflow as tf

    from tensorflow.python.framework import ops
    from tensorflow.python.framework import dtypes

    txtfilename = '/home/kang/Documents/work_code_PC1/data/UCLandUsedImages/UCImage_Labels.txt'

    def read_labeled_image_list(image_list_file):
        """Reads a .txt file containing pathes and labeles
        Args:
           image_list_file: a .txt file with one /path/to/image per line
           label: optionally, if set label will be pasted after each line
        Returns:
           List with all filenames in file image_list_file
        """
        f = open(image_list_file, 'r')
        filenames = []
        labels = []
        for line in f:
            filename, label = line[:-1].split(' ')
            filenames.append(filename)
            labels.append(int(label))
        return filenames, labels

    def read_images_from_disk(input_queue):
        """Consumes a single filename and label as a ' '-delimited string.
        Args:
          filename_and_label_tensor: A scalar string tensor.
        Returns:
          Two tensors: the decoded image, and the string label.
        """
        label = input_queue[1]
        file_contents = tf.read_file(input_queue[0])
        example = tf.image.decode_png(file_contents, channels=3)
        return example, label


    # Reads pfathes of images together with their labels
    image_list, label_list = read_labeled_image_list(txtfilename)

    images = ops.convert_to_tensor(image_list, dtype=dtypes.string)
    labels = ops.convert_to_tensor(label_list, dtype=dtypes.int32)

    # Makes an input queue
    input_queue = tf.train.slice_input_producer([images, labels],
                                                shuffle=True)

    with tf.Session() as sess:
        result = sess.run([input_queue])

I want to run the session and have a look at the input_queue, but I got the following error:

  File "<ipython-input-3-ddf11f8f84bc>", line 1, in <module>
    runfile('/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt.py', wdir='/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced')

  File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
    builtins.execfile(filename, *where)

  File "/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt.py", line 69, in <module>
    result = sess.run([input_queue])

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 340, in run
    run_metadata_ptr)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 523, in _run
    processed_fetches = self._process_fetches(fetches)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 493, in _process_fetches
    % (subfetch, fetch, type(subfetch), str(e)))

TypeError: Fetch argument [<tf.Tensor 'input_producer_2/Gather:0' shape=() dtype=string>, <tf.Tensor 'input_producer_2/Gather_1:0' shape=() dtype=int32>] of [<tf.Tensor 'input_producer_2/Gather:0' shape=() dtype=string>, <tf.Tensor 'input_producer_2/Gather_1:0' shape=() dtype=int32>] has invalid type <type 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.)

I checked the value of image_list, label_list, the outputs are right, so what is the problem here? Thank you very much.

Community
  • 1
  • 1
karl_TUM
  • 5,769
  • 10
  • 24
  • 41

1 Answers1

0

I would try removing the square brackets around input_queue in result = sess.run([input_queue]), since you want to specify the output feed there as a single tensor, not a list.

Pete Warden
  • 2,866
  • 1
  • 13
  • 12