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

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

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