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.2 kB

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

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