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.

ncnn2mem.cpp 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <stdio.h>
  16. #include <string.h>
  17. #include <cstddef>
  18. #include <string>
  19. #include <vector>
  20. #include "layer.h"
  21. static std::vector<std::string> layer_names;
  22. static std::vector<std::string> blob_names;
  23. static int find_blob_index_by_name(const char* name)
  24. {
  25. for (std::size_t i=0; i<blob_names.size(); i++)
  26. {
  27. if (blob_names[i] == name)
  28. {
  29. return i;
  30. }
  31. }
  32. fprintf(stderr, "find_blob_index_by_name %s failed\n", name);
  33. return -1;
  34. }
  35. static void sanitize_name(char* name)
  36. {
  37. for (std::size_t i=0; i<strlen(name); i++)
  38. {
  39. if (!isalnum(name[i]))
  40. {
  41. name[i] = '_';
  42. }
  43. }
  44. }
  45. static std::string path_to_varname(const char* path)
  46. {
  47. const char* lastslash = strrchr(path, '/');
  48. const char* name = lastslash == NULL ? path : lastslash + 1;
  49. std::string varname = name;
  50. sanitize_name((char*)varname.c_str());
  51. return varname;
  52. }
  53. static int dump_param(const char* parampath, const char* parambinpath, const char* idcpppath)
  54. {
  55. FILE* fp = fopen(parampath, "rb");
  56. FILE* mp = fopen(parambinpath, "wb");
  57. FILE* ip = fopen(idcpppath, "wb");
  58. std::string param_var = path_to_varname(parampath);
  59. std::string include_guard_var = path_to_varname(idcpppath);
  60. fprintf(ip, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  61. fprintf(ip, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  62. fprintf(ip, "namespace %s_id {\n", param_var.c_str());
  63. int layer_count = 0;
  64. int blob_count = 0;
  65. fscanf(fp, "%d %d", &layer_count, &blob_count);
  66. fwrite(&layer_count, sizeof(int), 1, mp);
  67. fwrite(&blob_count, sizeof(int), 1, mp);
  68. layer_names.resize(layer_count);
  69. blob_names.resize(blob_count);
  70. int layer_index = 0;
  71. int blob_index = 0;
  72. while (!feof(fp))
  73. {
  74. int nscan = 0;
  75. char layer_type[32];
  76. char layer_name[256];
  77. int bottom_count = 0;
  78. int top_count = 0;
  79. nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
  80. if (nscan != 4)
  81. {
  82. continue;
  83. }
  84. sanitize_name(layer_name);
  85. int typeindex = ncnn::layer_to_index(layer_type);
  86. fwrite(&typeindex, sizeof(int), 1, mp);
  87. fwrite(&bottom_count, sizeof(int), 1, mp);
  88. fwrite(&top_count, sizeof(int), 1, mp);
  89. fprintf(ip, "const int LAYER_%s = %d;\n", layer_name, layer_index);
  90. // layer->bottoms.resize(bottom_count);
  91. for (int i=0; i<bottom_count; i++)
  92. {
  93. char bottom_name[256];
  94. nscan = fscanf(fp, "%256s", bottom_name);
  95. if (nscan != 1)
  96. {
  97. continue;
  98. }
  99. sanitize_name(bottom_name);
  100. int bottom_blob_index = find_blob_index_by_name(bottom_name);
  101. fwrite(&bottom_blob_index, sizeof(int), 1, mp);
  102. }
  103. // layer->tops.resize(top_count);
  104. for (int i=0; i<top_count; i++)
  105. {
  106. char blob_name[256];
  107. nscan = fscanf(fp, "%256s", blob_name);
  108. if (nscan != 1)
  109. {
  110. continue;
  111. }
  112. sanitize_name(blob_name);
  113. blob_names[blob_index] = std::string(blob_name);
  114. fprintf(ip, "const int BLOB_%s = %d;\n", blob_name, blob_index);
  115. fwrite(&blob_index, sizeof(int), 1, mp);
  116. blob_index++;
  117. }
  118. // dump layer specific params
  119. char buffer[1024];
  120. fgets(buffer, 1024, fp);
  121. int pos = 0;
  122. int nconsumed = 0;
  123. while (1)
  124. {
  125. // skip whitespace
  126. nconsumed = 0;
  127. sscanf(buffer + pos, "%*[ \t]%n", &nconsumed);
  128. pos += nconsumed;
  129. bool isfloat = false;
  130. // look ahead for determine isfloat
  131. const char* bp = buffer + pos;
  132. for (int j=0; j<20; j++)
  133. {
  134. if (bp[j] == ' ' || bp[j] == '\t')
  135. {
  136. break;
  137. }
  138. if (bp[j] == '.')
  139. {
  140. isfloat = true;
  141. break;
  142. }
  143. }
  144. if (isfloat)
  145. {
  146. float vf;
  147. nconsumed = 0;
  148. nscan = sscanf(buffer + pos, "%f%n", &vf, &nconsumed);
  149. pos += nconsumed;
  150. if (nscan != 1)
  151. {
  152. break;
  153. }
  154. fwrite(&vf, sizeof(float), 1, mp);
  155. }
  156. else
  157. {
  158. int v;
  159. nconsumed = 0;
  160. nscan = sscanf(buffer + pos, "%d%n", &v, &nconsumed);
  161. pos += nconsumed;
  162. if (nscan != 1)
  163. {
  164. break;
  165. }
  166. fwrite(&v, sizeof(int), 1, mp);
  167. }
  168. }
  169. layer_names[layer_index] = std::string(layer_name);
  170. layer_index++;
  171. }
  172. fprintf(ip, "} // namespace %s_id\n", param_var.c_str());
  173. fprintf(ip, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  174. fclose(fp);
  175. fclose(mp);
  176. fclose(ip);
  177. return 0;
  178. }
  179. static int write_memcpp(const char* parambinpath, const char* modelpath, const char* memcpppath)
  180. {
  181. FILE* cppfp = fopen(memcpppath, "wb");
  182. // dump param
  183. std::string param_var = path_to_varname(parambinpath);
  184. std::string include_guard_var = path_to_varname(memcpppath);
  185. FILE* mp = fopen(parambinpath, "rb");
  186. fprintf(cppfp, "#ifndef NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  187. fprintf(cppfp, "#define NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  188. fprintf(cppfp, "static const unsigned char %s[] = {\n", param_var.c_str());
  189. int i = 0;
  190. while (!feof(mp))
  191. {
  192. int c = fgetc(mp);
  193. if (c == EOF)
  194. break;
  195. fprintf(cppfp, "0x%02x,", c);
  196. i++;
  197. if (i % 16 == 0)
  198. {
  199. fprintf(cppfp, "\n");
  200. }
  201. }
  202. fprintf(cppfp, "};\n");
  203. fclose(mp);
  204. // dump model
  205. std::string model_var = path_to_varname(modelpath);
  206. FILE* bp = fopen(modelpath, "rb");
  207. fprintf(cppfp, "static const unsigned char %s[] = {\n", model_var.c_str());
  208. i = 0;
  209. while (!feof(bp))
  210. {
  211. int c = fgetc(bp);
  212. if (c == EOF)
  213. break;
  214. fprintf(cppfp, "0x%02x,", c);
  215. i++;
  216. if (i % 16 == 0)
  217. {
  218. fprintf(cppfp, "\n");
  219. }
  220. }
  221. fprintf(cppfp, "};\n");
  222. fprintf(cppfp, "#endif // NCNN_INCLUDE_GUARD_%s\n", include_guard_var.c_str());
  223. fclose(bp);
  224. fclose(cppfp);
  225. return 0;
  226. }
  227. int main(int argc, char** argv)
  228. {
  229. if (argc != 5)
  230. {
  231. fprintf(stderr, "Usage: %s [ncnnproto] [ncnnbin] [idcpppath] [memcpppath]\n", argv[0]);
  232. return -1;
  233. }
  234. const char* parampath = argv[1];
  235. const char* modelpath = argv[2];
  236. const char* idcpppath = argv[3];
  237. const char* memcpppath = argv[4];
  238. const char* lastslash = strrchr(parampath, '/');
  239. const char* name = lastslash == NULL ? parampath : lastslash + 1;
  240. std::string parambinpath = std::string(name) + ".bin";
  241. dump_param(parampath, parambinpath.c_str(), idcpppath);
  242. write_memcpp(parambinpath.c_str(), modelpath, memcpppath);
  243. return 0;
  244. }