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.

modelbin.cpp 5.8 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "modelbin.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <vector>
  18. #include "datareader.h"
  19. namespace ncnn {
  20. ModelBin::~ModelBin()
  21. {
  22. }
  23. Mat ModelBin::load(int w, int h, int type) const
  24. {
  25. Mat m = load(w * h, type);
  26. if (m.empty())
  27. return m;
  28. return m.reshape(w, h);
  29. }
  30. Mat ModelBin::load(int w, int h, int c, int type) const
  31. {
  32. Mat m = load(w * h * c, type);
  33. if (m.empty())
  34. return m;
  35. return m.reshape(w, h, c);
  36. }
  37. ModelBinFromDataReader::ModelBinFromDataReader(const DataReader& _dr) : dr(_dr)
  38. {
  39. }
  40. Mat ModelBinFromDataReader::load(int w, int type) const
  41. {
  42. if (type == 0)
  43. {
  44. int nread;
  45. union
  46. {
  47. struct
  48. {
  49. unsigned char f0;
  50. unsigned char f1;
  51. unsigned char f2;
  52. unsigned char f3;
  53. };
  54. unsigned int tag;
  55. } flag_struct;
  56. nread = dr.read(&flag_struct, sizeof(flag_struct));
  57. if (nread != (int)sizeof(flag_struct))
  58. {
  59. fprintf(stderr, "ModelBin read flag_struct failed %d\n", nread);
  60. return Mat();
  61. }
  62. unsigned int flag = flag_struct.f0 + flag_struct.f1 + flag_struct.f2 + flag_struct.f3;
  63. if (flag_struct.tag == 0x01306B47)
  64. {
  65. // half-precision data
  66. int align_data_size = alignSize(w * sizeof(unsigned short), 4);
  67. std::vector<unsigned short> float16_weights;
  68. float16_weights.resize(align_data_size);
  69. nread = dr.read(float16_weights.data(), align_data_size);
  70. if (nread != align_data_size)
  71. {
  72. fprintf(stderr, "ModelBin read float16_weights failed %d\n", nread);
  73. return Mat();
  74. }
  75. return Mat::from_float16(float16_weights.data(), w);
  76. }
  77. else if (flag_struct.tag == 0x000D4B38)
  78. {
  79. // int8 data
  80. int align_data_size = alignSize(w, 4);
  81. std::vector<signed char> int8_weights;
  82. int8_weights.resize(align_data_size);
  83. nread = dr.read(int8_weights.data(), align_data_size);
  84. if (nread != align_data_size)
  85. {
  86. fprintf(stderr, "ModelBin read int8_weights failed %d\n", nread);
  87. return Mat();
  88. }
  89. Mat m(w, (size_t)1u);
  90. if (m.empty())
  91. return m;
  92. memcpy(m.data, int8_weights.data(), w);
  93. return m;
  94. }
  95. else if (flag_struct.tag == 0x0002C056)
  96. {
  97. Mat m(w);
  98. if (m.empty())
  99. return m;
  100. // raw data with extra scaling
  101. nread = dr.read(m, w * sizeof(float));
  102. if (nread != w * (int)sizeof(float))
  103. {
  104. fprintf(stderr, "ModelBin read weight_data failed %d\n", nread);
  105. return Mat();
  106. }
  107. return m;
  108. }
  109. Mat m(w);
  110. if (m.empty())
  111. return m;
  112. if (flag != 0)
  113. {
  114. // quantized data
  115. float quantization_value[256];
  116. nread = dr.read(quantization_value, 256 * sizeof(float));
  117. if (nread != 256 * (int)sizeof(float))
  118. {
  119. fprintf(stderr, "ModelBin read quantization_value failed %d\n", nread);
  120. return Mat();
  121. }
  122. int align_weight_data_size = alignSize(w * sizeof(unsigned char), 4);
  123. std::vector<unsigned char> index_array;
  124. index_array.resize(align_weight_data_size);
  125. nread = dr.read(index_array.data(), align_weight_data_size);
  126. if (nread != align_weight_data_size)
  127. {
  128. fprintf(stderr, "ModelBin read index_array failed %d\n", nread);
  129. return Mat();
  130. }
  131. float* ptr = m;
  132. for (int i = 0; i < w; i++)
  133. {
  134. ptr[i] = quantization_value[ index_array[i] ];
  135. }
  136. }
  137. else if (flag_struct.f0 == 0)
  138. {
  139. // raw data
  140. nread = dr.read(m, w * sizeof(float));
  141. if (nread != w * (int)sizeof(float))
  142. {
  143. fprintf(stderr, "ModelBin read weight_data failed %d\n", nread);
  144. return Mat();
  145. }
  146. }
  147. return m;
  148. }
  149. else if (type == 1)
  150. {
  151. Mat m(w);
  152. if (m.empty())
  153. return m;
  154. // raw data
  155. int nread = dr.read(m, w * sizeof(float));
  156. if (nread != w * (int)sizeof(float))
  157. {
  158. fprintf(stderr, "ModelBin read weight_data failed %d\n", nread);
  159. return Mat();
  160. }
  161. return m;
  162. }
  163. else
  164. {
  165. fprintf(stderr, "ModelBin load type %d not implemented\n", type);
  166. return Mat();
  167. }
  168. return Mat();
  169. }
  170. ModelBinFromMatArray::ModelBinFromMatArray(const Mat* _weights) : weights(_weights)
  171. {
  172. }
  173. Mat ModelBinFromMatArray::load(int /*w*/, int /*type*/) const
  174. {
  175. if (!weights)
  176. return Mat();
  177. Mat m = weights[0];
  178. weights++;
  179. return m;
  180. }
  181. } // namespace ncnn