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.

resize.h 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * \file dnn/test/common/resize.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
  10. * implied.
  11. */
  12. #pragma once
  13. #include <iostream>
  14. #include "megdnn/basic_types.h"
  15. #include "megdnn/opr_param_defs.h"
  16. #include "./rng.h"
  17. namespace megdnn {
  18. namespace test {
  19. namespace resize {
  20. using IMode = param::Resize::InterpolationMode;
  21. struct TestArg {
  22. param::Resize param;
  23. TensorShape src;
  24. TensorShape dst;
  25. TestArg(param::Resize param_, TensorShape src_, TensorShape dst_)
  26. : param(param_), src(src_), dst(dst_) {}
  27. };
  28. // Get the args for linear test
  29. static void set_linear_args(std::vector<TestArg>& args) {
  30. // test src_rows == dst_rows * 2 && src_cols == dst_cols * 2
  31. param::Resize cur_param;
  32. cur_param.format = param::Resize::Format::NHWC;
  33. cur_param.imode = param::Resize::InterpolationMode::INTER_LINEAR;
  34. args.emplace_back(cur_param, TensorShape{1, 6, 6, 1},
  35. TensorShape{1, 3, 3, 1});
  36. // test resize_linear_Restric_kernel
  37. // CH == 3 && dst_rows < src_rows && dst_cols < src_cols
  38. args.emplace_back(cur_param, TensorShape{1, 4, 4, 3},
  39. TensorShape{1, 3, 3, 3});
  40. // test else
  41. args.emplace_back(cur_param, TensorShape{1, 4, 4, 1},
  42. TensorShape{1, 3, 3, 1});
  43. args.emplace_back(cur_param, TensorShape{1, 4, 6, 1},
  44. TensorShape{1, 10, 9, 1});
  45. args.emplace_back(cur_param, TensorShape{1, 4, 6, 3},
  46. TensorShape{1, 10, 9, 3});
  47. }
  48. static void set_nchw_args(std::vector<TestArg>& args) {
  49. param::Resize param;
  50. param.format = param::Resize::Format::NCHW;
  51. param.imode = param::Resize::InterpolationMode::LINEAR;
  52. args.emplace_back(param, TensorShape{2, 2, 3, 4}, TensorShape{2, 2, 6, 8});
  53. args.emplace_back(param, TensorShape{1, 2, 2, 2}, TensorShape{1, 2, 4, 3});
  54. args.emplace_back(param, TensorShape{1, 2, 6, 8}, TensorShape{1, 2, 3, 4});
  55. }
  56. static inline std::vector<TestArg> get_args(IMode imode = IMode::INTER_LINEAR) {
  57. std::vector<TestArg> args;
  58. set_nchw_args(args);
  59. if (imode == IMode::INTER_LINEAR) {
  60. //! test NHWC with ch != 1 or ch != 3
  61. param::Resize param;
  62. param.format = param::Resize::Format::NHWC;
  63. param.imode = imode;
  64. args.emplace_back(param, TensorShape{2, 2, 3, 4},
  65. TensorShape{2, 4, 6, 4});
  66. args.emplace_back(param, TensorShape{2, 4, 6, 4},
  67. TensorShape{2, 2, 3, 4});
  68. }
  69. return args;
  70. }
  71. static inline std::vector<TestArg> get_nhwcd4_args() {
  72. std::vector<TestArg> args;
  73. param::Resize param;
  74. param.format = param::Resize::Format::NHWCD4;
  75. param.imode = param::Resize::InterpolationMode::LINEAR;
  76. args.emplace_back(param, TensorShape{2, 2, 1, 3, 4},
  77. TensorShape{2, 4, 1, 6, 4});
  78. args.emplace_back(param, TensorShape{2, 4, 1, 6, 4},
  79. TensorShape{2, 2, 1, 3, 4});
  80. return args;
  81. }
  82. static inline std::vector<TestArg> get_nchw4_args() {
  83. std::vector<TestArg> args;
  84. param::Resize param;
  85. param.format = param::Resize::Format::NCHW4;
  86. param.imode = param::Resize::InterpolationMode::LINEAR;
  87. args.emplace_back(param, TensorShape{1, 1, 2, 3, 4},
  88. TensorShape{1, 1, 2, 6, 4});
  89. args.emplace_back(param, TensorShape{2, 2, 2, 2, 4},
  90. TensorShape{2, 2, 2, 4, 4});
  91. args.emplace_back(param, TensorShape{2, 4, 6, 8, 4},
  92. TensorShape{2, 4, 3, 4, 4});
  93. return args;
  94. }
  95. static inline std::vector<TestArg> get_nchw44_args() {
  96. std::vector<TestArg> args;
  97. param::Resize param;
  98. param.format = param::Resize::Format::NCHW44;
  99. param.imode = param::Resize::InterpolationMode::LINEAR;
  100. rep(n, 4ul) rep(c, 4ul) rep(ih, 4ul) rep(iw, 4ul) rep(oh, 4ul) rep(ow, 4ul)
  101. args.emplace_back(
  102. param,
  103. TensorShape{n + 1ul, c + 1ul, ih + 1ul, iw + 1ul, 4ul},
  104. TensorShape{n + 1ul, c + 1ul, oh + 1ul, ow + 1ul, 4ul});
  105. param.imode = param::Resize::InterpolationMode::NEAREST;
  106. rep(n, 4ul) rep(c, 4ul) rep(ih, 4ul) rep(iw, 4ul) rep(oh, 4ul) rep(ow, 4ul)
  107. args.emplace_back(
  108. param,
  109. TensorShape{n + 1ul, c + 1ul, ih + 1ul, iw + 1ul, 4ul},
  110. TensorShape{n + 1ul, c + 1ul, oh + 1ul, ow + 1ul, 4ul});
  111. return args;
  112. }
  113. static inline std::vector<TestArg> get_nchw88_args() {
  114. std::vector<TestArg> args;
  115. param::Resize param;
  116. param.format = param::Resize::Format::NCHW88;
  117. param.imode = param::Resize::InterpolationMode::LINEAR;
  118. rep(n, 4ul) rep(c, 4ul) rep(ih, 4ul) rep(iw, 4ul) rep(oh, 4ul) rep(ow, 4ul)
  119. args.emplace_back(
  120. param,
  121. TensorShape{n + 1ul, c + 1ul, ih + 1ul, iw + 1ul, 8ul},
  122. TensorShape{n + 1ul, c + 1ul, oh + 1ul, ow + 1ul, 8ul});
  123. param.imode = param::Resize::InterpolationMode::NEAREST;
  124. rep(n, 4ul) rep(c, 4ul) rep(ih, 4ul) rep(iw, 4ul) rep(oh, 4ul) rep(ow, 4ul)
  125. args.emplace_back(
  126. param,
  127. TensorShape{n + 1ul, c + 1ul, ih + 1ul, iw + 1ul, 8ul},
  128. TensorShape{n + 1ul, c + 1ul, oh + 1ul, ow + 1ul, 8ul});
  129. return args;
  130. }
  131. static inline std::vector<TestArg> get_cv_args() {
  132. std::vector<TestArg> args;
  133. set_linear_args(args);
  134. param::Resize cur_param;
  135. cur_param.format = param::Resize::Format::NHWC;
  136. for (size_t i = 8; i < 129; i *= 4) {
  137. cur_param.imode = param::Resize::InterpolationMode::INTER_NEAREST;
  138. args.emplace_back(cur_param, TensorShape{1, i, i, 3},
  139. TensorShape{1, i / 2, i / 2, 3});
  140. args.emplace_back(cur_param, TensorShape{1, i, i, 1},
  141. TensorShape{1, 8, 8, 1});
  142. cur_param.imode = param::Resize::InterpolationMode::INTER_AREA;
  143. args.emplace_back(cur_param, TensorShape{1, i, i, 3},
  144. TensorShape{1, 8, 8, 3});
  145. cur_param.imode = param::Resize::InterpolationMode::INTER_CUBIC;
  146. args.emplace_back(cur_param, TensorShape{1, i, i, 3},
  147. TensorShape{1, 8, 8, 3});
  148. cur_param.imode = param::Resize::InterpolationMode::INTER_LANCZOS4;
  149. args.emplace_back(cur_param, TensorShape{1, i, i, 3},
  150. TensorShape{1, 8, 8, 3});
  151. }
  152. //! cuda not use vector
  153. //! enlarge==true && dst_area_size > 500 * 500
  154. cur_param.imode = param::Resize::InterpolationMode::INTER_CUBIC;
  155. args.emplace_back(cur_param, TensorShape{1, 3, 3, 1},
  156. TensorShape{1, 500, 600, 1});
  157. cur_param.imode = param::Resize::InterpolationMode::INTER_LANCZOS4;
  158. args.emplace_back(cur_param, TensorShape{1, 3, 3, 1},
  159. TensorShape{1, 500, 600, 1});
  160. return args;
  161. }
  162. } // namespace resize
  163. } // namespace test
  164. } // namespace megdnn
  165. // vim: syntax=cpp.doxygen

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