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