ncnn is a high-performance neural network inference framework optimized for mobile, embedded, and desktop deployment. It has no third-party runtime dependencies, runs across CPU and Vulkan GPU backends, and provides tools such as pnnx for converting PyTorch and ONNX models to ncnn. Developers can deploy deep learning models efficiently on phones, PCs, browsers, and edge devices. ncnn is currently being used in many Tencent applications, such as QQ, Qzone, WeChat, Pitu, and so on.
ncnn 是一个面向移动端、嵌入式和桌面端部署优化的高性能神经网络推理框架。 ncnn 无第三方运行时依赖,支持 CPU 和 Vulkan GPU 后端,并提供 pnnx 等工具将 PyTorch 和 ONNX 模型转换为 ncnn 模型。 基于 ncnn,开发者可以将深度学习模型高效部署到手机、PC、浏览器和边缘设备上。 ncnn 目前已在腾讯多款应用中使用,如:QQ,Qzone,微信,天天 P 图等。
The recommended beginner path is PyTorch -> pnnx -> ncnn.
|
Install pnnx in a PyTorch environment pip3 install pnnxExport a PyTorch model to ncnn import torch
import torch.nn as nn
import pnnx
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(3, 8, 1)
self.relu = nn.ReLU()
self.fc = nn.Linear(8, 4)
def forward(self, x):
x = self.conv(x)
x = self.relu(x)
x = x.mean((2, 3))
return self.fc(x)
model = Model().eval()
x = torch.rand(1, 3, 224, 224)
pnnx.export(model, "model.pt", (x,))This generates |
Run with ncnn C++ API #include "net.h"
ncnn::Net net;
net.load_param("model.ncnn.param");
net.load_model("model.ncnn.bin");
ncnn::Mat in(224, 224, 3);
auto ex = net.create_extractor();
ex.input("in0", in);
ncnn::Mat out;
ex.extract("out0", out);Or use Python import numpy as np
import ncnn
net = ncnn.Net()
net.load_param("model.ncnn.param")
net.load_model("model.ncnn.bin")
x = np.zeros((3, 224, 224), np.float32)
mat = ncnn.Mat(x)
ex = net.create_extractor()
ex.input("in0", mat)
ret, out = ex.extract("out0")
print(np.array(out).shape) |
See pnnx, use ncnn with PyTorch or ONNX, Python API, and examples for complete workflows.
