def flip_with_bboxes(image, bboxes):
uniform_random = tf.random_uniform([], 0, 1.0)
mirror_cond = tf.less(uniform_random, .5)
stride = tf.where(mirror_cond, -1, 1)
def flip(image, bboxes, stride):
image = image[:, ::stride, :]
img_w = tf.cast(tf.shape(image)[1], dtype=tf.float32)
bbox_coords = tf.unstack(bboxes, num=4, axis=1)
y_min = bbox_coords[0]
x_min = bbox_coords[1]
y_max = bbox_coords[2]
x_max = bbox_coords[3]
x_min_flip = img_w - x_max
x_max_flip = img_w - x_min
bboxes = tf.stack([y_min, x_min_flip, y_max, x_max_flip], 1, name='flip_bboxes')
return image, bboxes
def not_flip(image, bboxes):
return image, bboxes
image_fliped, bboxes = tf.cond(mirror_cond, lambda: flip(image, bboxes, stride), lambda: not_flip(image, bboxes))
return tf_image.fix_image_flip_shape(image, image_fliped), bboxes
评论列表
文章目录