【发布时间】:2019-01-19 23:00:22
【问题描述】:
从Tensorflow CNN example 开始,我正在尝试修改模型以将多个图像作为输入(这样输入不仅有 3 个输入通道,而且通过堆叠图像获得了 3 的倍数)。 为了增加输入,我尝试使用random image operations,例如TensorFlow中提供的翻转、对比度和亮度。 我目前对所有输入图像应用相同随机失真的解决方案是对这些操作使用固定的种子值:
def distort_image(image):
flipped_image = tf.image.random_flip_left_right(image, seed=42)
contrast_image = tf.image.random_contrast(flipped_image, lower=0.2, upper=1.8, seed=43)
brightness_image = tf.image.random_brightness(contrast_image, max_delta=0.2, seed=44)
return brightness_image
在构建图时,每个图像都会多次调用此方法,因此我认为对于每个图像,它将使用相同的随机数序列,因此,它将导致我的图像输入序列具有相同的应用图像操作。
# ...
# distort images
distorted_prediction = distort_image(seq_record.prediction)
distorted_input = []
for i in xrange(INPUT_SEQ_LENGTH):
distorted_input.append(distort_image(seq_record.input[i,:,:,:]))
stacked_distorted_input = tf.concat(2, distorted_input)
# Ensure that the random shuffling has good mixing properties.
min_queue_examples = int(num_examples_per_epoch *
MIN_FRACTION_EXAMPLES_IN_QUEUE)
# Generate a batch of sequences and prediction by building up a queue of examples.
return generate_sequence_batch(stacked_distorted_input, distorted_prediction, min_queue_examples,
batch_size, shuffle=True)
理论上,这可以正常工作。在做了一些测试之后,这似乎真的解决了我的问题。但过了一会儿,我发现我有一个 race-condition,因为我使用了多线程的 CNN 示例代码的输入管道(这是 TensorFlow 中建议改进的方法性能并减少运行时的内存消耗):
def generate_sequence_batch(sequence_in, prediction, min_queue_examples,
batch_size):
num_preprocess_threads = 8 # <-- !!!
sequence_batch, prediction_batch = tf.train.shuffle_batch(
[sequence_in, prediction],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples)
return sequence_batch, prediction_batch
由于多个线程创建了我的示例,因此不再保证所有图像操作都以正确的顺序执行(在随机操作的正确顺序意义上)。
在这里,我完全陷入了困境。 有谁知道如何解决这个问题以将相同的图像失真应用于多张图像?
我的一些想法:
- 我想过围绕这些图像失真方法做一些同步,但我可以找到 TensorFlow 提供的任何东西
- 我尝试生成一个随机数,例如我自己使用 tf.random_uniform() 的随机亮度增量,并将此值用于 tf.image.adjust_contrast()。但是TensorFlow随机生成器的结果总是一个张量,我还没有找到一种方法来使用这个张量作为tf.image.adjust_contrast()的参数,它期望一个简单的float32作为它的contrast_factor 参数。
- 一种(部分)可行的解决方案是使用 tf.concat() 将所有图像组合成一个巨大的图像,应用随机操作来更改对比度和亮度,然后分割图像。但这不适用于随机翻转,因为这会(至少在我的情况下)改变图像的顺序,并且无法检测 tf.image.random_flip_left_right() 是否已执行是否翻转,必要时需要修复错误的图像顺序。
【问题讨论】:
标签: tensorflow distortion random-seed