用于 YOLO26 模型和部署的 MNN 导出

MNN

MNN mobile neural network inference framework

MNN 是一个高效且轻量级的深度学习框架。它支持深度学习模型的推理和训练,并拥有业界领先的端侧推理和训练性能。目前,MNN 已集成到阿里巴巴集团 30 多款 App 中,例如淘宝、天猫、优酷、钉钉、闲鱼等,覆盖了直播、短视频捕捉、搜索推荐、以图搜图、互动营销、权益发放、安全风控等 70 多个使用场景。此外,MNN 也被应用于物联网等嵌入式设备中。



Watch: How to Export Ultralytics YOLO26 to MNN Format | Speed up Inference on Mobile Devices📱

导出至 MNN:转换你的 YOLO26 模型

通过将 Ultralytics YOLO 模型转换为 MNN 格式,你可以扩展模型的兼容性和部署灵活性。这种转换能为移动和嵌入式环境优化你的模型,确保在资源受限的设备上实现高效性能。

安装

要安装所需的软件包,请运行:

安装
# Install the required package for YOLO26 and MNN
pip install ultralytics
pip install MNN

使用方法

所有 Ultralytics YOLO26 模型 均设计为支持直接开箱导出,使你能够轻松将其集成到你首选的部署工作流中。你可以查看支持的导出格式和配置选项的完整列表,为你的应用选择最佳设置。

使用方法
  from ultralytics import YOLO

  # Load the YOLO26 model
  model = YOLO("yolo26n.pt")

  # Export the model to MNN format
  model.export(format="mnn")  # creates 'yolo26n.mnn'

  # Load the exported MNN model
  mnn_model = YOLO("yolo26n.mnn")

  # Run inference
  results = mnn_model("https://ultralytics.com/images/bus.jpg")

导出参数

参数类型默认值描述
formatstr'mnn'导出模型的目标格式,定义与各种部署环境的兼容性。
imgszinttuple640模型输入的所需图像尺寸。可以是正方形图像的整数,也可以是特定维度的元组 (height, width)
halfboolFalse启用 FP16(半精度)量化,减小模型大小并可能在受支持的硬件上加快推理速度。
int8boolFalse激活 INT8 量化,进一步压缩模型并以极小的准确性损失加快推理速度,主要适用于边缘设备。
batchint1指定导出模型的批量推理大小,或者导出模型在 predict 模式下将同时处理的最大图像数量。
devicestrNone指定导出的设备:GPU (device=0)、CPU (device=cpu)、适用于 Apple 芯片的 MPS (device=mps)。

有关导出过程的更多详细信息,请访问 Ultralytics 导出文档页面

仅使用 MNN 推理

实现了一个仅依赖 MNN 进行 YOLO26 推理和预处理的函数,并提供了 Python 和 C++ 版本,方便在任何场景下进行部署。

MNN
import argparse

import MNN
import MNN.cv as cv2
import MNN.numpy as np

def inference(model, img, precision, backend, thread):
    config = {}
    config["precision"] = precision
    config["backend"] = backend
    config["numThread"] = thread
    rt = MNN.nn.create_runtime_manager((config,))
    # net = MNN.nn.load_module_from_file(model, ['images'], ['output0'], runtime_manager=rt)
    net = MNN.nn.load_module_from_file(model, [], [], runtime_manager=rt)
    original_image = cv2.imread(img)
    ih, iw, _ = original_image.shape
    length = max((ih, iw))
    scale = length / 640
    image = np.pad(original_image, [[0, length - ih], [0, length - iw], [0, 0]], "constant")
    image = cv2.resize(
        image, (640, 640), 0.0, 0.0, cv2.INTER_LINEAR, -1, [0.0, 0.0, 0.0], [1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0]
    )
    image = image[..., ::-1]  # BGR to RGB
    input_var = image[None]
    input_var = MNN.expr.convert(input_var, MNN.expr.NC4HW4)
    output_var = net.forward(input_var)
    output_var = MNN.expr.convert(output_var, MNN.expr.NCHW)
    output_var = output_var.squeeze()
    # output_var shape: [84, 8400]; 84 means: [cx, cy, w, h, prob * 80]
    cx = output_var[0]
    cy = output_var[1]
    w = output_var[2]
    h = output_var[3]
    probs = output_var[4:]
    # [cx, cy, w, h] -> [y0, x0, y1, x1]
    x0 = cx - w * 0.5
    y0 = cy - h * 0.5
    x1 = cx + w * 0.5
    y1 = cy + h * 0.5
    boxes = np.stack([x0, y0, x1, y1], axis=1)
    # ensure ratio is within the valid range [0.0, 1.0]
    boxes = np.clip(boxes, 0, 1)
    # get max prob and idx
    scores = np.max(probs, 0)
    class_ids = np.argmax(probs, 0)
    result_ids = MNN.expr.nms(boxes, scores, 100, 0.45, 0.25)
    print(result_ids.shape)
    # nms result box, score, ids
    result_boxes = boxes[result_ids]
    result_scores = scores[result_ids]
    result_class_ids = class_ids[result_ids]
    for i in range(len(result_boxes)):
        x0, y0, x1, y1 = result_boxes[i].read_as_tuple()
        y0 = int(y0 * scale)
        y1 = int(y1 * scale)
        x0 = int(x0 * scale)
        x1 = int(x1 * scale)
        # clamp to the original image size to handle cases where padding was applied
        x1 = min(iw, x1)
        y1 = min(ih, y1)
        print(result_class_ids[i])
        cv2.rectangle(original_image, (x0, y0), (x1, y1), (0, 0, 255), 2)
    cv2.imwrite("res.jpg", original_image)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str, required=True, help="the yolo26 model path")
    parser.add_argument("--img", type=str, required=True, help="the input image path")
    parser.add_argument("--precision", type=str, default="normal", help="inference precision: normal, low, high, lowBF")
    parser.add_argument(
        "--backend",
        type=str,
        default="CPU",
        help="inference backend: CPU, OPENCL, OPENGL, NN, VULKAN, METAL, TRT, CUDA, HIAI",
    )
    parser.add_argument("--thread", type=int, default=4, help="inference using thread: int")
    args = parser.parse_args()
    inference(args.model, args.img, args.precision, args.backend, args.thread)

总结

在本指南中,我们将介绍如何将 Ultralytics YOLO26 模型导出到 MNN,以及如何使用 MNN 进行推理。MNN 格式为边缘 AI 应用提供了卓越的性能,使其成为在资源受限设备上部署计算机视觉模型的理想选择。

如需更多用法,请参考 MNN 文档

常见问题 (FAQ)

如何将 Ultralytics YOLO26 模型导出为 MNN 格式?

要将你的 Ultralytics YOLO26 模型导出为 MNN 格式,请遵循以下步骤:

导出
from ultralytics import YOLO

# Load the YOLO26 model
model = YOLO("yolo26n.pt")

# Export to MNN format
model.export(format="mnn")  # creates 'yolo26n.mnn' with fp32 weight
model.export(format="mnn", half=True)  # creates 'yolo26n.mnn' with fp16 weight
model.export(format="mnn", int8=True)  # creates 'yolo26n.mnn' with int8 weight

有关详细的导出选项,请查看文档中的 导出 页面。

如何使用导出的 YOLO26 MNN 模型进行预测?

要使用导出的 YOLO26 MNN 模型进行预测,请使用 YOLO 类中的 predict 函数。

预测
from ultralytics import YOLO

# Load the YOLO26 MNN model
model = YOLO("yolo26n.mnn")

# Export to MNN format
results = model("https://ultralytics.com/images/bus.jpg")  # predict with `fp32`
results = model("https://ultralytics.com/images/bus.jpg", half=True)  # predict with `fp16` if device support

for result in results:
    result.show()  # display to screen
    result.save(filename="result.jpg")  # save to disk

MNN 支持哪些平台?

MNN 用途广泛,支持多种平台:

  • 移动端:Android、iOS、Harmony。
  • 嵌入式系统和 IoT 设备:例如 Raspberry Pi 和 NVIDIA Jetson 等设备。
  • 桌面和服务器:Linux、Windows 和 macOS。

如何部署 Ultralytics YOLO26 MNN 模型到移动设备上?

要将你的 YOLO26 模型部署到移动设备上:

  1. 构建 Android 版:遵循 MNN Android 指南。
  2. 构建 iOS 版:遵循 MNN iOS 指南。
  3. 构建 Harmony 版:遵循 MNN Harmony 指南。

评论