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_crop_oom.cpp 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2025 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 "testutil.h"
  15. static int test_crop_oom(const ncnn::Mat& a, int woffset, int hoffset, int doffset, int coffset, int outw, int outh, int outd, int outc, int woffset2, int hoffset2, int doffset2, int coffset2)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, woffset);
  19. pd.set(1, hoffset);
  20. pd.set(13, doffset);
  21. pd.set(2, coffset);
  22. pd.set(3, outw);
  23. pd.set(4, outh);
  24. pd.set(14, outd);
  25. pd.set(5, outc);
  26. pd.set(6, woffset2);
  27. pd.set(7, hoffset2);
  28. pd.set(15, doffset2);
  29. pd.set(8, coffset2);
  30. std::vector<ncnn::Mat> weights(0);
  31. int ret = test_layer_oom("Crop", pd, weights, a);
  32. if (ret != 0)
  33. {
  34. fprintf(stderr, "test_crop_oom failed a.dims=%d a=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d outw=%d outh=%d outd=%d outc=%d woffset2=%d hoffset2=%d doffset2=%d coffset2=%d\n", a.dims, a.w, a.h, a.d, a.c, woffset, hoffset, doffset, coffset, outw, outh, outd, outc, woffset2, hoffset2, doffset2, coffset2);
  35. }
  36. return ret;
  37. }
  38. static int test_crop_oom(const ncnn::Mat& a, const char* starts_expr, const char* ends_expr, const char* axes_expr)
  39. {
  40. ncnn::ParamDict pd;
  41. pd.set(19, std::string(starts_expr));
  42. pd.set(20, std::string(ends_expr));
  43. pd.set(21, std::string(axes_expr));
  44. std::vector<ncnn::Mat> weights(0);
  45. int ret = test_layer_oom("Crop", pd, weights, a);
  46. if (ret != 0)
  47. {
  48. fprintf(stderr, "test_crop_oom failed a.dims=%d a=(%d %d %d %d) starts_expr=%s ends_expr=%s axes_expr=%s\n", a.dims, a.w, a.h, a.d, a.c, starts_expr, ends_expr, axes_expr);
  49. }
  50. return ret;
  51. }
  52. static int test_crop_oom(const std::vector<ncnn::Mat>& as, const char* starts_expr, const char* ends_expr, const char* axes_expr)
  53. {
  54. ncnn::ParamDict pd;
  55. pd.set(19, std::string(starts_expr));
  56. pd.set(20, std::string(ends_expr));
  57. pd.set(21, std::string(axes_expr));
  58. std::vector<ncnn::Mat> weights(0);
  59. int ret = test_layer_oom("Crop", pd, weights, as, 1);
  60. if (ret != 0)
  61. {
  62. fprintf(stderr, "test_crop_oom failed a.dims=%d a=(%d %d %d %d) starts_expr=%s ends_expr=%s axes_expr=%s\n", as[0].dims, as[0].w, as[0].h, as[0].d, as[0].c, starts_expr, ends_expr, axes_expr);
  63. }
  64. return ret;
  65. }
  66. static int test_crop_0()
  67. {
  68. ncnn::Mat a = RandomMat(13, 12, 25, 48);
  69. ncnn::Mat b = RandomMat(13, 12, 48);
  70. ncnn::Mat c = RandomMat(13, 48);
  71. ncnn::Mat d = RandomMat(128);
  72. return 0
  73. || test_crop_oom(a, 1, 1, 1, 1, -233, -233, -233, -233, 1, 1, 1, 1)
  74. || test_crop_oom(b, 1, 1, 0, 1, -233, -233, 0, -233, 1, 1, 0, 1)
  75. || test_crop_oom(c, 1, 1, 0, 0, -233, -233, 0, 0, 1, 1, 0, 0)
  76. || test_crop_oom(d, 1, 0, 0, 0, -233, 0, 0, 0, 1, 0, 0, 0)
  77. || test_crop_oom(a, 2, 2, 2, 2, 6, 6, 6, 16, 0, 0, 0, 0)
  78. || test_crop_oom(b, 2, 2, 0, 2, 6, 6, 0, 16, 0, 0, 0, 0)
  79. || test_crop_oom(c, 2, 2, 0, 0, 6, 16, 0, 0, 0, 0, 0, 0)
  80. || test_crop_oom(d, 2, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0)
  81. || test_crop_oom(a, 3, 3, 3, 16, 3, 4, 5, 16, 0, 0, 0, 0)
  82. || test_crop_oom(b, 3, 3, 0, 16, 3, 4, 0, 16, 0, 0, 0, 0)
  83. || test_crop_oom(c, 3, 16, 0, 0, 3, 16, 0, 0, 0, 0, 0, 0)
  84. || test_crop_oom(d, 16, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0);
  85. }
  86. static int test_crop_1()
  87. {
  88. ncnn::Mat a = RandomMat(13, 12, 25, 47);
  89. ncnn::Mat b = RandomMat(13, 12, 47);
  90. ncnn::Mat c = RandomMat(13, 47);
  91. ncnn::Mat d = RandomMat(129);
  92. return 0
  93. || test_crop_oom(a, 1, 1, 1, 1, -233, -233, -233, -233, 1, 1, 1, 1)
  94. || test_crop_oom(b, 1, 1, 0, 1, -233, -233, 0, -233, 1, 1, 0, 1)
  95. || test_crop_oom(c, 1, 1, 0, 0, -233, -233, 0, 0, 1, 1, 0, 0)
  96. || test_crop_oom(d, 1, 0, 0, 0, -233, 0, 0, 0, 1, 0, 0, 0)
  97. || test_crop_oom(a, 2, 2, 2, 2, 6, 6, 6, 16, 0, 0, 0, 0)
  98. || test_crop_oom(b, 2, 2, 0, 2, 6, 6, 0, 16, 0, 0, 0, 0)
  99. || test_crop_oom(c, 2, 2, 0, 0, 6, 16, 0, 0, 0, 0, 0, 0)
  100. || test_crop_oom(d, 2, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0)
  101. || test_crop_oom(a, 3, 3, 3, 16, 6, 6, 6, 16, 0, 0, 0, 0)
  102. || test_crop_oom(b, 3, 3, 0, 16, 6, 6, 0, 16, 0, 0, 0, 0)
  103. || test_crop_oom(c, 3, 16, 0, 0, 6, 16, 0, 0, 0, 0, 0, 0)
  104. || test_crop_oom(d, 16, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0);
  105. }
  106. static int test_crop_2()
  107. {
  108. ncnn::Mat a = RandomMat(13, 12, 25, 48);
  109. ncnn::Mat b = RandomMat(13, 12, 48);
  110. ncnn::Mat c = RandomMat(13, 48);
  111. ncnn::Mat d = RandomMat(128);
  112. return 0
  113. || test_crop_oom(a, "2", "-2", "0")
  114. || test_crop_oom(b, "2", "-2", "0")
  115. || test_crop_oom(c, "2", "-2", "0")
  116. || test_crop_oom(d, "2", "-2", "0")
  117. || test_crop_oom(a, "16", "32", "-4")
  118. || test_crop_oom(b, "16", "32", "-3")
  119. || test_crop_oom(c, "16", "32", "-2")
  120. || test_crop_oom(d, "16", "32", "-1")
  121. || test_crop_oom(a, "16,//(0d,4),2,1", "32,-1,-2,-3", "0,1,2,3")
  122. || test_crop_oom(b, "16,//(0h,4),2", "32,-1,-(0w,2)", "0,1,2")
  123. || test_crop_oom(c, "16,//(0w,4)", "32,-2", "0,1")
  124. || test_crop_oom(a, "10", "11", "1")
  125. || test_crop_oom(b, "1,1", "-(0c,15),-(0w,5)", "0,2")
  126. || test_crop_oom(a, "-(0w,3),0h//2,floor(*(0c,0.3))", "-1,0h,ceil(*(0c,0.9))", "3,2,0")
  127. || test_crop_oom(b, "-(0w,3),0h//2,floor(*(0c,0.3))", "-1,0h,ceil(*(0c,0.9))", "2,1,0")
  128. || test_crop_oom(c, "-(0w,3),floor(*(0h,0.3))", "-1,ceil(*(0h,0.9))", "1,0")
  129. || test_crop_oom(d, "floor(*(0w,0.3))", "ceil(*(0w,0.9))", "0");
  130. }
  131. static int test_crop_3()
  132. {
  133. std::vector<ncnn::Mat> as(2);
  134. as[0] = RandomMat(14, 15, 13, 48);
  135. as[1] = RandomMat(8, 5, 3, 4);
  136. std::vector<ncnn::Mat> bs(2);
  137. bs[0] = RandomMat(14, 15, 48);
  138. bs[1] = RandomMat(28, 45, 16);
  139. std::vector<ncnn::Mat> cs(2);
  140. cs[0] = RandomMat(24, 48);
  141. cs[1] = RandomMat(28, 6);
  142. std::vector<ncnn::Mat> ds(3);
  143. ds[0] = RandomMat(128);
  144. ds[1] = RandomMat(16);
  145. ds[2] = RandomMat(64);
  146. return 0
  147. || test_crop_oom(as, "*(1c,4)", "*(1c,8)", "-4")
  148. || test_crop_oom(bs, "1c", "-(0c,1c)", "-3")
  149. || test_crop_oom(cs, "+(1h,10)", "-(1h,22)", "-2")
  150. || test_crop_oom(ds, "1w", "2w", "-1")
  151. || test_crop_oom(as, "16,//(min(0w,1d),4),2,3", "32,-1,-2,-3", "0,1,2,3")
  152. || test_crop_oom(bs, "16,//(min(0w,1h),4),2", "32,-1,-(0w,2)", "0,1,2")
  153. || test_crop_oom(cs, "16,//(min(0w,1w),4)", "32,-2", "0,1")
  154. || test_crop_oom(bs, "1,//(1w,7)", "+(1c,1),-(0w,2)", "0,2")
  155. || test_crop_oom(as, "-(1w,4)", "neg(1h,3)", "0")
  156. || test_crop_oom(bs, "-(1w,20)", "-2", "0")
  157. || test_crop_oom(bs, "//(1h,15)", "neg(//(1w,7))", "2")
  158. || test_crop_oom(bs, "//(100,0h),round(fmod(100,0c))", "-233,min(1c,0c)", "1,0");
  159. }
  160. int main()
  161. {
  162. SRAND(776757);
  163. return 0
  164. || test_crop_0()
  165. || test_crop_1()
  166. || test_crop_2()
  167. || test_crop_3();
  168. }