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

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

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