| 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 | #if __AVX__ |
| 15 | #include <immintrin.h> |
| 16 | #endif |
| 17 | |
| 18 | #include "yolov3detectionoutput_x86.h" |
| 19 | |
| 20 | #include <float.h> |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | Yolov3DetectionOutput_x86::Yolov3DetectionOutput_x86() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | static inline float sigmoid(float x) |
| 29 | { |
| 30 | return 1.f / (1.f + expf(-x)); |
| 31 | } |
| 32 | |
| 33 | int Yolov3DetectionOutput_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 34 | { |
| 35 | // gather all box |
| 36 | std::vector<BBoxRect> all_bbox_rects; |
| 37 | |
| 38 | for (size_t b = 0; b < bottom_blobs.size(); b++) |
| 39 | { |
| 40 | std::vector<std::vector<BBoxRect> > all_box_bbox_rects; |
| 41 | all_box_bbox_rects.resize(num_box); |
| 42 | const Mat& bottom_top_blobs = bottom_blobs[b]; |
| 43 | |
| 44 | int w = bottom_top_blobs.w; |
| 45 | int h = bottom_top_blobs.h; |
| 46 | int channels = bottom_top_blobs.c; |
| 47 | //printf("%d %d %d\n", w, h, channels); |
| 48 | const int channels_per_box = channels / num_box; |
| 49 | |
| 50 | // anchor coord + box score + num_class |
| 51 | if (channels_per_box != 4 + 1 + num_class) |
| 52 | return -1; |
| 53 | size_t mask_offset = b * num_box; |
| 54 | int net_w = (int)(anchors_scale[b] * w); |
| 55 | int net_h = (int)(anchors_scale[b] * h); |
| 56 | //printf("%d %d\n", net_w, net_h); |
| 57 | |
| 58 | //printf("%d %d %d\n", w, h, channels); |
| 59 | #pragma omp parallel for num_threads(opt.num_threads) |
| 60 | for (int pp = 0; pp < num_box; pp++) |
| 61 | { |
| 62 | int p = pp * channels_per_box; |
| 63 | int biases_index = static_cast<int>(mask[pp + mask_offset]); |
| 64 | //printf("%d\n", biases_index); |
| 65 | const float bias_w = biases[biases_index * 2]; |
| 66 | const float bias_h = biases[biases_index * 2 + 1]; |
| 67 | //printf("%f %f\n", bias_w, bias_h); |
| 68 | const float* xptr = bottom_top_blobs.channel(p); |
| 69 | const float* yptr = bottom_top_blobs.channel(p + 1); |
| 70 | const float* wptr = bottom_top_blobs.channel(p + 2); |
| 71 | const float* hptr = bottom_top_blobs.channel(p + 3); |
| 72 | |
| 73 | const float* box_score_ptr = bottom_top_blobs.channel(p + 4); |
| 74 | |
| 75 | // softmax class scores |
| 76 | Mat scores = bottom_top_blobs.channel_range(p + 5, num_class); |
| 77 | //softmax->forward_inplace(scores, opt); |
| 78 | |
| 79 | const int cs = (int)scores.cstep; |
| 80 | |
| 81 | #if __AVX2__ |
| 82 | const __m256i vi = _mm256_setr_epi32( |
| 83 | 0, cs * 1, cs * 2, cs * 3, cs * 4, cs * 5, cs * 6, cs * 7); |
| 84 | #endif |
| 85 | |
| 86 | for (int i = 0; i < h; i++) |
| 87 | { |
| 88 | for (int j = 0; j < w; j++) |
| 89 | { |
| 90 | #if 0 |
| 91 | int class_index = 0; |
| 92 | float class_score = -FLT_MAX; |
| 93 | for (int q = 0; q < num_class; q++) |
| 94 | { |
| 95 | float score = scores.channel(q).row(i)[j]; |
| 96 | if (score > class_score) |
| 97 | { |
| 98 | class_index = q; |
| 99 | class_score = score; |
| 100 | } |
| 101 | } |
| 102 | #else |
| 103 | // find class index with max class score |
| 104 | int class_index = 0; |
| 105 | float class_score = -FLT_MAX; |
| 106 | float* ptr = ((float*)scores.data) + i * w + j; |
| 107 | float* end = ptr + num_class * cs; |
| 108 | int q = 0; |
| 109 | #if __AVX2__ |
| 110 | float* end8 = ptr + (num_class & -8) * cs; |
| 111 | unsigned long index; |
| 112 | |
| 113 | for (; ptr < end8; ptr += 8 * cs, q += 8) |
| 114 | { |
| 115 | __m256 p = _mm256_i32gather_ps(ptr, vi, 4); |
| 116 | __m256 t = _mm256_max_ps(p, _mm256_permute2f128_ps(p, p, 1)); |
| 117 | t = _mm256_max_ps(t, _mm256_permute_ps(t, 0x4e)); |
| 118 | t = _mm256_max_ps(t, _mm256_permute_ps(t, 0xb1)); |
| 119 | float score = _mm_cvtss_f32(_mm256_extractf128_ps(t, 0)); |
| 120 | |
| 121 | if (score > class_score) |
| 122 | { |
| 123 | __m256 mi = _mm256_cmp_ps(p, t, _CMP_EQ_OQ); |
| 124 | int mask = _mm256_movemask_ps(mi); |
| 125 | #ifdef _MSC_VER |
| 126 | BitScanForward(&index, mask); |
| 127 | #else |
| 128 | index = __builtin_ctz(mask); |
| 129 | #endif |
| 130 | class_index = q + index; |
| 131 | class_score = score; |
| 132 | } |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | for (; ptr < end; ptr += cs, q++) |
| 137 | { |
| 138 | if (*ptr > class_score) |
| 139 | { |
| 140 | class_index = q; |
| 141 | class_score = *ptr; |
| 142 | } |
| 143 | } |
| 144 | #endif |
| 145 | //sigmoid(box_score) * sigmoid(class_score) |
| 146 | float confidence = 1.f / ((1.f + expf(-box_score_ptr[0]) * (1.f + expf(-class_score)))); |
| 147 | if (confidence >= confidence_threshold) |
| 148 | { |
| 149 | // region box |
| 150 | float bbox_cx = (j + sigmoid(xptr[0])) / w; |
| 151 | float bbox_cy = (i + sigmoid(yptr[0])) / h; |
| 152 | float bbox_w = expf(wptr[0]) * bias_w / net_w; |
| 153 | float bbox_h = expf(hptr[0]) * bias_h / net_h; |
| 154 | |
| 155 | float bbox_xmin = bbox_cx - bbox_w * 0.5f; |
| 156 | float bbox_ymin = bbox_cy - bbox_h * 0.5f; |
| 157 | float bbox_xmax = bbox_cx + bbox_w * 0.5f; |
| 158 | float bbox_ymax = bbox_cy + bbox_h * 0.5f; |
| 159 | |
| 160 | float area = bbox_w * bbox_h; |
| 161 | |
| 162 | BBoxRect c = {confidence, bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax, area, class_index}; |
| 163 | all_box_bbox_rects[pp].push_back(c); |
| 164 | } |
| 165 | |
| 166 | xptr++; |
| 167 | yptr++; |
| 168 | wptr++; |
| 169 | hptr++; |
| 170 | |
| 171 | box_score_ptr++; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | for (int i = 0; i < num_box; i++) |
| 177 | { |
| 178 | const std::vector<BBoxRect>& box_bbox_rects = all_box_bbox_rects[i]; |
| 179 | |
| 180 | all_bbox_rects.insert(all_bbox_rects.end(), box_bbox_rects.begin(), box_bbox_rects.end()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // global sort inplace |
| 185 | qsort_descent_inplace(all_bbox_rects); |
| 186 | |
| 187 | // apply nms |
| 188 | std::vector<size_t> picked; |
| 189 | nms_sorted_bboxes(all_bbox_rects, picked, nms_threshold); |
| 190 | |
| 191 | // select |
| 192 | std::vector<BBoxRect> bbox_rects; |
| 193 | |
| 194 | for (size_t i = 0; i < picked.size(); i++) |
| 195 | { |
| 196 | size_t z = picked[i]; |
| 197 | bbox_rects.push_back(all_bbox_rects[z]); |
| 198 | } |
| 199 | |
| 200 | // fill result |
| 201 | int num_detected = static_cast<int>(bbox_rects.size()); |
| 202 | if (num_detected == 0) |
| 203 | return 0; |
| 204 | |
| 205 | Mat& top_blob = top_blobs[0]; |
| 206 | top_blob.create(6, num_detected, 4u, opt.blob_allocator); |
| 207 | if (top_blob.empty()) |
| 208 | return -100; |
| 209 | |
| 210 | for (int i = 0; i < num_detected; i++) |
| 211 | { |
| 212 | const BBoxRect& r = bbox_rects[i]; |
| 213 | float score = r.score; |
| 214 | float* outptr = top_blob.row(i); |
| 215 | |
| 216 | outptr[0] = r.label + 1.0f; // +1 for prepend background class |
| 217 | outptr[1] = score; |
| 218 | outptr[2] = r.xmin; |
| 219 | outptr[3] = r.ymin; |
| 220 | outptr[4] = r.xmax; |
| 221 | outptr[5] = r.ymax; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | } // namespace ncnn |