我目前正在尝试实现自己的功能来在tensorflow
. 为此,我需要对输入图像应用随机图像变换,并对输出掩码应用相关变换(即旋转和翻转)。但是,图像翻转/反转无法正常工作。我目前拥有的代码示例如下:
def paired_data_augmentation(image_masks_tuple):
#the image_masks_tuple contains an image as its first
#and masks as the remaining elements
def flipper(image_masks,mode = 'lr'):
image = image_masks[0]
masks = image_masks[1:]
if mode == 'lr':
image_masks = [
tf.reverse(m,[0]) for m in image_masks]
elif mode == 'ud':
image_masks = [
tf.reverse(m,[1]) for m in image_masks]
image = image_masks[0]
masks = image_masks[1:]
return [image,*new_masks]
image = image_masks_tuple[0]
masks = image_masks_tuple[1:]
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
flip_lr = tf.random_uniform([])
flip_ud = tf.random_uniform([])
tmp = [image,*masks]
tmp = tf.cond(
tf.greater_equal(flip_lr,0.5),
lambda: flipper(tmp,mode = 'lr'),
lambda: tmp)
set_shape(tmp)
tmp = tf.cond(
tf.greater_equal(flip_ud,0.5),
lambda: flipper(tmp,mode = 'ud'),
lambda: tmp)
set_shape(tmp)
return tmp
我期望这样做是以相同的方式翻转所有图像/蒙版 ifflip_lr > 0.5
或flip_ud > 0.5
,但实际发生的是某些图像将被翻转而没有翻转蒙版和 v/v。有没有人经历过这个并且知道如何解决它?
提前致谢