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.

test_interp.cpp 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "layer/interp.h"
  15. #include "testutil.h"
  16. static int test_interp(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, resize_type);
  20. pd.set(1, height_scale);
  21. pd.set(2, width_scale);
  22. pd.set(3, output_height);
  23. pd.set(4, output_width);
  24. std::vector<ncnn::Mat> weights(0);
  25. int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
  26. if (ret != 0)
  27. {
  28. fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width);
  29. }
  30. return ret;
  31. }
  32. static int test_interp_ref(const ncnn::Mat& a, int resize_type, int output_height, int output_width)
  33. {
  34. ncnn::ParamDict pd;
  35. pd.set(0, resize_type);
  36. pd.set(5, 1);
  37. std::vector<ncnn::Mat> as(2);
  38. as[0] = a;
  39. as[1] = ncnn::Mat(output_width, output_height, 1);
  40. std::vector<ncnn::Mat> weights(0);
  41. int ret = test_layer<ncnn::Interp>("Interp", pd, weights, as);
  42. if (ret != 0)
  43. {
  44. fprintf(stderr, "test_interp_ref failed a.dims=%d a=(%d %d %d) resize_type=%d output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, output_height, output_width);
  45. }
  46. return ret;
  47. }
  48. static int test_interp_align_corner(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width, int align_corner)
  49. {
  50. ncnn::ParamDict pd;
  51. pd.set(0, resize_type);
  52. pd.set(1, height_scale);
  53. pd.set(2, width_scale);
  54. pd.set(3, output_height);
  55. pd.set(4, output_width);
  56. pd.set(6, align_corner);
  57. std::vector<ncnn::Mat> weights(0);
  58. int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
  59. if (ret != 0)
  60. {
  61. fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d align_corner=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width, align_corner);
  62. }
  63. return ret;
  64. }
  65. static int test_interp_0()
  66. {
  67. ncnn::Mat a = RandomMat(15, 16, 7);
  68. ncnn::Mat b = RandomMat(14, 17, 12);
  69. ncnn::Mat c = RandomMat(13, 14, 24);
  70. return 0
  71. || test_interp(a, 1, 2.f, 2.f, 0, 0)
  72. || test_interp(a, 1, 4.f, 0.5f, 0, 0)
  73. || test_interp(a, 1, 1.2f, 1.2f, 0, 0)
  74. || test_interp(a, 1, 0.8f, 0.8f, 0, 0)
  75. || test_interp(a, 1, 1.f, 1.f, 10, 12)
  76. || test_interp(a, 1, 1.f, 1.f, 2, 2)
  77. || test_interp(a, 1, 1.f, 1.f, 15, 16)
  78. || test_interp_ref(a, 1, 10, 12)
  79. || test_interp_ref(a, 1, 2, 2)
  80. || test_interp_ref(a, 1, 15, 16)
  81. || test_interp(b, 1, 2.f, 2.f, 0, 0)
  82. || test_interp(b, 1, 4.f, 0.5f, 0, 0)
  83. || test_interp(b, 1, 1.2f, 1.2f, 0, 0)
  84. || test_interp(b, 1, 0.8f, 0.8f, 0, 0)
  85. || test_interp(b, 1, 1.f, 1.f, 10, 12)
  86. || test_interp(b, 1, 1.f, 1.f, 2, 2)
  87. || test_interp(b, 1, 1.f, 1.f, 14, 17)
  88. || test_interp_ref(b, 1, 10, 12)
  89. || test_interp_ref(b, 1, 2, 2)
  90. || test_interp_ref(b, 1, 14, 17)
  91. || test_interp(c, 1, 2.f, 2.f, 0, 0)
  92. || test_interp(c, 1, 4.f, 0.5f, 0, 0)
  93. || test_interp(c, 1, 1.2f, 1.2f, 0, 0)
  94. || test_interp(c, 1, 0.8f, 0.8f, 0, 0)
  95. || test_interp(c, 1, 1.f, 1.f, 10, 12)
  96. || test_interp(c, 1, 1.f, 1.f, 2, 2)
  97. || test_interp(c, 1, 1.f, 1.f, 14, 17)
  98. || test_interp_ref(c, 1, 10, 12)
  99. || test_interp_ref(c, 1, 2, 2)
  100. || test_interp_ref(c, 1, 14, 17);
  101. }
  102. static int test_interp_1()
  103. {
  104. ncnn::Mat a = RandomMat(15, 16, 7);
  105. ncnn::Mat b = RandomMat(14, 17, 12);
  106. ncnn::Mat c = RandomMat(13, 14, 24);
  107. return 0
  108. || test_interp(a, 2, 2.f, 2.f, 0, 0)
  109. || test_interp(a, 2, 4.f, 0.5f, 0, 0)
  110. || test_interp(a, 2, 1.2f, 1.2f, 0, 0)
  111. || test_interp(a, 2, 0.8f, 0.8f, 0, 0)
  112. || test_interp(a, 2, 1.f, 1.f, 10, 12)
  113. || test_interp(a, 2, 1.f, 1.f, 2, 2)
  114. || test_interp(a, 2, 1.f, 1.f, 15, 16)
  115. || test_interp_align_corner(a, 2, 2.f, 2.f, 0, 0, 1)
  116. || test_interp_align_corner(a, 2, 4.f, 0.5f, 0, 0, 1)
  117. || test_interp_align_corner(a, 2, 1.2f, 1.2f, 0, 0, 1)
  118. || test_interp_align_corner(a, 2, 0.8f, 0.8f, 0, 0, 1)
  119. || test_interp_align_corner(a, 2, 1.f, 1.f, 10, 12, 1)
  120. || test_interp_align_corner(a, 2, 1.f, 1.f, 2, 2, 1)
  121. || test_interp_align_corner(a, 2, 1.f, 1.f, 15, 16, 1)
  122. || test_interp_ref(a, 2, 10, 12)
  123. || test_interp_ref(a, 2, 2, 2)
  124. || test_interp_ref(a, 2, 15, 16)
  125. || test_interp(b, 2, 2.f, 2.f, 0, 0)
  126. || test_interp(b, 2, 4.f, 0.5f, 0, 0)
  127. || test_interp(b, 2, 1.2f, 1.2f, 0, 0)
  128. || test_interp(b, 2, 0.8f, 0.8f, 0, 0)
  129. || test_interp(b, 2, 1.f, 1.f, 10, 12)
  130. || test_interp(b, 2, 1.f, 1.f, 2, 2)
  131. || test_interp(b, 2, 1.f, 1.f, 14, 17)
  132. || test_interp_align_corner(b, 2, 2.f, 2.f, 0, 0, 1)
  133. || test_interp_align_corner(b, 2, 4.f, 0.5f, 0, 0, 1)
  134. || test_interp_align_corner(b, 2, 1.2f, 1.2f, 0, 0, 1)
  135. || test_interp_align_corner(b, 2, 0.8f, 0.8f, 0, 0, 1)
  136. || test_interp_align_corner(b, 2, 1.f, 1.f, 10, 12, 1)
  137. || test_interp_align_corner(b, 2, 1.f, 1.f, 2, 2, 1)
  138. || test_interp_align_corner(b, 2, 1.f, 1.f, 14, 17, 1)
  139. || test_interp_ref(b, 2, 10, 12)
  140. || test_interp_ref(b, 2, 2, 2)
  141. || test_interp_ref(b, 2, 14, 17)
  142. || test_interp(c, 2, 2.f, 2.f, 0, 0)
  143. || test_interp(c, 2, 4.f, 0.5f, 0, 0)
  144. || test_interp(c, 2, 1.2f, 1.2f, 0, 0)
  145. || test_interp(c, 2, 0.8f, 0.8f, 0, 0)
  146. || test_interp(c, 2, 1.f, 1.f, 10, 12)
  147. || test_interp(c, 2, 1.f, 1.f, 2, 2)
  148. || test_interp(c, 2, 1.f, 1.f, 14, 17)
  149. || test_interp_align_corner(c, 2, 2.f, 2.f, 0, 0, 1)
  150. || test_interp_align_corner(c, 2, 4.f, 0.5f, 0, 0, 1)
  151. || test_interp_align_corner(c, 2, 1.2f, 1.2f, 0, 0, 1)
  152. || test_interp_align_corner(c, 2, 0.8f, 0.8f, 0, 0, 1)
  153. || test_interp_align_corner(c, 2, 1.f, 1.f, 10, 12, 1)
  154. || test_interp_align_corner(c, 2, 1.f, 1.f, 2, 2, 1)
  155. || test_interp_align_corner(c, 2, 1.f, 1.f, 14, 17, 1)
  156. || test_interp_ref(c, 2, 10, 12)
  157. || test_interp_ref(c, 2, 2, 2)
  158. || test_interp_ref(c, 2, 14, 17);
  159. }
  160. static int test_interp_2()
  161. {
  162. ncnn::Mat a = RandomMat(16, 17, 13);
  163. ncnn::Mat b = RandomMat(18, 19, 12);
  164. ncnn::Mat c = RandomMat(13, 14, 24);
  165. return 0
  166. || test_interp(a, 3, 2.f, 2.f, 0, 0)
  167. || test_interp(a, 3, 4.f, 0.5f, 0, 0)
  168. || test_interp(a, 3, 1.2f, 1.2f, 0, 0)
  169. || test_interp(a, 3, 0.8f, 0.8f, 0, 0)
  170. || test_interp(a, 3, 1.f, 1.f, 10, 12)
  171. || test_interp(a, 3, 1.f, 1.f, 2, 2)
  172. || test_interp(a, 3, 1.f, 1.f, 6, 7)
  173. || test_interp(a, 3, 1.f, 1.f, 16, 17)
  174. || test_interp_ref(a, 3, 2, 2)
  175. || test_interp_ref(a, 3, 6, 7)
  176. || test_interp_ref(a, 3, 16, 17)
  177. || test_interp(b, 3, 2.f, 2.f, 0, 0)
  178. || test_interp(b, 3, 4.f, 0.5f, 0, 0)
  179. || test_interp(b, 3, 1.2f, 1.2f, 0, 0)
  180. || test_interp(b, 3, 0.8f, 0.8f, 0, 0)
  181. || test_interp(b, 3, 1.f, 1.f, 10, 12)
  182. || test_interp(b, 3, 1.f, 1.f, 2, 2)
  183. || test_interp(b, 3, 1.f, 1.f, 6, 7)
  184. || test_interp(b, 3, 1.f, 1.f, 18, 19)
  185. || test_interp_ref(b, 3, 2, 2)
  186. || test_interp_ref(b, 3, 6, 7)
  187. || test_interp_ref(b, 3, 18, 19)
  188. || test_interp(c, 3, 2.f, 2.f, 0, 0)
  189. || test_interp(c, 3, 4.f, 0.5f, 0, 0)
  190. || test_interp(c, 3, 1.2f, 1.2f, 0, 0)
  191. || test_interp(c, 3, 0.8f, 0.8f, 0, 0)
  192. || test_interp(c, 3, 1.f, 1.f, 10, 12)
  193. || test_interp(c, 3, 1.f, 1.f, 2, 2)
  194. || test_interp(c, 3, 1.f, 1.f, 6, 7)
  195. || test_interp(c, 3, 1.f, 1.f, 18, 19)
  196. || test_interp_ref(c, 3, 2, 2)
  197. || test_interp_ref(c, 3, 6, 7)
  198. || test_interp_ref(c, 3, 18, 19);
  199. }
  200. static int test_interp_3()
  201. {
  202. ncnn::Mat a = RandomMat(17);
  203. ncnn::Mat b = RandomMat(20);
  204. ncnn::Mat c = RandomMat(48);
  205. return 0
  206. || test_interp(a, 1, 2.f, 3.f, 0, 0)
  207. || test_interp(a, 1, 1.f, 1.f, 10, 12)
  208. || test_interp(a, 1, 1.f, 1.f, 15, 16)
  209. || test_interp_ref(a, 1, 10, 12)
  210. || test_interp_ref(a, 1, 4, 4)
  211. || test_interp_ref(a, 1, 15, 16)
  212. || test_interp(b, 1, 4.f, 5.f, 0, 0)
  213. || test_interp(b, 1, 1.f, 1.f, 10, 12)
  214. || test_interp(b, 1, 1.f, 1.f, 14, 17)
  215. || test_interp_ref(b, 1, 5, 5)
  216. || test_interp_ref(b, 1, 14, 17)
  217. || test_interp(c, 1, 6.f, 7.f, 0, 0)
  218. || test_interp(c, 1, 1.f, 1.f, 10, 12)
  219. || test_interp(c, 1, 1.f, 1.f, 14, 17)
  220. || test_interp_ref(c, 1, 6, 6)
  221. || test_interp_ref(c, 1, 14, 17);
  222. }
  223. int main()
  224. {
  225. SRAND(7767517);
  226. return 0
  227. || test_interp_0()
  228. || test_interp_1()
  229. || test_interp_2()
  230. || test_interp_3();
  231. }