当前位置:网站首页>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
边栏推荐
- Redis - 7、事務操作
- 表格转换为LaTex格式
- 0179-Largest Number( 最大数)
- Deadlock found when trying to get lock; try restarting transaction 【MySQL死锁问题解决】
- getenv、setenv函数(获取和设置系统环境变量) 与 环境变量
- Nansen年度报告
- OpenCV调用usb摄像头出现“select timeout”解决方法
- [cloud native | kubernetes] Introduction to kubernetes (I)
- Oracle用游标分解号码次数
- Vue混写Minxin、虚拟Dom/ref、动态组件、缓存keep
猜你喜欢

Duanyongping, the "Buffett of China": a wise investment

第十三届 蓝桥杯 嵌入式设计与开发项目 决赛

Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation

What is homology??? Cross domain error??? How to solve???

【李宏毅】机器学习深度学习笔记 -- 训练模型通关攻略

【云原生 | Kubernetes篇】Kubernetes简介(一)

TIS教程01-安装

New progress in the construction of meituan's Flink based real-time data warehouse platform
![[high frequency written test questions] 513 Find the value in the lower left corner of the tree](/img/05/bedfb4343b93da32b81aeb0018e643.jpg)
[high frequency written test questions] 513 Find the value in the lower left corner of the tree

OceanBase数据库助力理想汽车智能生产线 可实现30秒内自动恢复
随机推荐
磁盘rejecting I/O to offline device故障导致4TB生产数据库无法访问
ONNX调研
linux设置 让oracle10g自启动
V4L2像素格式及其对应的含义
Redis - 11. Cluster
sql行列转换
Redis - 3、发布和订阅
运行sqoop1.4.5报Warning: does not exist! HCatalog jobs will fail.
Redis - 7、事务操作
分享7个免费超清的影视资源站!不管是剪辑还是珍藏都实用到哭!
ppt数据采集方法与分析技能
Foreign lead needs energy, interest, research, diligence and is indispensable
Find all prime numbers between 100 and 200
Struggle, programmer chapter 38 in the old days, when the road turned to the edge of the forest, I suddenly saw
查看gstreamer插件
SQL Server到Oracle连接服务器的实现_包括查询的实现
软件架构设计原则
Redis - 5. Jedis operation redis6
OGG12处理OGG-01163故障错误
OpenCV调用usb摄像头出现“select timeout”解决方法