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_deformableconv2d_3.cpp 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_deformableconv2d(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
  5. {
  6. const int kernel_extent_w = dilation * (kernel - 1) + 1;
  7. const int kernel_extent_h = dilation * (kernel - 1) + 1;
  8. const int out_w = (w + pad + pad - kernel_extent_w) / stride + 1;
  9. const int out_h = (h + pad + pad - kernel_extent_h) / stride + 1;
  10. std::vector<ncnn::Mat> a(3);
  11. a[0] = RandomMat(w, h, c);
  12. a[1] = RandomMat(out_w, out_h, kernel * kernel * 2);
  13. a[2] = RandomMat(out_w, out_h, kernel * kernel);
  14. ncnn::ParamDict pd;
  15. pd.set(0, outch);
  16. pd.set(1, kernel);
  17. pd.set(2, dilation);
  18. pd.set(3, stride);
  19. pd.set(4, pad);
  20. pd.set(5, bias);
  21. pd.set(6, outch * c * kernel * kernel);
  22. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  23. ncnn::Mat activation_params(2);
  24. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  25. activation_params[1] = RandomFloat(0, 1); // beta
  26. pd.set(9, activation_type);
  27. pd.set(10, activation_params);
  28. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  29. weights[0] = RandomMat(outch * c * kernel * kernel);
  30. if (bias)
  31. weights[1] = RandomMat(outch);
  32. float epsilon = 0.001;
  33. int ret = test_layer("DeformableConv2D", pd, weights, a, 1, epsilon);
  34. if (ret != 0)
  35. {
  36. fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  37. }
  38. {
  39. ncnn::Option opt;
  40. opt.num_threads = 1;
  41. opt.use_packing_layout = true;
  42. opt.use_fp16_packed = false;
  43. opt.use_fp16_storage = false;
  44. opt.use_fp16_arithmetic = false;
  45. opt.use_bf16_storage = false;
  46. opt.use_shader_pack8 = false;
  47. opt.use_sgemm_convolution = false;
  48. opt.use_winograd_convolution = false;
  49. ret = test_layer_opt("DeformableConv2D", pd, weights, opt, a, 1, epsilon);
  50. if (ret != 0)
  51. {
  52. fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  53. }
  54. }
  55. {
  56. ncnn::Option opt;
  57. opt.num_threads = 1;
  58. opt.use_packing_layout = true;
  59. opt.use_fp16_packed = true;
  60. opt.use_fp16_storage = true;
  61. opt.use_fp16_arithmetic = true;
  62. opt.use_bf16_storage = true;
  63. opt.use_shader_pack8 = true;
  64. opt.use_sgemm_convolution = false;
  65. opt.use_winograd_convolution = false;
  66. ret = test_layer_opt("DeformableConv2D", pd, weights, opt, a, 1, epsilon);
  67. if (ret != 0)
  68. {
  69. fprintf(stderr, "test_deformableconv2d failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  70. }
  71. }
  72. return ret;
  73. }
  74. static int test_deformableconv2d_0()
  75. {
  76. static const int kdsp[10][4] = {
  77. {1, 1, 1, 0},
  78. {1, 1, 2, 0},
  79. {2, 1, 1, 1},
  80. {2, 1, 2, 0},
  81. {3, 1, 1, 1},
  82. {3, 1, 2, 1},
  83. {3, 2, 1, 1},
  84. {4, 1, 2, 1},
  85. {5, 1, 2, 2},
  86. {5, 2, 2, 2},
  87. };
  88. for (int i = 8; i < 10; i++)
  89. {
  90. const int k = kdsp[i][0];
  91. const int d = kdsp[i][1];
  92. const int s = kdsp[i][2];
  93. const int p = kdsp[i][3];
  94. int ret = 0
  95. || test_deformableconv2d(9, 7, 1, 1, k, d, s, p, 1)
  96. || test_deformableconv2d(9, 7, 4, 13, k, d, s, p, 0)
  97. || test_deformableconv2d(9, 7, 13, 4, k, d, s, p, 1)
  98. || test_deformableconv2d(9, 7, 4, 8, k, d, s, p, 0)
  99. || test_deformableconv2d(9, 7, 8, 4, k, d, s, p, 1)
  100. || test_deformableconv2d(9, 7, 8, 13, k, d, s, p, 0)
  101. || test_deformableconv2d(9, 7, 13, 8, k, d, s, p, 1)
  102. || test_deformableconv2d(9, 7, 16, 16, k, d, s, p, 0)
  103. || test_deformableconv2d(16, 16, 1 * 3, 1 * 3, k, d, s, p, 1)
  104. || test_deformableconv2d(16, 16, 1 * 3, 4 * 3, k, d, s, p, 1)
  105. || test_deformableconv2d(16, 16, 1 * 3, 8 * 3, k, d, s, p, 1)
  106. || test_deformableconv2d(16, 16, 1 * 3, 16 * 3, k, d, s, p, 1)
  107. || test_deformableconv2d(16, 16, 4 * 3, 1 * 3, k, d, s, p, 1)
  108. || test_deformableconv2d(16, 16, 4 * 3, 4 * 3, k, d, s, p, 1)
  109. || test_deformableconv2d(16, 16, 4 * 3, 8 * 3, k, d, s, p, 1)
  110. || test_deformableconv2d(16, 16, 4 * 3, 16 * 3, k, d, s, p, 1)
  111. || test_deformableconv2d(16, 16, 8 * 3, 1 * 3, k, d, s, p, 1)
  112. || test_deformableconv2d(16, 16, 8 * 3, 4 * 3, k, d, s, p, 1)
  113. || test_deformableconv2d(16, 16, 8 * 3, 8 * 3, k, d, s, p, 1)
  114. || test_deformableconv2d(16, 16, 8 * 3, 16 * 3, k, d, s, p, 1)
  115. || test_deformableconv2d(16, 16, 16 * 3, 1 * 3, k, d, s, p, 1)
  116. || test_deformableconv2d(16, 16, 16 * 3, 4 * 3, k, d, s, p, 1)
  117. || test_deformableconv2d(16, 16, 16 * 3, 8 * 3, k, d, s, p, 1)
  118. || test_deformableconv2d(16, 16, 16 * 3, 16 * 3, k, d, s, p, 1);
  119. if (ret != 0)
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. int main()
  125. {
  126. SRAND(7767517);
  127. return test_deformableconv2d_0();
  128. }