NCNN source code learning [1]: learning sequence

I have been doing model deployment work recently, mainly using NCNN to deploy to Android. I followed online blogs and open source projects, did a lot of work, and learned a lot. However, I did not study the source code of NCNN carefully. However, to me, it seems to be a mysterious existence. Next, I will study the source code of NCNN to deepen my understanding of NCNN.

text

code version

As of now, ncnn has been around for almost 5 years, and the code is getting bigger and bigger, and many of them support newly added functions. Reading the code this time, we are more concerned about the entire architecture of ncnn and how its code logic works, so here I chose the earliest release code of ncnn. The overall usage logic of this code is the same as the current one, and The amount of code is relatively small and it seems more convenient. I use this:
https://github.com/Tencent/ncnn/releases/tag/20170724
Insert image description here

[demo]
Let’s take a look at the code of a classic demo that comes with ncnn: squeezenet. Here I only post the parts related to ncnn:

// 网络加载
ncnn::Net squeezenet;
squeezenet.load_param("squeezenet_v1.1.param");
squeezenet.load_model("squeezenet_v1.1.bin");

// 数据预处理
ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR, image.cols, image.rows, 227, 227);
const float mean_vals[3] = {
    
     104.f, 117.f, 123.f };
in.substract_mean_normalize(mean_vals, 0);

// 网络推理
ncnn::Extractor ex = squeezenet.create_extractor();
ex.set_light_mode(true);
ex.input("data", in);
ncnn::Mat out;
ex.extract("prob", out);

Insert image description here

As you can see from the above code, the entire process can be roughly divided into four parts:

1. Network loading

  1. load_param
  2. load_model

2. Data preprocessing

  1. from_pixels_resize
  2. substract_mean_normalize

3. Network reasoning

  1. create_extractor
  2. input
  3. extract

4. Inference at each level (implicit in the model)

The next learning sequence is also very simple. It mainly follows the code designed in these four parts, and goes in layer by layer to read and study.

reference:

Acknowledgments: https://zhuanlan.zhihu.com/p/454835595

Guess you like

Origin blog.csdn.net/s1_0_2_4/article/details/134951504