当前位置:网站首页>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
边栏推荐
- Could not determine which parameter to assign generated keys to
- Invisible traffic Commander: on Urban Rail Transit Signal System
- mysql 执行脚本的正确姿势
- OGG-01431、01003、01151无奈的处理方式
- C#中,数组、List、ArrayList、Directory、LinkedList的区别与性能
- When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
- Latex希腊字母对照表
- 《Go题库·10》channel和锁的对比
- 客户成员价值分析
- Tensorflow2 模型优化环境快速搭建
猜你喜欢

Redis - 9、持久化之AOF(Append OnlyFile)

Endeavouros installation configuration introduction!

0179-Largest Number( 最大数)

LDA学习笔记

TIS教程04-客户端
![[an Xun cup 2019]iamthinking](/img/56/ffdcdeb8246d9ae7fb1eb72b45b614.png)
[an Xun cup 2019]iamthinking

磁盘rejecting I/O to offline device故障导致4TB生产数据库无法访问

Authenticated cookies, sessions, JWT

When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes

Redis - 12. Application problem solving
随机推荐
V4L2像素格式及其对应的含义
Software architecture design principles
表格转换为LaTex格式
Vue mixed write Minxin, virtual dom/ref, dynamic components, cache keep
Invisible traffic Commander: on Urban Rail Transit Signal System
How to count the growth of daily business data from the database level?
"Meng Hua Lu" has become the king of gold: advertisers can't line up with 5million yuan. Has Tencent video won?
Struggle, programmer chapter 38 in the old days, when the road turned to the edge of the forest, I suddenly saw
Nansen年度报告
查看gstreamer插件
4tb production database cannot be accessed due to disk rejecting i/o to offline device failure
oracle处理插入重复记录的技巧
Redis - 9. Persistent AOF (append onlyfile)
oracle存储过程2
DevSecOps: CI/CD 流水线安全的最佳实践
awk语言与oracle数据库交互取近半年情况
OGG12处理OGG-01163故障错误
论文解读——Scalable Collaborative Filtering with Jointly Derived Neighborhood Interpolation Weights
Redis - 7、事務操作
[安洵杯 2019]iamthinking