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

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