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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * \file dnn/test/arm_common/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 <string>
  12. #include <vector>
  13. #include "test/arm_common/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(ARM_COMMON, WARP_PERSPECTIVE_CV) {
  22. //! Just for the format NHWC
  23. Checker<WarpPerspective> 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. checker.exec({{10, 128, 108, 3}, {10, 3, 3}, {10, 56, 128, 3}});
  80. }
  81. // resize nan case
  82. UniformFloatRNG rng_zero(0, 0);
  83. checker.set_rng(1, &rng_zero);
  84. {
  85. param.bmode = BMode::CONSTANT;
  86. param.border_val = 1.737;
  87. checker.set_param(param);
  88. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  89. }
  90. // add linear test
  91. param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
  92. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  93. BMode::WRAP, BMode::CONSTANT}) {
  94. param.bmode = mode;
  95. param.border_val = 1.737;
  96. checker.set_param(param);
  97. checker.exec({{10, 128, 108, 3}, {10, 3, 3}, {10, 56, 128, 3}});
  98. }
  99. // resize nan case
  100. checker.set_rng(1, &rng_zero);
  101. {
  102. param.bmode = BMode::CONSTANT;
  103. param.border_val = 1.737;
  104. checker.set_param(param);
  105. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  106. }
  107. auto args = warp_perspective::get_cv_args();
  108. for (auto&& arg : args) {
  109. checker.set_param(arg.param)
  110. .set_dtype(0, dtype::Uint8())
  111. .set_dtype(1, dtype::Float32())
  112. .set_dtype(2, dtype::Uint8())
  113. .execs({arg.src, arg.trans, arg.dst});
  114. }
  115. for (auto&& arg : args) {
  116. checker.set_param(arg.param)
  117. .set_dtype(0, dtype::Float32())
  118. .set_dtype(1, dtype::Float32())
  119. .set_dtype(2, dtype::Float32())
  120. .execs({arg.src, arg.trans, arg.dst});
  121. }
  122. }
  123. TEST_F(ARM_COMMON_MULTI_THREADS, WARP_PERSPECTIVE_CV) {
  124. //! Just for the format NHWC
  125. Checker<WarpPerspective> checker(handle());
  126. param::WarpPerspective param;
  127. class ResizeMatRNG : public RNG {
  128. void gen(const TensorND& tensor_) override {
  129. auto& gen = RandomState::generator();
  130. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
  131. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  132. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  133. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  134. std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
  135. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  136. std::uniform_int_distribution<int> dice(0, 5);
  137. float* ptr = tensor_.ptr<dt_float32>();
  138. auto N = tensor_.layout.shape[0];
  139. for (size_t n = 0; n < N; ++n) {
  140. for (size_t i = 0; i < 9; ++i) {
  141. switch (dice(gen)) {
  142. case 0:
  143. ptr[i] = pdist3(gen);
  144. break;
  145. case 1:
  146. ptr[i] = pdist(gen);
  147. break;
  148. case 2:
  149. ptr[i] = pdisth(gen);
  150. break;
  151. case 3:
  152. ptr[i] = ndist(gen);
  153. break;
  154. case 4:
  155. ptr[i] = ndist3(gen);
  156. break;
  157. case 5:
  158. ptr[i] = ndisth(gen);
  159. break;
  160. }
  161. }
  162. // is resize?
  163. if (n & 1) {
  164. ptr[1] = 0;
  165. ptr[3] = 0;
  166. ptr[6] = ptr[7] = 0;
  167. }
  168. ptr += 9;
  169. }
  170. }
  171. } rng;
  172. using BMode = param::WarpPerspective::BorderMode;
  173. param.format = param::WarpPerspective::Format::NHWC;
  174. // add for nearest test
  175. param.imode = param::WarpPerspective::InterpolationMode::NEAREST;
  176. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  177. BMode::WRAP, BMode::CONSTANT}) {
  178. param.bmode = mode;
  179. param.border_val = 1.737;
  180. checker.set_param(param);
  181. checker.exec({{10, 128, 108, 3}, {10, 3, 3}, {10, 56, 128, 3}});
  182. }
  183. // resize nan case
  184. UniformFloatRNG rng_zero(0, 0);
  185. checker.set_rng(1, &rng_zero);
  186. {
  187. param.bmode = BMode::CONSTANT;
  188. param.border_val = 1.737;
  189. checker.set_param(param);
  190. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  191. }
  192. // add linear test
  193. param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
  194. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  195. BMode::WRAP, BMode::CONSTANT}) {
  196. param.bmode = mode;
  197. param.border_val = 1.737;
  198. checker.set_param(param);
  199. checker.exec({{10, 128, 108, 3}, {10, 3, 3}, {10, 56, 128, 3}});
  200. }
  201. // resize nan case
  202. checker.set_rng(1, &rng_zero);
  203. {
  204. param.bmode = BMode::CONSTANT;
  205. param.border_val = 1.737;
  206. checker.set_param(param);
  207. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  208. }
  209. auto args = warp_perspective::get_cv_args();
  210. for (auto&& arg : args) {
  211. checker.set_param(arg.param)
  212. .set_dtype(0, dtype::Uint8())
  213. .set_dtype(1, dtype::Float32())
  214. .set_dtype(2, dtype::Uint8())
  215. .execs({arg.src, arg.trans, arg.dst});
  216. }
  217. for (auto&& arg : args) {
  218. checker.set_param(arg.param)
  219. .set_dtype(0, dtype::Float32())
  220. .set_dtype(1, dtype::Float32())
  221. .set_dtype(2, dtype::Float32())
  222. .execs({arg.src, arg.trans, arg.dst});
  223. }
  224. }
  225. #if MEGDNN_WITH_BENCHMARK
  226. TEST_F(ARM_COMMON, BENCHMARK_WARP_PERSPECTIVE_FORWARD) {
  227. Benchmarker<WarpPerspectiveForward> benchmarker(handle());
  228. auto handle_naive = create_cpu_handle(2);
  229. Benchmarker<WarpPerspectiveForward> benchmarker_naive(handle_naive.get());
  230. constexpr size_t NR_RUN = 50;
  231. using BMode = param::WarpPerspective::BorderMode;
  232. using IMode = param::WarpPerspective::InterpolationMode;
  233. WarpPerspective::Param param;
  234. param.border_val = 0.3f;
  235. param.format = param::WarpPerspective::Format::NHWC;
  236. auto run = [&](size_t N, size_t C, size_t IH, size_t IW, size_t OH,
  237. size_t OW, size_t scale) {
  238. printf("src={%zu, %zu, %zu, %zu}, dst={%zu, %zu, %zu, %zu}\n", N, IH,
  239. IW, C, N, OH, OW, C);
  240. auto time_ms =
  241. benchmarker.exec({{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
  242. NR_RUN;
  243. auto time_naive_ms =
  244. benchmarker_naive.exec(
  245. {{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
  246. NR_RUN;
  247. auto bandwidth = N * C * (scale * OH * OW) * dtype::Float32().size();
  248. printf("aarch64: %.3f, perf: %.3f GBPS naive: %.3f, perf %.3f GBPS "
  249. "speedup: %f\n",
  250. time_ms, bandwidth / time_ms / 1e6, time_naive_ms,
  251. bandwidth / time_naive_ms / 1e6, time_naive_ms / time_ms);
  252. };
  253. std::vector<std::string> bmodestringmap = {
  254. "REPLICATE", "REFLECT", "REFLECT_101", "WARP", "CONSTANT"};
  255. std::vector<std::string> imodestringmap = {"NEAREST", "INTER_LINEAR"};
  256. size_t scales[2] = {2, 5};
  257. for (auto imode : {IMode::NEAREST, IMode::INTER_LINEAR}) {
  258. for (auto bmode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  259. BMode::WRAP, BMode::CONSTANT}) {
  260. param.imode = imode;
  261. param.bmode = bmode;
  262. benchmarker.set_param(param).set_display(false).set_times(NR_RUN);
  263. benchmarker_naive.set_param(param).set_display(false).set_times(
  264. NR_RUN);
  265. size_t scale = scales[(int)imode];
  266. printf("\n\n\n warpperspective InterpolationMode::%s "
  267. "BorderMode::%s start\n",
  268. imodestringmap[(int)imode].c_str(),
  269. bmodestringmap[(int)bmode].c_str());
  270. for (auto&& shape :
  271. std::vector<std::pair<size_t, size_t>>{{700, 490},
  272. {500, 334},
  273. {472, 342},
  274. {448, 306},
  275. {626, 412},
  276. {140, 144},
  277. {120, 128},
  278. {180, 176}}) {
  279. for (size_t ch : {1, 2, 3}) {
  280. run(1, ch, shape.first, shape.second, 256, 256, scale);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. namespace {
  287. void benchmark_impl(const typename WarpPerspective::Param& param,
  288. std::vector<SmallVector<TensorShape>> shapes, size_t RUNS,
  289. TaskExecutorConfig&& multi_thread_config,
  290. TaskExecutorConfig&& single_thread_config) {
  291. std::vector<float> multi_thread_times, single_thread_times;
  292. {
  293. auto multi_thread_hanle =
  294. create_cpu_handle(0, true, &multi_thread_config);
  295. auto benchmarker =
  296. Benchmarker<WarpPerspective>(multi_thread_hanle.get());
  297. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  298. for (auto shape : shapes) {
  299. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  300. }
  301. }
  302. {
  303. auto single_thread_handle =
  304. create_cpu_handle(0, true, &single_thread_config);
  305. auto benchmarker =
  306. Benchmarker<WarpPerspective>(single_thread_handle.get());
  307. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  308. for (auto shape : shapes) {
  309. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  310. }
  311. }
  312. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  313. printf("core_ids:");
  314. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  315. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  316. }
  317. printf(", Single thread core_id %zu\n",
  318. single_thread_config.affinity_core_set[0]);
  319. for (size_t i = 0; i < shapes.size(); i++) {
  320. auto shape = shapes[i];
  321. printf("Case: ");
  322. for (auto sh : shape)
  323. printf("%s ", sh.to_string().c_str());
  324. printf("%zu threads time: %f,\n single thread time: "
  325. "%f. spead up = %f, speedup/cores=%f\n",
  326. multi_thread_config.nr_thread, multi_thread_times[i],
  327. single_thread_times[i],
  328. single_thread_times[i] / multi_thread_times[i],
  329. single_thread_times[i] / multi_thread_times[i] /
  330. multi_thread_config.nr_thread);
  331. }
  332. }
  333. } // namespace
  334. TEST_F(ARM_COMMON_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  335. constexpr size_t RUNS = 50;
  336. using BMode = param::WarpPerspective::BorderMode;
  337. using IMode = param::WarpPerspective::InterpolationMode;
  338. WarpPerspective::Param param;
  339. param.border_val = 0.3f;
  340. param.format = param::WarpPerspective::Format::NHWC;
  341. param.imode = IMode::INTER_LINEAR;
  342. param.bmode = BMode::REPLICATE;
  343. std::vector<SmallVector<TensorShape>> shapes;
  344. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  345. SmallVector<TensorShape> shape{
  346. {N, H, W, C}, {N, 3, 3}, {N, 224, 224, C}};
  347. shapes.push_back(shape);
  348. };
  349. bench_case(1, 700, 490, 1);
  350. bench_case(1, 700, 490, 2);
  351. bench_case(1, 700, 490, 3);
  352. bench_case(1, 500, 334, 1);
  353. bench_case(1, 500, 334, 2);
  354. bench_case(1, 500, 334, 3);
  355. bench_case(1, 140, 144, 1);
  356. bench_case(1, 140, 144, 2);
  357. bench_case(1, 140, 114, 3);
  358. printf("Benchmark warp perspective\n");
  359. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  360. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  361. benchmark_impl(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  362. }
  363. #endif
  364. } // namespace test
  365. } // namespace megdnn
  366. // vim: syntax=cpp.doxygen

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