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