test_infer_params.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <limits>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "infer_params.hpp"
  26. namespace cnstream {
  27. TEST(Inferencer, infer_param_desc_less_compare) {
  28. InferParamDesc desc1, desc2;
  29. desc1.name = "abc";
  30. desc2.name = "abcd";
  31. InferParamDescLessCompare less_compare;
  32. EXPECT_TRUE(less_compare(desc1, desc2));
  33. }
  34. TEST(Inferencer, infer_param_desc_is_legal) {
  35. InferParamDesc desc;
  36. desc.name = "abc";
  37. desc.type = "string";
  38. desc.parser = [] (const std::string &value, InferParams *param_set) -> bool { return true; };
  39. EXPECT_TRUE(desc.IsLegal());
  40. desc.name = "";
  41. EXPECT_FALSE(desc.IsLegal());
  42. desc.name = "abc";
  43. desc.type = "";
  44. EXPECT_FALSE(desc.IsLegal());
  45. desc.name = "abc";
  46. desc.type = "string";
  47. desc.parser = NULL;
  48. EXPECT_FALSE(desc.IsLegal());
  49. }
  50. bool InferParamsEQ(const InferParams &p1, const InferParams &p2) {
  51. return p1.device_id == p2.device_id &&
  52. p1.object_infer == p2.object_infer &&
  53. p1.threshold == p2.threshold &&
  54. p1.use_scaler == p2.use_scaler &&
  55. p1.infer_interval == p2.infer_interval &&
  56. p1.batching_timeout == p2.batching_timeout &&
  57. p1.keep_aspect_ratio == p2.keep_aspect_ratio &&
  58. p1.data_order == p2.data_order &&
  59. p1.func_name == p2.func_name &&
  60. p1.model_path == p2.model_path &&
  61. p1.preproc_name == p2.preproc_name &&
  62. p1.postproc_name == p2.postproc_name &&
  63. p1.obj_filter_name == p2.obj_filter_name &&
  64. p1.dump_resized_image_dir == p2.dump_resized_image_dir &&
  65. p1.model_input_pixel_format == p2.model_input_pixel_format &&
  66. p1.custom_preproc_params == p2.custom_preproc_params &&
  67. p1.custom_postproc_params == p2.custom_postproc_params;
  68. }
  69. TEST(Inferencer, infer_param_manager) {
  70. InferParamManager manager;
  71. ParamRegister param_register;
  72. manager.RegisterAll(&param_register);
  73. std::vector<std::string> infer_param_list = {
  74. "device_id",
  75. "object_infer",
  76. "threshold",
  77. "use_scaler",
  78. "infer_interval",
  79. "batching_timeout",
  80. "keep_aspect_ratio",
  81. "data_order",
  82. "func_name",
  83. "model_path",
  84. "preproc_name",
  85. "postproc_name",
  86. "obj_filter_name",
  87. "dump_resized_image_dir",
  88. "model_input_pixel_format",
  89. "custom_preproc_params",
  90. "custom_postproc_params"
  91. };
  92. for (const auto &it : infer_param_list)
  93. EXPECT_TRUE(param_register.IsRegisted(it));
  94. // check parse params right
  95. InferParams expect_ret;
  96. expect_ret.device_id = 1;
  97. expect_ret.object_infer = true;
  98. expect_ret.threshold = 0.5;
  99. expect_ret.use_scaler = true;
  100. expect_ret.infer_interval = 1;
  101. expect_ret.batching_timeout = 3;
  102. expect_ret.keep_aspect_ratio = false;
  103. expect_ret.data_order = edk::DimOrder::NCHW;
  104. expect_ret.func_name = "fake_name";
  105. expect_ret.model_path = "fake_path";
  106. expect_ret.preproc_name = "fake_name";
  107. expect_ret.postproc_name = "fake_name";
  108. expect_ret.obj_filter_name = "filter_name";
  109. expect_ret.dump_resized_image_dir = "dir";
  110. expect_ret.model_input_pixel_format = CNDataFormat::CN_PIXEL_FORMAT_BGRA32;
  111. expect_ret.custom_preproc_params = {
  112. std::make_pair(std::string("param"), std::string("value"))};
  113. expect_ret.custom_postproc_params = {
  114. std::make_pair(std::string("param"), std::string("value"))};
  115. ModuleParamSet raw_params;
  116. raw_params["device_id"] = std::to_string(expect_ret.device_id);
  117. raw_params["object_infer"] = std::to_string(expect_ret.object_infer);
  118. raw_params["threshold"] = std::to_string(expect_ret.threshold);
  119. raw_params["use_scaler"] = std::to_string(expect_ret.use_scaler);
  120. raw_params["infer_interval"] = std::to_string(expect_ret.infer_interval);
  121. raw_params["batching_timeout"] = std::to_string(expect_ret.batching_timeout);
  122. raw_params["keep_aspect_ratio"] = std::to_string(expect_ret.keep_aspect_ratio);
  123. raw_params["data_order"] = "NCHW";
  124. raw_params["func_name"] = expect_ret.func_name;
  125. raw_params["model_path"] = expect_ret.model_path;
  126. raw_params["preproc_name"] = expect_ret.preproc_name;
  127. raw_params["postproc_name"] = expect_ret.postproc_name;
  128. raw_params["obj_filter_name"] = expect_ret.obj_filter_name;
  129. raw_params["dump_resized_image_dir"] = expect_ret.dump_resized_image_dir;
  130. raw_params["model_input_pixel_format"] = "BGRA32";
  131. raw_params["custom_preproc_params"] = "{\"param\" : \"value\"}";
  132. raw_params["custom_postproc_params"] = "{\"param\" : \"value\"}";
  133. {
  134. InferParams ret;
  135. EXPECT_TRUE(manager.ParseBy(raw_params, &ret));
  136. EXPECT_TRUE(InferParamsEQ(expect_ret, ret));
  137. }
  138. // check default value
  139. raw_params.clear();
  140. {
  141. InferParams default_value;
  142. default_value.device_id = 0;
  143. default_value.object_infer = false;
  144. default_value.threshold = 0.0;
  145. default_value.use_scaler = false;
  146. default_value.infer_interval = 1;
  147. default_value.batching_timeout = 3000;
  148. default_value.keep_aspect_ratio = false;
  149. default_value.data_order = edk::DimOrder::NHWC;
  150. default_value.func_name = "";
  151. default_value.model_path = "";
  152. default_value.preproc_name = "";
  153. default_value.postproc_name = "";
  154. default_value.obj_filter_name = "";
  155. default_value.dump_resized_image_dir = "";
  156. default_value.model_input_pixel_format = CNDataFormat::CN_PIXEL_FORMAT_RGBA32;
  157. InferParams ret;
  158. EXPECT_TRUE(manager.ParseBy(raw_params, &ret));
  159. EXPECT_FALSE(InferParamsEQ(default_value, ret));
  160. }
  161. // check value type
  162. raw_params.clear();
  163. {
  164. InferParams ret;
  165. raw_params["device_id"] = "wrong";
  166. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  167. }
  168. raw_params.clear();
  169. {
  170. InferParams ret;
  171. raw_params["object_infer"] = "wrong";
  172. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  173. }
  174. raw_params.clear();
  175. {
  176. InferParams ret;
  177. raw_params["threshold"] = "wrong";
  178. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  179. }
  180. raw_params.clear();
  181. {
  182. InferParams ret;
  183. raw_params["use_scaler"] = "wrong";
  184. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  185. }
  186. raw_params.clear();
  187. {
  188. InferParams ret;
  189. raw_params["infer_interval"] = "wrong";
  190. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  191. }
  192. raw_params.clear();
  193. {
  194. InferParams ret;
  195. raw_params["batching_timeout"] = "wrong";
  196. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  197. }
  198. raw_params.clear();
  199. {
  200. InferParams ret;
  201. raw_params["keep_aspect_ratio"] = "2";
  202. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  203. }
  204. raw_params.clear();
  205. {
  206. InferParams ret;
  207. raw_params["data_order"] = "CHWN";
  208. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  209. }
  210. raw_params.clear();
  211. {
  212. InferParams ret;
  213. raw_params["device_id"] = std::to_string(1ULL << 33);
  214. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  215. }
  216. }
  217. TEST(Inferencer, custom_preproc_params_parse) {
  218. InferParamManager manager;
  219. ParamRegister param_register;
  220. manager.RegisterAll(&param_register);
  221. ModuleParamSet raw_params;
  222. raw_params["custom_preproc_params"] = "{wrong_json_format,}";
  223. InferParams ret;
  224. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  225. }
  226. TEST(Inferencer, custom_postproc_params_parse) {
  227. InferParamManager manager;
  228. ParamRegister param_register;
  229. manager.RegisterAll(&param_register);
  230. ModuleParamSet raw_params;
  231. raw_params["custom_postproc_params"] = "{wrong_json_format,}";
  232. InferParams ret;
  233. EXPECT_FALSE(manager.ParseBy(raw_params, &ret));
  234. }
  235. } // namespace cnstream