ONNX 模型库
返回模型

说明文档

Segment Anything 8位 ONNX

如何运行:

import onnxruntime as ort
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# 图像文件路径
image_path = "example.png"

# 加载图像并进行预处理
image = Image.open(image_path).convert("RGB")
orig_width, orig_height = image.size
input_tensor = np.array(image)
mean = np.array([123.675, 116.28, 103.53])
std = np.array([58.395, 57.12, 57.375])
input_tensor = (input_tensor - mean) / std
input_tensor = input_tensor.transpose(2, 0, 1)[None, :, :, :].astype(np.float32)

# 将输入张量填充至 1024x1024
pad_height = 1024 - input_tensor.shape[2]
pad_width = 1024 - input_tensor.shape[3]
input_tensor = np.pad(input_tensor, ((0, 0), (0, 0), (0, pad_height), (0, pad_width)))

# 加载编码器模型并运行推理
encoder = ort.InferenceSession("sam_encoder.onnx")
embeddings = encoder.run(None, {"images": input_tensor})[0]

# 在原始图像中选择一个点(例如 x=150, y=100)
point = [150, 100]

# 转换点坐标以匹配填充后的图像
point = np.array([[point]])
coords = point.astype(float)
coords[..., 0] = coords[..., 0] * (1024 / orig_width)
coords[..., 1] = coords[..., 1] * (1024 / orig_height)
onnx_coord = coords.astype("float32")

# 为解码器准备输入
onnx_mask_input = np.zeros((1, 1, 256, 256), dtype=np.float32)
onnx_has_mask_input = np.zeros(1, dtype=np.float32)
onnx_label = np.array([1, -1]).astype(np.float32)[None, :]

# 加载解码器模型并运行推理
decoder = ort.InferenceSession("sam_decoder.onnx")
masks_output, _, _ = decoder.run(None, {
    "image_embeddings": embeddings,
    "point_coords": onnx_coord,
    "point_labels": onnx_label,
    "mask_input": onnx_mask_input,
    "has_mask_input": onnx_has_mask_input,
    "orig_im_size": np.array([orig_height, orig_width], dtype=np.float32)
})

# 处理输出掩码
mask = masks_output[0][0]
mask = (mask > 0).astype('uint8') * 255

robgonsalves/segment-anything-8bit-onnx

作者 robgonsalves

↓ 0 ♥ 0

创建时间: 2024-08-29 06:05:47+00:00

更新时间: 2024-08-29 06:20:36+00:00

在 Hugging Face 上查看

文件 (4)

.gitattributes
README.md
sam_decoder.onnx ONNX
sam_encoder.onnx ONNX