当前位置:网站首页>Use of ncnn (compulsory for beginners)

Use of ncnn (compulsory for beginners)

2022-06-22 12:24:00 Alen. Wang

use ncnn with alexnet.zh

wiki-sync-bot edited this page 11 hours ago · 1 revision

First , Thank you very much for ncnn Component concerns For the convenience of everyone ncnn Components ,up The Lord specially wrote this article using North pointing , In a rotten street alexnet As an example

Get ready caffe Networks and models

caffe The network and model are usually trained by researchers engaged in deep learning , Generally speaking, there will be

train.prototxt
deploy.prototxt
snapshot_10000.caffemodel

When deploying, you only need TEST The process , So there is deploy.prototxt and caffemodel That's enough

alexnet Of deploy.prototxt Download here  https://github.com/BVLC/caffe/tree/master/models/bvlc_alexnet

alexnet Of caffemodel Download here  http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel

transformation ncnn Networks and models

caffe With its own tools, you can put the old version of caffe Network and model conversion to new version (ncnn Our tools only know the new version

upgrade_net_proto_text [ The old prototxt] [ new prototxt]
upgrade_net_proto_binary [ The old caffemodel] [ new caffemodel]

Input layer uses Input, Because you only need to make one picture at a time , So the first one dim Set to 1

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 3 dim: 227 dim: 227 } }
}

Use caffe2ncnn Tools are converted to ncnn Network description and model

caffe2ncnn deploy.prototxt bvlc_alexnet.caffemodel alexnet.param alexnet.bin

Remove visible strings

Yes param and bin The file is actually ready to use , however param The description file is clear text , If you put it in APP It is easy to spy on the network structure when distributed ( It seems as if you can't see it without writing Use ncnn2mem Tools convert to binary description files and memory models , Generate alexnet.param.bin And two static array code files

ncnn2mem alexnet.param alexnet.bin alexnet.id.h alexnet.mem.h

Load model

Direct load param and bin, It is suitable for rapid verification of effect

ncnn::Net net;
net.load_param("alexnet.param");
net.load_model("alexnet.bin");

Load binary param.bin and bin, No visible strings , fit APP Distribute model resources

ncnn::Net net;
net.load_param_bin("alexnet.param.bin");
net.load_model("alexnet.bin");

Load networks and models from memory references , No visible strings , The model data is all in the code , No external files in addition ,android apk The packaged resource file is read out as a memory block

#include "alexnet.mem.h"
ncnn::Net net;
net.load_param(alexnet_param_bin);
net.load_model(alexnet_bin);

All three of the above can be loaded into the model , The memory reference mode loading is zero-copy Of , So use net The source memory block of the model must exist

Unload the model

net.clear();

Input and output

ncnn Use your own data structure Mat To store input and output data The data of the input image should be converted into Mat, Subtract the mean and multiplication coefficients as needed

#include "mat.h"
unsigned char* rgbdata;// data pointer to RGB image pixels
int w;// image width
int h;// image height
ncnn::Mat in = ncnn::Mat::from_pixels(rgbdata, ncnn::Mat::PIXEL_RGB, w, h);

const float mean_vals[3] = {104.f, 117.f, 123.f};
in.substract_mean_normalize(mean_vals, 0);

Perform forward networking , Get the calculation results

#include "net.h"
ncnn::Mat in;// input blob as above
ncnn::Mat out;
ncnn::Extractor ex = net.create_extractor();
ex.set_light_mode(true);
ex.input("data", in);
ex.extract("prob", out);

If it's binary param.bin The way , No visible strings , utilize alexnet.id.h Instead of blob Name

#include "net.h"
#include "alexnet.id.h"
ncnn::Mat in;// input blob as above
ncnn::Mat out;
ncnn::Extractor ex = net.create_extractor();
ex.set_light_mode(true);
ex.input(alexnet_param_id::BLOB_data, in);
ex.extract(alexnet_param_id::BLOB_prob, out);

obtain Mat Output data in ,Mat Internal data is usually three-dimensional ,c / h / w, Traverse all to get the scores of all categories

ncnn::Mat out_flatterned = out.reshape(out.w * out.h * out.c);
std::vector<float> scores;
scores.resize(out_flatterned.w);
for (int j=0; j<out_flatterned.w; j++)
{
    scores[j] = out_flatterned[j];
}

Some tips

Extractor There is a switch for multithreading acceleration , Setting the number of threads can speed up the calculation

ex.set_num_threads(4);

Mat When converting images, you can also convert colors and zoom sizes , These incidental operations are also optimized Support RGB2GRAY GRAY2RGB RGB2BGR And other common conversions , Support zoom out and zoom in

#include "mat.h"
unsigned char* rgbdata;// data pointer to RGB image pixels
int w;// image width
int h;// image height
int target_width = 227;// target resized width
int target_height = 227;// target resized height
ncnn::Mat in = ncnn::Mat::from_pixels_resize(rgbdata, ncnn::Mat::PIXEL_RGB2GRAY, w, h, target_width, target_height);

Net Have from FILE* The file describes the loaded interface , You can use this to combine multiple network and model files into one , It will be more convenient to distribute , Memory references don't matter

$ cat alexnet.param.bin alexnet.bin > alexnet-all.bin

#include "net.h"
FILE* fp = fopen("alexnet-all.bin", "rb");
net.load_param_bin(fp);
net.load_model(fp);
fclose(fp);

 

from :https://github.com/Tencent/ncnn/wiki/use-ncnn-with-alexnet.zh

原网站

版权声明
本文为[Alen. Wang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221155200583.html