nihui的bilibili直播间:https://live.bilibili.com/1264617
github
gitee
full-source.zip
cmake 工具链怎么设置啊?
sudo apt-get install libprotobuf-dev protobuf-compiler
https://github.com/Tencent/ncnn/issues/1873
set(OpenCV_DIR)
set(ncnn_DIR)
如何安装 vulkan sdk
cmake版本 3.10,否则没有带 FindVulkan.cmake
android-api >= 24
macos 要先执行安装脚本
find_package(ncnn)
opencv rtti -> opencv-mobile
升级编译器 / libgcc_s libgcc
升级 gcc
./caffe2ncnn caffe.prototxt caffe.caffemodel ncnn.param ncnn.bin
./mxnet2ncnn mxnet-symbol.json mxnet.params ncnn.param ncnn.bin
https://github.com/xiangweizeng/darknet2ncnn
https://github.com/MarsTechHAN/keras2ncnn @MarsTechHAN
通过MLIR将tensorflow2模型转换到ncnn @nihui
onnx-simplifier 静态shape
https://convertmodel.com/ @大老师
https://github.com/lutzroeder/netron
Input 0=w 1=h 2=c
why gpu能更快
ncnnoptimize model.param model.bin yolov5s-opt.param yolov5s-opt.bin 65536
Interp Reshape
ncnn2mem
https://zhuanlan.zhihu.com/p/268327784
Yes,全平台通用
检测
参考up的一篇文章(https://zhuanlan.zhihu.com/p/128974102),步骤三就是去掉后处理,再导出onnx,其中去掉后处理可以是项目内测试时去掉后续步骤的结果。
ONNX_ATEN_FALLBACK
完全自定义的op,先改成能导出的(如 concat slice),转到 ncnn 后再修改 param
驱动
opengl
python setup.py develop
文件路径
working dir
File not found or not readable. Make sure that XYZ.param/XYZ.bin is accessible.
layer name vs blob name
param.bin 应该用 xxx.id.h 的枚举
模型本身有问题
Your model file is being the old format converted by an old caffe2ncnn tool.
Checkout the latest ncnn code, build it and regenerate param and model binary files, and that should work.
Make sure that your param file starts with the magic number 7767517.
you may find more info on use-ncnn-with-alexnet
你应该在 load_param / load_model 之前设置 net.opt.use_vulkan_compute = true;
多次执行ex.input() 和 ex.extract()
ex.input();
ex.input();
ex.extract();
ex.extract();
cmake -DNCNN_BENCHMARK=ON ..
from_pixels to_pixels
https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx
mat.fill(0.f);
get_affine_transform
warpaffine_bilinear_c3
ncnn::Mat output;
ex.extract("your_blob_name", output);
windows 10 任务管理器 - 性能选项卡 - GPU - 选择其中一个视图左上角的下拉箭头切换到 Compute_0 / Compute_1 / Cuda
你还可以安装软件:GPU-Z
Your network contains some operations that are not implemented in ncnn.
You may implement them as custom layer followed in how-to-implement-custom-layer-step-by-step.
Or you could simply register them as no-op if you are sure those operations make no sense.
class Noop : public ncnn::Layer {};
DEFINE_LAYER_CREATOR(Noop)
net.register_custom_layer("LinearRegressionOutput", Noop_layer_creator);
net.register_custom_layer("MAERegressionOutput", Noop_layer_creator);
You shall call Net::load_param() first, then Net::load_model().
This error may also happens when Net::load_param() failed, but not properly handled.
For more information about the ncnn model load api, see ncnn-load-model
The pointer passed to Net::load_param() or Net::load_model() is not 32bit aligned.
In practice, the head pointer of std::vector is not guaranteed to be 32bit aligned.
you can store your binary buffer in ncnn::Mat structure, its internal memory is aligned.
This usually happens if you bundle multiple shared library with openmp linked
It is actually an issue of the android ndk https://github.com/android/ndk/issues/1028
On old android ndk, modify the link flags as
-Wl,-Bstatic -lomp -Wl,-Bdynamic
For recent ndk >= 21
-fstatic-openmp
Newer android ndk defaults to dynamic openmp runtime
modify the link flags as
-fstatic-openmp -fopenmp
for optimal performance, the openmp threadpool spin waits for about a second prior to shutting down in case more work becomes available.
If you unload a dynamic library that's in the process of spin-waiting, it will crash in the manner you see (most of the time).
Just set OMP_WAIT_POLICY=passive in your environment, before calling loadlibrary. or Just wait a few seconds before calling freelibrary.
You can also use the following method to set environment variables in your code:
for msvc++:
SetEnvironmentVariable(_T("OMP_WAIT_POLICY"), _T("passive"));
for g++:
setenv("OMP_WAIT_POLICY", "passive", 1)
reference: https://stackoverflow.com/questions/34439956/vc-crash-when-freeing-a-dll-built-with-openmp
void pretty_print(const ncnn::Mat& m)
{
for (int q=0; q<m.c; q++)
{
const float* ptr = m.channel(q);
for (int y=0; y<m.h; y++)
{
for (int x=0; x<m.w; x++)
{
printf("%f ", ptr[x]);
}
ptr += m.w;
printf("\n");
}
printf("------------------------\n");
}
}
In Android Studio, printf will not work, you can use __android_log_print instead. Example :
#include <android/log.h> // Don't forget this
void pretty_print(const ncnn::Mat& m)
{
for (int q=0; q<m.c; q++)
{
for (int y=0; y<m.h; y++)
{
for (int x=0; x<m.w; x++)
{
__android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","ncnn Mat is : %f", m.channel(q).row(y)[x]);
}
}
}
}
void visualize(const char* title, const ncnn::Mat& m)
{
std::vector<cv::Mat> normed_feats(m.c);
for (int i=0; i<m.c; i++)
{
cv::Mat tmp(m.h, m.w, CV_32FC1, (void*)(const float*)m.channel(i));
cv::normalize(tmp, normed_feats[i], 0, 255, cv::NORM_MINMAX, CV_8U);
cv::cvtColor(normed_feats[i], normed_feats[i], cv::COLOR_GRAY2BGR);
// check NaN
for (int y=0; y<m.h; y++)
{
const float* tp = tmp.ptr<float>(y);
uchar* sp = normed_feats[i].ptr<uchar>(y);
for (int x=0; x<m.w; x++)
{
float v = tp[x];
if (v != v)
{
sp[0] = 0;
sp[1] = 0;
sp[2] = 255;
}
sp += 3;
}
}
}
int tw = m.w < 10 ? 32 : m.w < 20 ? 16 : m.w < 40 ? 8 : m.w < 80 ? 4 : m.w < 160 ? 2 : 1;
int th = (m.c - 1) / tw + 1;
cv::Mat show_map(m.h * th, m.w * tw, CV_8UC3);
show_map = cv::Scalar(127);
// tile
for (int i=0; i<m.c; i++)
{
int ty = i / tw;
int tx = i % tw;
normed_feats[i].copyTo(show_map(cv::Rect(tx * m.w, ty * m.h, m.w, m.h)));
}
cv::resize(show_map, show_map, cv::Size(0,0), 2, 2, cv::INTER_NEAREST);
cv::imshow(title, show_map);
}
复用 Extractor?!
net.opt.use_fp16_packed = false;
net.opt.use_fp16_storage = false;
net.opt.use_fp16_arithmetic = false;
https://github.com/Tencent/ncnn/wiki/FAQ-ncnn-produce-wrong-result
Post Training Quantization Tools
net.opt.openmp_blocktime = 0;
OMP_WAIT_POLICY=passive
先 extract 分类,判断后,再 extract bbox
net.opt.use_packing_layout = true;
net.opt.use_bf16_storage = true;
A53
https://github.com/Tencent/ncnn/wiki/build-minimal-library
对内存消耗的影响
| 软件类型 | 软件名称 |
|---|---|
| 系统 | Fedora |
| 桌面环境 | KDE |
| 编辑器 | Kate |
| 画草图 | kolourpaint |
| 画函数图像 | kmplot |
| bilibili直播 | OBS |