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.

warp_perspective.cpp 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * \file dnn/test/x86/warp_perspective.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/x86/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/random_state.h"
  15. #include "test/common/rng.h"
  16. #include "test/common/task_record_check.h"
  17. #include "test/common/warp_affine.h"
  18. #include "test/common/warp_perspective.h"
  19. namespace megdnn {
  20. namespace test {
  21. TEST_F(X86, WARP_PERSPECTIVE_MAT_IDX) {
  22. warp_perspective::run_mat_idx_test(handle());
  23. }
  24. TEST_F(X86, WARP_PERSPECTIVE_MAT_IDX_RECORD) {
  25. constexpr int N_SRC = 5;
  26. TaskRecordChecker<WarpPerspectiveForward, WarpPerspectiveMatIdxProxy> checker(0);
  27. WarpPerspectiveMatRNG mat_rng;
  28. checker.set_rng(1, &mat_rng);
  29. UniformIntRNG mat_idx_rng{0, N_SRC - 1};
  30. checker.set_dtype(2, dtype::Int32());
  31. checker.set_rng(2, &mat_idx_rng);
  32. WarpPerspective::Param param;
  33. param.bmode = WarpPerspective::Param::BorderMode::REFLECT;
  34. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  35. checker.set_param(param);
  36. checker.execs({{N_SRC, 3, 10, 11}, {2, 3, 3}, {2}, {2, 3, 11, 12}});
  37. checker.execs({{N_SRC, 14, 17, 13}, {123, 3, 3}, {123}, {123, 14, 16, 15}});
  38. }
  39. TEST_F(X86_MULTI_THREADS, WARP_PERSPECTIVE_MAT_IDX) {
  40. warp_perspective::run_mat_idx_test(handle());
  41. }
  42. TEST_F(X86_MULTI_THREADS, WARP_AFFINE_CV) {
  43. using namespace warp_affine;
  44. std::vector<TestArg> args = get_cv_args();
  45. Checker<WarpAffine> checker(handle());
  46. for (auto&& arg : args) {
  47. checker.set_param(arg.param)
  48. .set_epsilon(1 + 1e-3)
  49. .set_dtype(0, dtype::Uint8())
  50. .set_dtype(1, dtype::Float32())
  51. .set_dtype(2, dtype::Uint8())
  52. .execs({arg.src, arg.trans, arg.dst});
  53. }
  54. for (auto&& arg : args) {
  55. checker.set_param(arg.param)
  56. .set_dtype(0, dtype::Float32())
  57. .set_dtype(1, dtype::Float32())
  58. .set_dtype(2, dtype::Float32())
  59. .execs({arg.src, arg.trans, arg.dst});
  60. }
  61. }
  62. TEST_F(X86_MULTI_THREADS, WARP_AFFINE_CV_RECORD) {
  63. using namespace warp_affine;
  64. std::vector<TestArg> args = get_cv_args();
  65. TaskRecordChecker<WarpAffine> checker(0);
  66. for (auto&& arg : args) {
  67. checker.set_param(arg.param)
  68. .set_epsilon(1 + 1e-3)
  69. .set_dtype(0, dtype::Uint8())
  70. .set_dtype(1, dtype::Float32())
  71. .set_dtype(2, dtype::Uint8())
  72. .execs({arg.src, arg.trans, arg.dst});
  73. }
  74. for (auto&& arg : args) {
  75. checker.set_param(arg.param)
  76. .set_dtype(0, dtype::Float32())
  77. .set_dtype(1, dtype::Float32())
  78. .set_dtype(2, dtype::Float32())
  79. .execs({arg.src, arg.trans, arg.dst});
  80. }
  81. }
  82. #if MEGDNN_WITH_BENCHMARK
  83. namespace {
  84. template <typename Opr>
  85. void benchmark_impl(
  86. const typename Opr::Param& param, std::vector<SmallVector<TensorShape>> shapes,
  87. size_t RUNS, TaskExecutorConfig&& multi_thread_config,
  88. TaskExecutorConfig&& single_thread_config) {
  89. std::vector<float> multi_thread_times, single_thread_times;
  90. {
  91. auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
  92. auto benchmarker = Benchmarker<Opr>(multi_thread_hanle.get());
  93. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  94. for (auto shape : shapes) {
  95. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  96. }
  97. }
  98. {
  99. auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
  100. auto benchmarker = Benchmarker<Opr>(single_thread_handle.get());
  101. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  102. for (auto shape : shapes) {
  103. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  104. }
  105. }
  106. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  107. printf("core_ids:");
  108. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  109. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  110. }
  111. printf(", Single thread core_id %zu\n", single_thread_config.affinity_core_set[0]);
  112. for (size_t i = 0; i < shapes.size(); i++) {
  113. auto shape = shapes[i];
  114. printf("Case: ");
  115. for (auto sh : shape)
  116. printf("%s ", sh.to_string().c_str());
  117. printf("%zu threads time: %f,\n single thread time: "
  118. "%f. spead up = %f, speedup/cores=%f\n",
  119. multi_thread_config.nr_thread, multi_thread_times[i],
  120. single_thread_times[i], single_thread_times[i] / multi_thread_times[i],
  121. single_thread_times[i] / multi_thread_times[i] /
  122. multi_thread_config.nr_thread);
  123. }
  124. }
  125. } // namespace
  126. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  127. constexpr size_t RUNS = 50;
  128. using BMode = param::WarpPerspective::BorderMode;
  129. using IMode = param::WarpPerspective::InterpolationMode;
  130. WarpPerspective::Param param;
  131. param.border_val = 0.3f;
  132. param.format = param::WarpPerspective::Format::NHWC;
  133. param.imode = IMode::INTER_LINEAR;
  134. param.bmode = BMode::REPLICATE;
  135. std::vector<SmallVector<TensorShape>> shapes;
  136. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  137. SmallVector<TensorShape> shape{{N, H, W, C}, {N, 3, 3}, {N, 224, 224, C}};
  138. shapes.push_back(shape);
  139. };
  140. bench_case(1, 700, 490, 1);
  141. bench_case(1, 700, 490, 2);
  142. bench_case(1, 700, 490, 3);
  143. bench_case(1, 500, 334, 1);
  144. bench_case(1, 500, 334, 2);
  145. bench_case(1, 500, 334, 3);
  146. bench_case(1, 140, 144, 1);
  147. bench_case(1, 140, 144, 2);
  148. bench_case(1, 140, 114, 3);
  149. printf("Benchmark warp perspective\n");
  150. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  151. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  152. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  153. }
  154. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_AFFINE) {
  155. constexpr size_t RUNS = 50;
  156. using BMode = param::WarpAffine::BorderMode;
  157. using IMode = param::WarpAffine::InterpolationMode;
  158. WarpAffine::Param param;
  159. param.border_val = 0.3f;
  160. param.format = param::WarpAffine::Format::NHWC;
  161. param.imode = IMode::LINEAR;
  162. param.border_mode = BMode::BORDER_CONSTANT;
  163. std::vector<SmallVector<TensorShape>> shapes;
  164. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  165. SmallVector<TensorShape> shape{{N, H, W, C}, {N, 2, 3}, {N, 224, 224, C}};
  166. shapes.push_back(shape);
  167. };
  168. bench_case(1, 700, 490, 1);
  169. bench_case(1, 700, 490, 2);
  170. bench_case(1, 700, 490, 3);
  171. bench_case(1, 500, 334, 1);
  172. bench_case(1, 500, 334, 2);
  173. bench_case(1, 500, 334, 3);
  174. bench_case(1, 140, 144, 1);
  175. bench_case(1, 140, 144, 2);
  176. bench_case(1, 140, 114, 3);
  177. printf("Benchmark warp perspective\n");
  178. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  179. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  180. benchmark_impl<WarpAffine>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  181. }
  182. #endif
  183. } // namespace test
  184. } // namespace megdnn
  185. // vim: syntax=cpp.doxygen