如何使TensorFlow map()函数返回多个值?

问题描述 投票:1回答:1

我正在尝试编写一个可增强数据集中图像的功能。我能够成功扩充现有图像并将其返回,但是我希望能够对单个图像进行多次扩充,然后分别返回这些扩充后的图像,然后将其添加到原始数据集中。

扩展功能:

def augment_data(image, label):

h_flipped_image = tf.image.flip_left_right(image)
v_flipped_image = tf.image.flip_up_down(image)

return h_flipped_image, label

地图功能:

train_ds = train_ds.map(augment_data)

train_ds是具有以下形状的tf.data数据集:

<PrefetchDataset shapes: ((None, 224, 224, 3), (None, 238)), types: (tf.float32, tf.bool)>

[如何使map函数以这样的方式返回多个值,例如,我可以将h_flipped_image和v_flipped_image以及它们都返回给train_ds数据集?

python tensorflow data-augmentation
1个回答
0
投票

事实证明,我不是从正确的方向看问题。我意识到我不需要在al之后将增强样本包含在数据集中。相反,我选择在培训过程中选择扩充数据。

由于训练过程将花费多个时间,因此我可以在网络需要图像之前就对其进行放大。我是通过修改我的expand_data函数来完成的,因此它现在有随机的机会执行某种扩充。在每个时期,将对图像执行增强的随机组合,从而每次为网络生成不同的输入图像。

def augment_data(image, label):

rand = tf.random.uniform([])
if(rand > 0.5):
    image = tf.image.flip_up_down(image)

# Additional augmented techniques should be defined here

return image, label

请确保您使用TensorFlow函数生成随机数。由于TensorFlow解释python代码的方式,一个简单的random.random()将不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.