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_scale.cpp 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/scale.h"
  15. #include "testutil.h"
  16. static int test_scale(const ncnn::Mat& a, int scale_data_size, int bias)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, scale_data_size);
  20. pd.set(1, bias);
  21. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  22. weights[0] = RandomMat(scale_data_size);
  23. if (bias)
  24. weights[1] = RandomMat(scale_data_size);
  25. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, a);
  26. if (ret != 0)
  27. {
  28. fprintf(stderr, "test_scale failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias);
  29. }
  30. return ret;
  31. }
  32. static int test_scale_attention(const ncnn::Mat& a, int scale_data_size)
  33. {
  34. ncnn::ParamDict pd;
  35. pd.set(0, -233);
  36. std::vector<ncnn::Mat> weights(0);
  37. std::vector<ncnn::Mat> ab(2);
  38. ab[0] = a;
  39. ab[1] = RandomMat(scale_data_size);
  40. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, ab, 2);
  41. if (ret != 0)
  42. {
  43. fprintf(stderr, "test_scale_attention failed a.dims=%d a=(%d %d %d) scale_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size);
  44. }
  45. return ret;
  46. }
  47. static int test_scale_0()
  48. {
  49. return 0
  50. || test_scale(RandomMat(5, 7, 16), 16, 0)
  51. || test_scale(RandomMat(5, 7, 16), 16, 1)
  52. || test_scale(RandomMat(3, 5, 13), 13, 0)
  53. || test_scale(RandomMat(3, 5, 13), 13, 1);
  54. }
  55. static int test_scale_1()
  56. {
  57. return 0
  58. || test_scale(RandomMat(6, 16), 16, 0)
  59. || test_scale(RandomMat(6, 16), 16, 1)
  60. || test_scale(RandomMat(7, 15), 15, 0)
  61. || test_scale(RandomMat(7, 15), 15, 1);
  62. }
  63. static int test_scale_2()
  64. {
  65. return 0
  66. || test_scale(RandomMat(128), 128, 0)
  67. || test_scale(RandomMat(128), 128, 1)
  68. || test_scale(RandomMat(127), 127, 0)
  69. || test_scale(RandomMat(127), 127, 1);
  70. }
  71. static int test_scale_3()
  72. {
  73. return 0
  74. || test_scale_attention(RandomMat(5, 7, 16), 16)
  75. || test_scale_attention(RandomMat(3, 5, 13), 13);
  76. }
  77. static int test_scale_4()
  78. {
  79. return 0
  80. || test_scale_attention(RandomMat(6, 16), 16)
  81. || test_scale_attention(RandomMat(7, 15), 15);
  82. }
  83. static int test_scale_5()
  84. {
  85. return 0
  86. || test_scale_attention(RandomMat(128), 128)
  87. || test_scale_attention(RandomMat(127), 127);
  88. }
  89. int main()
  90. {
  91. SRAND(7767517);
  92. return 0
  93. || test_scale_0()
  94. || test_scale_1()
  95. || test_scale_2()
  96. || test_scale_3()
  97. || test_scale_4()
  98. || test_scale_5();
  99. }