test_param.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************
  2. * Copyright (C) [2021] 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 <string>
  22. #include <unordered_map>
  23. #include <vector>
  24. #include "private/cnstream_param.hpp"
  25. namespace cnstream {
  26. typedef struct TestParam {
  27. bool dump_image;
  28. int device_id;
  29. int width;
  30. int height;
  31. float ratio;
  32. double threshold;
  33. std::string padding_type;
  34. } TestParam;
  35. TEST(CoreParam, RegistParams) {
  36. std::string module_name = "test_module";
  37. ModuleParamsHelper<TestParam> params_helper(module_name);
  38. std::vector<ModuleParamDesc> regist_params = {
  39. {"device_id", "0", "device id", PARAM_OPTIONAL, OFFSET(TestParam, device_id), ModuleParamParser<int>::Parser},
  40. {"dump_image", "false", "dump image", PARAM_OPTIONAL, OFFSET(TestParam, dump_image),
  41. ModuleParamParser<bool>::Parser},
  42. {"width", "110", "width of input image", PARAM_OPTIONAL, OFFSET(TestParam, width),
  43. ModuleParamParser<int>::Parser},
  44. {"height", "120", "height of input image", PARAM_OPTIONAL, OFFSET(TestParam, height),
  45. ModuleParamParser<int>::Parser},
  46. {"ratio", "1.0", "resize ratrio of input image", PARAM_OPTIONAL, OFFSET(TestParam, ratio),
  47. ModuleParamParser<float>::Parser},
  48. {"threshold", "0.6", "threshold for obj score", PARAM_OPTIONAL, OFFSET(TestParam, threshold),
  49. ModuleParamParser<double>::Parser},
  50. {"padding_type", "middle", "input image padding method", PARAM_OPTIONAL, OFFSET(TestParam, padding_type),
  51. ModuleParamParser<std::string>::Parser}};
  52. EXPECT_TRUE(params_helper.Register(regist_params));
  53. std::unordered_map<std::string, std::string> params_map;
  54. params_map["device_id"] = "1";
  55. params_map["dump_image"] = "False";
  56. params_map["height"] = "120";
  57. params_map["width"] = "240";
  58. params_map["ratio"] = "0.56";
  59. params_map["padding_type"] = "middle";
  60. params_map["threshold"] = "0.88";
  61. params_map["json_file_dir"] = "../../";
  62. EXPECT_TRUE(params_helper.ParseParams(params_map));
  63. TestParam params = params_helper.GetParams();
  64. LOGI(CORE) << params.device_id << " " << params.dump_image << " " << params.height << " " << params.width << " "
  65. << params.ratio << " " << params.threshold << " " << params.padding_type;
  66. }
  67. TEST(CoreParam, ParamsParser) {
  68. std::string module_name = "test_module";
  69. ModuleParamsHelper<TestParam> params_helper(module_name);
  70. ModuleParamDesc desc;
  71. desc.name = "device_id";
  72. desc.optional = false;
  73. desc.default_value = "0";
  74. desc.str_desc = "which device to run inference";
  75. desc.offset = OFFSET(TestParam, device_id);
  76. desc.parser = ModuleParamParser<int>::Parser;
  77. EXPECT_TRUE(params_helper.Register(desc));
  78. desc.name = "dump_image";
  79. desc.optional = false;
  80. desc.default_value = "false";
  81. desc.str_desc = "wheater to dump image";
  82. desc.offset = OFFSET(TestParam, dump_image);
  83. desc.parser = ModuleParamParser<bool>::Parser;
  84. EXPECT_TRUE(params_helper.Register(desc));
  85. desc.name = "height";
  86. desc.optional = true;
  87. desc.default_value = "0";
  88. desc.str_desc = "which the height of input image";
  89. desc.offset = OFFSET(TestParam, height);
  90. desc.parser = ModuleParamParser<int>::Parser;
  91. EXPECT_TRUE(params_helper.Register(desc));
  92. desc.name = "width";
  93. desc.optional = true;
  94. desc.default_value = "0";
  95. desc.str_desc = "which the width of input image";
  96. desc.offset = OFFSET(TestParam, width);
  97. desc.parser = ModuleParamParser<int>::Parser;
  98. EXPECT_TRUE(params_helper.Register(desc));
  99. desc.name = "ratio";
  100. desc.optional = false;
  101. desc.default_value = "1";
  102. desc.str_desc = "which ratio to resize image";
  103. desc.offset = OFFSET(TestParam, ratio);
  104. desc.parser = ModuleParamParser<float>::Parser;
  105. EXPECT_TRUE(params_helper.Register(desc));
  106. desc.name = "padding_type";
  107. desc.optional = false;
  108. desc.default_value = "center";
  109. desc.str_desc = "which method to padding image";
  110. desc.offset = OFFSET(TestParam, padding_type);
  111. desc.parser = ModuleParamParser<std::string>::Parser;
  112. EXPECT_TRUE(params_helper.Register(desc));
  113. desc.name = "threshold";
  114. desc.optional = false;
  115. desc.default_value = "0.66";
  116. desc.str_desc = "threshold for score";
  117. desc.offset = OFFSET(TestParam, threshold);
  118. desc.parser = ModuleParamParser<double>::Parser;
  119. EXPECT_TRUE(params_helper.Register(desc));
  120. std::unordered_map<std::string, std::string> params_map;
  121. params_map["device_id"] = "1";
  122. params_map["dump_image"] = "False";
  123. params_map["height"] = "120";
  124. params_map["width"] = "240";
  125. params_map["ratio"] = "0.56";
  126. params_map["padding_type"] = "middle";
  127. params_map["threshold"] = "0.88";
  128. params_map["json_file_dir"] = "../../";
  129. EXPECT_TRUE(params_helper.ParseParams(params_map));
  130. }
  131. TEST(CoreParam, ValidParams) {
  132. std::string module_name = "test_module";
  133. ModuleParamsHelper<TestParam> params_helper(module_name);
  134. std::vector<ModuleParamDesc> regist_params = {
  135. {"device_id", "0", "device id", PARAM_OPTIONAL, OFFSET(TestParam, device_id), ModuleParamParser<int>::Parser},
  136. {"dump_image", "false", "dump image", PARAM_OPTIONAL, OFFSET(TestParam, dump_image),
  137. ModuleParamParser<bool>::Parser},
  138. {"width", "110", "width of input image", PARAM_OPTIONAL, OFFSET(TestParam, width),
  139. ModuleParamParser<int>::Parser},
  140. {"height", "120", "height of input image", PARAM_OPTIONAL, OFFSET(TestParam, height),
  141. ModuleParamParser<int>::Parser},
  142. {"ratio", "1.0", "resize ratrio of input image", PARAM_OPTIONAL, OFFSET(TestParam, ratio),
  143. ModuleParamParser<float>::Parser},
  144. {"threshold", "0.6", "threshold for obj score", PARAM_OPTIONAL, OFFSET(TestParam, threshold),
  145. ModuleParamParser<double>::Parser},
  146. {"padding_type", "middle", "input image padding method", PARAM_OPTIONAL, OFFSET(TestParam, padding_type),
  147. ModuleParamParser<std::string>::Parser}};
  148. EXPECT_TRUE(params_helper.Register(regist_params));
  149. std::unordered_map<std::string, std::string> params_map;
  150. params_map["device_id"] = "1";
  151. params_map["dump_image"] = "False";
  152. params_map["height"] = "120";
  153. params_map["width"] = "240";
  154. params_map["ratio"] = "0.56";
  155. params_map["padding_type"] = "middle";
  156. params_map["threshold"] = "0.88";
  157. params_map["WRONG_DATADIR"] = "../../";
  158. params_map["wrong_param"] = "99999";
  159. EXPECT_FALSE(params_helper.ParseParams(params_map));
  160. }
  161. } // namespace cnstream