You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Home.md 3.9 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ### input data and extract output
  2. ```cpp
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include "net.h"
  6. int main()
  7. {
  8. cv::Mat img = cv::imread("image.ppm", CV_LOAD_IMAGE_GRAYSCALE);
  9. int w = img.cols;
  10. int h = img.rows;
  11. // subtract 128, norm to -1 ~ 1
  12. ncnn::Mat in = ncnn::Mat::from_pixels_resize(img.data, ncnn::Mat::PIXEL_GRAY, w, h, 60, 60);
  13. float mean[1] = { 128.f };
  14. float norm[1] = { 1/128.f };
  15. in.substract_mean_normalize(mean, norm);
  16. ncnn::Net net;
  17. net.load_param("model.param");
  18. net.load_model("model.bin");
  19. ncnn::Extractor ex = net.create_extractor();
  20. ex.set_light_mode(true);
  21. ex.set_num_threads(4);
  22. ex.input("data", in);
  23. ncnn::Mat feat;
  24. ex.extract("output", feat);
  25. return 0;
  26. }
  27. ```
  28. ### print Mat content
  29. ```cpp
  30. void pretty_print(const ncnn::Mat& m)
  31. {
  32. for (int q=0; q<m.c; q++)
  33. {
  34. const float* ptr = m.channel(q);
  35. for (int y=0; y<m.h; y++)
  36. {
  37. for (int x=0; x<m.w; x++)
  38. {
  39. printf("%f ", ptr[x]);
  40. }
  41. ptr += m.w;
  42. printf("\n");
  43. }
  44. printf("------------------------\n");
  45. }
  46. }
  47. ```
  48. ### visualize Mat content
  49. ```cpp
  50. void visualize(const char* title, const ncnn::Mat& m)
  51. {
  52. std::vector<cv::Mat> normed_feats(m.c);
  53. for (int i=0; i<m.c; i++)
  54. {
  55. cv::Mat tmp(m.h, m.w, CV_32FC1, (void*)(const float*)m.channel(i));
  56. cv::normalize(tmp, normed_feats[i], 0, 255, cv::NORM_MINMAX, CV_8U);
  57. cv::cvtColor(normed_feats[i], normed_feats[i], cv::COLOR_GRAY2BGR);
  58. // check NaN
  59. for (int y=0; y<m.h; y++)
  60. {
  61. const float* tp = tmp.ptr<float>(y);
  62. uchar* sp = normed_feats[i].ptr<uchar>(y);
  63. for (int x=0; x<m.w; x++)
  64. {
  65. float v = tp[x];
  66. if (v != v)
  67. {
  68. sp[0] = 0;
  69. sp[1] = 0;
  70. sp[2] = 255;
  71. }
  72. sp += 3;
  73. }
  74. }
  75. }
  76. int tw = m.w < 10 ? 32 : m.w < 20 ? 16 : m.w < 40 ? 8 : m.w < 80 ? 4 : m.w < 160 ? 2 : 1;
  77. int th = (m.c - 1) / tw + 1;
  78. cv::Mat show_map(m.h * th, m.w * tw, CV_8UC3);
  79. show_map = cv::Scalar(127);
  80. // tile
  81. for (int i=0; i<m.c; i++)
  82. {
  83. int ty = i / tw;
  84. int tx = i % tw;
  85. normed_feats[i].copyTo(show_map(cv::Rect(tx * m.w, ty * m.h, m.w, m.h)));
  86. }
  87. cv::resize(show_map, show_map, cv::Size(0,0), 2, 2, cv::INTER_NEAREST);
  88. cv::imshow(title, show_map);
  89. }
  90. ```
  91. ### caffe-android-lib+openblas vs ncnn
  92. use squeezenet v1.1, nexus6p, android 7.1.2
  93. memory usage is the RSS item in top utility output
  94. |compare item|caffe-android-lib+openblas|ncnn|
  95. |---|---|---|
  96. |inference time(1 thread)|228ms|88ms|
  97. |inference time(8 thread)|152ms|38ms|
  98. |memory usage|138.16M|21.56M|
  99. |library binary size|6.9M|<500K|
  100. |compatibility|armeabi-v7a-hard with neon or arm64-v8a|armeabi-v7a with neon or arm64-v8a|
  101. |thirdparty dependency|boost gflags glog lmdb openblas opencv protobuf|none|
  102. ### FAQ
  103. Q ncnn的起源
  104. A 深度学习算法要在手机上落地,caffe依赖太多,手机上也没有cuda,需要个又快又小的前向网络实现
  105. Q ncnn名字的来历
  106. A cnn就是卷积神经网络的缩写,开头的n算是一语n关。比如new/next(全新的实现),naive(ncnn是naive实现),neon(ncnn最初为手机优化),up主名字(←_←)
  107. Q 支持哪些平台
  108. A 跨平台,主要支持 android,次要支持 ios / linux / windows
  109. Q 计算精度如何
  110. A armv7 neon float 不遵照 ieee754 标准,有些采用快速实现(如exp sin等),速度快但确保精度足够高
  111. Q pc 上的速度很慢
  112. A pc都是x86架构的,基本没做什么优化,主要用来核对结果,毕竟up主精力是有限的(
  113. Q logo
  114. A up主是mc玩家,所以灵魂手绘像素猫,还可以找到ncnn...