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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * \file dnn/test/aarch64/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 <string>
  12. #include <vector>
  13. #include "test/aarch64/fixture.h"
  14. #include "test/common/benchmarker.h"
  15. #include "test/common/checker.h"
  16. #include "test/common/random_state.h"
  17. #include "test/common/rng.h"
  18. #include "test/common/warp_perspective.h"
  19. namespace megdnn {
  20. namespace test {
  21. TEST_F(AARCH64, WARP_PERSPECTIVE_CV) {
  22. //! Just for the format NHWC
  23. Checker<WarpPerspective, WarpPerspectiveMatIdxProxy> checker(handle());
  24. param::WarpPerspective param;
  25. class ResizeMatRNG : public RNG {
  26. void gen(const TensorND& tensor_) override {
  27. auto& gen = RandomState::generator();
  28. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
  29. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  30. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  31. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  32. std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
  33. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  34. std::uniform_int_distribution<int> dice(0, 5);
  35. float* ptr = tensor_.ptr<dt_float32>();
  36. auto N = tensor_.layout.shape[0];
  37. for (size_t n = 0; n < N; ++n) {
  38. for (size_t i = 0; i < 9; ++i) {
  39. switch (dice(gen)) {
  40. case 0:
  41. ptr[i] = pdist3(gen);
  42. break;
  43. case 1:
  44. ptr[i] = pdist(gen);
  45. break;
  46. case 2:
  47. ptr[i] = pdisth(gen);
  48. break;
  49. case 3:
  50. ptr[i] = ndist(gen);
  51. break;
  52. case 4:
  53. ptr[i] = ndist3(gen);
  54. break;
  55. case 5:
  56. ptr[i] = ndisth(gen);
  57. break;
  58. }
  59. }
  60. // is resize?
  61. if (n & 1) {
  62. ptr[1] = 0;
  63. ptr[3] = 0;
  64. ptr[6] = ptr[7] = 0;
  65. }
  66. ptr += 9;
  67. }
  68. }
  69. } rng;
  70. using BMode = param::WarpPerspective::BorderMode;
  71. param.format = param::WarpPerspective::Format::NHWC;
  72. // add for nearest test
  73. param.imode = param::WarpPerspective::InterpolationMode::NEAREST;
  74. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  75. BMode::WRAP, BMode::CONSTANT}) {
  76. param.bmode = mode;
  77. param.border_val = 1.737;
  78. checker.set_param(param);
  79. UniformIntRNG rng(0, 1);
  80. checker.set_rng(2, &rng);
  81. checker.set_dtype(2, dtype::Int32());
  82. checker.exec({{2, 5, 5, 1}, {4, 3, 3}, {4}, {4, 5, 5, 1}});
  83. }
  84. // resize nan case
  85. UniformFloatRNG rng_zero(0, 0);
  86. checker.set_rng(1, &rng_zero);
  87. {
  88. param.bmode = BMode::CONSTANT;
  89. param.border_val = 1.737;
  90. checker.set_param(param);
  91. UniformIntRNG rng(0, 999);
  92. checker.set_rng(2, &rng);
  93. checker.set_dtype(2, dtype::Int32());
  94. checker.exec(
  95. {{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  96. }
  97. // add linear test
  98. param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
  99. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  100. BMode::WRAP, BMode::CONSTANT}) {
  101. param.bmode = mode;
  102. param.border_val = 1.737;
  103. checker.set_param(param);
  104. UniformIntRNG rng(0, 9);
  105. checker.set_rng(2, &rng);
  106. checker.set_dtype(2, dtype::Int32());
  107. checker.exec({{10, 128, 108, 3}, {20, 3, 3}, {20}, {20, 56, 128, 3}});
  108. }
  109. // resize nan case
  110. checker.set_rng(1, &rng_zero);
  111. {
  112. param.bmode = BMode::CONSTANT;
  113. param.border_val = 1.737;
  114. checker.set_param(param);
  115. UniformIntRNG rng(0, 999);
  116. checker.set_rng(2, &rng);
  117. checker.set_dtype(2, dtype::Int32());
  118. checker.exec(
  119. {{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  120. }
  121. auto args = warp_perspective::get_cv_args();
  122. for (auto&& arg : args) {
  123. ConstValue rng(0.f);
  124. checker.set_param(arg.param)
  125. .set_rng(2, &rng)
  126. .set_dtype(0, dtype::Uint8())
  127. .set_dtype(1, dtype::Float32())
  128. .set_dtype(2, dtype::Int32())
  129. .set_dtype(3, dtype::Uint8())
  130. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  131. }
  132. for (auto&& arg : args) {
  133. ConstValue rng(0.f);
  134. checker.set_param(arg.param)
  135. .set_rng(2, &rng)
  136. .set_dtype(0, dtype::Float32())
  137. .set_dtype(1, dtype::Float32())
  138. .set_dtype(2, dtype::Int32())
  139. .set_dtype(3, dtype::Float32())
  140. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  141. }
  142. }
  143. #if MEGDNN_WITH_BENCHMARK
  144. TEST_F(AARCH64, BENCHMARK_WARP_PERSPECTIVE_FORWARD) {
  145. Benchmarker<WarpPerspectiveForward> benchmarker(handle());
  146. auto handle_naive = create_cpu_handle(2);
  147. Benchmarker<WarpPerspectiveForward> benchmarker_naive(handle_naive.get());
  148. constexpr size_t NR_RUN = 50;
  149. using BMode = param::WarpPerspective::BorderMode;
  150. using IMode = param::WarpPerspective::InterpolationMode;
  151. WarpPerspective::Param param;
  152. param.border_val = 0.3f;
  153. param.format = param::WarpPerspective::Format::NHWC;
  154. auto run = [&](size_t N, size_t C, size_t IH, size_t IW, size_t OH,
  155. size_t OW, size_t scale) {
  156. printf("src={%zu, %zu, %zu, %zu}, dst={%zu, %zu, %zu, %zu}\n", N, IH,
  157. IW, C, N, OH, OW, C);
  158. auto time_ms =
  159. benchmarker.exec({{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
  160. NR_RUN;
  161. auto time_naive_ms =
  162. benchmarker_naive.exec(
  163. {{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
  164. NR_RUN;
  165. auto bandwidth = N * C * (scale * OH * OW) * dtype::Float32().size();
  166. printf("aarch64: %.3f, perf: %.3f GBPS naive: %.3f, perf %.3f GBPS "
  167. "speedup: %f\n",
  168. time_ms, bandwidth / time_ms / 1e6, time_naive_ms,
  169. bandwidth / time_naive_ms / 1e6, time_naive_ms / time_ms);
  170. };
  171. std::vector<std::string> bmodestringmap = {
  172. "REPLICATE", "REFLECT", "REFLECT_101", "WARP", "CONSTANT"};
  173. std::vector<std::string> imodestringmap = {"NEAREST", "INTER_LINEAR"};
  174. size_t scales[2] = {2, 5};
  175. for (auto imode : {IMode::NEAREST, IMode::INTER_LINEAR}) {
  176. for (auto bmode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  177. BMode::WRAP, BMode::CONSTANT}) {
  178. param.imode = imode;
  179. param.bmode = bmode;
  180. benchmarker.set_param(param).set_display(false).set_times(NR_RUN);
  181. benchmarker_naive.set_param(param).set_display(false).set_times(
  182. NR_RUN);
  183. size_t scale = scales[(int)imode];
  184. printf("\n\n\n warpperspective InterpolationMode::%s "
  185. "BorderMode::%s start\n",
  186. imodestringmap[(int)imode].c_str(),
  187. bmodestringmap[(int)bmode].c_str());
  188. for (auto&& shape :
  189. std::vector<std::pair<size_t, size_t>>{{700, 490},
  190. {500, 334},
  191. {472, 342},
  192. {448, 306},
  193. {626, 412},
  194. {140, 144},
  195. {120, 128},
  196. {180, 176}}) {
  197. for (size_t ch : {1, 2, 3}) {
  198. run(1, ch, shape.first, shape.second, 256, 256, scale);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. #endif
  205. } // namespace test
  206. } // namespace megdnn
  207. // vim: syntax=cpp.doxygen

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