Meet YOLO26: next-gen vision AI.

Link to this sectionتصدير MNN لنماذج YOLO26 والنشر#

Link to this sectionMNN#

MNN mobile neural network inference framework

MNN هو إطار عمل تعليم عميق عالي الكفاءة وخفيف الوزن. وهو يدعم الاستدلال وتدريب نماذج التعلم العميق ويتمتع بأداء رائد في الصناعة للاستدلال والتدريب على الجهاز. في الوقت الحالي، تم دمج MNN في أكثر من 30 تطبيقاً لشركة Alibaba Inc، مثل Taobao وTmall وYouku وDingTalk وXianyu وغيرها، وهي تغطي أكثر من 70 سيناريو استخدام مثل البث المباشر، والتقاط الفيديو القصير، وتوصيات البحث، والبحث عن المنتجات بالصور، والتسويق التفاعلي، وتوزيع الأسهم، والتحكم في المخاطر الأمنية. بالإضافة إلى ذلك، يُستخدم MNN أيضاً على الأجهزة المضمنة، مثل إنترنت الأشياء (IoT).



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

Link to this sectionالتصدير إلى MNN: تحويل نموذج YOLO26 الخاص بك#

يمكنك توسيع نطاق توافق النموذج ومرونة النشر عن طريق تحويل نماذج Ultralytics YOLO إلى تنسيق MNN. يعمل هذا التحويل على تحسين نماذجك لبيئات الهاتف المحمول والبيئات المضمنة، مما يضمن أداءً فعالاً على الأجهزة ذات الموارد المحدودة.

Link to this sectionالتثبيت#

لتثبيت الحزم المطلوبة، قم بتشغيل:

التثبيت
# Install the required package for YOLO26 and MNN
pip install ultralytics
pip install MNN

Link to this sectionالاستخدام#

تم تصميم جميع نماذج Ultralytics YOLO26 لدعم التصدير فورًا، مما يسهل دمجها في سير عمل النشر المفضل لديك. يمكنك عرض القائمة الكاملة لتنسيقات التصدير المدعومة وخيارات التكوين لاختيار أفضل إعداد لتطبيقك.

يدعم تنسيق MNN أوضاع Export وPredict وValidate. قم بتصدير نموذجك، ثم قم بتحميل النموذج المصدر لإجراء الاستدلال أو التحقق من دقته.

التصدير
from ultralytics import YOLO

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

# Export the model to MNN format
model.export(format="mnn")  # creates 'yolo26n.mnn'
التنبؤ
from ultralytics import YOLO

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

# Run inference
results = model("https://ultralytics.com/images/bus.jpg")
التحقق
from ultralytics import YOLO

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

# Validate accuracy on the COCO8 dataset
metrics = model.val(data="coco8.yaml")

Link to this sectionوسائط التصدير#

الوسيطالنوعالافتراضيالوصف
formatstr'mnn'التنسيق المستهدف للنموذج المصدر، والذي يحدد التوافق مع بيئات النشر المختلفة.
imgszint أو tuple640حجم الصورة المطلوب لمدخل النموذج. يمكن أن يكون عدداً صحيحاً للصور المربعة أو زوجاً مرتباً (height, width) لأبعاد محددة.
quantizeint أو strNoneدقة التكميم (Quantization): 16 (FP16)، أو 8 (تكميم الوزن INT8)، أو 32/غير محدد (FP32). تحل محل أعلام half/int8 التي تم إيقافها.
batchint1يحدد حجم استدلال دفعة النموذج المصدر أو الحد الأقصى لعدد الصور التي سيعالجها النموذج المصدر في وقت واحد في وضع predict.
devicestrNoneيحدد الجهاز للتصدير: GPU (device=0)، CPU (device=cpu)، أو MPS لأجهزة Apple silicon (device=mps).

لمزيد من التفاصيل حول عملية التصدير، تفضل بزيارة صفحة وثائق Ultralytics حول التصدير.

Link to this sectionاستدلال 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)

Link to this sectionملخص#

في هذا الدليل، نقدم كيفية تصدير نموذج Ultralytics YOLO26 إلى MNN واستخدام MNN للاستدلال. يوفر تنسيق MNN أداءً ممتازاً لتطبيقات edge AI، مما يجعله مثالياً لنشر نماذج رؤية الكمبيوتر على الأجهزة ذات الموارد المحدودة.

لمزيد من الاستخدام، يرجى الرجوع إلى توثيق MNN.

Link to this sectionالأسئلة الشائعة#

Link to this sectionكيف أقوم بتصدير نماذج Ultralytics YOLO26 إلى تنسيق MNN؟#

لتصدير نموذج Ultralytics YOLO26 الخاص بك إلى تنسيق MNN، اتبع الخطوات التالية:

التصدير
from ultralytics import YOLO

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

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

للحصول على خيارات تصدير مفصلة، راجع صفحة Export في التوثيق.

Link to this sectionكيف أقوم بالتنبؤ باستخدام نموذج YOLO26 MNN مصدر؟#

للتنبؤ باستخدام نموذج YOLO26 MNN مصدر، استخدم وظيفة predict من فئة YOLO.

التنبؤ
from ultralytics import YOLO

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

# Run inference
results = model("https://ultralytics.com/images/bus.jpg")  # predict with `fp32`
results = model("https://ultralytics.com/images/bus.jpg", quantize=16)  # predict with `fp16` if device support

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

Link to this sectionما هي المنصات المدعومة لـ MNN؟#

يتميز MNN بتعدد الاستخدامات ويدعم منصات متنوعة:

  • الهاتف المحمول: Android وiOS وHarmony.
  • الأنظمة المضمنة وأجهزة إنترنت الأشياء: أجهزة مثل Raspberry Pi وNVIDIA Jetson.
  • سطح المكتب والخوادم: Linux و Windows و macOS.

Link to this sectionكيف يمكنني نشر نماذج Ultralytics YOLO26 MNN على الأجهزة المحمولة؟#

لنشر نماذج YOLO26 الخاصة بك على الأجهزة المحمولة:

  1. البناء لنظام Android: اتبع دليل MNN Android.
  2. البناء لنظام iOS: اتبع دليل MNN iOS.
  3. البناء لنظام Harmony: اتبع دليل MNN Harmony.

التعليقات