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

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