当前位置:网站首页>ncnn paramdict&modelbin
ncnn paramdict&modelbin
2022-07-19 03:09:00 【HySmiley】
paramdict、modelbin Load the model and store the model form : Dictionary and binary.
Parameter dictionary private class 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 float
int type;// Parameter type storage
union
{
int i;
float f;
};
Mat v;// Parameter value storage
} params[NCNN_MAX_PARAM_COUNT];//32
};union usage :C++ in union How to use _ Cotton monkey blog -CSDN Blog _c++ union
ParamDictPrivate There is a union inside the public structure in the class .
Structure nested union - Byte alignment
Union byte alignment : Take inside Type bytes maximal ( Because all its internal variables share the initial address and only one of them can be assigned at a time , Multiple variables are assigned at the same time , Only the last assigned variable is valid , Others are 0)
Structure byte alignment : It is necessary to consider the alignment of the accumulation of internal variable type bytes with the current variable type bytes and the overall byte number .
Parameter dictionary class ParamDict
class NCNN_EXPORT ParamDict
{
public:
// empty
ParamDict();
virtual ~ParamDict();
// copy
ParamDict(const ParamDict&);
// assign
ParamDict& operator=(const ParamDict&);
// get type
int type(int id) const;
// get int
int get(int id, int def) const;
// get float
float get(int id, float def) const;
// get array
Mat get(int id, const Mat& def) const;
// set int
void set(int id, int i);
// set float
void set(int id, float f);
// set array
void 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;
};
Load model , Parse the model file and store the value and type in mat and type in .
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 pair
int 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 abstract class
from ModelBinFromDataReader and ModelBinFromMatArray Derived classes implement .
ModelBinFromDataReader Directly read the model file storage bin form
ModelBinFromMatArray from Mat Type redeposit bin form .
class NCNN_EXPORT ModelBin
{
public:
ModelBin();
virtual ~ModelBin();
// element type
// 0 = auto
// 1 = float32
// 2 = float16
// 3 = int8
// load vec
virtual Mat load(int w, int type) const = 0;
// load image
virtual Mat load(int w, int h, int type) const;
// load dim
virtual 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 array
explicit 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
边栏推荐
- 人脸检测几种方法
- Affine transformation implementation
- ncnn 部分算子不支持的替换操作
- A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
- Systick timer basic learning and hand tearing code
- zsh: command not found: mysql
- Redis和其他数据库的比较
- 5. Is the asynctool framework flawed?
- [MCU simulation] (IV) addressing mode register addressing and direct addressing
- 多锻炼身体有好处
猜你喜欢
![[face recognition] face recognition based on histogram histogram with matlab code](/img/a6/ae776d084a207647501ca951e32000.png)
[face recognition] face recognition based on histogram histogram with matlab code
4. Some thoughts on asynctool framework

After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
![2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )](/img/e4/ff7f1e19583f42377307de7291f870.png)
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )

Mysql优化之索引

Has DDD surpassed MVC

2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)

JDBC connection to MySQL database

05_服务调用Ribbon

CorelDRAW 安装不了解决方法
随机推荐
Detailed explanation of case when usage of SQL
C语言基础Day4-数组
人脸检测几种方法
First blog ----- self introduction
SQL经典练习题(x30)
JDBC connection to MySQL database
【单片机仿真】(八)指令系统 — 数据传送指令
Pytorch best practices and code templates
一个优酷VIP会员帐号可以几个人用的设备同时登录如何共享多人使用优酷会员账号?
D. Permutation Restoration(贪心/双指针/set)
Thinking for half a year
仿射变换实现
MySQL面试题(2022)
From the perspective of MySQL architecture, how does an SQL statement execute?
zsh: command not found: mysql
需要慢一点点
【单片机仿真】(十一)指令系统逻辑运算指令 — 逻辑与指令ANL、逻辑或指令ORL
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
JDBC连接Mysql数据库
Fiddler grabbing