Tensorflow中如何实现数据增强?

4
基于Tensorflow ConvNet教程,有些点对我来说不是很明显:
  • 这些被扭曲的图像实际上被添加到原始图像池中吗?
  • 还是用扭曲后的图像替换原始图像?
  • 产生了多少扭曲图像?(即定义了什么样的增强因子?)

教程的函数流似乎如下:

cifar_10_train.py

def train
    """Train CIFAR-10 for a number of steps."""
    with tf.Graph().as_default():
        [...]
        # Get images and labels for CIFAR-10.
        images, labels = cifar10.distorted_inputs()
        [...]

cifar10.py

def distorted_inputs():
    """Construct distorted input for CIFAR training using the Reader ops.

    Returns:
      images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
      labels: Labels. 1D tensor of [batch_size] size.

    Raises:
      ValueError: If no data_dir
    """
    if not FLAGS.data_dir:
        raise ValueError('Please supply a data_dir')
    data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin')
    return cifar10_input.distorted_inputs(data_dir=data_dir,
                                          batch_size=FLAGS.batch_size)

最后是 cifar10_input.py

def distorted_inputs(data_dir, batch_size):
    """Construct distorted input for CIFAR training using the Reader ops.

    Args:
    data_dir: Path to the CIFAR-10 data directory.
    batch_size: Number of images per batch.

    Returns:
    images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
    labels: Labels. 1D tensor of [batch_size] size.
    """
    filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in xrange(1, 6)]
    for f in filenames:
        if not tf.gfile.Exists(f):
            raise ValueError('Failed to find file: ' + f)

    # Create a queue that produces the filenames to read.
    filename_queue = tf.train.string_input_producer(filenames)

    # Read examples from files in the filename queue.
    read_input = read_cifar10(filename_queue)
    reshaped_image = tf.cast(read_input.uint8image, tf.float32)

    height = IMAGE_SIZE
    width = IMAGE_SIZE

    # Image processing for training the network. Note the many random
    # distortions applied to the image.

    # Randomly crop a [height, width] section of the image.
    distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

    # Randomly flip the image horizontally.
    distorted_image = tf.image.random_flip_left_right(distorted_image)

    # Because these operations are not commutative, consider randomizing
    # the order their operation.
    distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
    distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)

    # Subtract off the mean and divide by the variance of the pixels.
    float_image = tf.image.per_image_whitening(distorted_image)

    # Ensure that the random shuffling has good mixing properties.
    min_fraction_of_examples_in_queue = 0.4
    min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN *
                             min_fraction_of_examples_in_queue)
    print('Filling queue with %d CIFAR images before starting to train.'
          'This will take a few minutes.' % min_queue_examples)

    # Generate a batch of images and labels by building up a queue of examples.
    return _generate_image_and_label_batch(float_image, read_input.label,
                                           min_queue_examples, batch_size,
                                           shuffle=True)
1个回答

10
这些被扭曲的图像是否被添加到原始图像池中?
这要取决于"pool"的定义。在Tensorflow中,您拥有在网络图中的基本对象 "ops"。在这里,数据生成本身就是一个 "op"。因此,您没有固定的训练样本集,而是从训练集中生成的可能无限的样本集。
或者这些扭曲的图像是用来代替原始图像的吗?
正如您所包含的源代码所示-样本是从训练批次中取出的,然后随机变换,因此很少使用未改变的图像(特别是使用了裁剪,它总是修改图像)。
有多少扭曲的图像被生成了?(即定义了什么增强因子?)
没有这样的事情,这是永无止境的过程。将其视为对可能无限的数据源进行随机访问,并且这正是在此处高效发生的。每个批次都可以与前一个不同。

谢谢 @lejlot 的见解 -- 我很难理解这个问题。问题是:您是否意味着通过扭曲我们可以回到训练池,假设我们可以拿取相同的一批图像,但由于随机扭曲这些情况永远不会完全相同,因此它们作为“新”情况?如果是这样,那么这就是如何从有限的数据集中请求100k或更多次迭代而少担心过度拟合的方法? - pepe
2
基本上是的。您可以获得相同的“原始”数据,但在将其传递到网络之前,它会以随机方式被扭曲,因此网络实际上永远不会看到两个样本重复出现。 - lejlot
@lejlot,所以每次我们开始训练时,网络基本上会在不同的图像训练集上进行训练,因为每次训练集都会以不同的方式“增强”,对吗? - Konrad
@Konrad 在某种程度上是的,如果变换足够“丰富”,则两次运行生成的图像将不完全相同。 - lejlot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接