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.1 kB

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