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.h 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * \file dnn/test/common/warp_perspective.h
  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. #pragma once
  12. #include "test/common/rng.h"
  13. #include "test/common/random_state.h"
  14. #include "test/common/workspace_wrapper.h"
  15. #include "megdnn/oprs/imgproc.h"
  16. #include "megdnn/opr_param_defs.h"
  17. namespace megdnn {
  18. namespace test {
  19. struct WarpPerspectiveMatIdxProxy {
  20. WorkspaceWrapper W;
  21. static void deduce_layout(WarpPerspective*, TensorLayoutArray&);
  22. static void deduce_layout(WarpPerspectiveBackwardData*, TensorLayoutArray&);
  23. static void deduce_layout(WarpPerspectiveBackwardMat*, TensorLayoutArray&);
  24. void exec(WarpPerspective* opr, const TensorNDArray& tensors);
  25. void exec(WarpPerspectiveBackwardData* opr, const TensorNDArray& tensors);
  26. void exec(WarpPerspectiveBackwardMat* opr, const TensorNDArray& tensors);
  27. };
  28. class WarpPerspectiveMatRNG final : public IIDRNG {
  29. public:
  30. WarpPerspectiveMatRNG() : idx(0) {}
  31. dt_float32 gen_single_val() override {
  32. std::normal_distribution<float_t> dist;
  33. switch (idx) {
  34. case 6:
  35. case 7:
  36. dist = std::normal_distribution<float_t>(0.0f, 0.01f);
  37. break;
  38. case 8:
  39. dist = std::normal_distribution<float_t>(1.0f, 0.1f);
  40. break;
  41. default:
  42. dist = std::normal_distribution<float_t>(0.0f, 1.0f);
  43. break;
  44. }
  45. auto res = dist(RandomState::generator());
  46. idx = (idx + 1) % 9;
  47. return res;
  48. }
  49. private:
  50. size_t idx;
  51. };
  52. class WarpPerspectiveMatRNG_V2 final : public IIDRNG {
  53. public:
  54. WarpPerspectiveMatRNG_V2() : idx(0) {}
  55. void set_hw(size_t h, size_t w) {
  56. ih = h;
  57. iw = w;
  58. idx = 0;
  59. rng.seed(time(NULL));
  60. }
  61. dt_float32 gen_single_val() override {
  62. auto rand_real = [&](double lo, double hi) {
  63. return rng() / (std::mt19937::max() + 1.0) * (hi - lo) + lo;
  64. };
  65. auto rand_real2 = [&](double range) {
  66. return rand_real(-range, range);
  67. };
  68. dt_float32 res = 0;
  69. switch (idx) {
  70. case 0:
  71. rot = rand_real(0, M_PI * 2);
  72. scale = rand_real(0.8, 1.2);
  73. sheer = rand_real(0.9, 1.1);
  74. res = cos(rot) * scale;
  75. break;
  76. case 1:
  77. res = -sin(rot) * scale;
  78. break;
  79. case 2:
  80. res = rand_real2(iw * 0.5);
  81. break;
  82. case 3:
  83. res = sin(rot) * scale * sheer;
  84. break;
  85. case 4:
  86. res = cos(rot) * scale * sheer;
  87. break;
  88. case 5:
  89. res = rand_real2(ih * 0.5);
  90. break;
  91. case 6:
  92. res = rand_real2(0.1 / iw);
  93. break;
  94. case 7:
  95. res = rand_real2(0.1 / ih);
  96. break;
  97. case 8:
  98. res = rand_real2(0.1) + 1;
  99. break;
  100. }
  101. idx = (idx + 1) % 9;
  102. return res;
  103. }
  104. private:
  105. size_t idx, ih, iw;
  106. float rot, scale, sheer;
  107. std::mt19937 rng;
  108. };
  109. namespace warp_perspective {
  110. struct TestArg {
  111. param::WarpPerspective param;
  112. TensorShape src;
  113. TensorShape trans;
  114. TensorShape mat_idx;
  115. TensorShape dst;
  116. TestArg(param::WarpPerspective param_, TensorShape src_, TensorShape trans_, TensorShape mat_idx_,
  117. TensorShape dst_)
  118. : param(param_), src(src_), trans(trans_), mat_idx(mat_idx_), dst(dst_) {}
  119. };
  120. //! Test args for the WarpPerspective with format NHWC
  121. std::vector<TestArg> get_cv_args();
  122. void run_mat_idx_test(Handle* handle);
  123. void run_int8_test(Handle* handle);
  124. void run_quint8_test(Handle* handle);
  125. } // namespace warp_perspective
  126. } // namespace test
  127. } // namespace megdnn
  128. // vim: syntax=cpp.doxygen

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