【问题标题】:tensorflow input pipeline returns multiple valuestensorflow 输入管道返回多个值
【发布时间】:2017-11-16 03:33:58
【问题描述】:

我正在尝试在 tensorflow 中创建用于图像分类的输入管道,因此我想制作批量图像和相应的标签。 Tensorflow 文档建议我们可以使用 tf.train.batch 进行批量输入:

train_batch, train_label_batch = tf.train.batch(
[train_image, train_image_label],
batch_size=batch_size,
num_threads=1,
capacity=10*batch_size,
enqueue_many=False,
shapes=[[224,224,3], [len(labels),]],
allow_smaller_final_batch=True
)

但是,我想如果我像这样输入图表会不会有问题:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=Model(train_batch)))

问题是成本函数中的操作是使图像及其对应的标签出队,还是分别返回它们?因此导致使用错误的图像和标签进行训练。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您需要考虑几件事情来保持图像和标签的顺序。

    假设我们需要一个为我们提供图像和标签的函数。

    def _get_test_images(_train=False):
    
    
    """
    Gets the test images and labels as a batch
    
    Inputs:
    ======
    _train      : Boolean if images are from training set
    random_crop     : Boolean if random cropping is allowed
    random_flip         : Boolean if random horizontal flip is allowed
    distortion      : Boolean if distortions are allowed
    
    Outputs:
    ========
    images_batch    : Batch of images containing BATCH_SIZE images at a time
    label_batch     : Batch of labels corresponding to the images in images_batch
    idx         : Batch of indexes of images
    """
    
    #get images and labels
    _,_img_names,_img_class,index= _get_list(_train = _train)
    
    #total number of distinct images used for train will be equal to the images
    #fed in tf.train.slice_input_producer as _img_names
    
    img_path,label,idx = tf.train.slice_input_producer([_img_names,_img_class,index],shuffle=False)
    
    img_path,label,idx = tf.convert_to_tensor(img_path),tf.convert_to_tensor(label),tf.convert_to_tensor(idx)
    img_path = tf.cast(img_path,dtype=tf.string)
    
    #read file 
    image_file = tf.read_file(img_path)
    
    #decode jpeg/png/bmp
    #tf.image.decode_image won't give shape out. So it will give error while resizing
    image = tf.image.decode_jpeg(image_file)
    
    #image preprocessing
    image = tf.image.resize_images(image, [IMG_DIM,IMG_DIM])
    
    float_image = tf.cast(image,dtype=tf.float32)
    
    #subtracting mean and divide by standard deviation
    float_image = tf.image.per_image_standardization(float_image)
    
    #set the shape
    float_image.set_shape(IMG_SIZE)
    labels_original = tf.cast(label,dtype=tf.int32)
    img_index = tf.cast(idx,dtype=tf.int32)
    
    #parameters for shuffle
    batch_size = BATCH_SIZE
    min_fraction_of_examples_in_queue = 0.3
    num_preprocess_threads = 1
    num_examples_per_epoch = MAX_TEST_EXAMPLE
    min_queue_examples = int(num_examples_per_epoch *
                           min_fraction_of_examples_in_queue)
    
    images_batch, label_batch,idx = tf.train.batch(
            [float_image,label,img_index],
            batch_size=batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * batch_size)
    
    # Display the training images in the visualizer.
    tf.summary.image('images', images_batch)
    
    return images_batch, label_batch,idx
    

    在这里,tf.train.slice_input_producer([_img_names,_img_class,index],shuffle=False) 是一件有趣的事情,如果你把 shuffle=True 放在哪里,它会协调所有三个数组。

    第二件事是num_preprocess_threads。只要您使用单线程进行出队操作,批处理将以确定的方式出现。但是不止一个线程会随机打乱数组。例如,对于图像 0001.jpg,如果 True 标签为 1,您可能会得到 2 或 4。一旦出队,它就是张量形式。 tf.nn.softmax_cross_entropy_with_logits 这样的张量应该没有问题。

    【讨论】:

      猜你喜欢
      • 2017-06-29
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2017-04-25
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多