Tensorflow错误:InvalidArgumentError:不同数量的组件类型。

时间:2016-06-11 09:40:15

标签: python tensorflow deep-learning

我想输入批量的混洗图像进行训练,然后根据the generic input images in TensorVision编写代码,但是我收到错误。我无法弄清楚错误的地方。 这是我的代码:

import os
import tensorflow as tf


def read_labeled_image_list(image_list_file):
    """
    Read a .txt file containing pathes and labeles.
    Parameters
    ----------
     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.
    Parameters
    ----------
      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)
#    example = rescale_image(example)
    # processed_label = label
    return example, label

def random_resize(image, lower_size, upper_size):
    """Randomly resizes an image
    Parameters
    ----------
    lower_size:
    upper_size:
    Returns
    -------
      a randomly resized image
    """

    new_size = tf.to_int32(
        tf.random_uniform([], lower_size, upper_size))

    return tf.image.resize_images(image, new_size, new_size,
                                  method=0)
def _input_pipeline(filename, batch_size,
                    processing_image=lambda x: x,
                    processing_label=lambda y: y,
                    num_epochs=None):
    """The input pipeline for reading images classification data.
    The data should be stored in a single text file of using the format:
     /path/to/image_0 label_0
     /path/to/image_1 label_1
     /path/to/image_2 label_2
     ...
     Args:
       filename: the path to the txt file
       batch_size: size of batches produced
       num_epochs: optionally limited the amount of epochs
    Returns:
       List with all filenames in file image_list_file
    """

    # Reads pfathes of images together with there labels
    image_list, label_list = read_labeled_image_list(filename)

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

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

    # Reads the actual images from
    image, label = read_images_from_disk(input_queue)
    pr_image = processing_image(image)
    pr_label = processing_label(label)

    image_batch, label_batch = tf.train.batch([pr_image, pr_label],
                                              batch_size=batch_size, 
                                              shapes = [256,256,3])

    # Display the training images in the visualizer.
    tensor_name = image.op.name
    tf.image_summary(tensor_name + 'images', image_batch)
    return image_batch, label_batch
def test_pipeline():
    data_folder = '/home/kang/Documents/work_code_PC1/data/UCLandUsedImages/'
    data_file = 'UCImage_Labels.txt'

    filename = os.path.join(data_folder, data_file)

    image_batch, label_batch = _input_pipeline(filename, 75)

    # Create the graph, etc.
    init_op = tf.initialize_all_variables()
    sess = tf.InteractiveSession()
    sess.run(init_op)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    a = sess.run([image_batch, label_batch])

    coord.request_stop()
    coord.join(threads)
    print("Finish Test")
    return a

if __name__ == '__main__':
#    aa = test_preprocc()
#    matplotlib.pyplot.imshow(aa[1])
    a1 = test_pipeline()
    a2 = test_pipeline()

但是它出错了,它让我困惑了很长时间:

Traceback (most recent call last):

  File "<ipython-input-7-e24901ce3365>", line 1, in <module>
    runfile('/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt1.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/readUClandUsedImagetxt1.py", line 254, in <module>
    a1 = test_pipeline()

  File "/home/kang/Documents/work_code_PC1/VGG_tensorflow_UCMerced/readUClandUsedImagetxt1.py", line 244, in test_pipeline
    a = sess.run([image_batch, label_batch])

  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 564, in _run
    feed_dict_string, options, run_metadata)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 637, in _do_run
    target_list, options, run_metadata)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 659, in _do_call
    e.code)

InvalidArgumentError: Different number of component types.  Types: uint8, int32, Shapes: [[256,256,3]]
     [[Node: batch_11/fifo_queue = FIFOQueue[capacity=32, component_types=[DT_UINT8, DT_INT32], container="", shapes=[[256,256,3]], shared_name="", _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'batch_11/fifo_queue', defined at:

1 个答案:

答案 0 :(得分:0)

错误是由函数tf.train.batch的错误参数shapes引起的。参数shapes应该保留为默认值,或者应该是:

  

shapes :(可选)每个示例的形状。默认为tensor_list

的推断形状

在这里您要shapes = [256, 256, 3],但是您应该在列表中指定pr_imagepr_label的形状:

image_batch, label_batch = tf.train.batch(
    [pr_image, pr_label],
    batch_size=batch_size,
    shapes = [[256,256,3], pr_label.get_shape()])