NCNN Neural Network Inference Framework

1.0.20260114 · active · verified Thu Apr 16

NCNN is a high-performance neural network inference framework optimized for mobile platforms. The `ncnn` Python library provides official Python bindings, allowing users to load and run NCNN models from Python applications. It enables efficient deep learning inference on devices with limited computational resources. The library's versioning (`1.0.YYYYMMDD`) reflects a frequent release cadence, often aligned with new features or fixes in the core C++ NCNN project.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic API flow for initializing an NCNN network, loading (dummy) model files, and preparing input data using `ncnn.Mat`. It uses placeholder model files to be runnable without actual model conversion, but you would replace them with your own `.param` and `.bin` files for real inference.

import ncnn
import numpy as np
import os

# NCNN requires model files (.param and .bin)
# For a runnable quickstart, we'll demonstrate the API flow.
# In a real scenario, you'd replace these with actual converted model files.

# Create dummy files - these are NOT functional NCNN models
# but allow the API calls to proceed without immediate file not found errors.
# A real NCNN model conversion would generate proper .param and .bin.
with open("dummy_model.param", "w") as f:
    f.write("7767517\n0\n") # Minimal valid param content for an empty net
with open("dummy_model.bin", "wb") as f:
    f.write(b'') # Empty bin content

try:
    # 1. Initialize NCNN network
    net = ncnn.Net()
    print("NCNN Net initialized.")

    # Optional: Configure options (e.g., enable Vulkan if available)
    # net.opt.use_vulkan_compute = True 

    # 2. Load model structure (.param) and weights (.bin)
    # Note: These dummy files will load but won't perform actual inference.
    # Replace "dummy_model.param" and "dummy_model.bin" with your converted NCNN model paths.
    ret_param = net.load_param("dummy_model.param")
    ret_bin = net.load_model("dummy_model.bin")

    if ret_param == 0 and ret_bin == 0:
        print("Dummy NCNN model files loaded successfully.")
    else:
        print(f"Failed to load dummy model. param_ret={ret_param}, bin_ret={ret_bin}")
        # A non-zero return code means failure, e.g., malformed model files.

    # 3. Prepare input data (e.g., from an image or NumPy array)
    # This step is for demonstration; actual inference won't happen with dummy model.
    dummy_input_array = np.random.rand(224, 224, 3).astype(np.float32) * 255
    mat_in = ncnn.Mat.from_pixels(dummy_input_array, ncnn.PIXEL_RGB, 224, 224)
    print(f"Dummy input Mat created with shape: {mat_in.w}x{mat_in.h}x{mat_in.c}")

    # 4. Create an extractor and push input (for a real model)
    # ex = net.create_extractor()
    # ex.input("data", mat_in) # "data" is a common input blob name

    # 5. Run inference and extract output (for a real model)
    # ret, mat_out = ex.extract("output") # "output" is a common output blob name

    print("NCNN API usage demonstrated. For real inference, replace dummy files with actual NCNN models.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy files
    if os.path.exists("dummy_model.param"):
        os.remove("dummy_model.param")
    if os.path.exists("dummy_model.bin"):
        os.remove("dummy_model.bin")

view raw JSON →