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 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-2020 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/checker.h"
  13. #include "test/common/benchmarker.h"
  14. #include "test/common/random_state.h"
  15. #include "test/common/rng.h"
  16. #include "test/common/warp_perspective.h"
  17. #include "test/common/warp_affine.h"
  18. namespace megdnn {
  19. namespace test {
  20. TEST_F(X86, WARP_PERSPECTIVE_MAT_IDX) {
  21. warp_perspective::run_mat_idx_test(handle());
  22. }
  23. TEST_F(X86_MULTI_THREADS, WARP_PERSPECTIVE_MAT_IDX) {
  24. warp_perspective::run_mat_idx_test(handle());
  25. }
  26. TEST_F(X86_MULTI_THREADS, WARP_AFFINE_CV)
  27. {
  28. using namespace warp_affine;
  29. std::vector<TestArg> args = get_cv_args();
  30. Checker<WarpAffine> checker(handle());
  31. for (auto &&arg : args) {
  32. checker.set_param(arg.param)
  33. .set_epsilon(1 + 1e-3)
  34. .set_dtype(0, dtype::Uint8())
  35. .set_dtype(1, dtype::Float32())
  36. .set_dtype(2, dtype::Uint8())
  37. .execs({arg.src, arg.trans, arg.dst});
  38. }
  39. for (auto &&arg: args) {
  40. checker.set_param(arg.param)
  41. .set_dtype(0, dtype::Float32())
  42. .set_dtype(1, dtype::Float32())
  43. .set_dtype(2, dtype::Float32())
  44. .execs({arg.src, arg.trans, arg.dst});
  45. }
  46. }
  47. #if MEGDNN_WITH_BENCHMARK
  48. namespace {
  49. template<typename Opr>
  50. void benchmark_impl(const typename Opr::Param& param,
  51. std::vector<SmallVector<TensorShape>> shapes, size_t RUNS,
  52. TaskExecutorConfig&& multi_thread_config,
  53. TaskExecutorConfig&& single_thread_config) {
  54. std::vector<float> multi_thread_times, single_thread_times;
  55. {
  56. auto multi_thread_hanle =
  57. create_cpu_handle(0, true, &multi_thread_config);
  58. auto benchmarker = Benchmarker<Opr>(multi_thread_hanle.get());
  59. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  60. for (auto shape : shapes) {
  61. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  62. }
  63. }
  64. {
  65. auto single_thread_handle =
  66. create_cpu_handle(0, true, &single_thread_config);
  67. auto benchmarker = Benchmarker<Opr>(single_thread_handle.get());
  68. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  69. for (auto shape : shapes) {
  70. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  71. }
  72. }
  73. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  74. printf("core_ids:");
  75. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  76. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  77. }
  78. printf(", Single thread core_id %zu\n",
  79. single_thread_config.affinity_core_set[0]);
  80. for (size_t i = 0; i < shapes.size(); i++) {
  81. auto shape = shapes[i];
  82. printf("Case: ");
  83. for (auto sh : shape)
  84. printf("%s ", sh.to_string().c_str());
  85. printf("%zu threads time: %f,\n single thread time: "
  86. "%f. spead up = %f, speedup/cores=%f\n",
  87. multi_thread_config.nr_thread, multi_thread_times[i],
  88. single_thread_times[i],
  89. single_thread_times[i] / multi_thread_times[i],
  90. single_thread_times[i] / multi_thread_times[i] /
  91. multi_thread_config.nr_thread);
  92. }
  93. }
  94. } // namespace
  95. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  96. constexpr size_t RUNS = 50;
  97. using BMode = param::WarpPerspective::BorderMode;
  98. using IMode = param::WarpPerspective::InterpolationMode;
  99. WarpPerspective::Param param;
  100. param.border_val = 0.3f;
  101. param.format = param::WarpPerspective::Format::NHWC;
  102. param.imode = IMode::INTER_LINEAR;
  103. param.bmode = BMode::REPLICATE;
  104. std::vector<SmallVector<TensorShape>> shapes;
  105. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  106. SmallVector<TensorShape> shape{
  107. {N, H, W, C}, {N, 3, 3}, {N, 224, 224, C}};
  108. shapes.push_back(shape);
  109. };
  110. bench_case(1, 700, 490, 1);
  111. bench_case(1, 700, 490, 2);
  112. bench_case(1, 700, 490, 3);
  113. bench_case(1, 500, 334, 1);
  114. bench_case(1, 500, 334, 2);
  115. bench_case(1, 500, 334, 3);
  116. bench_case(1, 140, 144, 1);
  117. bench_case(1, 140, 144, 2);
  118. bench_case(1, 140, 114, 3);
  119. printf("Benchmark warp perspective\n");
  120. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}},
  121. {1, {4}});
  122. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}},
  123. {1, {7}});
  124. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  125. }
  126. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_AFFINE) {
  127. constexpr size_t RUNS = 50;
  128. using BMode = param::WarpAffine::BorderMode;
  129. using IMode = param::WarpAffine::InterpolationMode;
  130. WarpAffine::Param param;
  131. param.border_val = 0.3f;
  132. param.format = param::WarpAffine::Format::NHWC;
  133. param.imode = IMode::LINEAR;
  134. param.border_mode = BMode::BORDER_CONSTANT;
  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{
  138. {N, H, W, C}, {N, 2, 3}, {N, 224, 224, C}};
  139. shapes.push_back(shape);
  140. };
  141. bench_case(1, 700, 490, 1);
  142. bench_case(1, 700, 490, 2);
  143. bench_case(1, 700, 490, 3);
  144. bench_case(1, 500, 334, 1);
  145. bench_case(1, 500, 334, 2);
  146. bench_case(1, 500, 334, 3);
  147. bench_case(1, 140, 144, 1);
  148. bench_case(1, 140, 144, 2);
  149. bench_case(1, 140, 114, 3);
  150. printf("Benchmark warp perspective\n");
  151. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}},
  152. {1, {4}});
  153. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}},
  154. {1, {7}});
  155. benchmark_impl<WarpAffine>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  156. }
  157. #endif
  158. } // namespace test
  159. } // namespace megdnn
  160. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台