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 5.6 kB

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