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

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