bsa*_*ter 6 distortion random-seed tensorflow
从Tensorflow CNN示例开始,我试图修改模型以将多个图像作为输入(这样输入不仅仅有3个输入通道,而是通过堆叠图像的3倍).为了增加输入,我尝试使用随机图像操作,例如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
Run Code Online (Sandbox Code Playgroud)
在图形构造时,每个图像都会多次调用此方法,因此我认为对于每个图像,它将使用相同的随机数序列,因此,它将导致对我的图像输入序列具有相同的应用图像操作.
# ...
# 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)
Run Code Online (Sandbox Code Playgroud)
从理论上讲,这很好.经过一些测试后,这似乎解决了我的问题.但过了一段时间,我发现我有一个竞争条件,因为我使用带有多个线程的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
Run Code Online (Sandbox Code Playgroud)
因为多个线程创建了我的示例,所以不能保证所有图像操作都以正确的顺序执行(在随机操作的正确顺序意义上).
在这里,我完全陷入了困境.有谁知道如何解决这个问题,将相同的图像失真应用于多个图像?
我的一些想法:
以下是通过查看tensorflow中random_flip_up_down和random_flip_left_right的代码得出的结果:
def image_distortions(image, distortions):
distort_left_right_random = distortions[0]
mirror = tf.less(tf.pack([1.0, distort_left_right_random, 1.0]), 0.5)
image = tf.reverse(image, mirror)
distort_up_down_random = distortions[1]
mirror = tf.less(tf.pack([distort_up_down_random, 1.0, 1.0]), 0.5)
image = tf.reverse(image, mirror)
return image
distortions = tf.random_uniform([2], 0, 1.0, dtype=tf.float32)
image = image_distortions(image, distortions)
label = image_distortions(label, distortions)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4366 次 |
| 最近记录: |