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 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <stdarg.h>
  16. #include <stdio.h>
  17. #include "paramdict.h"
  18. #include "platform.h"
  19. namespace ncnn {
  20. ParamDict::ParamDict()
  21. {
  22. clear();
  23. }
  24. int ParamDict::get(int id, int def) const
  25. {
  26. return params[id].loaded ? params[id].i : def;
  27. }
  28. float ParamDict::get(int id, float def) const
  29. {
  30. return params[id].loaded ? params[id].f : def;
  31. }
  32. Mat ParamDict::get(int id, const Mat& def) const
  33. {
  34. return params[id].loaded ? params[id].v : def;
  35. }
  36. void ParamDict::set(int id, int i)
  37. {
  38. params[id].loaded = 1;
  39. params[id].i = i;
  40. }
  41. void ParamDict::set(int id, float f)
  42. {
  43. params[id].loaded = 1;
  44. params[id].f = f;
  45. }
  46. void ParamDict::set(int id, const Mat& v)
  47. {
  48. params[id].loaded = 1;
  49. params[id].v = v;
  50. }
  51. void ParamDict::clear()
  52. {
  53. for (int i = 0; i < NCNN_MAX_PARAM_COUNT; i++)
  54. {
  55. params[i].loaded = 0;
  56. params[i].v = Mat();
  57. }
  58. }
  59. #if NCNN_STDIO
  60. #if NCNN_STRING
  61. static bool vstr_is_float(const char vstr[16])
  62. {
  63. // look ahead for determine isfloat
  64. for (int j=0; j<16; j++)
  65. {
  66. if (vstr[j] == '\0')
  67. break;
  68. if (vstr[j] == '.' || tolower(vstr[j]) == 'e')
  69. return true;
  70. }
  71. return false;
  72. }
  73. int ParamDict::load_param(FILE* fp)
  74. {
  75. clear();
  76. // 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0
  77. // parse each key=value pair
  78. int id = 0;
  79. while (fscanf(fp, "%d=", &id) == 1)
  80. {
  81. bool is_array = id <= -23300;
  82. if (is_array)
  83. {
  84. id = -id - 23300;
  85. }
  86. if (is_array)
  87. {
  88. int len = 0;
  89. int nscan = fscanf(fp, "%d", &len);
  90. if (nscan != 1)
  91. {
  92. fprintf(stderr, "ParamDict read array length fail\n");
  93. return -1;
  94. }
  95. params[id].v.create(len);
  96. for (int j = 0; j < len; j++)
  97. {
  98. char vstr[16];
  99. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  100. if (nscan != 1)
  101. {
  102. fprintf(stderr, "ParamDict read array element fail\n");
  103. return -1;
  104. }
  105. bool is_float = vstr_is_float(vstr);
  106. if (is_float)
  107. {
  108. float* ptr = params[id].v;
  109. nscan = sscanf(vstr, "%f", &ptr[j]);
  110. }
  111. else
  112. {
  113. int* ptr = params[id].v;
  114. nscan = sscanf(vstr, "%d", &ptr[j]);
  115. }
  116. if (nscan != 1)
  117. {
  118. fprintf(stderr, "ParamDict parse array element fail\n");
  119. return -1;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. char vstr[16];
  126. int nscan = fscanf(fp, "%15s", vstr);
  127. if (nscan != 1)
  128. {
  129. fprintf(stderr, "ParamDict read value fail\n");
  130. return -1;
  131. }
  132. bool is_float = vstr_is_float(vstr);
  133. if (is_float)
  134. nscan = sscanf(vstr, "%f", &params[id].f);
  135. else
  136. nscan = sscanf(vstr, "%d", &params[id].i);
  137. if (nscan != 1)
  138. {
  139. fprintf(stderr, "ParamDict parse value fail\n");
  140. return -1;
  141. }
  142. }
  143. params[id].loaded = 1;
  144. }
  145. return 0;
  146. }
  147. #if _MSC_VER
  148. static inline int mem_sscanf_with_n(int* _internal_nconsumed_ptr, const char*& ptr, const char* format, ...)
  149. {
  150. *_internal_nconsumed_ptr = 0;
  151. va_list args;
  152. va_start(args, format);
  153. int _n = vsscanf(ptr, format, args);
  154. va_end(args);
  155. ptr += *_internal_nconsumed_ptr;
  156. return *_internal_nconsumed_ptr > 0 ? _n : 0;
  157. }
  158. #define mem_sscanf(ptr, format, ...) mem_sscanf_with_n(&_internal_nconsumed, ptr, format "%n", __VA_ARGS__, &_internal_nconsumed)
  159. #else
  160. // return value from macro requires gcc extension https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
  161. #define mem_sscanf(ptr, format, ...) ({int _b=0; int _n = sscanf(ptr, format "%n", __VA_ARGS__, &_b); ptr+=_b;_b>0?_n:0;})
  162. #endif // _MSC_VER
  163. int ParamDict::load_param_mem(const char*& mem)
  164. {
  165. #if _MSC_VER
  166. int _internal_nconsumed;
  167. #endif
  168. clear();
  169. // 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0
  170. // parse each key=value pair
  171. int id = 0;
  172. while (mem_sscanf(mem, "%d=", &id) == 1)
  173. {
  174. bool is_array = id <= -23300;
  175. if (is_array)
  176. {
  177. id = -id - 23300;
  178. }
  179. if (is_array)
  180. {
  181. int len = 0;
  182. int nscan = mem_sscanf(mem, "%d", &len);
  183. if (nscan != 1)
  184. {
  185. fprintf(stderr, "ParamDict read array length fail\n");
  186. return -1;
  187. }
  188. params[id].v.create(len);
  189. for (int j = 0; j < len; j++)
  190. {
  191. char vstr[16];
  192. nscan = mem_sscanf(mem, ",%15[^,\n ]", vstr);
  193. if (nscan != 1)
  194. {
  195. fprintf(stderr, "ParamDict read array element fail\n");
  196. return -1;
  197. }
  198. bool is_float = vstr_is_float(vstr);
  199. if (is_float)
  200. {
  201. float* ptr = params[id].v;
  202. nscan = sscanf(vstr, "%f", &ptr[j]);
  203. }
  204. else
  205. {
  206. int* ptr = params[id].v;
  207. nscan = sscanf(vstr, "%d", &ptr[j]);
  208. }
  209. if (nscan != 1)
  210. {
  211. fprintf(stderr, "ParamDict parse array element fail\n");
  212. return -1;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. char vstr[16];
  219. int nscan = mem_sscanf(mem, "%15s", vstr);
  220. if (nscan != 1)
  221. {
  222. fprintf(stderr, "ParamDict read value fail\n");
  223. return -1;
  224. }
  225. bool is_float = vstr_is_float(vstr);
  226. if (is_float)
  227. nscan = sscanf(vstr, "%f", &params[id].f);
  228. else
  229. nscan = sscanf(vstr, "%d", &params[id].i);
  230. if (nscan != 1)
  231. {
  232. fprintf(stderr, "ParamDict parse value fail\n");
  233. return -1;
  234. }
  235. }
  236. params[id].loaded = 1;
  237. }
  238. return 0;
  239. }
  240. #endif // NCNN_STRING
  241. int ParamDict::load_param_bin(FILE* fp)
  242. {
  243. clear();
  244. // binary 0
  245. // binary 100
  246. // binary 1
  247. // binary 1.250000
  248. // binary 3 | array_bit
  249. // binary 5
  250. // binary 0.1
  251. // binary 0.2
  252. // binary 0.4
  253. // binary 0.8
  254. // binary 1.0
  255. // binary -233(EOP)
  256. int id = 0;
  257. fread(&id, sizeof(int), 1, fp);
  258. while (id != -233)
  259. {
  260. bool is_array = id <= -23300;
  261. if (is_array)
  262. {
  263. id = -id - 23300;
  264. }
  265. if (is_array)
  266. {
  267. int len = 0;
  268. fread(&len, sizeof(int), 1, fp);
  269. params[id].v.create(len);
  270. float* ptr = params[id].v;
  271. fread(ptr, sizeof(float), len, fp);
  272. }
  273. else
  274. {
  275. fread(&params[id].f, sizeof(float), 1, fp);
  276. }
  277. params[id].loaded = 1;
  278. fread(&id, sizeof(int), 1, fp);
  279. }
  280. return 0;
  281. }
  282. #endif // NCNN_STDIO
  283. int ParamDict::load_param(const unsigned char*& mem)
  284. {
  285. clear();
  286. int id = *(int*)(mem);
  287. mem += 4;
  288. while (id != -233)
  289. {
  290. bool is_array = id <= -23300;
  291. if (is_array)
  292. {
  293. id = -id - 23300;
  294. }
  295. if (is_array)
  296. {
  297. int len = *(int*)(mem);
  298. mem += 4;
  299. params[id].v.create(len);
  300. memcpy(params[id].v.data, mem, len * 4);
  301. mem += len * 4;
  302. }
  303. else
  304. {
  305. params[id].f = *(float*)(mem);
  306. mem += 4;
  307. }
  308. params[id].loaded = 1;
  309. id = *(int*)(mem);
  310. mem += 4;
  311. }
  312. return 0;
  313. }
  314. } // namespace ncnn