| 1 | |
| 2 | |
| 3 | # 如何加入技术交流QQ群? |
| 4 | |
| 5 | - 打开QQ→点击群聊搜索→搜索群号637093648→输入问题答案:卷卷卷卷卷→进入群聊→准备接受图灵测试(bushi) |
| 6 | - 前往QQ搜索Pocky群:677104663(超多大佬),问题答案:multi level intermediate representation |
| 7 | |
| 8 | # 如何看作者b站直播? |
| 9 | |
| 10 | - nihui的bilibili直播间:[水竹院落](https://live.bilibili.com/1264617) |
| 11 | |
| 12 | # 编译 |
| 13 | |
| 14 | - ## 怎样下载完整源码? |
| 15 | |
| 16 | git clone --recursive https://github.com/Tencent/ncnn/ |
| 17 | |
| 18 | 或者 |
| 19 | |
| 20 | 下载 [ncnn-xxxxx-full-source.zip](https://github.com/Tencent/ncnn/releases) |
| 21 | |
| 22 | - ## 怎么交叉编译?cmake 工具链怎么设置啊? |
| 23 | |
| 24 | 参见 https://github.com/Tencent/ncnn/wiki/how-to-build |
| 25 | |
| 26 | - ## The submodules were not downloaded! Please update submodules with "git submodule update --init" and try again |
| 27 | |
| 28 | 如上,下载完整源码。或者按提示执行: git submodule update --init |
| 29 | |
| 30 | - ## Could NOT find Protobuf (missing: Protobuf_INCLUDE_DIR) |
| 31 | |
| 32 | sudo apt-get install libprotobuf-dev protobuf-compiler |
| 33 | |
| 34 | - ## Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) |
| 35 | |
| 36 | https://github.com/Tencent/ncnn/issues/1873 |
| 37 | |
| 38 | - ## Could not find a package configuration file provided by "OpenCV" with any of the following names: OpenCVConfig.cmake opencv-config.cmake |
| 39 | |
| 40 | sudo apt-get install libopencv-dev |
| 41 | |
| 42 | 或者自行编译安装,set(OpenCV_DIR {OpenCVConfig.cmake所在目录}) |
| 43 | |
| 44 | - ## Could not find a package configuration file provided by "ncnn" with any of the following names: ncnnConfig.cmake ncnn-config.cmake |
| 45 | |
| 46 | set(ncnn_DIR {ncnnConfig.cmake所在目录}) |
| 47 | |
| 48 | - ## 找不到 Vulkan, |
| 49 | |
| 50 | cmake版本 3.10,否则没有带 FindVulkan.cmake |
| 51 | |
| 52 | android-api >= 24 |
| 53 | |
| 54 | macos 要先执行安装脚本 |
| 55 | |
| 56 | - ## 如何安装 vulkan sdk |
| 57 | |
| 58 | - ## 找不到库(需要根据系统/编译器指定) |
| 59 | |
| 60 | undefined reference to __kmpc_for_static_init_4 __kmpc_for_static_fini __kmpc_fork_call ... |
| 61 | |
| 62 | 需要链接openmp库 |
| 63 | |
| 64 | undefined reference to vkEnumerateInstanceExtensionProperties vkGetInstanceProcAddr vkQueueSubmit ... |
| 65 | |
| 66 | 需要 vulkan-1.lib |
| 67 | |
| 68 | undefined reference to glslang::InitializeProcess() glslang::TShader::TShader(EShLanguage) ... |
| 69 | |
| 70 | 需要 glslang.lib OGLCompiler.lib SPIRV.lib OSDependent.lib |
| 71 | |
| 72 | undefined reference to AAssetManager_fromJava AAssetManager_open AAsset_seek ... |
| 73 | |
| 74 | find_library和target_like_libraries中增加 android |
| 75 | |
| 76 | find_package(ncnn) |
| 77 | |
| 78 | - ## undefined reference to typeinfo for ncnn::Layer |
| 79 | |
| 80 | opencv rtti -> opencv-mobile |
| 81 | |
| 82 | - ## undefined reference to __cpu_model |
| 83 | |
| 84 | 升级编译器 / libgcc_s libgcc |
| 85 | |
| 86 | - ## unrecognized command line option "-mavx2" |
| 87 | |
| 88 | 升级 gcc |
| 89 | |
| 90 | - ## 为啥自己编译的ncnn android库特别大? |
| 91 | |
| 92 | https://github.com/Tencent/ncnn/wiki/build-for-android.zh 以及见 如何裁剪更小的 ncnn 库 |
| 93 | |
| 94 | - ## ncnnoptimize和自定义层 |
| 95 | |
| 96 | 先ncnnoptimize再增加自定义层,避免ncnnoptimize不能处理自定义层保存。 |
| 97 | |
| 98 | |
| 99 | - ## rtti/exceptions冲突 |
| 100 | |
| 101 | 产生原因是项目工程中使用的库配置不一样导致冲突,根据自己的实际情况分析是需要开启还是关闭。ncnn默认是ON,在重新编译ncnn时增加以下2个参数即可: |
| 102 | - 开启:-DNCNN_DISABLE_RTTI=OFF -DNCNN_DISABLE_EXCEPTION=OFF |
| 103 | - 关闭:-DNCNN_DISABLE_RTTI=ON -DNCNN_DISABLE_EXCEPTION=ON |
| 104 | |
| 105 | |
| 106 | - ## error: undefined symbol: ncnn::Extractor::extract(char const*, ncnn::Mat&) |
| 107 | |
| 108 | 可能的情况: |
| 109 | - 尝试升级 Android Studio 的 NDK 版本 |
| 110 | |
| 111 | - ## CMake 3.14.0 or higher is required. You are running version 2.8.12.2 |
| 112 | ```shell |
| 113 | wget https://github.com/Kitware/CMake/releases/download/v3.18.2/cmake-3.18.2-Linux-x86_64.tar.gz |
| 114 | tar zxvf cmake-3.18.2-Linux-x86_64.tar.gz |
| 115 | mv cmake-3.18.2-Linux-x86_64 /opt/cmake-3.18.2 |
| 116 | ln -sf /opt/cmake-3.18.2/bin/* /usr/bin/ |
| 117 | ``` |
| 118 | |
| 119 | # 怎样添加ncnn库到项目中?cmake方式怎么用? |
| 120 | |
| 121 | 编译ncnn,make install。linux/windows set/export ncnn_DIR 指向 install目录下包含ncnnConfig.cmake 的目录 |
| 122 | |
| 123 | - ## android |
| 124 | |
| 125 | - ## ios |
| 126 | |
| 127 | - ## linux |
| 128 | |
| 129 | - ## windows |
| 130 | |
| 131 | - ## macos |
| 132 | |
| 133 | - ## arm linux |
| 134 | |
| 135 | |
| 136 | # 转模型问题 |
| 137 | |
| 138 | - ## caffe |
| 139 | |
| 140 | `./caffe2ncnn caffe.prototxt caffe.caffemodel ncnn.param ncnn.bin` |
| 141 | |
| 142 | - ## mxnet |
| 143 | |
| 144 | ` ./mxnet2ncnn mxnet-symbol.json mxnet.params ncnn.param ncnn.bin` |
| 145 | |
| 146 | - ## darknet |
| 147 | |
| 148 | [https://github.com/xiangweizeng/darknet2ncnn](https://github.com/xiangweizeng/darknet2ncnn) |
| 149 | |
| 150 | - ## pytorch - onnx |
| 151 | |
| 152 | [use ncnn with pytorch or onnx](https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx) |
| 153 | |
| 154 | - ## tensorflow 1.x/2.x - keras |
| 155 | |
| 156 | [https://github.com/MarsTechHAN/keras2ncnn](https://github.com/MarsTechHAN/keras2ncnn) **[@MarsTechHAN](https://github.com/MarsTechHAN)** |
| 157 | |
| 158 | - ## tensorflow 2.x - mlir |
| 159 | |
| 160 | [通过MLIR将tensorflow2模型转换到ncnn](https://zhuanlan.zhihu.com/p/152535430) **@[nihui](https://www.zhihu.com/people/nihui-2)** |
| 161 | |
| 162 | - ## Shape not supported yet! Gather not supported yet! Cast not supported yet! |
| 163 | |
| 164 | onnx-simplifier 静态shape |
| 165 | |
| 166 | - ## convertmodel |
| 167 | |
| 168 | [https://convertmodel.com/](https://convertmodel.com/) **[@大老师](https://github.com/daquexian)** |
| 169 | |
| 170 | - ## netron |
| 171 | |
| 172 | [https://github.com/lutzroeder/netron](https://github.com/lutzroeder/netron) |
| 173 | |
| 174 | - ## 怎么生成有固定 shape 信息的模型? |
| 175 | |
| 176 | Input 0=w 1=h 2=c |
| 177 | |
| 178 | - ## why gpu能更快 |
| 179 | |
| 180 | - ## ncnnoptimize 怎么转成 fp16 模型 |
| 181 | |
| 182 | `ncnnoptimize model.param model.bin yolov5s-opt.param yolov5s-opt.bin 65536` |
| 183 | |
| 184 | - ## ncnnoptimize 怎样查看模型的 FLOPS / 内存占用情况 |
| 185 | |
| 186 | - ## 怎么修改模型支持动态 shape? |
| 187 | |
| 188 | Interp Reshape |
| 189 | |
| 190 | - ## 如何将模型转换为代码内嵌到程序里? |
| 191 | |
| 192 | ncnn2mem |
| 193 | |
| 194 | - ## 如何加密模型? |
| 195 | |
| 196 | https://zhuanlan.zhihu.com/p/268327784 |
| 197 | |
| 198 | - ## Linux下转的ncnn模型,Windows/MacOS/Android/.. 也能直接用吗? |
| 199 | |
| 200 | Yes,全平台通用 |
| 201 | |
| 202 | - ## 如何去掉后处理,再导出 onnx? |
| 203 | |
| 204 | 检测: |
| 205 | |
| 206 | 参考up的一篇文章<https://zhuanlan.zhihu.com/p/128974102>,步骤三就是去掉后处理,再导出onnx,其中去掉后处理可以是项目内测试时去掉后续步骤的结果。 |
| 207 | |
| 208 | - ## pytorch 有的层导不出 onnx 怎么办? |
| 209 | |
| 210 | 方式一: |
| 211 | |
| 212 | ONNX_ATEN_FALLBACK |
| 213 | 完全自定义的op,先改成能导出的(如 concat slice),转到 ncnn 后再修改 param |
| 214 | |
| 215 | 方式二: |
| 216 | |
| 217 | 可以使用PNNX来试试,参考以下文章大概说明: |
| 218 | |
| 219 | 1. [Windows/Linux/macOS 编译 PNNX 步骤](https://zhuanlan.zhihu.com/p/431833958) |
| 220 | |
| 221 | 2. [5分钟学会!用 PNNX 转换 TorchScript 模型到 ncnn 模型](https://zhuanlan.zhihu.com/p/427512763) |
| 222 | |
| 223 | # 使用 |
| 224 | |
| 225 | - ## vkEnumeratePhysicalDevices failed -3 |
| 226 | |
| 227 | - ## vkCreateInstance failed -9 |
| 228 | |
| 229 | 出现此类问题请先更新GPU驱动。Please upgrade your GPU driver if you encounter this crash or error. |
| 230 | 这里提供了一些品牌的GPU驱动下载网址.We have provided some drivers' download pages here. |
| 231 | [Intel](https://downloadcenter.intel.com/product/80939/Graphics-Drivers),[AMD](https://www.amd.com/en/support),[Nvidia](https://www.nvidia.com/Download/index.aspx) |
| 232 | |
| 233 | - ## ModuleNotFoundError: No module named 'ncnn.ncnn' |
| 234 | |
| 235 | python setup.py develop |
| 236 | |
| 237 | - ## fopen nanodet-m.param failed |
| 238 | |
| 239 | 文件路径 working dir |
| 240 | |
| 241 | File not found or not readable. Make sure that XYZ.param/XYZ.bin is accessible. |
| 242 | |
| 243 | - ## find_blob_index_by_name data / output / ... failed |
| 244 | |
| 245 | layer name vs blob name |
| 246 | |
| 247 | param.bin 应该用 xxx.id.h 的枚举 |
| 248 | |
| 249 | - ## parse magic failed |
| 250 | |
| 251 | - ## param is too old, please regenerate |
| 252 | |
| 253 | 模型本身有问题 |
| 254 | |
| 255 | Your model file is being the old format converted by an old caffe2ncnn tool. |
| 256 | |
| 257 | Checkout the latest ncnn code, build it and regenerate param and model binary files, and that should work. |
| 258 | |
| 259 | Make sure that your param file starts with the magic number 7767517. |
| 260 | |
| 261 | you may find more info on use-ncnn-with-alexnet |
| 262 | |
| 263 | When adding the softmax layer yourself, you need to add 1=1 |
| 264 | |
| 265 | - ## set_vulkan_compute failed, network use_vulkan_compute disabled |
| 266 | |
| 267 | 你应该在 load_param / load_model 之前设置 net.opt.use_vulkan_compute = true; |
| 268 | |
| 269 | - ## 多个blob输入,多个blob输出,怎么做? |
| 270 | 多次执行`ex.input()` 和 `ex.extract()` |
| 271 | ``` |
| 272 | ex.input("data1", in_1); |
| 273 | ex.input("data2", in_2); |
| 274 | ex.extract("output1", out_1); |
| 275 | ex.extract("output2", out_2); |
| 276 | ``` |
| 277 | - ## Extractor extract 多次会重复计算吗? |
| 278 | |
| 279 | 不会 |
| 280 | |
| 281 | - ## 如何看每一层的耗时? |
| 282 | |
| 283 | cmake -DNCNN_BENCHMARK=ON .. |
| 284 | |
| 285 | - ## 如何转换 cv::Mat CV_8UC3 BGR 图片 |
| 286 | |
| 287 | from_pixels to_pixels |
| 288 | |
| 289 | - ## 如何转换 float 数据为 ncnn::Mat |
| 290 | |
| 291 | 首先,自己申请的内存需要自己管理,此时ncnn::Mat不会自动给你释放你传过来的float数据 |
| 292 | ``` c++ |
| 293 | std::vector<float> testData(60, 1.0); // 利用std::vector<float>自己管理内存的申请和释放 |
| 294 | ncnn::Mat in1 = ncnn::Mat(60, (void*)testData.data()).reshape(4, 5, 3); // 把float数据的指针转成void*传过去即可,甚至还可以指定维度(up说最好使用reshape用来解决channel gap) |
| 295 | float* a = new float[60]; // 自己new一块内存,后续需要自己释放 |
| 296 | ncnn::Mat in2 = ncnn::Mat(60, (void*)a).reshape(4, 5, 3).clone(); // 使用方法和上面相同,clone() to transfer data owner |
| 297 | ``` |
| 298 | |
| 299 | - ## 如何初始化 ncnn::Mat 为全 0 |
| 300 | |
| 301 | `mat.fill(0.f);` |
| 302 | |
| 303 | - ## 如何查看/获取版本号 |
| 304 | |
| 305 | cmake时会打印 |
| 306 | |
| 307 | c_api.h ncnn_version() |
| 308 | |
| 309 | 自己拼 1.0+yyyymmdd |
| 310 | |
| 311 | - ## 如何转换 yuv 数据 |
| 312 | |
| 313 | yuv420sp2rgb yuv420sp2rgb_nv12 |
| 314 | |
| 315 | **[@metarutaiga](https://github.com/metarutaiga/xxYUV)** |
| 316 | |
| 317 | - ## 如何 resize crop rotate 图片 |
| 318 | |
| 319 | [efficient roi resize rotate](https://github.com/Tencent/ncnn/wiki/efficient-roi-resize-rotate) |
| 320 | |
| 321 | - ## 如何人脸5点对齐 |
| 322 | |
| 323 | get_affine_transform |
| 324 | |
| 325 | warpaffine_bilinear_c3 |
| 326 | |
| 327 | ```c |
| 328 | // 计算变换矩阵 并且求逆变换 |
| 329 | int type = 0; // 0->区域外填充为v[0],v[1],v[2], -233->区域外不处理 |
| 330 | unsigned int v = 0; |
| 331 | float tm[6]; |
| 332 | float tm_inv[6]; |
| 333 | // 人脸区域在原图上的坐标和宽高 |
| 334 | float src_x = target->det.rect.x / target->det.w * pIveImageU8C3->u32Width; |
| 335 | float src_y = target->det.rect.y / target->det.h * pIveImageU8C3->u32Height; |
| 336 | float src_w = target->det.rect.w / target->det.w * pIveImageU8C3->u32Width; |
| 337 | float src_h = target->det.rect.h / target->det.h * pIveImageU8C3->u32Height; |
| 338 | float point_src[10] = { |
| 339 | src_x + src_w * target->attr.land[0][0], src_x + src_w * target->attr.land[0][1], |
| 340 | src_x + src_w * target->attr.land[1][0], src_x + src_w * target->attr.land[1][1], |
| 341 | src_x + src_w * target->attr.land[2][0], src_x + src_w * target->attr.land[2][1], |
| 342 | src_x + src_w * target->attr.land[3][0], src_x + src_w * target->attr.land[3][1], |
| 343 | src_x + src_w * target->attr.land[4][0], src_x + src_w * target->attr.land[4][1], |
| 344 | }; |
| 345 | float point_dst[10] = { // +8 是因为我们处理112*112的图 |
| 346 | 30.2946f + 8.0f, 51.6963f, |
| 347 | 65.5318f + 8.0f, 51.5014f, |
| 348 | 48.0252f + 8.0f, 71.7366f, |
| 349 | 33.5493f + 8.0f, 92.3655f, |
| 350 | 62.7299f + 8.0f, 92.2041f, |
| 351 | }; |
| 352 | // 第一种方式:先计算变换在求逆 |
| 353 | AffineTrans::get_affine_transform(point_src, point_dst, 5, tm); |
| 354 | AffineTrans::invert_affine_transform(tm, tm_inv); |
| 355 | // 第二种方式:直接拿到求逆的结果 |
| 356 | // AffineTrans::get_affine_transform(point_dst, point_src, 5, tm_inv); |
| 357 | // rgb 分离的,所以要单独处理 |
| 358 | for(int c = 0; c < 3; c++) |
| 359 | { |
| 360 | unsigned char* pSrc = malloc(xxx); |
| 361 | unsigned char* pDst = malloc(xxx); |
| 362 | ncnn::warpaffine_bilinear_c1(pSrc, SrcWidth, SrcHeight, SrcStride[c], pDst, DstWidth, DstHeight, DstStride[c], tm_inv, type, v); |
| 363 | } |
| 364 | // rgb packed则可以一次处理 |
| 365 | ncnn::warpaffine_bilinear_c3(pSrc, SrcWidth, SrcHeight, SrcStride, pDst, DstWidth, DstHeight, DstStride, tm_inv, type, v); |
| 366 | ``` |
| 367 | |
| 368 | - ## 如何获得中间层的blob输出 |
| 369 | |
| 370 | ncnn::Mat output; |
| 371 | |
| 372 | ex.extract("your_blob_name", output); |
| 373 | |
| 374 | - ## 为什么我使用GPU,但是GPU占用为0 |
| 375 | |
| 376 | windows 10 任务管理器 - 性能选项卡 - GPU - 选择其中一个视图左上角的下拉箭头切换到 Compute_0 / Compute_1 / Cuda |
| 377 | |
| 378 | 你还可以安装软件:GPU-Z |
| 379 | |
| 380 | - ## layer XYZ not exists or registered |
| 381 | |
| 382 | Your network contains some operations that are not implemented in ncnn. |
| 383 | |
| 384 | You may implement them as custom layer followed in how-to-implement-custom-layer-step-by-step. |
| 385 | |
| 386 | Or you could simply register them as no-op if you are sure those operations make no sense. |
| 387 | |
| 388 | ``` |
| 389 | class Noop : public ncnn::Layer {}; |
| 390 | DEFINE_LAYER_CREATOR(Noop) |
| 391 | |
| 392 | net.register_custom_layer("LinearRegressionOutput", Noop_layer_creator); |
| 393 | net.register_custom_layer("MAERegressionOutput", Noop_layer_creator); |
| 394 | ``` |
| 395 | |
| 396 | - ## network graph not ready |
| 397 | |
| 398 | You shall call Net::load_param() first, then Net::load_model(). |
| 399 | |
| 400 | This error may also happens when Net::load_param() failed, but not properly handled. |
| 401 | |
| 402 | For more information about the ncnn model load api, see ncnn-load-model |
| 403 | |
| 404 | - ## memory not 32-bit aligned at XYZ |
| 405 | |
| 406 | The pointer passed to Net::load_param() or Net::load_model() is not 32bit aligned. |
| 407 | |
| 408 | In practice, the head pointer of std::vector is not guaranteed to be 32bit aligned. |
| 409 | |
| 410 | you can store your binary buffer in ncnn::Mat structure, its internal memory is aligned. |
| 411 | |
| 412 | - ## crash on android with '__kmp_abort_process' |
| 413 | |
| 414 | This usually happens if you bundle multiple shared library with openmp linked |
| 415 | |
| 416 | It is actually an issue of the android ndk https://github.com/android/ndk/issues/1028 |
| 417 | |
| 418 | On old android ndk, modify the link flags as |
| 419 | |
| 420 | -Wl,-Bstatic -lomp -Wl,-Bdynamic |
| 421 | |
| 422 | For recent ndk >= 21 |
| 423 | |
| 424 | -fstatic-openmp |
| 425 | |
| 426 | - ## dlopen failed: library "libomp.so" not found |
| 427 | Newer android ndk defaults to dynamic openmp runtime |
| 428 | |
| 429 | modify the link flags as |
| 430 | |
| 431 | -fstatic-openmp -fopenmp |
| 432 | |
| 433 | - ## crash when freeing a ncnn dynamic library(.dll/.so) built with openMP |
| 434 | |
| 435 | for optimal performance, the openmp threadpool spin waits for about a second prior to shutting down in case more work becomes available. |
| 436 | |
| 437 | 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). |
| 438 | |
| 439 | Just set OMP_WAIT_POLICY=passive in your environment, before calling loadlibrary. or Just wait a few seconds before calling freelibrary. |
| 440 | |
| 441 | You can also use the following method to set environment variables in your code: |
| 442 | |
| 443 | for msvc++: |
| 444 | |
| 445 | SetEnvironmentVariable(_T("OMP_WAIT_POLICY"), _T("passive")); |
| 446 | |
| 447 | for g++: |
| 448 | |
| 449 | setenv("OMP_WAIT_POLICY", "passive", 1) |
| 450 | |
| 451 | reference: https://stackoverflow.com/questions/34439956/vc-crash-when-freeing-a-dll-built-with-openmp |
| 452 | |
| 453 | # 跑出来的结果对不上 |
| 454 | |
| 455 | [ncnn-produce-wrong-result](https://github.com/Tencent/ncnn/wiki/FAQ-ncnn-produce-wrong-result) |
| 456 | |
| 457 | - ## 如何打印 ncnn::Mat 的值? |
| 458 | |
| 459 | ```C++ |
| 460 | void pretty_print(const ncnn::Mat& m) |
| 461 | { |
| 462 | for (int q=0; q<m.c; q++) |
| 463 | { |
| 464 | const float* ptr = m.channel(q); |
| 465 | for (int y=0; y<m.h; y++) |
| 466 | { |
| 467 | for (int x=0; x<m.w; x++) |
| 468 | { |
| 469 | printf("%f ", ptr[x]); |
| 470 | } |
| 471 | ptr += m.w; |
| 472 | printf("\n"); |
| 473 | } |
| 474 | printf("------------------------\n"); |
| 475 | } |
| 476 | } |
| 477 | ``` |
| 478 | In Android Studio, `printf` will not work, you can use `__android_log_print` instead. Example : |
| 479 | ```C++ |
| 480 | #include <android/log.h> // Don't forget this |
| 481 | |
| 482 | void pretty_print(const ncnn::Mat& m) |
| 483 | { |
| 484 | for (int q=0; q<m.c; q++) |
| 485 | { |
| 486 | for (int y=0; y<m.h; y++) |
| 487 | { |
| 488 | for (int x=0; x<m.w; x++) |
| 489 | { |
| 490 | __android_log_print(ANDROID_LOG_DEBUG,"LOG_TAG","ncnn Mat is : %f", m.channel(q).row(y)[x]); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | ``` |
| 496 | |
| 497 | - ## 如何可视化 ncnn::Mat 的值? |
| 498 | |
| 499 | ``` |
| 500 | void visualize(const char* title, const ncnn::Mat& m) |
| 501 | { |
| 502 | std::vector<cv::Mat> normed_feats(m.c); |
| 503 | |
| 504 | for (int i=0; i<m.c; i++) |
| 505 | { |
| 506 | cv::Mat tmp(m.h, m.w, CV_32FC1, (void*)(const float*)m.channel(i)); |
| 507 | |
| 508 | cv::normalize(tmp, normed_feats[i], 0, 255, cv::NORM_MINMAX, CV_8U); |
| 509 | |
| 510 | cv::cvtColor(normed_feats[i], normed_feats[i], cv::COLOR_GRAY2BGR); |
| 511 | |
| 512 | // check NaN |
| 513 | for (int y=0; y<m.h; y++) |
| 514 | { |
| 515 | const float* tp = tmp.ptr<float>(y); |
| 516 | uchar* sp = normed_feats[i].ptr<uchar>(y); |
| 517 | for (int x=0; x<m.w; x++) |
| 518 | { |
| 519 | float v = tp[x]; |
| 520 | if (v != v) |
| 521 | { |
| 522 | sp[0] = 0; |
| 523 | sp[1] = 0; |
| 524 | sp[2] = 255; |
| 525 | } |
| 526 | |
| 527 | sp += 3; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | int tw = m.w < 10 ? 32 : m.w < 20 ? 16 : m.w < 40 ? 8 : m.w < 80 ? 4 : m.w < 160 ? 2 : 1; |
| 533 | int th = (m.c - 1) / tw + 1; |
| 534 | |
| 535 | cv::Mat show_map(m.h * th, m.w * tw, CV_8UC3); |
| 536 | show_map = cv::Scalar(127); |
| 537 | |
| 538 | // tile |
| 539 | for (int i=0; i<m.c; i++) |
| 540 | { |
| 541 | int ty = i / tw; |
| 542 | int tx = i % tw; |
| 543 | |
| 544 | normed_feats[i].copyTo(show_map(cv::Rect(tx * m.w, ty * m.h, m.w, m.h))); |
| 545 | } |
| 546 | |
| 547 | cv::resize(show_map, show_map, cv::Size(0,0), 2, 2, cv::INTER_NEAREST); |
| 548 | cv::imshow(title, show_map); |
| 549 | } |
| 550 | ``` |
| 551 | |
| 552 | - ## 总是输出第一张图的结果 |
| 553 | |
| 554 | 复用 Extractor?! |
| 555 | |
| 556 | - ## 启用fp16时的精度有差异 |
| 557 | |
| 558 | net.opt.use_fp16_packed = false; |
| 559 | |
| 560 | net.opt.use_fp16_storage = false; |
| 561 | |
| 562 | net.opt.use_fp16_arithmetic = false; |
| 563 | |
| 564 | [ncnn-produce-wrong-result](https://github.com/Tencent/ncnn/wiki/FAQ-ncnn-produce-wrong-result) |
| 565 | |
| 566 | |
| 567 | # 如何跑得更快?内存占用更少?库体积更小? |
| 568 | |
| 569 | - ## fp32 fp16 |
| 570 | |
| 571 | - ## 大小核绑定 |
| 572 | ncnn::set_cpu_powersave(int)绑定大核或小核 |
| 573 | 注意windows系统不支持绑核。 |
| 574 | ncnn支持不同的模型运行在不同的核心。假设硬件平台有2个大核,4个小核,你想把netA运行在大核,netB运行在小核。 |
| 575 | 可以通过std::thread or pthread创建两个线程,运行如下代码: |
| 576 | 0:全部 |
| 577 | 1:小核 |
| 578 | 2:大核 |
| 579 | ``` |
| 580 | void thread_1() |
| 581 | { |
| 582 | ncnn::set_cpu_powersave(2); // bind to big cores |
| 583 | netA.opt.num_threads = 2; |
| 584 | } |
| 585 | |
| 586 | void thread_2() |
| 587 | { |
| 588 | ncnn::set_cpu_powersave(1); // bind to little cores |
| 589 | netB.opt.num_threads = 4; |
| 590 | } |
| 591 | ``` |
| 592 | |
| 593 | [openmp-best-practice.zh.md](https://github.com/Tencent/ncnn/blob/master/docs/how-to-use-and-FAQ/openmp-best-practice.zh.md) |
| 594 | |
| 595 | - ## 查看 CPU 或 GPU 数量 |
| 596 | get_cpu_count |
| 597 | |
| 598 | get_gpu_count |
| 599 | |
| 600 | - ## ncnnoptimize |
| 601 | |
| 602 | 使用方式一: |
| 603 | - ./ncnnoptimize ncnn.param ncnn.bin new.param new.bin flag |
| 604 | <br/>注意这里的flag指的是fp32和fp16,其中0指的是fp32,1指的是fp16 |
| 605 | |
| 606 | 使用方式二: |
| 607 | - ./ncnnoptimize ncnn.param ncnn.bin new.param new.bin flag cutstartname cutendname |
| 608 | <br/>cutstartname:模型截取的起点 |
| 609 | <br/>cutendname:模型截取的终点 |
| 610 | |
| 611 | |
| 612 | - ## 如何使用量化工具? |
| 613 | |
| 614 | [Post Training Quantization Tools](https://github.com/Tencent/ncnn/tree/master/tools/quantize) |
| 615 | |
| 616 | - ## 如何设置线程数? |
| 617 | |
| 618 | opt.num_threads |
| 619 | |
| 620 | - ## 如何降低CPU占用率? |
| 621 | |
| 622 | net.opt.openmp_blocktime = 0; |
| 623 | |
| 624 | OMP_WAIT_POLICY=passive |
| 625 | |
| 626 | - ## 如何 batch inference? |
| 627 | |
| 628 | ``` |
| 629 | int max_batch_size = vkdev->info.compute_queue_count; |
| 630 | |
| 631 | ncnn::Mat inputs[1000]; |
| 632 | ncnn::Mat outputs[1000]; |
| 633 | |
| 634 | #pragma omp parallel for num_threads(max_batch_size) |
| 635 | for (int i=0; i<1000; i++) |
| 636 | { |
| 637 | ncnn::Extractor ex = net1.create_extractor(); |
| 638 | ex.input("data", inputs[i]); |
| 639 | ex.extract("prob", outputs[i]); |
| 640 | } |
| 641 | ``` |
| 642 | |
| 643 | |
| 644 | |
| 645 | - ## partial graph inference |
| 646 | |
| 647 | 先 extract 分类,判断后,再 extract bbox |
| 648 | |
| 649 | - ## 如何启用 bf16s 加速? |
| 650 | |
| 651 | ``` |
| 652 | net.opt.use_packing_layout = true; |
| 653 | net.opt.use_bf16_storage = true; |
| 654 | ``` |
| 655 | |
| 656 | [用bf16加速ncnn](https://zhuanlan.zhihu.com/p/112564372) **@[nihui](https://www.zhihu.com/people/nihui-2)** |
| 657 | |
| 658 | A53 |
| 659 | |
| 660 | - ## 如何裁剪更小的 ncnn 库? |
| 661 | |
| 662 | [build-minimal-library](https://github.com/Tencent/ncnn/wiki/build-minimal-library) |
| 663 | |
| 664 | - ## net.opt sgemm winograd fp16_storage 各是有什么作用? |
| 665 | |
| 666 | 对内存消耗的影响 |
| 667 | |
| 668 | - ## 如何解决显卡进入节能模式造成的一系列问题? |
| 669 | |
| 670 | nVidia显卡(Intel和AMD估计也有)会在它认为的所谓空闲模式下,自动进入 `节能模式`,显存和核心频率就都会降低。 |
| 671 | |
| 672 | 简单来说就是如果你的计算任务是 `非连续的`,那么可能会让耗时看起来非常 `不均匀`,当期间有运算空闲间隔发生,显卡进入节能模式,则会在下一次冷启动时发生计算耗时远超正常耗时几倍的情况,如下日志所示: |
| 673 | |
| 674 | ```cpp |
| 675 | //开始播放 |
| 676 | Total: 162ms, Diff: 0ms, GLTex2Mat: 7ms, calc: 152ms, Mat2GLTex: 3ms |
| 677 | Total: 43ms, Diff: 0ms, GLTex2Mat: 3ms, calc: 35ms, Mat2GLTex: 2ms |
| 678 | Total: 45ms, Diff: 0ms, GLTex2Mat: 3ms, calc: 37ms, Mat2GLTex: 3ms |
| 679 | Total: 40ms, Diff: 0ms, GLTex2Mat: 3ms, calc: 32ms, Mat2GLTex: 4ms |
| 680 | //暂停3秒 |
| 681 | //继续播放 |
| 682 | Total: 190ms, Diff: 0ms, GLTex2Mat: 9ms, calc: 177ms, Mat2GLTex: 3ms |
| 683 | Total: 134ms, Diff: 0ms, GLTex2Mat: 5ms, calc: 110ms, Mat2GLTex: 18ms |
| 684 | Total: 40ms, Diff: 0ms, GLTex2Mat: 3ms, calc: 34ms, Mat2GLTex: 2ms |
| 685 | Total: 42ms, Diff: 0ms, GLTex2Mat: 3ms, calc: 36ms, Mat2GLTex: 2ms |
| 686 | Total: 47ms, Diff: 0ms, GLTex2Mat: 5ms, calc: 38ms, Mat2GLTex: 3ms |
| 687 | ... |
| 688 | ``` |
| 689 | |
| 690 | 在对时间不敏感的项目上,这个问题没什么大不了的,完全可以忽略,但是有些业务场景上必须精准推估下一帧及其未来几帧的从上传、计算到渲染的耗时情况,则这种现象将会给开发者打开些许困扰。 |
| 691 | |
| 692 | ### 3种解决方法 |
| 693 | * 联系显卡厂商,让其更新驱动将你的应用加入到免节能模式的白名单。 |
| 694 | * 优点:你什么都不用改。缺点:沟通困难,很可能显卡厂商根本不理你。 |
| 695 | * [显卡控制面板] - [管理3D设置] - [电源管理模式],改成:[最高性能优先]。 |
| 696 | * 优点:不用改代码。缺点:如果是部署端是小白用户,需要编写手册手把手教他。 |
| 697 | * 可以空闲(暂停)时定期灌一些心跳计算包的任务进去(放1x1小图)让GPU维持在高性能状态。 |
| 698 | * 优点:需要改代码。缺点:不低碳不环保。 |
| 699 | |
| 700 | # 白嫖项目 |
| 701 | |
| 702 | - ## nanodet |
| 703 | |
| 704 | # 其他 |
| 705 | |
| 706 | - ## up主用的什么系统/编辑器/开发环境? |
| 707 | |
| 708 | | 软件类型 | 软件名称 | |
| 709 | | ------------| ----------- | |
| 710 | | 系统 | Fedora | |
| 711 | | 桌面环境 | KDE | |
| 712 | | 编辑器 | Kate | |
| 713 | | 画草图 | kolourpaint | |
| 714 | | 画函数图像 | kmplot | |
| 715 | | bilibili直播 | OBS | |