Pipeline 层

[源代码]

Pipeline

keras.layers.Pipeline(layers, name=None)

将一系列层应用于输入。

此类可用于构建预处理管道,特别是图像数据增强管道。与 Sequential 模型相比,Pipeline 具有一些重要区别:

  • 它不是一个 Model,而是一个普通的层。
  • 当管道中的层与 tf.data 兼容时,管道也将保持 tf.data 兼容。也就是说,在 tf.data 上下文中,管道不会尝试将其输入转换为后端原生张量(与 Sequential 模型不同)。

示例

from keras import layers
preprocessing_pipeline = layers.Pipeline([
    layers.AutoContrast(),
    layers.RandomZoom(0.2),
    layers.RandomRotation(0.2),
])

# `ds` is a tf.data.Dataset
preprocessed_ds = ds.map(
    preprocessing_pipeline,
    num_parallel_calls=4,
)