KerasHub: 预训练模型 / API 文档 / 模型架构 / ViT / ViTImageClassifierPreprocessor 层

ViTImageClassifierPreprocessor 层

[源]

ViTImageClassifierPreprocessor

keras_hub.models.ViTImageClassifierPreprocessor(image_converter=None, **kwargs)

图像分类预处理层的基类。

ImageClassifierPreprocessor 任务包装了一个 keras_hub.layers.ImageConverter,以创建一个用于图像分类任务的预处理层。它旨在与 keras_hub.models.ImageClassifier 任务配对使用。

所有 ImageClassifierPreprocessor 都接受三个输入:xysample_weightx 作为第一个输入,必须始终包含。它可以是一张图像或一批图像。请参阅下面的示例。ysample_weight 是可选输入,将直接按原样传递。通常,y 将是分类标签,而 sample_weight 不会提供。

如果提供了标签,该层将输出 x 或一个 (x, y) 元组;如果提供了标签和样本权重,则输出一个 (x, y, sample_weight) 元组。x 将是应用所有模型预处理后的输入图像。

所有 ImageClassifierPreprocessor 任务都包含一个 from_preset() 构造函数,可用于加载预训练的配置和词汇表。您可以直接在此基类上调用 from_preset() 构造函数,在这种情况下,将自动实例化适用于您的模型的正确类。

示例。

preprocessor = keras_hub.models.ImageClassifierPreprocessor.from_preset(
    "resnet_50",
)

# Resize a single image for resnet 50.
x = np.random.randint(0, 256, (512, 512, 3))
x = preprocessor(x)

# Resize a labeled image.
x, y = np.random.randint(0, 256, (512, 512, 3)), 1
x, y = preprocessor(x, y)

# Resize a batch of labeled images.
x, y = [
    np.random.randint(0, 256, (512, 512, 3)),
    np.zeros((512, 512, 3))
], [1, 0]
x, y = preprocessor(x, y)

# Use a [`tf.data.Dataset`](https://tensorflowcn.cn/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(2)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[源]

from_preset 方法

ViTImageClassifierPreprocessor.from_preset(
    preset, config_file="preprocessor.json", **kwargs
)

从模型预设实例化一个 keras_hub.models.Preprocessor

预设是一个包含配置、权重和其他文件资产的目录,用于保存和加载预训练模型。preset 可以以下列方式之一传入:

  1. 内置预设标识符,例如 'bert_base_en'
  2. Kaggle Models 句柄,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 句柄,例如 'hf://user/bert_base_en'
  4. 本地预设目录的路径,例如 './bert_base_en'

对于任何 Preprocessor 子类,您都可以运行 cls.presets.keys() 来列出该类上所有可用的内置预设。

由于给定模型通常有多个预处理类,因此此方法应在特定的子类上调用,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()

参数

  • preset: 字符串。内置预设标识符、Kaggle Models 句柄、Hugging Face 句柄或本地目录的路径。

示例

# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
预设 参数 描述
vit_base_patch16_224_imagenet 85.80M ViT-B16 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 224x224
vit_base_patch16_224_imagenet21k 85.80M ViT-B16 主干网络在 ImageNet 21k 数据集上预训练,图像分辨率为 224x224
vit_base_patch16_384_imagenet 86.09M ViT-B16 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 384x384
vit_base_patch32_224_imagenet21k 87.46M ViT-B32 主干网络在 ImageNet 21k 数据集上预训练,图像分辨率为 224x224
vit_base_patch32_384_imagenet 87.53M ViT-B32 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 384x384
vit_large_patch16_224_imagenet 303.30M ViT-L16 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 224x224
vit_large_patch16_224_imagenet21k 303.30M ViT-L16 主干网络在 ImageNet 21k 数据集上预训练,图像分辨率为 224x224
vit_large_patch16_384_imagenet 303.69M ViT-L16 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 384x384
vit_large_patch32_224_imagenet21k 305.51M ViT-L32 主干网络在 ImageNet 21k 数据集上预训练,图像分辨率为 224x224
vit_large_patch32_384_imagenet 305.61M ViT-L32 模型在 ImageNet 1k 数据集上预训练,图像分辨率为 384x384
vit_huge_patch14_224_imagenet21k 630.76M ViT-H14 主干网络在 ImageNet 21k 数据集上预训练,图像分辨率为 224x224

image_converter 属性

keras_hub.models.ViTImageClassifierPreprocessor.image_converter

用于预处理图像数据的图像转换器。