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_deconvolution1d.cpp 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2022 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_deconvolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_w)
  16. {
  17. ncnn::Mat a = RandomMat(w, h);
  18. if (output_w > 0 && pad != -233 && pad != -234)
  19. {
  20. pad = -233;
  21. }
  22. ncnn::ParamDict pd;
  23. pd.set(0, outh);
  24. pd.set(1, kernel);
  25. pd.set(2, dilation);
  26. pd.set(3, stride);
  27. pd.set(4, pad);
  28. pd.set(5, bias);
  29. pd.set(6, outh * h * kernel);
  30. int activation_type = RAND() % 5; // 0 1 2 3 4
  31. ncnn::Mat activation_params(2);
  32. activation_params[0] = RandomFloat(-1, 0); // alpha
  33. activation_params[1] = RandomFloat(0, 1); // beta
  34. pd.set(9, activation_type);
  35. pd.set(10, activation_params);
  36. pd.set(18, output_pad_right);
  37. pd.set(20, output_w);
  38. std::vector<ncnn::Mat> weights(2);
  39. weights[0] = RandomMat(outh * h * kernel);
  40. weights[1] = RandomMat(outh);
  41. int ret = test_layer("Deconvolution1D", pd, weights, a);
  42. if (ret != 0)
  43. {
  44. fprintf(stderr, "test_deconvolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
  45. }
  46. return ret;
  47. }
  48. static int test_deconvolution1d_0()
  49. {
  50. static const int kdsp[16][4] = {
  51. {1, 1, 1, 0},
  52. {1, 1, 2, 0},
  53. {2, 1, 1, 1},
  54. {2, 1, 2, -233},
  55. {3, 1, 1, 1},
  56. {3, 1, 2, 1},
  57. {3, 2, 1, 1},
  58. {4, 1, 1, -233},
  59. {4, 1, 2, -234},
  60. {4, 2, 1, -234},
  61. {5, 1, 1, 2},
  62. {5, 1, 2, 2},
  63. {5, 2, 2, 2},
  64. {7, 1, 1, 3},
  65. {7, 1, 2, 3},
  66. {7, 2, 1, -233},
  67. };
  68. for (int i = 0; i < 16; i++)
  69. {
  70. const int k = kdsp[i][0];
  71. const int d = kdsp[i][1];
  72. const int s = kdsp[i][2];
  73. const int p = kdsp[i][3];
  74. int ret = 0
  75. || test_deconvolution1d(9, 1, 1, k, d, s, p, 1, 0, 0)
  76. || test_deconvolution1d(9, 4, 13, k, d, s, p, 0, 1, 7)
  77. || test_deconvolution1d(9, 13, 4, k, d, s, p, 1, 1, 0)
  78. || test_deconvolution1d(9, 4, 8, k, d, s, p, 0, 0, 0)
  79. || test_deconvolution1d(9, 8, 4, k, d, s, p, 1, 0, 7)
  80. || test_deconvolution1d(9, 8, 13, k, d, s, p, 0, 2, 0)
  81. || test_deconvolution1d(9, 13, 8, k, d, s, p, 1, 2, 0)
  82. || test_deconvolution1d(9, 16, 16, k, d, s, p, 0, 0, 7);
  83. if (ret != 0)
  84. return -1;
  85. }
  86. return 0;
  87. }
  88. static int test_deconvolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_w)
  89. {
  90. ncnn::Mat a = RandomMat(w, h);
  91. if (output_w > 0 && pad != -233 && pad != -234)
  92. {
  93. pad = -233;
  94. }
  95. ncnn::ParamDict pd;
  96. pd.set(0, 0);
  97. pd.set(1, 0);
  98. pd.set(2, dilation);
  99. pd.set(3, stride);
  100. pd.set(4, pad);
  101. pd.set(5, bias);
  102. pd.set(6, 0);
  103. pd.set(28, 1); // dynamic weight
  104. int activation_type = RAND() % 5; // 0 1 2 3 4
  105. ncnn::Mat activation_params(2);
  106. activation_params[0] = RandomFloat(-1, 0); // alpha
  107. activation_params[1] = RandomFloat(0, 1); // beta
  108. pd.set(9, activation_type);
  109. pd.set(10, activation_params);
  110. pd.set(18, output_pad_right);
  111. pd.set(20, output_w);
  112. std::vector<ncnn::Mat> as(bias ? 3 : 2);
  113. as[0] = a;
  114. as[1] = RandomMat(kernel, outh, h);
  115. if (bias)
  116. as[2] = RandomMat(outh);
  117. std::vector<ncnn::Mat> weights(0);
  118. int ret = test_layer("Deconvolution1D", pd, weights, as);
  119. if (ret != 0)
  120. {
  121. fprintf(stderr, "test_deconvolution1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
  122. }
  123. return ret;
  124. }
  125. static int test_deconvolution1d_1()
  126. {
  127. static const int kdsp[16][4] = {
  128. {1, 1, 1, 0},
  129. {1, 1, 2, 0},
  130. {2, 1, 1, 1},
  131. {2, 1, 2, -233},
  132. {3, 1, 1, 1},
  133. {3, 1, 2, 1},
  134. {3, 2, 1, 1},
  135. {4, 1, 1, -233},
  136. {4, 1, 2, -234},
  137. {4, 2, 1, -234},
  138. {5, 1, 1, 2},
  139. {5, 1, 2, 2},
  140. {5, 2, 2, 2},
  141. {7, 1, 1, 3},
  142. {7, 1, 2, 3},
  143. {7, 2, 1, -233},
  144. };
  145. for (int i = 0; i < 16; i++)
  146. {
  147. const int k = kdsp[i][0];
  148. const int d = kdsp[i][1];
  149. const int s = kdsp[i][2];
  150. const int p = kdsp[i][3];
  151. int ret = 0
  152. || test_deconvolution1d_dynamic(9, 1, 1, k, d, s, p, 1, 0, 0)
  153. || test_deconvolution1d_dynamic(9, 4, 13, k, d, s, p, 0, 1, 7)
  154. || test_deconvolution1d_dynamic(9, 13, 4, k, d, s, p, 1, 1, 0)
  155. || test_deconvolution1d_dynamic(9, 4, 8, k, d, s, p, 0, 0, 0)
  156. || test_deconvolution1d_dynamic(9, 8, 4, k, d, s, p, 1, 0, 7)
  157. || test_deconvolution1d_dynamic(9, 8, 13, k, d, s, p, 0, 2, 0)
  158. || test_deconvolution1d_dynamic(9, 13, 8, k, d, s, p, 1, 2, 0)
  159. || test_deconvolution1d_dynamic(9, 16, 16, k, d, s, p, 0, 0, 7);
  160. if (ret != 0)
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. int main()
  166. {
  167. SRAND(7767517);
  168. return test_deconvolution1d_0() || test_deconvolution1d_1();
  169. }