当前位置:   article > 正文

ncnn paramdict&modelbin_ncnn::paramdict

ncnn::paramdict

paramdict、modelbin加载模型并存储模型形式:字典及binary。

参数字典私有类ParamDictPrivate

  1. class ParamDictPrivate
  2. {
  3. public:
  4. struct
  5. {
  6. // 0 = null
  7. // 1 = int/float
  8. // 2 = int
  9. // 3 = float
  10. // 4 = array of int/float
  11. // 5 = array of int
  12. // 6 = array of float
  13. int type;//参数类型存放
  14. union
  15. {
  16. int i;
  17. float f;
  18. };
  19. Mat v;//参数值存放
  20. } params[NCNN_MAX_PARAM_COUNT];//32
  21. };

union用法:C++中union的使用方法_棉猴的博客-CSDN博客_c++ union

ParamDictPrivate类中公有结构体内部再有联合体。

结构体嵌套联合体-字节对齐

 联合体字节对齐:取内部类型字节数最大的(因其内部所有的变量共用初始地址且每次只能对其中一个变量赋值起作用,多个变量同时赋值,只有最后被赋值的变量有效,其余为0)

结构体字节对齐:需要考虑内部变量类型字节数累加与当前变量类型字节数对齐以及整体字节数对齐。

参数字典类ParamDict

  1. class NCNN_EXPORT ParamDict
  2. {
  3. public:
  4. // empty
  5. ParamDict();
  6. virtual ~ParamDict();
  7. // copy
  8. ParamDict(const ParamDict&);
  9. // assign
  10. ParamDict& operator=(const ParamDict&);
  11. // get type
  12. int type(int id) const;
  13. // get int
  14. int get(int id, int def) const;
  15. // get float
  16. float get(int id, float def) const;
  17. // get array
  18. Mat get(int id, const Mat& def) const;
  19. // set int
  20. void set(int id, int i);
  21. // set float
  22. void set(int id, float f);
  23. // set array
  24. void set(int id, const Mat& v);
  25. protected:
  26. friend class Net;
  27. void clear();
  28. int load_param(const DataReader& dr);
  29. int load_param_bin(const DataReader& dr);
  30. private:
  31. ParamDictPrivate* const d;
  32. };

加载模型,解析模型文件将值及类型分别存储到mat和type中。

  1. int ParamDict::load_param(const DataReader& dr)
  2. {
  3. clear();
  4. // 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0
  5. // parse each key=value pair
  6. int id = 0;
  7. while (dr.scan("%d=", &id) == 1)
  8. {
  9. bool is_array = id <= -23300;
  10. if (is_array)
  11. {
  12. id = -id - 23300;
  13. }
  14. if (id >= NCNN_MAX_PARAM_COUNT)
  15. {
  16. NCNN_LOGE("id < NCNN_MAX_PARAM_COUNT failed (id=%d, NCNN_MAX_PARAM_COUNT=%d)", id, NCNN_MAX_PARAM_COUNT);
  17. return -1;
  18. }
  19. if (is_array)
  20. {
  21. int len = 0;
  22. int nscan = dr.scan("%d", &len);
  23. if (nscan != 1)
  24. {
  25. NCNN_LOGE("ParamDict read array length failed");
  26. return -1;
  27. }
  28. d->params[id].v.create(len);
  29. for (int j = 0; j < len; j++)
  30. {
  31. char vstr[16];
  32. nscan = dr.scan(",%15[^,\n ]", vstr);
  33. if (nscan != 1)
  34. {
  35. NCNN_LOGE("ParamDict read array element failed");
  36. return -1;
  37. }
  38. bool is_float = vstr_is_float(vstr);
  39. if (is_float)
  40. {
  41. float* ptr = d->params[id].v;
  42. ptr[j] = vstr_to_float(vstr);
  43. }
  44. else
  45. {
  46. int* ptr = d->params[id].v;
  47. nscan = sscanf(vstr, "%d", &ptr[j]);
  48. if (nscan != 1)
  49. {
  50. NCNN_LOGE("ParamDict parse array element failed");
  51. return -1;
  52. }
  53. }
  54. d->params[id].type = is_float ? 6 : 5;
  55. }
  56. }
  57. else
  58. {
  59. char vstr[16];
  60. int nscan = dr.scan("%15s", vstr);
  61. if (nscan != 1)
  62. {
  63. NCNN_LOGE("ParamDict read value failed");
  64. return -1;
  65. }
  66. bool is_float = vstr_is_float(vstr);
  67. if (is_float)
  68. {
  69. d->params[id].f = vstr_to_float(vstr);
  70. }
  71. else
  72. {
  73. nscan = sscanf(vstr, "%d", &d->params[id].i);
  74. if (nscan != 1)
  75. {
  76. NCNN_LOGE("ParamDict parse value failed");
  77. return -1;
  78. }
  79. }
  80. d->params[id].type = is_float ? 3 : 2;
  81. }
  82. }
  83. return 0;
  84. }

ModelBin抽象类

由ModelBinFromDataReader 和ModelBinFromMatArray 派生类分别实现。

ModelBinFromDataReader 直接读取模型文件存储bin形式

ModelBinFromMatArray 从Mat类型转存bin形式。

  1. class NCNN_EXPORT ModelBin
  2. {
  3. public:
  4. ModelBin();
  5. virtual ~ModelBin();
  6. // element type
  7. // 0 = auto
  8. // 1 = float32
  9. // 2 = float16
  10. // 3 = int8
  11. // load vec
  12. virtual Mat load(int w, int type) const = 0;
  13. // load image
  14. virtual Mat load(int w, int h, int type) const;
  15. // load dim
  16. virtual Mat load(int w, int h, int c, int type) const;
  17. };
  18. class ModelBinFromDataReaderPrivate;
  19. class NCNN_EXPORT ModelBinFromDataReader : public ModelBin
  20. {
  21. public:
  22. explicit ModelBinFromDataReader(const DataReader& dr);
  23. virtual ~ModelBinFromDataReader();
  24. virtual Mat load(int w, int type) const;
  25. private:
  26. ModelBinFromDataReader(const ModelBinFromDataReader&);
  27. ModelBinFromDataReader& operator=(const ModelBinFromDataReader&);
  28. private:
  29. ModelBinFromDataReaderPrivate* const d;
  30. };
  31. class ModelBinFromMatArrayPrivate;
  32. class NCNN_EXPORT ModelBinFromMatArray : public ModelBin
  33. {
  34. public:
  35. // construct from weight blob array
  36. explicit ModelBinFromMatArray(const Mat* weights);
  37. virtual ~ModelBinFromMatArray();
  38. virtual Mat load(int w, int type) const;
  39. private:
  40. ModelBinFromMatArray(const ModelBinFromMatArray&);
  41. ModelBinFromMatArray& operator=(const ModelBinFromMatArray&);
  42. private:
  43. ModelBinFromMatArrayPrivate* const d;
  44. };
  45. } // namespace ncnn

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号