Keras 3 API 文档 / Layers API / 预处理层 / 图像增强层 / RandomTranslation 层

RandomTranslation 层

[源代码]

RandomTranslation

keras.layers.RandomTranslation(
    height_factor,
    width_factor,
    fill_mode="reflect",
    interpolation="bilinear",
    seed=None,
    fill_value=0.0,
    data_format=None,
    **kwargs
)

在训练期间随机平移图像的预处理层。

此层将在训练期间对每个图像应用随机平移,并根据 fill_mode 填充空白区域。

输入像素值可以是任意范围(例如 [0., 1.)[0, 255]),并且可以是整数或浮点 dtype。默认情况下,该层将输出浮点数。

注意:该层可以在 tf.datagrain 管道中使用(无论您使用的是哪个后端),且是安全的。

输入形状

3D(未批处理)或 4D(已批处理)张量,形状为:(..., height, width, channels),采用 "channels_last" 格式;或者 (..., channels, height, width),采用 "channels_first" 格式。

输出形状

3D(未批处理)或 4D(已批处理)张量,形状为:(..., target_height, target_width, channels),或者 (..., channels, target_height, target_width),采用 "channels_first" 格式。

参数

  • height_factor:表示值比例的浮点数,或表示垂直偏移下限和上限的长度为 2 的元组。负值表示向上移动图像,正值表示向下移动图像。当表示为单个正浮点数时,该值将用于上限和下限。例如,height_factor=(-0.2, 0.3) 将导致输出偏移随机范围 [-20%, +30%]height_factor=0.2 将导致输出高度偏移随机范围 [-20%, +20%]
  • width_factor:表示值比例的浮点数,或表示水平偏移下限和上限的长度为 2 的元组。负值表示向左移动图像,正值表示向右移动图像。当表示为单个正浮点数时,该值将用于上限和下限。例如,width_factor=(-0.2, 0.3) 将导致输出向左偏移 20%,并向右偏移 30%。width_factor=0.2 将导致输出高度向左或向右偏移 20%。
  • fill_mode: 输入边界之外的点根据给定的模式填充。可用方法有"constant""nearest""wrap""reflect"。默认为"constant"
    • "reflect": `(d c b a | a b c d | d c b a)` 输入通过围绕最后一个像素的边缘进行反射来扩展。
    • "constant": `(k k k k | a b c d | k k k k)` 输入通过用 `fill_value` 指定的相同常量值 k 填充边缘以外的所有值来扩展。
    • "wrap": `(a b c d | a b c d | a b c d)` 输入通过环绕到相对边缘进行扩展。
    • "nearest"(a a a a | a b c d | d d d d) 输入通过最近的像素进行扩展。请注意,在使用 torch 后端时,"reflect" 将重定向到 "mirror" (c d c b | a b c d | c b a b),因为 torch 不支持 "reflect"。请注意,torch 后端不支持 "wrap"
  • interpolation:插值模式。支持的值:"nearest""bilinear"
  • seed:整数。用于创建随机种子。
  • fill_value:一个浮点数,表示当 fill_mode="constant" 时用于填充边界之外的值。
  • data_format: string,可以是 "channels_last""channels_first"。输入的维度顺序。"channels_last" 对应输入形状为 (batch, height, width, channels),而 "channels_first" 对应输入形状为 (batch, channels, height, width)。它默认为 Keras 配置文件 ~/.keras/keras.json 中的 image_data_format 值。如果您从未设置过,则默认为 "channels_last"
  • **kwargs:基础层关键字参数,例如 namedtype