Keras 3 API 文档 / KerasCV / 模型 / 主干网络 / ResNetV2 主干网络

ResNetV2 主干网络

[源代码]

ResNetV2Backbone

keras_cv.models.ResNetV2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

实例化 ResNetV2 架构。

参考文献

Resnet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • stackwise_filters:整数列表,模型中每个堆栈的过滤器数量。
  • stackwise_blocks:整数列表,模型中每个堆栈的块数量。
  • stackwise_strides:整数列表,模型中每个堆栈的步长。
  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • stackwise_dilations:整数列表,模型中每个堆栈的扩张率。如果为None(默认值),则不使用扩张率。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。
  • block_type:字符串,"basic_block" 或 "block" 之一。要堆叠的块类型。对于较小的模型(如 ResNet18 和 ResNet34),请使用 "basic_block"。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Pretrained backbone
model = keras_cv.models.ResNetV2Backbone.from_preset("resnet50_v2_imagenet")
output = model(input_data)

# Randomly initialized backbone with a custom config
model = ResNetV2Backbone(
    stackwise_filters=[64, 128, 256, 512],
    stackwise_blocks=[2, 2, 2, 2],
    stackwise_strides=[1, 2, 2, 2],
    include_rescaling=False,
)
output = model(input_data)

[源代码]

from_preset 方法

ResNetV2Backbone.from_preset()

从预设配置和权重实例化 ResNetV2Backbone 模型。

参数

  • preset:字符串。必须是 "resnet18_v2"、"resnet34_v2"、"resnet50_v2"、"resnet101_v2"、"resnet152_v2"、"resnet50_v2_imagenet" 之一。如果正在寻找具有预训练权重的预设,请选择 "resnet50_v2_imagenet" 之一。
  • load_weights:是否将预训练权重加载到模型中。默认为None,遵循预设是否提供预训练权重。

示例

# Load architecture and weights from preset
model = keras_cv.models.ResNetV2Backbone.from_preset(
    "resnet50_v2_imagenet",
)

# Load randomly initialized model from preset architecture with weights
model = keras_cv.models.ResNetV2Backbone.from_preset(
    "resnet50_v2_imagenet",
    load_weights=False,
预设名称 参数 描述
resnet18_v2 11.18M 具有 18 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。
resnet34_v2 21.30M 具有 34 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。
resnet50_v2 23.56M 具有 50 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。
resnet101_v2 42.63M 具有 101 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。
resnet152_v2 58.33M 具有 152 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。
resnet50_v2_imagenet 23.56M 具有 50 层的 ResNet 模型,其中批标准化和 ReLU 激活在卷积层之前(v2 样式)。在 ImageNet 2012 分类任务上训练。

[源代码]

ResNet18V2Backbone

keras_cv.models.ResNet18V2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

具有 18 层的 ResNetV2Backbone 模型。

参考文献

ResNet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone
model = ResNet18V2Backbone()
output = model(input_data)

[源代码]

ResNet34V2Backbone

keras_cv.models.ResNet34V2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

具有 34 层的 ResNetV2Backbone 模型。

参考文献

ResNet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone
model = ResNet34V2Backbone()
output = model(input_data)

[源代码]

ResNet50V2Backbone

keras_cv.models.ResNet50V2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

具有 50 层的 ResNetV2Backbone 模型。

参考文献

ResNet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone
model = ResNet50V2Backbone()
output = model(input_data)

[源代码]

ResNet101V2Backbone

keras_cv.models.ResNet101V2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

具有 101 层的 ResNetV2Backbone 模型。

参考文献

ResNet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone
model = ResNet101V2Backbone()
output = model(input_data)

[源代码]

ResNet152V2Backbone

keras_cv.models.ResNet152V2Backbone(
    stackwise_filters,
    stackwise_blocks,
    stackwise_strides,
    include_rescaling,
    stackwise_dilations=None,
    input_shape=(None, None, 3),
    input_tensor=None,
    block_type="block",
    **kwargs
)

具有 152 层的 ResNetV2Backbone 模型。

参考文献

ResNet 和 ResNetV2 的区别在于它们各自构建块的结构。在 ResNetV2 中,批标准化和 ReLU 激活在卷积层之前,而 ResNetV1 中的批标准化和 ReLU 激活则应用于卷积层之后。

对于迁移学习用例,请务必阅读迁移学习和微调指南

参数

  • include_rescaling:布尔值,是否重新缩放输入。如果设置为True,则输入将通过Rescaling(1/255.0)层。
  • input_shape:可选的形状元组,默认为 (None, None, 3)。
  • input_tensor:可选的 Keras 张量(即layers.Input()的输出),用作模型的图像输入。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone
model = ResNet152V2Backbone()
output = model(input_data)