{"library":"mnn","title":"MNN Python Binding","description":"MNN (Mobile Neural Network) is a blazing-fast, lightweight deep learning inference engine developed by Alibaba. It supports inference and training of deep learning models, offering high performance on various devices, including mobile and embedded systems. The Python package `mnn` provides APIs for inference, training, image processing, and numerical computation, allowing ML engineers to use MNN without dipping their toes in C++ code. It is currently at version 3.5.0 and maintains an active development and release cadence.","language":"python","status":"active","last_verified":"Mon May 18","install":{"commands":["pip install MNN"],"cli":{"name":"mnn","version":"Traceback (most recent call last):"}},"imports":["import MNN","import MNN.nn as nn","import MNN.cv as cv","import MNN.numpy as np","import MNN.expr as expr","from MNN import Interpreter"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import MNN.nn as nn\nimport MNN.cv as cv\nimport MNN.numpy as np\nimport MNN.expr as expr\nimport os\n\n# Assuming 'mobilenet_v1.mnn' and 'cat.jpg' exist in the current directory\n# In a real scenario, you'd download these or replace with your model/image.\n# For demonstration, we'll create dummy files if they don't exist.\n\n# Create a dummy MNN model file if it doesn't exist (for runnable example)\nif not os.path.exists('mobilenet_v1.mnn'):\n    print(\"Creating dummy mobilenet_v1.mnn for quickstart. This won't run a real model.\")\n    with open('mobilenet_v1.mnn', 'w') as f:\n        f.write('dummy_model_content')\n\n# Create a dummy image file if it doesn't exist (for runnable example)\nif not os.path.exists('cat.jpg'):\n    print(\"Creating dummy cat.jpg for quickstart. This won't process a real image.\")\n    # Using PIL to create a simple dummy image\n    try:\n        from PIL import Image\n        img = Image.new('RGB', (224, 224), color = 'red')\n        img.save('cat.jpg')\n    except ImportError:\n        print(\"Pillow not installed. Skipping dummy image creation.\")\n        print(\"Please install Pillow (pip install Pillow) or provide a 'cat.jpg'.\")\n\n# Configure runtime (e.g., backend, threads, precision)\n# Backend 0 typically refers to CPU. 'low' precision might enable FP16 if hardware supports.\nconfig = {\n    'precision': 'low', \n    'backend': 0,      \n    'numThread': 4     \n}\nruntime_manager = nn.create_runtime_manager((config,))\n\n# Load model using the Module API\n# 'data' and 'prob' are example input/output names, adapt to your model\ntry:\n    net = nn.load_module_from_file('mobilenet_v1.mnn', ['data'], ['prob'], runtime_manager=runtime_manager)\nexcept Exception as e:\n    print(f\"Could not load dummy model: {e}. Please ensure 'mobilenet_v1.mnn' is a valid MNN model.\")\n    exit()\n\n# Read and preprocess image using MNN.cv and MNN.numpy\n# Mean and norm values are typical for MobileNet preprocessing\nimage = cv.imread('cat.jpg')\nif image is not None:\n    image = cv.resize(image, (224, 224), mean=[103.94, 116.78, 123.68], norm=[0.017, 0.017, 0.017])\n    input_var = np.expand_dims(image, 0) # Add batch dimension (HWC to NHWC)\n    input_var = expr.convert(input_var, expr.NC4HW4) # NHWC to NC4HW4 for MNN\n\n    # Perform inference\n    output_var = net.forward(input_var)\n\n    # Post-process output\n    output_var = expr.convert(output_var, expr.NHWC) # NC4HW4 to NHWC\n    # For a real classification model, you would interpret 'output_var' here\n    print(f\"Inference output shape: {output_var.shape}\")\n    # Example: print top-1 class if it's a classification model\n    # print(f\"Output belongs to class: {np.argmax(output_var)}\")\nelse:\n    print(\"Could not read image. Ensure 'cat.jpg' exists and is a valid image.\")\n","lang":"python","description":"This quickstart demonstrates loading an MNN model, preprocessing an image using `MNN.cv` and `MNN.numpy`, performing inference with the `Module API`, and post-processing the output. Replace `mobilenet_v1.mnn` and `cat.jpg` with your actual model and input data. The code includes creation of dummy files for runnable demonstration purposes if actual files are not present.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-18","installed_version":"3.5.0","pypi_latest":"3.5.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":50,"avg_install_s":4.8,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"MNN","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"MNN","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":4.1,"import_time_s":null,"mem_mb":null,"disk_size":"140M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"MNN","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"MNN","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":4.1,"import_time_s":null,"mem_mb":null,"disk_size":"147M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"MNN","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"MNN","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":3.9,"import_time_s":null,"mem_mb":null,"disk_size":"135M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"MNN","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"MNN","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":4.9,"import_time_s":null,"mem_mb":null,"disk_size":"135M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"MNN","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"MNN","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":6.8,"import_time_s":null,"mem_mb":null,"disk_size":"150M"}]}}