如何在 tensorflow tf.data.Dataset 中使用 cv2 图像增强函数?

How to use a cv2 image augmentation function with tensorflow tf.data.Dataset?

我正在使用 tf.data.Dataset 创建我的数据集并使用 keras 训练 CNN。我需要在图像上应用蒙版,蒙版取决于图像的形状,没有预定义的像素坐标。

在网上寻找答案时,我发现在 TensorFlow 中有两种访问图像形状的方法(在训练时间):

  1. 使用 eager execution(在我的情况下默认情况下未启用,我使用的是 tf v 12.0)

  2. 使用会话

我不想使用急切执行,因为它会减慢训练速度,并且不能使用会话,因为我使用 Keras 训练和测试 CNN(我使用 [=11 的迭代器将数据提供给 model.train() =]).

因此,我无法知道图像的形状,因此无法访问特定像素以进行数据扩充。

我使用 OpenCV (cv2) 编写了一个应用掩码的函数。有没有办法将其与 TensorFlow 数据管道集成?

EDIT :我找到了解决方案。我用 tf.py_func 包装了 python 函数

注意:由于您需要图像增强,我想提供一些关于各种图像增强库的信息。这不会向您展示如何将 OpenCV 函数添加到您的 tfdata 管道中。但是,如果您的要求足够标准,您可以使用其中之一:

  • tf.keras.preprocessing.image.ImageDataGenerator
  • imaug
  • albumentations

Python

中的数据增强
  1. 套餐:albumentations
    图书馆:外部
    url: Python albumentations library

  2. 包装:imaug:star:
    图书馆:外部
    url: Python imaug library

  3. 套餐:tf.keras.preprocessing.image.ImageDataGenerator
    图书馆:外部
    url: Pyhon - TensorFlow ImageDataGenerator library

例子

  1. albumentations 的示例/使用。

  2. imaug 的示例/使用。

  3. tf.keras.preprocessing.image.ImageDataGenerator 的示例/使用。

您可以使用 map 来转换数据集的元素。然后,您可以使用 tf.py_function 将 cv2 函数包装到急切执行的 tf op 中。在 tensorflow 1.x 中,您可以使用 tf.py_func 但行为有点不同。有关详细信息,请参阅 tf.py_function 文档。

因此,在 TF-2.x 中它看起来像:

def cv2_func(image, label):
    # your code goes here

def tf_cv2_func(image, label):
    [image, label] = tf.py_function(cv2_func, [image, label], [tf.float32, tf.float64])
    return image, label

train_ds = train_ds.shuffle(BUFFER_SIZE).map(tf_cv2_func).batch(BATCH_SIZE)