You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

paramdict.cpp 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include <ctype.h>
  15. #include "paramdict.h"
  16. #include "platform.h"
  17. namespace ncnn {
  18. ParamDict::ParamDict()
  19. {
  20. clear();
  21. }
  22. int ParamDict::get(int id, int def) const
  23. {
  24. return params[id].loaded ? params[id].i : def;
  25. }
  26. float ParamDict::get(int id, float def) const
  27. {
  28. return params[id].loaded ? params[id].f : def;
  29. }
  30. Mat ParamDict::get(int id, const Mat& def) const
  31. {
  32. return params[id].loaded ? params[id].v : def;
  33. }
  34. void ParamDict::set(int id, int i)
  35. {
  36. params[id].loaded = 1;
  37. params[id].i = i;
  38. }
  39. void ParamDict::set(int id, float f)
  40. {
  41. params[id].loaded = 1;
  42. params[id].f = f;
  43. }
  44. void ParamDict::set(int id, const Mat& v)
  45. {
  46. params[id].loaded = 1;
  47. params[id].v = v;
  48. }
  49. void ParamDict::clear()
  50. {
  51. for (int i = 0; i < NCNN_MAX_PARAM_COUNT; i++)
  52. {
  53. params[i].loaded = 0;
  54. params[i].v = Mat();
  55. }
  56. }
  57. #if NCNN_STDIO
  58. #if NCNN_STRING
  59. static bool vstr_is_float(const char vstr[16])
  60. {
  61. // look ahead for determine isfloat
  62. for (int j=0; j<16; j++)
  63. {
  64. if (vstr[j] == '\0')
  65. break;
  66. if (vstr[j] == '.' || tolower(vstr[j]) == 'e')
  67. return true;
  68. }
  69. return false;
  70. }
  71. int ParamDict::load_param(FILE* fp)
  72. {
  73. clear();
  74. // 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0
  75. // parse each key=value pair
  76. int id = 0;
  77. while (fscanf(fp, "%d=", &id) == 1)
  78. {
  79. bool is_array = id <= -23300;
  80. if (is_array)
  81. {
  82. id = -id - 23300;
  83. }
  84. if (is_array)
  85. {
  86. int len = 0;
  87. int nscan = fscanf(fp, "%d", &len);
  88. if (nscan != 1)
  89. {
  90. fprintf(stderr, "ParamDict read array length fail\n");
  91. return -1;
  92. }
  93. params[id].v.create(len);
  94. for (int j = 0; j < len; j++)
  95. {
  96. char vstr[16];
  97. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  98. if (nscan != 1)
  99. {
  100. fprintf(stderr, "ParamDict read array element fail\n");
  101. return -1;
  102. }
  103. bool is_float = vstr_is_float(vstr);
  104. if (is_float)
  105. {
  106. float* ptr = params[id].v;
  107. nscan = sscanf(vstr, "%f", &ptr[j]);
  108. }
  109. else
  110. {
  111. int* ptr = params[id].v;
  112. nscan = sscanf(vstr, "%d", &ptr[j]);
  113. }
  114. if (nscan != 1)
  115. {
  116. fprintf(stderr, "ParamDict parse array element fail\n");
  117. return -1;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. char vstr[16];
  124. int nscan = fscanf(fp, "%15s", vstr);
  125. if (nscan != 1)
  126. {
  127. fprintf(stderr, "ParamDict read value fail\n");
  128. return -1;
  129. }
  130. bool is_float = vstr_is_float(vstr);
  131. if (is_float)
  132. nscan = sscanf(vstr, "%f", &params[id].f);
  133. else
  134. nscan = sscanf(vstr, "%d", &params[id].i);
  135. if (nscan != 1)
  136. {
  137. fprintf(stderr, "ParamDict parse value fail\n");
  138. return -1;
  139. }
  140. }
  141. params[id].loaded = 1;
  142. }
  143. return 0;
  144. }
  145. #endif // NCNN_STRING
  146. int ParamDict::load_param_bin(FILE* fp)
  147. {
  148. clear();
  149. // binary 0
  150. // binary 100
  151. // binary 1
  152. // binary 1.250000
  153. // binary 3 | array_bit
  154. // binary 5
  155. // binary 0.1
  156. // binary 0.2
  157. // binary 0.4
  158. // binary 0.8
  159. // binary 1.0
  160. // binary -233(EOP)
  161. int id = 0;
  162. fread(&id, sizeof(int), 1, fp);
  163. while (id != -233)
  164. {
  165. bool is_array = id <= -23300;
  166. if (is_array)
  167. {
  168. id = -id - 23300;
  169. }
  170. if (is_array)
  171. {
  172. int len = 0;
  173. fread(&len, sizeof(int), 1, fp);
  174. params[id].v.create(len);
  175. float* ptr = params[id].v;
  176. fread(ptr, sizeof(float), len, fp);
  177. }
  178. else
  179. {
  180. fread(&params[id].f, sizeof(float), 1, fp);
  181. }
  182. params[id].loaded = 1;
  183. fread(&id, sizeof(int), 1, fp);
  184. }
  185. return 0;
  186. }
  187. #endif // NCNN_STDIO
  188. int ParamDict::load_param(const unsigned char*& mem)
  189. {
  190. clear();
  191. int id = *(int*)(mem);
  192. mem += 4;
  193. while (id != -233)
  194. {
  195. bool is_array = id <= -23300;
  196. if (is_array)
  197. {
  198. id = -id - 23300;
  199. }
  200. if (is_array)
  201. {
  202. int len = *(int*)(mem);
  203. mem += 4;
  204. params[id].v.create(len);
  205. memcpy(params[id].v.data, mem, len * 4);
  206. mem += len * 4;
  207. }
  208. else
  209. {
  210. params[id].f = *(float*)(mem);
  211. mem += 4;
  212. }
  213. params[id].loaded = 1;
  214. id = *(int*)(mem);
  215. mem += 4;
  216. }
  217. return 0;
  218. }
  219. } // namespace ncnn