KerasHub:预训练模型 / 开发者指南 / KerasHub 中的 Segment Anything

KerasHub 中的 Segment Anything

作者: Tirth Patel、Ian Stenbit、Divyashree Sreepathihalli

创建日期 2024/10/1

最后修改日期 2024/10/1

描述: 在 KerasHub 中使用文本、框和点提示分割任何内容。

在 Colab 中查看 GitHub 源代码


概述

Segment Anything 模型 (SAM) 从点或框等输入提示生成高质量的对象掩码,并且可以用于为图像中的所有对象生成掩码。它已经在包含 1100 万张图像和 11 亿个掩码的数据集上进行了训练,并在各种分割任务中具有强大的零样本性能。

在本指南中,我们将展示如何使用 KerasHub 的 Segment Anything 模型的实现,并展示 TensorFlow 和 JAX 的性能提升有多强大。

首先,让我们为我们的演示获取所有依赖项和图像。

!!pip install -Uq git+https://github.com/keras-team/keras-hub.git
!!pip install -Uq keras
!!wget -q https://raw.githubusercontent.com/facebookresearch/segment-anything/main/notebooks/images/truck.jpg

选择你的后端

使用 Keras 3,你可以选择使用你喜欢的后端!

import os

os.environ["KERAS_BACKEND"] = "jax"

import timeit
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import ops
import keras_hub

辅助函数

让我们定义一些辅助函数来可视化图像、提示和分割结果。

def show_mask(mask, ax, random_color=False):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    ax.imshow(mask_image)


def show_points(coords, labels, ax, marker_size=375):
    pos_points = coords[labels == 1]
    neg_points = coords[labels == 0]
    ax.scatter(
        pos_points[:, 0],
        pos_points[:, 1],
        color="green",
        marker="*",
        s=marker_size,
        edgecolor="white",
        linewidth=1.25,
    )
    ax.scatter(
        neg_points[:, 0],
        neg_points[:, 1],
        color="red",
        marker="*",
        s=marker_size,
        edgecolor="white",
        linewidth=1.25,
    )


def show_box(box, ax):
    box = box.reshape(-1)
    x0, y0 = box[0], box[1]
    w, h = box[2] - box[0], box[3] - box[1]
    ax.add_patch(
        plt.Rectangle((x0, y0), w, h, edgecolor="green", facecolor=(0, 0, 0, 0), lw=2)
    )


def inference_resizing(image, pad=True):
    # Compute Preprocess Shape
    image = ops.cast(image, dtype="float32")
    old_h, old_w = image.shape[0], image.shape[1]
    scale = 1024 * 1.0 / max(old_h, old_w)
    new_h = old_h * scale
    new_w = old_w * scale
    preprocess_shape = int(new_h + 0.5), int(new_w + 0.5)

    # Resize the image
    image = ops.image.resize(image[None, ...], preprocess_shape)[0]

    # Pad the shorter side
    if pad:
        pixel_mean = ops.array([123.675, 116.28, 103.53])
        pixel_std = ops.array([58.395, 57.12, 57.375])
        image = (image - pixel_mean) / pixel_std
        h, w = image.shape[0], image.shape[1]
        pad_h = 1024 - h
        pad_w = 1024 - w
        image = ops.pad(image, [(0, pad_h), (0, pad_w), (0, 0)])
        # KerasHub now rescales the images and normalizes them.
        # Just unnormalize such that when KerasHub normalizes them
        # again, the padded values map to 0.
        image = image * pixel_std + pixel_mean
    return image

获取预训练的 SAM 模型

我们可以使用 KerasHub 的 from_preset 工厂方法初始化经过训练的 SAM 模型。在这里,我们使用在 SA-1B 数据集 (sam_huge_sa1b) 上训练的巨大的 ViT 主干网络来获得高质量的分割掩码。你还可以使用 sam_large_sa1bsam_base_sa1b 来获得更好的性能(以降低分割掩码的质量为代价)。

model = keras_hub.models.SAMImageSegmenter.from_preset("sam_huge_sa1b")

理解提示

Segment Anything 允许使用点、框和掩码来提示图像

  1. 点提示是最基本的:模型尝试根据图像上的一个点猜测对象。该点可以是前景点(即所需的分割掩码包含该点)或背景点(即该点位于所需掩码之外)。
  2. 提示模型的另一种方法是使用框。给定一个边界框,模型尝试分割其中包含的对象。
  3. 最后,还可以使用掩码本身来提示模型。例如,这对于细化先前预测或已知的分割掩码的边界非常有用。

该模型非常强大的原因在于能够组合上述提示。点、框和掩码提示可以以多种不同的方式组合以获得最佳结果。

让我们看看在 KerasHub 中将这些提示传递给 Segment Anything 模型的语义。SAM 模型的输入是一个字典,其中包含以下键

  1. "images":要分割的图像批次。形状必须为 (B, 1024, 1024, 3)
  2. "points":点提示批次。每个点都是一个 (x, y) 坐标,该坐标起源于图像的左上角。换句话说,每个点的形式为 (r, c),其中 rc 是图像中像素的行和列。形状必须为 (B, N, 2)
  3. "labels":给定点的标签批次。1 表示前景点,0 表示背景点。形状必须为 (B, N)
  4. "boxes":框的批次。请注意,模型每个批次仅接受一个框。因此,预期的形状为 (B, 1, 2, 2)。每个框都是 2 个点的集合:框的左上角和右下角。此处的点遵循与点提示相同的语义。此处的第二个维度中的 1 表示存在框提示。如果缺少框提示,则必须传递形状为 (B, 0, 2, 2) 的占位符输入。
  5. "masks":掩码批次。与框提示一样,每个图像只允许一个掩码提示。如果存在输入掩码,则其形状必须为 (B, 1, 256, 256, 1),如果缺少掩码提示,则形状为 (B, 0, 256, 256, 1)

仅当直接调用模型(即 model(...))时才需要占位符提示。当调用 predict 方法时,可以从输入字典中省略缺少的提示。


点提示

首先,让我们使用点提示分割图像。我们加载图像并将其调整为形状 (1024, 1024),这是预训练的 SAM 模型期望的图像大小。

# Load our image
image = np.array(keras.utils.load_img("truck.jpg"))
image = inference_resizing(image)

plt.figure(figsize=(10, 10))
plt.imshow(ops.convert_to_numpy(image) / 255.0)
plt.axis("on")
plt.show()

png

接下来,我们将定义要分割的对象上的点。让我们尝试在坐标 (284, 213) 处分割卡车的窗玻璃。

# Define the input point prompt
input_point = np.array([[284, 213.5]])
input_label = np.array([1])

plt.figure(figsize=(10, 10))
plt.imshow(ops.convert_to_numpy(image) / 255.0)
show_points(input_point, input_label, plt.gca())
plt.axis("on")
plt.show()

png

现在,让我们调用模型的 predict 方法来获取分割掩码。

注意:我们不直接调用模型 (model(...)),因为这样做需要占位符提示。缺少提示由 predict 方法自动处理,因此我们改为调用它。此外,当没有框提示时,点和标签需要分别使用零点提示和 -1 标签提示进行填充。下面的单元格演示了它是如何工作的。

outputs = model.predict(
    {
        "images": image[np.newaxis, ...],
        "points": np.concatenate(
            [input_point[np.newaxis, ...], np.zeros((1, 1, 2))], axis=1
        ),
        "labels": np.concatenate(
            [input_label[np.newaxis, ...], np.full((1, 1), fill_value=-1)], axis=1
        ),
    }
)

SegmentAnythingModel.predict 返回两个输出。第一个是形状为 (1, 4, 256, 256) 的 logits(分割掩码),另一个是每个预测的掩码的 IoU 置信度分数(形状为 (1, 4))。预训练的 SAM 模型预测四个掩码:第一个是模型针对给定提示所能提出的最佳掩码,另外 3 个是替代掩码,如果最佳预测不包含所需的对象,则可以使用它们。用户可以选择他们喜欢的任何掩码。

让我们可视化模型返回的掩码!

# Resize the mask to our image shape i.e. (1024, 1024)
mask = inference_resizing(outputs["masks"][0][0][..., None], pad=False)[..., 0]
# Convert the logits to a numpy array
# and convert the logits to a boolean mask
mask = ops.convert_to_numpy(mask) > 0.0
iou_score = ops.convert_to_numpy(outputs["iou_pred"][0][0])

plt.figure(figsize=(10, 10))
plt.imshow(ops.convert_to_numpy(image) / 255.0)
show_mask(mask, plt.gca())
show_points(input_point, input_label, plt.gca())
plt.title(f"IoU Score: {iou_score:.3f}", fontsize=18)
plt.axis("off")
plt.show()

png

正如预期的那样,该模型返回卡车窗玻璃的分割掩码。但是,我们的点提示也可能意味着其他一系列事物。例如,另一个包含我们点的可能掩码只是窗玻璃的右侧或整个卡车。

让我们也可视化模型预测的其他掩码。

fig, ax = plt.subplots(1, 3, figsize=(20, 60))
masks, scores = outputs["masks"][0][1:], outputs["iou_pred"][0][1:]
for i, (mask, score) in enumerate(zip(masks, scores)):
    mask = inference_resizing(mask[..., None], pad=False)[..., 0]
    mask, score = map(ops.convert_to_numpy, (mask, score))
    mask = 1 * (mask > 0.0)
    ax[i].imshow(ops.convert_to_numpy(image) / 255.0)
    show_mask(mask, ax[i])
    show_points(input_point, input_label, ax[i])
    ax[i].set_title(f"Mask {i+1}, Score: {score:.3f}", fontsize=12)
    ax[i].axis("off")
plt.show()

png

太棒了!SAM 能够捕捉到我们点提示的歧义,并返回了其他可能的分割掩码。


框提示

现在,让我们看看如何使用框提示模型。该框使用两个点指定,即 xyxy 格式的边界框的左上角和右下角。让我们使用卡车左前轮胎周围的边界框来提示模型。

# Let's specify the box
input_box = np.array([[240, 340], [400, 500]])

outputs = model.predict(
    {"images": image[np.newaxis, ...], "boxes": input_box[np.newaxis, np.newaxis, ...]}
)
mask = inference_resizing(outputs["masks"][0][0][..., None], pad=False)[..., 0]
mask = ops.convert_to_numpy(mask) > 0.0

plt.figure(figsize=(10, 10))
plt.imshow(ops.convert_to_numpy(image) / 255.0)
show_mask(mask, plt.gca())
show_box(input_box, plt.gca())
plt.axis("off")
plt.show()

png

搞定!该模型完美地分割了我们边界框中的左前轮胎。


组合提示

为了发挥模型的真正潜力,让我们组合框和点提示,看看模型会做什么。

# Let's specify the box
input_box = np.array([[240, 340], [400, 500]])
# Let's specify the point and mark it background
input_point = np.array([[325, 425]])
input_label = np.array([0])

outputs = model.predict(
    {
        "images": image[np.newaxis, ...],
        "points": input_point[np.newaxis, ...],
        "labels": input_label[np.newaxis, ...],
        "boxes": input_box[np.newaxis, np.newaxis, ...],
    }
)
mask = inference_resizing(outputs["masks"][0][0][..., None], pad=False)[..., 0]
mask = ops.convert_to_numpy(mask) > 0.0

plt.figure(figsize=(10, 10))
plt.imshow(ops.convert_to_numpy(image) / 255.0)
show_mask(mask, plt.gca())
show_box(input_box, plt.gca())
show_points(input_point, input_label, plt.gca())
plt.axis("off")
plt.show()

png

瞧!该模型理解了我们想要从掩码中排除的对象是轮胎的轮辋。


文本提示

最后,让我们看看如何将文本提示与 KerasHub 的 SegmentAnythingModel 一起使用。

对于此演示,我们将使用官方 Grounding DINO 模型。Grounding DINO 模型将 (图像, 文本) 对作为输入,并在 图像 中生成由 文本 描述的对象周围的边界框。有关模型实现的更多详细信息,你可以参考论文

对于演示的这一部分,我们将需要从源代码安装 groundingdino

pip install -U git+https://github.com/IDEA-Research/GroundingDINO.git

然后,我们可以安装预训练模型的权重和配置

!!pip install -U git+https://github.com/IDEA-Research/GroundingDINO.git
!!wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
!!wget -q https://raw.githubusercontent.com/IDEA-Research/GroundingDINO/v0.1.0-alpha2/groundingdino/config/GroundingDINO_SwinT_OGC.py
from groundingdino.util.inference import Model as GroundingDINO

CONFIG_PATH = "GroundingDINO_SwinT_OGC.py"
WEIGHTS_PATH = "groundingdino_swint_ogc.pth"

grounding_dino = GroundingDINO(CONFIG_PATH, WEIGHTS_PATH)

让我们加载一张狗的图像来完成这一部分!

filepath = keras.utils.get_file(
    origin="https://storage.googleapis.com/keras-cv/test-images/mountain-dog.jpeg"
)
image = np.array(keras.utils.load_img(filepath))
image = ops.convert_to_numpy(inference_resizing(image))

plt.figure(figsize=(10, 10))
plt.imshow(image / 255.0)
plt.axis("on")
plt.show()

png

我们首先使用 Grounding DINO 模型预测我们要分割的对象的边界框。然后,我们使用边界框提示 SAM 模型以获取分割掩码。

让我们尝试分割出狗的挽具。更改下面的图像和文本,以使用图像中的文本分割你想要的任何内容!

# Let's predict the bounding box for the harness of the dog
boxes = grounding_dino.predict_with_caption(image.astype(np.uint8), "harness")
boxes = np.array(boxes[0].xyxy)

outputs = model.predict(
    {
        "images": np.repeat(image[np.newaxis, ...], boxes.shape[0], axis=0),
        "boxes": boxes.reshape(-1, 1, 2, 2),
    },
    batch_size=1,
)

就是这样!我们使用 Gounding DINO + SAM 的组合获得了文本提示的分割掩码!这是一种非常强大的技术,可以组合不同的模型来扩展应用程序!

让我们可视化结果。

plt.figure(figsize=(10, 10))
plt.imshow(image / 255.0)

for mask in outputs["masks"]:
    mask = inference_resizing(mask[0][..., None], pad=False)[..., 0]
    mask = ops.convert_to_numpy(mask) > 0.0
    show_mask(mask, plt.gca())
    show_box(boxes, plt.gca())

plt.axis("off")
plt.show()

png


优化 SAM

你可以使用 mixed_float16bfloat16 dtype 策略来获得巨大的速度提升和内存优化,而精度损失相对较低。

# Load our image
image = np.array(keras.utils.load_img("truck.jpg"))
image = inference_resizing(image)

# Specify the prompt
input_box = np.array([[240, 340], [400, 500]])

# Let's first see how fast the model is with float32 dtype
time_taken = timeit.repeat(
    'model.predict({"images": image[np.newaxis, ...], "boxes": input_box[np.newaxis, np.newaxis, ...]}, verbose=False)',
    repeat=3,
    number=3,
    globals=globals(),
)
print(f"Time taken with float32 dtype: {min(time_taken) / 3:.10f}s")

# Set the dtype policy in Keras
keras.mixed_precision.set_global_policy("mixed_float16")

model = keras_hub.models.SAMImageSegmenter.from_preset("sam_huge_sa1b")

time_taken = timeit.repeat(
    'model.predict({"images": image[np.newaxis, ...], "boxes": input_box[np.newaxis,np.newaxis, ...]}, verbose=False)',
    repeat=3,
    number=3,
    globals=globals(),
)
print(f"Time taken with float16 dtype: {min(time_taken) / 3:.10f}s")

这是 KerasHub 的实现与原始 PyTorch 实现的比较!

benchmark

用于生成基准的脚本位于此处


结论

KerasHub 的 SegmentAnythingModel 支持各种应用,并且在 Keras 3 的帮助下,能够在 TensorFlow、JAX 和 PyTorch 上运行模型!在 JAX 和 TensorFlow 中 XLA 的帮助下,该模型的运行速度比原始实现快数倍。此外,使用 Keras 的混合精度支持只需一行代码即可帮助优化内存使用和计算时间!

有关更高级的用法,请查看自动掩码生成器演示