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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * \file dnn/test/cpu/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 implied.
  10. */
  11. #include "test/cpu/fixture.h"
  12. #include "test/common/checker.h"
  13. #include "test/common/rng.h"
  14. #include "test/common/random_state.h"
  15. #include "test/common/warp_perspective.h"
  16. namespace megdnn {
  17. namespace test {
  18. TEST_F(CPU, WARP_PERSPECTIVE_CV) {
  19. //! Just for the format NHWC
  20. Checker<WarpPerspective> checker(handle());
  21. param::WarpPerspective param;
  22. class ResizeMatRNG : public RNG {
  23. void gen(const TensorND& tensor_) override {
  24. auto& gen = RandomState::generator();
  25. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
  26. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  27. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  28. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  29. std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
  30. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  31. std::uniform_int_distribution<int> dice(0, 5);
  32. float* ptr = tensor_.ptr<dt_float32>();
  33. auto N = tensor_.layout.shape[0];
  34. for (size_t n = 0; n < N; ++n) {
  35. for (size_t i = 0; i < 9; ++i) {
  36. switch (dice(gen)) {
  37. case 0:
  38. ptr[i] = pdist3(gen);
  39. break;
  40. case 1:
  41. ptr[i] = pdist(gen);
  42. break;
  43. case 2:
  44. ptr[i] = pdisth(gen);
  45. break;
  46. case 3:
  47. ptr[i] = ndist(gen);
  48. break;
  49. case 4:
  50. ptr[i] = ndist3(gen);
  51. break;
  52. case 5:
  53. ptr[i] = ndisth(gen);
  54. break;
  55. }
  56. }
  57. // is resize?
  58. if (n & 1) {
  59. ptr[1] = 0;
  60. ptr[3] = 0;
  61. ptr[6] = ptr[7] = 0;
  62. }
  63. ptr += 9;
  64. }
  65. }
  66. } rng;
  67. checker.set_rng(1, &rng);
  68. using BMode = param::WarpPerspective::BorderMode;
  69. param.format = param::WarpPerspective::Format::NHWC;
  70. for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
  71. BMode::WRAP, BMode::CONSTANT}) {
  72. param.bmode = mode;
  73. param.border_val = 1.737;
  74. checker.set_param(param);
  75. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  76. }
  77. // resize nan case
  78. UniformFloatRNG rng_zero(0, 0);
  79. checker.set_rng(1, &rng_zero);
  80. {
  81. param.bmode = BMode::CONSTANT;
  82. param.border_val = 1.737;
  83. checker.set_param(param);
  84. checker.exec({{1000, 2, 10, 3}, {1000, 3, 3}, {1000, 2, 12, 3}});
  85. }
  86. auto args = warp_perspective::get_cv_args();
  87. for (auto&& arg : args) {
  88. checker.set_param(arg.param)
  89. .set_epsilon(1 + 1e-3)
  90. .set_dtype(0, dtype::Uint8())
  91. .set_dtype(1, dtype::Float32())
  92. .set_dtype(2, dtype::Uint8())
  93. .execs({arg.src, arg.trans, arg.dst});
  94. }
  95. for (auto&& arg : args) {
  96. checker.set_param(arg.param)
  97. .set_dtype(0, dtype::Float32())
  98. .set_dtype(1, dtype::Float32())
  99. .set_dtype(2, dtype::Float32())
  100. .execs({arg.src, arg.trans, arg.dst});
  101. }
  102. }
  103. } // namespace test
  104. } // namespace megdnn
  105. // vim: syntax=cpp.doxygen

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