DeiTImageConverter 类keras_hub.layers.DeiTImageConverter(
image_size=None,
scale=None,
offset=None,
crop_to_aspect_ratio=True,
pad_to_aspect_ratio=False,
interpolation="bilinear",
antialias=False,
bounding_box_format="yxyx",
data_format=None,
**kwargs
)
将原始图像预处理为适合模型的输入。
该类将原始图像转换为适合模型的输入。此转换按以下步骤进行:
该层将接收一个 channels last 或 channels first 格式的原始图像张量作为输入,并输出一个用于建模的预处理图像输入。该张量可以是批处理的(秩为 4),也可以是非批处理的(秩为 3)。
此层可与 from_preset() 构造函数一起使用,以加载用于对特定预训练模型进行图像缩放和重塑的层。这样使用该层可以编写无需在模型检查点之间切换时更新的预处理代码。
参数
None。要应用于输入的缩放因子。如果 scale 是单个浮点数,则整个输入将乘以 scale。如果 scale 是元组,则假定其包含每个通道的缩放值,与输入图像的每个通道相乘。如果 scale 是 None,则不应用缩放。None。要应用于输入的偏移量。如果 offset 是单个浮点数,则整个输入将与 offset 相加。如果 offset 是元组,则假定其包含每个通道的偏移值,与输入图像的每个通道相加。如果 offset 是 None,则不应用偏移。True,则在不扭曲纵横比的情况下重塑图像。当原始纵横比与目标纵横比不同时,将裁剪输出图像,以便返回图像中(大小为 (height, width))与目标纵横比匹配的最大可能窗口。默认情况下(crop_to_aspect_ratio=False),可能不保留纵横比。"xyxy"、"rel_xyxy"、"xywh"、"center_xywh"、"yxyx"、"rel_yxyx" 中的一种。指定将与图像一起重塑到 image_size 的边界框的格式。要将边界框传递给此层,请在调用该层时传递一个包含 "images" 和 "bounding_boxes" 键的字典。"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"。示例
# Resize raw images and scale them to [0, 1].
converter = keras_hub.layers.ImageConverter(
image_size=(128, 128),
scale=1. / 255,
)
converter(np.random.randint(0, 256, size=(2, 512, 512, 3)))
# Resize images to the specific size needed for a PaliGemma preset.
converter = keras_hub.layers.ImageConverter.from_preset(
"pali_gemma_3b_224"
)
converter(np.random.randint(0, 256, size=(2, 512, 512, 3)))
from_preset 方法DeiTImageConverter.from_preset(preset, **kwargs)
从模型预设实例化一个 `keras_hub.layers.ImageConverter`。
预设是一个包含配置、权重和其他文件资产的目录,用于保存和加载预训练模型。preset 可以作为以下之一传递:
您可以运行 `cls.presets.keys()` 来列出该类上所有可用的内置预设。
参数
示例
batch = np.random.randint(0, 256, size=(2, 512, 512, 3))
# Resize images for `"pali_gemma_3b_224"`.
converter = keras_hub.layers.ImageConverter.from_preset(
"pali_gemma_3b_224"
)
converter(batch) # # Output shape (2, 224, 224, 3)
# Resize images for `"pali_gemma_3b_448"` without cropping.
converter = keras_hub.layers.ImageConverter.from_preset(
"pali_gemma_3b_448",
crop_to_aspect_ratio=False,
)
converter(batch) # # Output shape (2, 448, 448, 3)
| 预设 | 参数 | 描述 |
|---|---|---|
| deit_tiny_distilled_patch16_224_imagenet | 5.52M | DeiT-T16 模型在 ImageNet 1k 数据集上进行了预训练,图像分辨率为 224x224。 |
| deit_small_distilled_patch16_224_imagenet | 21.67M | DeiT-S16 模型在 ImageNet 1k 数据集上进行了预训练,图像分辨率为 224x224。 |
| deit_base_distilled_patch16_224_imagenet | 85.80M | DeiT-B16 模型在 ImageNet 1k 数据集上进行了预训练,图像分辨率为 224x224。 |
| deit_base_distilled_patch16_384_imagenet | 86.09M | DeiT-B16 模型在 ImageNet 1k 数据集上进行了预训练,图像分辨率为 384x384。 |