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_3.cpp 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(const ncnn::Mat& a, const char* starts_expr, const char* ends_expr, const char* axes_expr)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(19, std::string(starts_expr));
  19. pd.set(20, std::string(ends_expr));
  20. pd.set(21, std::string(axes_expr));
  21. std::vector<ncnn::Mat> weights(0);
  22. int ret = test_layer("Crop", pd, weights, a);
  23. if (ret != 0)
  24. {
  25. fprintf(stderr, "test_crop 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);
  26. }
  27. return ret;
  28. }
  29. static int test_crop(const std::vector<ncnn::Mat>& as, const char* starts_expr, const char* ends_expr, const char* axes_expr)
  30. {
  31. ncnn::ParamDict pd;
  32. pd.set(19, std::string(starts_expr));
  33. pd.set(20, std::string(ends_expr));
  34. pd.set(21, std::string(axes_expr));
  35. std::vector<ncnn::Mat> weights(0);
  36. int ret = test_layer("Crop", pd, weights, as, 1);
  37. if (ret != 0)
  38. {
  39. fprintf(stderr, "test_crop 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);
  40. }
  41. return ret;
  42. }
  43. static int test_crop_0()
  44. {
  45. ncnn::Mat a = RandomMat(13, 12, 25, 48);
  46. ncnn::Mat b = RandomMat(13, 12, 48);
  47. ncnn::Mat c = RandomMat(13, 48);
  48. ncnn::Mat d = RandomMat(128);
  49. return 0
  50. || test_crop(a, "2", "-2", "0")
  51. || test_crop(b, "2", "-2", "0")
  52. || test_crop(c, "2", "-2", "0")
  53. || test_crop(d, "2", "-2", "0")
  54. || test_crop(a, "16", "32", "-4")
  55. || test_crop(b, "16", "32", "-3")
  56. || test_crop(c, "16", "32", "-2")
  57. || test_crop(d, "16", "32", "-1")
  58. || test_crop(a, "16,//(0d,4),2,3", "32,-1,-2,-3", "0,1,2,3")
  59. || test_crop(b, "16,//(0h,4),2", "32,-1,-(0w,2)", "0,1,2")
  60. || test_crop(c, "16,//(0w,4)", "32,-2", "0,1")
  61. || test_crop(a, "10", "11", "1")
  62. || test_crop(b, "1,1", "-(0c,15),-(0w,5)", "0,2")
  63. || test_crop(a, "-(0w,3),0h//2,floor(*(0c,0.3))", "-1,0h,ceil(*(0c,0.9))", "3,2,0")
  64. || test_crop(b, "-(0w,3),0h//2,floor(*(0c,0.3))", "-1,0h,ceil(*(0c,0.9))", "2,1,0")
  65. || test_crop(c, "-(0w,3),floor(*(0h,0.3))", "-1,ceil(*(0h,0.9))", "1,0")
  66. || test_crop(d, "floor(*(0w,0.3))", "ceil(*(0w,0.9))", "0");
  67. }
  68. static int test_crop_1()
  69. {
  70. std::vector<ncnn::Mat> as(2);
  71. as[0] = RandomMat(14, 15, 13, 48);
  72. as[1] = RandomMat(8, 5, 3, 4);
  73. std::vector<ncnn::Mat> bs(2);
  74. bs[0] = RandomMat(14, 15, 48);
  75. bs[1] = RandomMat(28, 45, 16);
  76. std::vector<ncnn::Mat> cs(2);
  77. cs[0] = RandomMat(24, 48);
  78. cs[1] = RandomMat(28, 6);
  79. std::vector<ncnn::Mat> ds(3);
  80. ds[0] = RandomMat(128);
  81. ds[1] = RandomMat(16);
  82. ds[2] = RandomMat(64);
  83. return 0
  84. || test_crop(as, "*(1c,4)", "*(1c,8)", "-4")
  85. || test_crop(bs, "1c", "-(0c,1c)", "-3")
  86. || test_crop(cs, "+(1h,10)", "-(1h,22)", "-2")
  87. || test_crop(ds, "1w", "2w", "-1")
  88. || test_crop(as, "16,//(min(0w,1d),4),2,3", "32,-1,-2,-3", "0,1,2,3")
  89. || test_crop(bs, "16,//(min(0w,1h),4),2", "32,-1,-(0w,2)", "0,1,2")
  90. || test_crop(cs, "16,//(min(0w,1w),4)", "32,-2", "0,1")
  91. || test_crop(bs, "1,//(1w,7)", "+(1c,1),-(0w,2)", "0,2")
  92. || test_crop(as, "-(1w,4)", "neg(1h,3)", "0")
  93. || test_crop(bs, "-(1w,20)", "-2", "0")
  94. || test_crop(bs, "//(1h,15)", "neg(//(1w,7))", "2")
  95. || test_crop(bs, "//(100,0h),round(fmod(100,0c))", "-233,min(1c,0c)", "1,0");
  96. }
  97. int main()
  98. {
  99. SRAND(776757);
  100. return 0
  101. || test_crop_0()
  102. || test_crop_1();
  103. }