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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/task_record_check.h"
  19. #include "test/common/warp_perspective.h"
  20. namespace megdnn {
  21. namespace test {
  22. TEST_F(AARCH64, WARP_PERSPECTIVE_CV) {
  23. //! Just for the format NHWC
  24. Checker<WarpPerspective, WarpPerspectiveMatIdxProxy> checker(handle());
  25. param::WarpPerspective param;
  26. class ResizeMatRNG : public RNG {
  27. void gen(const TensorND& tensor_) override {
  28. auto& gen = RandomState::generator();
  29. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
  30. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  31. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  32. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  33. std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
  34. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  35. std::uniform_int_distribution<int> dice(0, 5);
  36. float* ptr = tensor_.ptr<dt_float32>();
  37. auto N = tensor_.layout.shape[0];
  38. for (size_t n = 0; n < N; ++n) {
  39. for (size_t i = 0; i < 9; ++i) {
  40. switch (dice(gen)) {
  41. case 0:
  42. ptr[i] = pdist3(gen);
  43. break;
  44. case 1:
  45. ptr[i] = pdist(gen);
  46. break;
  47. case 2:
  48. ptr[i] = pdisth(gen);
  49. break;
  50. case 3:
  51. ptr[i] = ndist(gen);
  52. break;
  53. case 4:
  54. ptr[i] = ndist3(gen);
  55. break;
  56. case 5:
  57. ptr[i] = ndisth(gen);
  58. break;
  59. }
  60. }
  61. // is resize?
  62. if (n & 1) {
  63. ptr[1] = 0;
  64. ptr[3] = 0;
  65. ptr[6] = ptr[7] = 0;
  66. }
  67. ptr += 9;
  68. }
  69. }
  70. } rng;
  71. using BMode = param::WarpPerspective::BorderMode;
  72. param.format = param::WarpPerspective::Format::NHWC;
  73. // add for nearest test
  74. param.imode = param::WarpPerspective::InterpolationMode::NEAREST;
  75. for (auto mode :
  76. {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT, BMode::WRAP,
  77. BMode::CONSTANT}) {
  78. param.bmode = mode;
  79. param.border_val = 1.737;
  80. checker.set_param(param);
  81. UniformIntRNG rng(0, 1);
  82. checker.set_rng(2, &rng);
  83. checker.set_dtype(2, dtype::Int32());
  84. checker.exec({{2, 5, 5, 1}, {4, 3, 3}, {4}, {4, 5, 5, 1}});
  85. }
  86. // resize nan case
  87. UniformFloatRNG rng_zero(0, 0);
  88. checker.set_rng(1, &rng_zero);
  89. {
  90. param.bmode = BMode::CONSTANT;
  91. param.border_val = 1.737;
  92. checker.set_param(param);
  93. UniformIntRNG rng(0, 999);
  94. checker.set_rng(2, &rng);
  95. checker.set_dtype(2, dtype::Int32());
  96. checker.exec({{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  97. }
  98. // add linear test
  99. param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
  100. for (auto mode :
  101. {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT, BMode::WRAP,
  102. BMode::CONSTANT}) {
  103. param.bmode = mode;
  104. param.border_val = 1.737;
  105. checker.set_param(param);
  106. UniformIntRNG rng(0, 9);
  107. checker.set_rng(2, &rng);
  108. checker.set_dtype(2, dtype::Int32());
  109. checker.exec({{10, 128, 108, 3}, {20, 3, 3}, {20}, {20, 56, 128, 3}});
  110. }
  111. // resize nan case
  112. checker.set_rng(1, &rng_zero);
  113. {
  114. param.bmode = BMode::CONSTANT;
  115. param.border_val = 1.737;
  116. checker.set_param(param);
  117. UniformIntRNG rng(0, 999);
  118. checker.set_rng(2, &rng);
  119. checker.set_dtype(2, dtype::Int32());
  120. checker.exec({{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  121. }
  122. auto args = warp_perspective::get_cv_args();
  123. for (auto&& arg : args) {
  124. ConstValue rng(0.f);
  125. checker.set_param(arg.param)
  126. .set_rng(2, &rng)
  127. .set_dtype(0, dtype::Uint8())
  128. .set_dtype(1, dtype::Float32())
  129. .set_dtype(2, dtype::Int32())
  130. .set_dtype(3, dtype::Uint8())
  131. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  132. }
  133. for (auto&& arg : args) {
  134. ConstValue rng(0.f);
  135. checker.set_param(arg.param)
  136. .set_rng(2, &rng)
  137. .set_dtype(0, dtype::Float32())
  138. .set_dtype(1, dtype::Float32())
  139. .set_dtype(2, dtype::Int32())
  140. .set_dtype(3, dtype::Float32())
  141. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  142. }
  143. }
  144. TEST_F(AARCH64, WARP_PERSPECTIVE_CV_RECORD) {
  145. //! Just for the format NHWC
  146. TaskRecordChecker<WarpPerspective, WarpPerspectiveMatIdxProxy> checker(0);
  147. param::WarpPerspective param;
  148. class ResizeMatRNG : public RNG {
  149. void gen(const TensorND& tensor_) override {
  150. auto& gen = RandomState::generator();
  151. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
  152. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  153. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  154. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  155. std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
  156. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  157. std::uniform_int_distribution<int> dice(0, 5);
  158. float* ptr = tensor_.ptr<dt_float32>();
  159. auto N = tensor_.layout.shape[0];
  160. for (size_t n = 0; n < N; ++n) {
  161. for (size_t i = 0; i < 9; ++i) {
  162. switch (dice(gen)) {
  163. case 0:
  164. ptr[i] = pdist3(gen);
  165. break;
  166. case 1:
  167. ptr[i] = pdist(gen);
  168. break;
  169. case 2:
  170. ptr[i] = pdisth(gen);
  171. break;
  172. case 3:
  173. ptr[i] = ndist(gen);
  174. break;
  175. case 4:
  176. ptr[i] = ndist3(gen);
  177. break;
  178. case 5:
  179. ptr[i] = ndisth(gen);
  180. break;
  181. }
  182. }
  183. // is resize?
  184. if (n & 1) {
  185. ptr[1] = 0;
  186. ptr[3] = 0;
  187. ptr[6] = ptr[7] = 0;
  188. }
  189. ptr += 9;
  190. }
  191. }
  192. } rng;
  193. using BMode = param::WarpPerspective::BorderMode;
  194. param.format = param::WarpPerspective::Format::NHWC;
  195. // add for nearest test
  196. param.imode = param::WarpPerspective::InterpolationMode::NEAREST;
  197. for (auto mode :
  198. {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT, BMode::WRAP,
  199. BMode::CONSTANT}) {
  200. param.bmode = mode;
  201. param.border_val = 1.737;
  202. checker.set_param(param);
  203. UniformIntRNG rng(0, 1);
  204. checker.set_rng(2, &rng);
  205. checker.set_dtype(2, dtype::Int32());
  206. checker.exec({{2, 5, 5, 1}, {4, 3, 3}, {4}, {4, 5, 5, 1}});
  207. }
  208. // resize nan case
  209. UniformFloatRNG rng_zero(0, 0);
  210. checker.set_rng(1, &rng_zero);
  211. {
  212. param.bmode = BMode::CONSTANT;
  213. param.border_val = 1.737;
  214. checker.set_param(param);
  215. UniformIntRNG rng(0, 999);
  216. checker.set_rng(2, &rng);
  217. checker.set_dtype(2, dtype::Int32());
  218. checker.exec({{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  219. }
  220. // add linear test
  221. param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
  222. for (auto mode :
  223. {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT, BMode::WRAP,
  224. BMode::CONSTANT}) {
  225. param.bmode = mode;
  226. param.border_val = 1.737;
  227. checker.set_param(param);
  228. UniformIntRNG rng(0, 9);
  229. checker.set_rng(2, &rng);
  230. checker.set_dtype(2, dtype::Int32());
  231. checker.exec({{10, 128, 108, 3}, {20, 3, 3}, {20}, {20, 56, 128, 3}});
  232. }
  233. // resize nan case
  234. checker.set_rng(1, &rng_zero);
  235. {
  236. param.bmode = BMode::CONSTANT;
  237. param.border_val = 1.737;
  238. checker.set_param(param);
  239. UniformIntRNG rng(0, 999);
  240. checker.set_rng(2, &rng);
  241. checker.set_dtype(2, dtype::Int32());
  242. checker.exec({{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
  243. }
  244. auto args = warp_perspective::get_cv_args();
  245. for (auto&& arg : args) {
  246. ConstValue rng(0.f);
  247. checker.set_param(arg.param)
  248. .set_rng(2, &rng)
  249. .set_dtype(0, dtype::Uint8())
  250. .set_dtype(1, dtype::Float32())
  251. .set_dtype(2, dtype::Int32())
  252. .set_dtype(3, dtype::Uint8())
  253. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  254. }
  255. for (auto&& arg : args) {
  256. ConstValue rng(0.f);
  257. checker.set_param(arg.param)
  258. .set_rng(2, &rng)
  259. .set_dtype(0, dtype::Float32())
  260. .set_dtype(1, dtype::Float32())
  261. .set_dtype(2, dtype::Int32())
  262. .set_dtype(3, dtype::Float32())
  263. .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
  264. }
  265. }
  266. #if MEGDNN_WITH_BENCHMARK
  267. TEST_F(AARCH64, BENCHMARK_WARP_PERSPECTIVE_FORWARD) {
  268. Benchmarker<WarpPerspectiveForward> benchmarker(handle());
  269. auto handle_naive = create_cpu_handle(2);
  270. Benchmarker<WarpPerspectiveForward> benchmarker_naive(handle_naive.get());
  271. constexpr size_t NR_RUN = 50;
  272. using BMode = param::WarpPerspective::BorderMode;
  273. using IMode = param::WarpPerspective::InterpolationMode;
  274. WarpPerspective::Param param;
  275. param.border_val = 0.3f;
  276. param.format = param::WarpPerspective::Format::NHWC;
  277. auto run = [&](size_t N, size_t C, size_t IH, size_t IW, size_t OH, size_t OW,
  278. size_t scale) {
  279. printf("src={%zu, %zu, %zu, %zu}, dst={%zu, %zu, %zu, %zu}\n", N, IH, IW, C, N,
  280. OH, OW, C);
  281. auto time_ms =
  282. benchmarker.exec({{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) / NR_RUN;
  283. auto time_naive_ms =
  284. benchmarker_naive.exec({{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
  285. NR_RUN;
  286. auto bandwidth = N * C * (scale * OH * OW) * dtype::Float32().size();
  287. printf("aarch64: %.3f, perf: %.3f GBPS naive: %.3f, perf %.3f GBPS "
  288. "speedup: %f\n",
  289. time_ms, bandwidth / time_ms / 1e6, time_naive_ms,
  290. bandwidth / time_naive_ms / 1e6, time_naive_ms / time_ms);
  291. };
  292. std::vector<std::string> bmodestringmap = {
  293. "REPLICATE", "REFLECT", "REFLECT_101", "WARP", "CONSTANT"};
  294. std::vector<std::string> imodestringmap = {"NEAREST", "INTER_LINEAR"};
  295. size_t scales[2] = {2, 5};
  296. for (auto imode : {IMode::NEAREST, IMode::INTER_LINEAR}) {
  297. for (auto bmode :
  298. {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT, BMode::WRAP,
  299. BMode::CONSTANT}) {
  300. param.imode = imode;
  301. param.bmode = bmode;
  302. benchmarker.set_param(param).set_display(false).set_times(NR_RUN);
  303. benchmarker_naive.set_param(param).set_display(false).set_times(NR_RUN);
  304. size_t scale = scales[(int)imode];
  305. printf("\n\n\n warpperspective InterpolationMode::%s "
  306. "BorderMode::%s start\n",
  307. imodestringmap[(int)imode].c_str(),
  308. bmodestringmap[(int)bmode].c_str());
  309. for (auto&& shape : std::vector<std::pair<size_t, size_t>>{
  310. {700, 490},
  311. {500, 334},
  312. {472, 342},
  313. {448, 306},
  314. {626, 412},
  315. {140, 144},
  316. {120, 128},
  317. {180, 176}}) {
  318. for (size_t ch : {1, 2, 3}) {
  319. run(1, ch, shape.first, shape.second, 256, 256, scale);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. #endif
  326. } // namespace test
  327. } // namespace megdnn
  328. // vim: syntax=cpp.doxygen