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

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

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