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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "paramdict.h"
  15. #include "platform.h"
  16. namespace ncnn {
  17. ParamDict::ParamDict()
  18. {
  19. clear();
  20. }
  21. void ParamDict::clear()
  22. {
  23. for (int i = 0; i < NCNN_MAX_PARAM_COUNT; i++)
  24. {
  25. params[i].loaded = 0;
  26. }
  27. }
  28. #if NCNN_STDIO
  29. #if NCNN_STRING
  30. static bool vstr_is_float(const char vstr[16])
  31. {
  32. // look ahead for determine isfloat
  33. for (int j=0; j<16; j++)
  34. {
  35. if (vstr[j] == '\0')
  36. break;
  37. if (vstr[j] == '.')
  38. return true;
  39. }
  40. return false;
  41. }
  42. int ParamDict::load_param(FILE* fp)
  43. {
  44. clear();
  45. // 0=100 1=1.250000 -23303=5,0.1,0.2,0.4,0.8,1.0
  46. // parse each key=value pair
  47. int id = 0;
  48. while (fscanf(fp, "%d=", &id) == 1)
  49. {
  50. bool is_array = id <= -23300;
  51. if (is_array)
  52. {
  53. id = -id - 23300;
  54. }
  55. if (is_array)
  56. {
  57. int len = 0;
  58. int nscan = fscanf(fp, "%d", &len);
  59. if (nscan != 1)
  60. {
  61. fprintf(stderr, "ParamDict read array length fail\n");
  62. return -1;
  63. }
  64. params[id].v.create(len);
  65. for (int j = 0; j < len; j++)
  66. {
  67. char vstr[16];
  68. nscan = fscanf(fp, ",%15[^,\n ]", vstr);
  69. if (nscan != 1)
  70. {
  71. fprintf(stderr, "ParamDict read array element fail\n");
  72. return -1;
  73. }
  74. bool is_float = vstr_is_float(vstr);
  75. if (is_float)
  76. nscan = sscanf(vstr, "%f", &params[id].v.data[j]);
  77. else
  78. nscan = sscanf(vstr, "%d", (int*)&params[id].v.data[j]);
  79. if (nscan != 1)
  80. {
  81. fprintf(stderr, "ParamDict parse array element fail\n");
  82. return -1;
  83. }
  84. }
  85. }
  86. else
  87. {
  88. char vstr[16];
  89. int nscan = fscanf(fp, "%15s", vstr);
  90. if (nscan != 1)
  91. {
  92. fprintf(stderr, "ParamDict read value fail\n");
  93. return -1;
  94. }
  95. bool is_float = vstr_is_float(vstr);
  96. if (is_float)
  97. nscan = sscanf(vstr, "%f", &params[id].f);
  98. else
  99. nscan = sscanf(vstr, "%d", &params[id].i);
  100. if (nscan != 1)
  101. {
  102. fprintf(stderr, "ParamDict parse value fail\n");
  103. return -1;
  104. }
  105. }
  106. params[id].loaded = 1;
  107. }
  108. return 0;
  109. }
  110. #endif // NCNN_STRING
  111. int ParamDict::load_param_bin(FILE* fp)
  112. {
  113. clear();
  114. // binary 0
  115. // binary 100
  116. // binary 1
  117. // binary 1.250000
  118. // binary 3 | array_bit
  119. // binary 5
  120. // binary 0.1
  121. // binary 0.2
  122. // binary 0.4
  123. // binary 0.8
  124. // binary 1.0
  125. // binary -233(EOP)
  126. int id = 0;
  127. fread(&id, sizeof(int), 1, fp);
  128. while (id != -233)
  129. {
  130. bool is_array = id <= -23300;
  131. if (is_array)
  132. {
  133. id = -id - 23300;
  134. }
  135. if (is_array)
  136. {
  137. int len = 0;
  138. fread(&len, sizeof(int), 1, fp);
  139. params[id].v.create(len);
  140. for (int j = 0; j < len; j++)
  141. {
  142. fread(&params[id].v.data[j], sizeof(float), 1, fp);
  143. }
  144. }
  145. else
  146. {
  147. fread(&params[id].f, sizeof(float), 1, fp);
  148. }
  149. params[id].loaded = 1;
  150. fread(&id, sizeof(int), 1, fp);
  151. }
  152. return 0;
  153. }
  154. #endif // NCNN_STDIO
  155. int ParamDict::load_param(const unsigned char*& mem)
  156. {
  157. clear();
  158. int id = *(int*)(mem);
  159. mem += 4;
  160. while (id != -233)
  161. {
  162. bool is_array = id <= -23300;
  163. if (is_array)
  164. {
  165. id = -id - 23300;
  166. }
  167. if (is_array)
  168. {
  169. int len = *(int*)(mem);
  170. mem += 4;
  171. params[id].v.create(len);
  172. for (int j = 0; j < len; j++)
  173. {
  174. params[id].v.data[j] = *(float*)(mem);
  175. mem += 4;
  176. }
  177. }
  178. else
  179. {
  180. params[id].f = *(float*)(mem);
  181. mem += 4;
  182. }
  183. params[id].loaded = 1;
  184. id = *(int*)(mem);
  185. mem += 4;
  186. }
  187. return 0;
  188. }
  189. } // namespace ncnn