def flip_randomly(inputs, horizontally, vertically, is_training, name=None):
"""Flip images randomly. Make separate flipping decision for each image.
Args:
inputs (4-D tensor): Input images (batch size, height, width, channels).
horizontally (bool): If True, flip horizontally with 50% probability. Otherwise, don't.
vertically (bool): If True, flip vertically with 50% probability. Otherwise, don't.
is_training (bool): If False, no flip is performed.
scope: A name for the operation.
"""
with tf.name_scope(name, "flip_randomly") as scope:
batch_size, height, width, _ = tf.unstack(tf.shape(inputs))
vertical_choices = (tf.random_uniform([batch_size], 0, 2, tf.int32) *
tf.to_int32(vertically) *
tf.to_int32(is_training))
horizontal_choices = (tf.random_uniform([batch_size], 0, 2, tf.int32) *
tf.to_int32(horizontally) *
tf.to_int32(is_training))
vertically_flipped = tf.reverse_sequence(inputs, vertical_choices * height, 1)
both_flipped = tf.reverse_sequence(vertically_flipped, horizontal_choices * width, 2)
return tf.identity(both_flipped, name=scope)
评论列表
文章目录