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.4 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 z=0; z<m.d; z++)
  36. {
  37. for (int y=0; y<m.h; y++)
  38. {
  39. for (int x=0; x<m.w; x++)
  40. {
  41. printf("%f ", ptr[x]);
  42. }
  43. ptr += m.w;
  44. printf("\n");
  45. }
  46. printf("\n");
  47. }
  48. printf("------------------------\n");
  49. }
  50. }
  51. ```
  52. ### visualize Mat content
  53. ```cpp
  54. void visualize(const char* title, const ncnn::Mat& m)
  55. {
  56. std::vector<cv::Mat> normed_feats(m.c);
  57. for (int i=0; i<m.c; i++)
  58. {
  59. cv::Mat tmp(m.h, m.w, CV_32FC1, (void*)(const float*)m.channel(i));
  60. cv::normalize(tmp, normed_feats[i], 0, 255, cv::NORM_MINMAX, CV_8U);
  61. cv::cvtColor(normed_feats[i], normed_feats[i], cv::COLOR_GRAY2BGR);
  62. // check NaN
  63. for (int y=0; y<m.h; y++)
  64. {
  65. const float* tp = tmp.ptr<float>(y);
  66. uchar* sp = normed_feats[i].ptr<uchar>(y);
  67. for (int x=0; x<m.w; x++)
  68. {
  69. float v = tp[x];
  70. if (v != v)
  71. {
  72. sp[0] = 0;
  73. sp[1] = 0;
  74. sp[2] = 255;
  75. }
  76. sp += 3;
  77. }
  78. }
  79. }
  80. int tw = m.w < 10 ? 32 : m.w < 20 ? 16 : m.w < 40 ? 8 : m.w < 80 ? 4 : m.w < 160 ? 2 : 1;
  81. int th = (m.c - 1) / tw + 1;
  82. cv::Mat show_map(m.h * th, m.w * tw, CV_8UC3);
  83. show_map = cv::Scalar(127);
  84. // tile
  85. for (int i=0; i<m.c; i++)
  86. {
  87. int ty = i / tw;
  88. int tx = i % tw;
  89. normed_feats[i].copyTo(show_map(cv::Rect(tx * m.w, ty * m.h, m.w, m.h)));
  90. }
  91. cv::resize(show_map, show_map, cv::Size(0,0), 2, 2, cv::INTER_NEAREST);
  92. cv::imshow(title, show_map);
  93. }
  94. ```
  95. ### FAQ
  96. Q ncnn的起源
  97. A 深度学习算法要在手机上落地,caffe依赖太多,手机上也没有cuda,需要个又快又小的前向网络实现
  98. Q ncnn名字的来历
  99. A cnn就是卷积神经网络的缩写,开头的n算是一语n关。比如new/next(全新的实现),naive(ncnn是naive实现),neon(ncnn最初为手机优化),up主名字(←_←)
  100. Q 支持哪些平台
  101. A 跨平台,支持 android / ios / linux / windows / macos,也支持裸机跑
  102. Q 计算精度如何
  103. A armv7 neon float 不遵照 ieee754 标准,有些采用快速实现(如exp sin等),速度快但确保精度足够高
  104. Q logo
  105. A up主是mc玩家,所以灵魂手绘像素猫,还可以找到ncnn...