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.1 kB

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