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_yolov3detectionoutput.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_yolov3detectionoutput(const std::vector<ncnn::Mat>& a, int num_class,
  5. int num_box, float confidence_threshold, float nms_threshold,
  6. ncnn::Mat& biases, ncnn::Mat& mask, ncnn::Mat& anchors_scale)
  7. {
  8. ncnn::ParamDict pd;
  9. pd.set(0, num_class);
  10. pd.set(1, num_box);
  11. pd.set(2, confidence_threshold);
  12. pd.set(3, nms_threshold);
  13. pd.set(4, biases);
  14. pd.set(5, mask);
  15. pd.set(6, anchors_scale);
  16. std::vector<ncnn::Mat> weights(0);
  17. int ret = test_layer("Yolov3DetectionOutput", pd, weights, a);
  18. if (ret != 0)
  19. {
  20. fprintf(stderr, "test_yolov3detectionoutput failed a.dims=%d a=(%d %d %d) ", a[0].dims, a[0].w, a[0].h, a[0].c);
  21. fprintf(stderr, " num_class=%d num_box=%d", num_class, num_box);
  22. fprintf(stderr, " confidence_threshold=%f nms_threshold=%f\n", confidence_threshold, nms_threshold);
  23. }
  24. return ret;
  25. }
  26. static ncnn::Mat create_mat_from(const float* src, int length)
  27. {
  28. ncnn::Mat ret(length);
  29. memcpy(ret.data, src, length * sizeof(float));
  30. return ret;
  31. }
  32. static ncnn::Mat MyRandomMat(int w, int h, int c)
  33. {
  34. ncnn::Mat m(w, h, c);
  35. Randomize(m, -15.f, 1.5f);
  36. return m;
  37. }
  38. static int test_yolov3detectionoutput_v4()
  39. {
  40. const float b[] = {12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401};
  41. const float m[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  42. const float s[] = {9.6, 17.6, 33.6};
  43. ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
  44. ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
  45. ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
  46. std::vector<ncnn::Mat> a(3);
  47. a[0] = MyRandomMat(76, 76, 255);
  48. a[1] = MyRandomMat(38, 38, 255);
  49. a[2] = MyRandomMat(19, 19, 255);
  50. return 0
  51. || test_yolov3detectionoutput(a, 80, 3, 0.55f, 0.45f, biases, mask, anchors_scale);
  52. }
  53. static int test_yolov3detectionoutput_v4tiny()
  54. {
  55. const float b[] = {10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319};
  56. const float m[] = {3, 4, 5, 1, 2, 3};
  57. const float s[] = {33.6, 16.8};
  58. ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
  59. ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
  60. ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
  61. std::vector<ncnn::Mat> a(2);
  62. a[0] = MyRandomMat(13, 13, 255);
  63. a[1] = MyRandomMat(26, 26, 255);
  64. return 0
  65. || test_yolov3detectionoutput(a, 80, 3, 0.4f, 0.45f, biases, mask, anchors_scale);
  66. }
  67. static int test_yolov3detectionoutput_v3()
  68. {
  69. const float b[] = {10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326};
  70. const float m[] = {6, 7, 8, 3, 4, 5, 0, 1, 2};
  71. const float s[] = {32, 16, 8};
  72. ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
  73. ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
  74. ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
  75. std::vector<ncnn::Mat> a(3);
  76. a[0] = MyRandomMat(19, 19, 255);
  77. a[1] = MyRandomMat(38, 38, 255);
  78. a[2] = MyRandomMat(76, 76, 255);
  79. return 0
  80. || test_yolov3detectionoutput(a, 80, 3, 0.6f, 0.45f, biases, mask, anchors_scale);
  81. }
  82. static int test_yolov3detectionoutput_v3tiny()
  83. {
  84. const float b[] = {10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319};
  85. const float m[] = {3, 4, 5, 1, 2, 3};
  86. const float s[] = {32, 16};
  87. ncnn::Mat biases = create_mat_from(b, sizeof(b) / sizeof(b[0]));
  88. ncnn::Mat mask = create_mat_from(m, sizeof(m) / sizeof(m[0]));
  89. ncnn::Mat anchors_scale = create_mat_from(s, sizeof(s) / sizeof(s[0]));
  90. std::vector<ncnn::Mat> a(2);
  91. a[0] = MyRandomMat(13, 13, 255);
  92. a[1] = MyRandomMat(26, 26, 255);
  93. return 0
  94. || test_yolov3detectionoutput(a, 80, 3, 0.3f, 0.45f, biases, mask, anchors_scale);
  95. }
  96. int main()
  97. {
  98. SRAND(7767517);
  99. return 0
  100. || test_yolov3detectionoutput_v3tiny()
  101. || test_yolov3detectionoutput_v3()
  102. || test_yolov3detectionoutput_v4tiny()
  103. || test_yolov3detectionoutput_v4();
  104. }