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.9 kB

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