DenseNetImageConverter 类keras_hub.layers.DenseNetImageConverter(
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 方法DenseNetImageConverter.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)
| 预设 | 参数 | 描述 |
|---|---|---|
| densenet_121_imagenet | 7.04M | 121 层 DenseNet 模型,在 224x224 分辨率的 ImageNet 1k 数据集上预训练。 |
| densenet_169_imagenet | 12.64M | 169 层 DenseNet 模型,在 224x224 分辨率的 ImageNet 1k 数据集上预训练。 |
| densenet_201_imagenet | 18.32M | 201 层 DenseNet 模型,在 224x224 分辨率的 ImageNet 1k 数据集上预训练。 |