ncnn paramdictmodelbin
paramdict、modelbin加载模型并存储模型形式:字典及binary。
参数字典私有类ParamDictPrivate
class ParamDictPrivate
{
public:struct{// 0 = null// 1 = int/float// 2 = int// 3 = float// 4 = array of int/float// 5 = array of int// 6 = array of floatint type;//参数类型存放union{int i;float f;};Mat v;//参数值存放} params[NCNN_MAX_PARAM_COUNT];//32
};
union用法:C++中union的使用方法_棉猴的博客-CSDN博客_c++ union
ParamDictPrivate类中公有结构体内部再有联合体。
结构体嵌套联合体-字节对齐
联合体字节对齐:取内部类型字节数最大的(因其内部所有的变量共用初始地址且每次只能对其中一个变量赋值起作用,多个变量同时赋值,只有最后被赋值的变量有效,其余为0)
结构体字节对齐:需要考虑内部变量类型字节数累加与当前变量类型字节数对齐以及整体字节数对齐。
参数字典类ParamDict
class NCNN_EXPORT ParamDict
{
public:// emptyParamDict();virtual ~ParamDict();// copyParamDict(const ParamDict&);// assignParamDict& operator=(const ParamDict&);// get typeint type(int id) const;// get intint get(int id, int def) const;// get floatfloat get(int id, float def) const;// get arrayMat get(int id, const Mat& def) const;// set intvoid set(int id, int i);// set floatvoid set(int id, float f);// set arrayvoid set(int id, const Mat& v);protected:friend class Net;void clear();int load_param(const DataReader& dr);int load_param_bin(const DataReader& dr);private:ParamDictPrivate* const d;
};
加载模型,解析模型文件将值及类型分别存储到mat和type中。
int ParamDict::load_param(const DataReader& dr)
{clear();// 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0// parse each key=value pairint id = 0;while (dr.scan("%d=", &id) == 1){bool is_array = id <= -23300;if (is_array){id = -id - 23300;}if (id >= NCNN_MAX_PARAM_COUNT){NCNN_LOGE("id < NCNN_MAX_PARAM_COUNT failed (id=%d, NCNN_MAX_PARAM_COUNT=%d)", id, NCNN_MAX_PARAM_COUNT);return -1;}if (is_array){int len = 0;int nscan = dr.scan("%d", &len);if (nscan != 1){NCNN_LOGE("ParamDict read array length failed");return -1;}d->params[id].v.create(len);for (int j = 0; j < len; j++){char vstr[16];nscan = dr.scan(",%15[^,\n ]", vstr);if (nscan != 1){NCNN_LOGE("ParamDict read array element failed");return -1;}bool is_float = vstr_is_float(vstr);if (is_float){float* ptr = d->params[id].v;ptr[j] = vstr_to_float(vstr);}else{int* ptr = d->params[id].v;nscan = sscanf(vstr, "%d", &ptr[j]);if (nscan != 1){NCNN_LOGE("ParamDict parse array element failed");return -1;}}d->params[id].type = is_float ? 6 : 5;}}else{char vstr[16];int nscan = dr.scan("%15s", vstr);if (nscan != 1){NCNN_LOGE("ParamDict read value failed");return -1;}bool is_float = vstr_is_float(vstr);if (is_float){d->params[id].f = vstr_to_float(vstr);}else{nscan = sscanf(vstr, "%d", &d->params[id].i);if (nscan != 1){NCNN_LOGE("ParamDict parse value failed");return -1;}}d->params[id].type = is_float ? 3 : 2;}}return 0;
}
ModelBin抽象类
由ModelBinFromDataReader 和ModelBinFromMatArray 派生类分别实现。
ModelBinFromDataReader 直接读取模型文件存储bin形式
ModelBinFromMatArray 从Mat类型转存bin形式。
class NCNN_EXPORT ModelBin
{
public:ModelBin();virtual ~ModelBin();// element type// 0 = auto// 1 = float32// 2 = float16// 3 = int8// load vecvirtual Mat load(int w, int type) const = 0;// load imagevirtual Mat load(int w, int h, int type) const;// load dimvirtual Mat load(int w, int h, int c, int type) const;
};class ModelBinFromDataReaderPrivate;
class NCNN_EXPORT ModelBinFromDataReader : public ModelBin
{
public:explicit ModelBinFromDataReader(const DataReader& dr);virtual ~ModelBinFromDataReader();virtual Mat load(int w, int type) const;private:ModelBinFromDataReader(const ModelBinFromDataReader&);ModelBinFromDataReader& operator=(const ModelBinFromDataReader&);private:ModelBinFromDataReaderPrivate* const d;
};class ModelBinFromMatArrayPrivate;
class NCNN_EXPORT ModelBinFromMatArray : public ModelBin
{
public:// construct from weight blob arrayexplicit ModelBinFromMatArray(const Mat* weights);virtual ~ModelBinFromMatArray();virtual Mat load(int w, int type) const;private:ModelBinFromMatArray(const ModelBinFromMatArray&);ModelBinFromMatArray& operator=(const ModelBinFromMatArray&);private:ModelBinFromMatArrayPrivate* const d;
};} // namespace ncnn
ncnn paramdictmodelbin相关推荐
- GitHub开源:17M超轻量级中文OCR模型、支持NCNN推理
目录 1.项目简介 2.项目配置 3.问题解决 1.项目简介 近期GitHub上一位大神开源了一个叫做chineseocr_lite的OCR的项目,这是一个超轻量级中文OCR,支持竖排文字识别.NCN ...
- Ubuntu18.04 编译 ncnn
官方文档(https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-linux-x86)上说的是: install g++ cmake p ...
- 重构ncnn,腾讯优图开源新一代移动端推理框架TNN
来源 | 腾讯优图 从学界到工业界,"开源"已经成为AI领域的一个关键词.一方面,它以"授人以渔"的方式为AI构建了一个开放共进的生态环境,帮助行业加速AI应用 ...
- 超轻量级中文OCR,支持竖排文字识别、ncnn推理,总模型仅17M
整理 | AI科技大本营 光学字符识别(OCR)技术已经得到了广泛应用.比如发票上用来识别关键字样,搜题App用来识别书本上的试题. 近期,这个叫做chineseocr_lite的OCR项目开源了,这 ...
- 实践教程|YOLOX目标检测ncnn实现
作者 | 唐超@知乎(已授权) 来源丨https://zhuanlan.zhihu.com/p/391788686 编辑丨极市平台 导读 本文将YOLOX训练的模型转到ncnn进行推理加速. YOLO ...
- 实操教程|详细记录solov2的ncnn实现和优化
作者丨欧阳慧宇@知乎(已授权) 来源丨https://zhuanlan.zhihu.com/p/361900997 编辑丨极市平台 极市导读 由于目前solo系列在GitHub中没有转ncnn的项目, ...
- Github标星3K+,超轻量级中文OCR,支持竖排文字识别、ncnn推理,总模型仅17M
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 光学字符识别(OCR)技术已经得到了广泛应用.比如发票上用来识别关键字样,搜题Ap ...
- 深度学习框架大PK:TNN决战MNN,ncnn依旧经典
近年来,开发者社区中,「开源」成了新流行趋势. 尤其是深度学习框架,自腾讯2017年将ncnn开源之后,各大AI实验室都「慷慨」的将自己的框架开源,以实现较为快速的创新. 今年6月10日,腾讯又宣布基 ...
- yolov5 ncnn
c++ input_size要设置为640,即onnx导出模型输入是320, 如果onnx输入改为640,Android执行会死机. 原版是394,375,output,3.0输出是: 394,374 ...
- ncnn tensorrt
ncnn: 已实现 winograd 卷积加速,int8 压缩和推断,还有基于 vulkan 的 gpu 推断 同时也支持将Caffe模型转化为int8精度. 而ONNX模型的转化则是近半年来的实现成 ...
最新文章
- JVM class加载机制的总结 收藏
- 2019年前端的3个趋势
- Nginx Install 记录
- mvc 视图和模型的对应_通过在酒吧订购饮料来解释模型视图控制器(MVC)
- 神经网络之预测共享单车使用情况
- Windows核心编程_获取鼠标指定位置的RGB颜色值
- PSQL容器带脚本初始化
- Dropthings - Ajax Web Portal
- 易语言网页填表取不到cookie_易语言置外部浏览器Cookie例子(支持所有浏览器)...
- Carrot2对文章进行聚类代码示例
- 定时器cron表达式
- Mac影音多媒体工具软件推荐
- python魔兽挂机_魔兽怀旧服玩家有多难?为了不用重新排队,自制绝不封号的外挂!...
- 鼠标滑过图片文字遮罩效果
- 华为手机如何更新鸿蒙系统_华为鸿蒙系统怎么升级?升级鸿蒙系统步骤
- android隐藏其他应用图标,【Android】隐藏app图标以及隐式启动其他APP
- XLNet: Generalized Autoregressive Pretraining for Language Understanding
- 一个架构师谈什么是架构以及怎么成为一个架构师
- 顺势腹式呼吸还是逆势
- 渗透测试之信息收集总结