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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "test/common/warp_perspective.h"
  2. #include "test/common/benchmarker.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/task_record_check.h"
  5. using namespace megdnn;
  6. using namespace test;
  7. using namespace warp_perspective;
  8. void WarpPerspectiveMatIdxProxy::deduce_layout(WarpPerspective*, TensorLayoutArray&) {}
  9. void WarpPerspectiveMatIdxProxy::deduce_layout(
  10. WarpPerspectiveBackwardData*, TensorLayoutArray&) {}
  11. void WarpPerspectiveMatIdxProxy::deduce_layout(
  12. WarpPerspectiveBackwardMat*, TensorLayoutArray&) {}
  13. void WarpPerspectiveMatIdxProxy::exec(
  14. WarpPerspective* opr, const TensorNDArray& tensors) {
  15. if (!W.valid()) {
  16. W = WorkspaceWrapper(opr->handle(), 0);
  17. }
  18. megdnn_assert(tensors.size() == 4);
  19. W.update(opr->get_workspace_in_bytes(
  20. tensors[0].layout, tensors[1].layout, tensors[2].layout,
  21. tensors[3].layout));
  22. opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], W.workspace());
  23. }
  24. void WarpPerspectiveMatIdxProxy::exec(
  25. WarpPerspectiveBackwardData* opr, const TensorNDArray& tensors) {
  26. if (!W.valid()) {
  27. W = WorkspaceWrapper(opr->handle(), 0);
  28. }
  29. megdnn_assert(tensors.size() == 4);
  30. W.update(opr->get_workspace_in_bytes(
  31. tensors[0].layout, tensors[1].layout, tensors[2].layout,
  32. tensors[3].layout));
  33. opr->exec(tensors[0], tensors[1], tensors[2], tensors[3], W.workspace());
  34. }
  35. void WarpPerspectiveMatIdxProxy::exec(
  36. WarpPerspectiveBackwardMat* opr, const TensorNDArray& tensors) {
  37. if (!W.valid()) {
  38. W = WorkspaceWrapper(opr->handle(), 0);
  39. }
  40. megdnn_assert(tensors.size() == 5);
  41. W.update(opr->get_workspace_in_bytes(
  42. tensors[0].layout, tensors[1].layout, tensors[2].layout, tensors[3].layout,
  43. tensors[4].layout));
  44. opr->exec(
  45. tensors[0], tensors[1], tensors[2], tensors[3], tensors[4], W.workspace());
  46. }
  47. std::vector<TestArg> warp_perspective::get_cv_args() {
  48. std::vector<TestArg> args;
  49. // in warp_perspective_cv INTER_AREA == INTER_LINEAR
  50. using BorderMode = param::WarpPerspective::BorderMode;
  51. using InterpolationMode = param::WarpPerspective::InterpolationMode;
  52. param::WarpPerspective cur_param;
  53. for (size_t i = 4; i < 129; i *= 4) {
  54. for (size_t ic : {1, 2, 3}) {
  55. for (BorderMode bmode : {
  56. BorderMode::REPLICATE,
  57. BorderMode::REFLECT,
  58. BorderMode::REFLECT_101,
  59. BorderMode::WRAP,
  60. BorderMode::CONSTANT,
  61. }) {
  62. for (InterpolationMode imode :
  63. {InterpolationMode::NEAREST, InterpolationMode::LINEAR,
  64. InterpolationMode::CUBIC, InterpolationMode::LANCZOS4}) {
  65. cur_param.bmode = bmode;
  66. cur_param.format = param::WarpPerspective::Format::NHWC;
  67. cur_param.imode = imode;
  68. args.emplace_back(
  69. cur_param, TensorShape{1, i, i, ic}, TensorShape{1, 3, 3},
  70. TensorShape{1}, TensorShape{1, i, i, ic});
  71. args.emplace_back(
  72. cur_param, TensorShape{1, i, i * 2, ic},
  73. TensorShape{1, 3, 3}, TensorShape{1},
  74. TensorShape{1, i, i * 2, ic});
  75. args.emplace_back(
  76. cur_param, TensorShape{1, i * 3, i, ic},
  77. TensorShape{1, 3, 3}, TensorShape{1},
  78. TensorShape{1, i * 3, i, ic});
  79. cur_param.border_val = 0.78f;
  80. args.emplace_back(
  81. cur_param, TensorShape{1, i, i, ic}, TensorShape{1, 3, 3},
  82. TensorShape{1}, TensorShape{1, 8, 8, ic});
  83. args.emplace_back(
  84. cur_param, TensorShape{1, i, i * 2, ic},
  85. TensorShape{1, 3, 3}, TensorShape{1},
  86. TensorShape{1, 8, 8, ic});
  87. args.emplace_back(
  88. cur_param, TensorShape{1, i * 3, i, ic},
  89. TensorShape{1, 3, 3}, TensorShape{1},
  90. TensorShape{1, 8, 8, ic});
  91. }
  92. }
  93. }
  94. }
  95. return args;
  96. }
  97. void warp_perspective::run_mat_idx_test(Handle* handle) {
  98. constexpr int N_SRC = 5;
  99. Checker<WarpPerspectiveForward, WarpPerspectiveMatIdxProxy> checker(handle);
  100. WarpPerspectiveMatRNG mat_rng;
  101. checker.set_rng(1, &mat_rng);
  102. UniformIntRNG mat_idx_rng{0, N_SRC - 1};
  103. checker.set_dtype(2, dtype::Int32());
  104. checker.set_rng(2, &mat_idx_rng);
  105. WarpPerspective::Param param;
  106. param.bmode = WarpPerspective::Param::BorderMode::REFLECT;
  107. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  108. checker.set_param(param);
  109. checker.execs({{N_SRC, 3, 10, 11}, {2, 3, 3}, {2}, {2, 3, 11, 12}});
  110. checker.execs({{N_SRC, 14, 17, 13}, {123, 3, 3}, {123}, {123, 14, 16, 15}});
  111. // test NHWC
  112. param.format = WarpPerspective::Param::Format::NHWC;
  113. checker.set_param(param)
  114. .set_rng(2, &mat_idx_rng)
  115. .set_epsilon(1e-1)
  116. .set_dtype(2, dtype::Int32());
  117. checker.execs({{N_SRC, 10, 11, 3}, {2, 3, 3}, {2}, {2, 11, 12, 3}});
  118. }
  119. void warp_perspective::run_int8_test_record(int debug_level) {
  120. using Param = WarpPerspective::Param;
  121. TaskRecordChecker<WarpPerspectiveForward> checker(debug_level);
  122. UniformIntRNG input_rng{-128, 127};
  123. WarpPerspectiveMatRNG mat_rng;
  124. class ResizeBy2xMatRNG : public RNG {
  125. void gen(const TensorND& tensor_) override {
  126. float* ptr = tensor_.ptr<float>();
  127. auto N = tensor_.layout.shape[0];
  128. megdnn_assert(
  129. tensor_.layout.is_contiguous() && tensor_.layout.ndim == 3 &&
  130. tensor_.layout[1] == 3 && tensor_.layout[2] == 3);
  131. for (size_t n = 0; n < N; ++n) {
  132. // | 1 0 0 |
  133. // mat = | 0 1 0 |
  134. // | 0 0 2 |
  135. // resize_2x
  136. ptr[0] = ptr[4] = 1;
  137. ptr[8] = 2;
  138. ptr[1] = ptr[2] = ptr[3] = ptr[5] = ptr[6] = ptr[7] = 0;
  139. ptr += 9;
  140. }
  141. }
  142. } resize_2x_mat_rng;
  143. checker.set_rng(0, &input_rng)
  144. .set_rng(1, &mat_rng)
  145. .set_dtype(0, dtype::Int8())
  146. .set_dtype(1, dtype::Float32())
  147. .set_dtype(2, dtype::Int8())
  148. .set_param(
  149. {Param::InterpolationMode::LINEAR, Param::BorderMode::CONSTANT,
  150. Param::Format::NCHW, 0.f});
  151. checker.execs({{99, 48, 17, 17}, {99, 3, 3}, {99, 48, 22, 22}})
  152. .execs({{12, 3, 224, 224}, {12, 3, 3}, {12, 3, 256, 256}});
  153. checker.set_rng(1, &resize_2x_mat_rng);
  154. checker.execs({{98, 48, 17, 17}, {98, 3, 3}, {98, 48, 34, 34}})
  155. .execs({{13, 3, 224, 224}, {13, 3, 3}, {13, 3, 448, 448}});
  156. }
  157. void warp_perspective::run_int8_test(Handle* handle) {
  158. using Param = WarpPerspective::Param;
  159. Checker<WarpPerspectiveForward> checker(handle);
  160. UniformIntRNG input_rng{-128, 127};
  161. WarpPerspectiveMatRNG mat_rng;
  162. class ResizeBy2xMatRNG : public RNG {
  163. void gen(const TensorND& tensor_) override {
  164. float* ptr = tensor_.ptr<float>();
  165. auto N = tensor_.layout.shape[0];
  166. megdnn_assert(
  167. tensor_.layout.is_contiguous() && tensor_.layout.ndim == 3 &&
  168. tensor_.layout[1] == 3 && tensor_.layout[2] == 3);
  169. for (size_t n = 0; n < N; ++n) {
  170. // | 1 0 0 |
  171. // mat = | 0 1 0 |
  172. // | 0 0 2 |
  173. // resize_2x
  174. ptr[0] = ptr[4] = 1;
  175. ptr[8] = 2;
  176. ptr[1] = ptr[2] = ptr[3] = ptr[5] = ptr[6] = ptr[7] = 0;
  177. ptr += 9;
  178. }
  179. }
  180. } resize_2x_mat_rng;
  181. if (handle->type() == Handle::HandleType::CUDA) {
  182. // As currently the computation is performed in floating points instead
  183. // of full int, it could be slightly different on GPU.
  184. checker.set_epsilon(1.1).set_max_avg_error(7e-5);
  185. }
  186. checker.set_rng(0, &input_rng)
  187. .set_rng(1, &mat_rng)
  188. .set_dtype(0, dtype::Int8())
  189. .set_dtype(1, dtype::Float32())
  190. .set_dtype(2, dtype::Int8())
  191. .set_param(
  192. {Param::InterpolationMode::LINEAR, Param::BorderMode::CONSTANT,
  193. Param::Format::NCHW, 0.f});
  194. checker.execs({{99, 48, 17, 17}, {99, 3, 3}, {99, 48, 22, 22}})
  195. .execs({{12, 3, 224, 224}, {12, 3, 3}, {12, 3, 256, 256}});
  196. checker.set_rng(1, &resize_2x_mat_rng);
  197. checker.execs({{98, 48, 17, 17}, {98, 3, 3}, {98, 48, 34, 34}})
  198. .execs({{13, 3, 224, 224}, {13, 3, 3}, {13, 3, 448, 448}});
  199. }
  200. void warp_perspective::run_quint8_test(Handle* handle) {
  201. using Param = WarpPerspective::Param;
  202. Checker<WarpPerspectiveForward> checker(handle);
  203. UniformIntRNG input_rng{0, 255};
  204. WarpPerspectiveMatRNG mat_rng;
  205. class ResizeBy2xMatRNG : public RNG {
  206. void gen(const TensorND& tensor_) override {
  207. float* ptr = tensor_.ptr<float>();
  208. auto N = tensor_.layout.shape[0];
  209. megdnn_assert(
  210. tensor_.layout.is_contiguous() && tensor_.layout.ndim == 3 &&
  211. tensor_.layout[1] == 3 && tensor_.layout[2] == 3);
  212. for (size_t n = 0; n < N; ++n) {
  213. // | 1 0 0 |
  214. // mat = | 0 1 0 |
  215. // | 0 0 2 |
  216. // resize_2x
  217. ptr[0] = ptr[4] = 1;
  218. ptr[8] = 2;
  219. ptr[1] = ptr[2] = ptr[3] = ptr[5] = ptr[6] = ptr[7] = 0;
  220. ptr += 9;
  221. }
  222. }
  223. } resize_2x_mat_rng;
  224. if (handle->type() == Handle::HandleType::CUDA) {
  225. // As currently the computation is performed in floating points instead
  226. // of full int, it could be slightly different on GPU.
  227. checker.set_epsilon(1.1).set_max_avg_error(7e-5);
  228. }
  229. checker.set_rng(0, &input_rng)
  230. .set_rng(1, &mat_rng)
  231. .set_dtype(0, dtype::Quantized8Asymm(0.6f, static_cast<uint8_t>(127)))
  232. .set_dtype(1, dtype::Float32())
  233. .set_dtype(2, dtype::Quantized8Asymm(0.6f, static_cast<uint8_t>(127)))
  234. .set_param(
  235. {Param::InterpolationMode::LINEAR, Param::BorderMode::CONSTANT,
  236. Param::Format::NCHW, 0.f});
  237. checker.execs({{99, 48, 17, 17}, {99, 3, 3}, {99, 48, 22, 22}})
  238. .execs({{12, 3, 224, 224}, {12, 3, 3}, {12, 3, 256, 256}});
  239. checker.set_rng(1, &resize_2x_mat_rng);
  240. checker.execs({{98, 48, 17, 17}, {98, 3, 3}, {98, 48, 34, 34}})
  241. .execs({{13, 3, 224, 224}, {13, 3, 3}, {13, 3, 448, 448}});
  242. }
  243. // vim: syntax=cpp.doxygen