Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "DetectionPostProcess.hpp" |
| 7 | |
| 8 | #include <armnn/ArmNN.hpp> |
| 9 | |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 10 | #include <boost/assert.hpp> |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 11 | #include <boost/numeric/conversion/cast.hpp> |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <numeric> |
| 15 | |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 16 | namespace armnn |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 17 | { |
| 18 | |
| 19 | std::vector<unsigned int> GenerateRangeK(unsigned int k) |
| 20 | { |
| 21 | std::vector<unsigned int> range(k); |
| 22 | std::iota(range.begin(), range.end(), 0); |
| 23 | return range; |
| 24 | } |
| 25 | |
| 26 | void TopKSort(unsigned int k, unsigned int* indices, const float* values, unsigned int numElement) |
| 27 | { |
| 28 | std::partial_sort(indices, indices + k, indices + numElement, |
| 29 | [&values](unsigned int i, unsigned int j) { return values[i] > values[j]; }); |
| 30 | } |
| 31 | |
| 32 | float IntersectionOverUnion(const float* boxI, const float* boxJ) |
| 33 | { |
| 34 | // Box-corner format: ymin, xmin, ymax, xmax. |
| 35 | const int yMin = 0; |
| 36 | const int xMin = 1; |
| 37 | const int yMax = 2; |
| 38 | const int xMax = 3; |
| 39 | float areaI = (boxI[yMax] - boxI[yMin]) * (boxI[xMax] - boxI[xMin]); |
| 40 | float areaJ = (boxJ[yMax] - boxJ[yMin]) * (boxJ[xMax] - boxJ[xMin]); |
| 41 | float yMinIntersection = std::max(boxI[yMin], boxJ[yMin]); |
| 42 | float xMinIntersection = std::max(boxI[xMin], boxJ[xMin]); |
| 43 | float yMaxIntersection = std::min(boxI[yMax], boxJ[yMax]); |
| 44 | float xMaxIntersection = std::min(boxI[xMax], boxJ[xMax]); |
| 45 | float areaIntersection = std::max(yMaxIntersection - yMinIntersection, 0.0f) * |
| 46 | std::max(xMaxIntersection - xMinIntersection, 0.0f); |
| 47 | float areaUnion = areaI + areaJ - areaIntersection; |
| 48 | return areaIntersection / areaUnion; |
| 49 | } |
| 50 | |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 51 | std::vector<unsigned int> NonMaxSuppression(unsigned int numBoxes, |
| 52 | const std::vector<float>& boxCorners, |
| 53 | const std::vector<float>& scores, |
| 54 | float nmsScoreThreshold, |
| 55 | unsigned int maxDetection, |
| 56 | float nmsIouThreshold) |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 57 | { |
| 58 | // Select boxes that have scores above a given threshold. |
| 59 | std::vector<float> scoresAboveThreshold; |
| 60 | std::vector<unsigned int> indicesAboveThreshold; |
| 61 | for (unsigned int i = 0; i < numBoxes; ++i) |
| 62 | { |
| 63 | if (scores[i] >= nmsScoreThreshold) |
| 64 | { |
| 65 | scoresAboveThreshold.push_back(scores[i]); |
| 66 | indicesAboveThreshold.push_back(i); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Sort the indices based on scores. |
| 71 | unsigned int numAboveThreshold = boost::numeric_cast<unsigned int>(scoresAboveThreshold.size()); |
| 72 | std::vector<unsigned int> sortedIndices = GenerateRangeK(numAboveThreshold); |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 73 | TopKSort(numAboveThreshold, sortedIndices.data(), scoresAboveThreshold.data(), numAboveThreshold); |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 74 | |
| 75 | // Number of output cannot be more than max detections specified in the option. |
| 76 | unsigned int numOutput = std::min(maxDetection, numAboveThreshold); |
| 77 | std::vector<unsigned int> outputIndices; |
| 78 | std::vector<bool> visited(numAboveThreshold, false); |
| 79 | |
| 80 | // Prune out the boxes with high intersection over union by keeping the box with higher score. |
| 81 | for (unsigned int i = 0; i < numAboveThreshold; ++i) |
| 82 | { |
| 83 | if (outputIndices.size() >= numOutput) |
| 84 | { |
| 85 | break; |
| 86 | } |
| 87 | if (!visited[sortedIndices[i]]) |
| 88 | { |
| 89 | outputIndices.push_back(indicesAboveThreshold[sortedIndices[i]]); |
| 90 | } |
| 91 | for (unsigned int j = i + 1; j < numAboveThreshold; ++j) |
| 92 | { |
| 93 | unsigned int iIndex = indicesAboveThreshold[sortedIndices[i]] * 4; |
| 94 | unsigned int jIndex = indicesAboveThreshold[sortedIndices[j]] * 4; |
| 95 | if (IntersectionOverUnion(&boxCorners[iIndex], &boxCorners[jIndex]) > nmsIouThreshold) |
| 96 | { |
| 97 | visited[sortedIndices[j]] = true; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return outputIndices; |
| 102 | } |
| 103 | |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 104 | void AllocateOutputData(unsigned int numOutput, |
| 105 | unsigned int numSelected, |
| 106 | const std::vector<float>& boxCorners, |
| 107 | const std::vector<unsigned int>& outputIndices, |
| 108 | const std::vector<unsigned int>& selectedBoxes, |
| 109 | const std::vector<unsigned int>& selectedClasses, |
| 110 | const std::vector<float>& selectedScores, |
| 111 | float* detectionBoxes, |
| 112 | float* detectionScores, |
| 113 | float* detectionClasses, |
| 114 | float* numDetections) |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 115 | { |
| 116 | for (unsigned int i = 0; i < numOutput; ++i) |
| 117 | { |
| 118 | unsigned int boxIndex = i * 4; |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 119 | if (i < numSelected) |
| 120 | { |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 121 | unsigned int boxCornorIndex = selectedBoxes[outputIndices[i]] * 4; |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 122 | detectionScores[i] = selectedScores[outputIndices[i]]; |
| 123 | detectionClasses[i] = boost::numeric_cast<float>(selectedClasses[outputIndices[i]]); |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 124 | detectionBoxes[boxIndex] = boxCorners[boxCornorIndex]; |
| 125 | detectionBoxes[boxIndex + 1] = boxCorners[boxCornorIndex + 1]; |
| 126 | detectionBoxes[boxIndex + 2] = boxCorners[boxCornorIndex + 2]; |
| 127 | detectionBoxes[boxIndex + 3] = boxCorners[boxCornorIndex + 3]; |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
| 131 | detectionScores[i] = 0.0f; |
| 132 | detectionClasses[i] = 0.0f; |
| 133 | detectionBoxes[boxIndex] = 0.0f; |
| 134 | detectionBoxes[boxIndex + 1] = 0.0f; |
| 135 | detectionBoxes[boxIndex + 2] = 0.0f; |
| 136 | detectionBoxes[boxIndex + 3] = 0.0f; |
| 137 | } |
| 138 | } |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 139 | numDetections[0] = boost::numeric_cast<float>(numSelected); |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 142 | void DetectionPostProcess(const TensorInfo& boxEncodingsInfo, |
| 143 | const TensorInfo& scoresInfo, |
| 144 | const TensorInfo& anchorsInfo, |
| 145 | const TensorInfo& detectionBoxesInfo, |
| 146 | const TensorInfo& detectionClassesInfo, |
| 147 | const TensorInfo& detectionScoresInfo, |
| 148 | const TensorInfo& numDetectionsInfo, |
| 149 | const DetectionPostProcessDescriptor& desc, |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 150 | Decoder<float>& boxEncodings, |
| 151 | Decoder<float>& scores, |
| 152 | Decoder<float>& anchors, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 153 | float* detectionBoxes, |
| 154 | float* detectionClasses, |
| 155 | float* detectionScores, |
| 156 | float* numDetections) |
| 157 | { |
| 158 | // Transform center-size format which is (ycenter, xcenter, height, width) to box-corner format, |
| 159 | // which represents the lower left corner and the upper right corner (ymin, xmin, ymax, xmax) |
| 160 | std::vector<float> boxCorners(boxEncodingsInfo.GetNumElements()); |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 161 | |
| 162 | const unsigned int numBoxes = boxEncodingsInfo.GetShape()[1]; |
| 163 | const unsigned int numScores = scoresInfo.GetNumElements(); |
| 164 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 165 | for (unsigned int i = 0; i < numBoxes; ++i) |
| 166 | { |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 167 | // Y |
| 168 | float boxEncodingY = boxEncodings.Get(); |
| 169 | float anchorY = anchors.Get(); |
| 170 | |
| 171 | ++boxEncodings; |
| 172 | ++anchors; |
| 173 | |
| 174 | // X |
| 175 | float boxEncodingX = boxEncodings.Get(); |
| 176 | float anchorX = anchors.Get(); |
| 177 | |
| 178 | ++boxEncodings; |
| 179 | ++anchors; |
| 180 | |
| 181 | // H |
| 182 | float boxEncodingH = boxEncodings.Get(); |
| 183 | float anchorH = anchors.Get(); |
| 184 | |
| 185 | ++boxEncodings; |
| 186 | ++anchors; |
| 187 | |
| 188 | // W |
| 189 | float boxEncodingW = boxEncodings.Get(); |
| 190 | float anchorW = anchors.Get(); |
| 191 | |
| 192 | ++boxEncodings; |
| 193 | ++anchors; |
| 194 | |
| 195 | float yCentre = boxEncodingY / desc.m_ScaleY * anchorH + anchorY; |
| 196 | float xCentre = boxEncodingX / desc.m_ScaleX * anchorW + anchorX; |
| 197 | |
| 198 | float halfH = 0.5f * expf(boxEncodingH / desc.m_ScaleH) * anchorH; |
| 199 | float halfW = 0.5f * expf(boxEncodingW / desc.m_ScaleW) * anchorW; |
| 200 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 201 | unsigned int indexY = i * 4; |
| 202 | unsigned int indexX = indexY + 1; |
| 203 | unsigned int indexH = indexX + 1; |
| 204 | unsigned int indexW = indexH + 1; |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 205 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 206 | // ymin |
| 207 | boxCorners[indexY] = yCentre - halfH; |
| 208 | // xmin |
| 209 | boxCorners[indexX] = xCentre - halfW; |
| 210 | // ymax |
| 211 | boxCorners[indexH] = yCentre + halfH; |
| 212 | // xmax |
| 213 | boxCorners[indexW] = xCentre + halfW; |
| 214 | |
| 215 | BOOST_ASSERT(boxCorners[indexY] < boxCorners[indexH]); |
| 216 | BOOST_ASSERT(boxCorners[indexX] < boxCorners[indexW]); |
| 217 | } |
| 218 | |
| 219 | unsigned int numClassesWithBg = desc.m_NumClasses + 1; |
| 220 | |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 221 | // Decode scores |
| 222 | std::vector<float> decodedScores; |
| 223 | decodedScores.reserve(numScores); |
| 224 | |
| 225 | for (unsigned int i = 0u; i < numScores; ++i) |
| 226 | { |
| 227 | decodedScores.emplace_back(scores.Get()); |
| 228 | ++scores; |
| 229 | } |
| 230 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 231 | // Perform Non Max Suppression. |
| 232 | if (desc.m_UseRegularNms) |
| 233 | { |
| 234 | // Perform Regular NMS. |
| 235 | // For each class, perform NMS and select max detection numbers of the highest score across all classes. |
| 236 | std::vector<float> classScores(numBoxes); |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 237 | |
| 238 | std::vector<unsigned int> selectedBoxesAfterNms; |
| 239 | selectedBoxesAfterNms.reserve(numBoxes); |
| 240 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 241 | std::vector<float> selectedScoresAfterNms; |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 242 | selectedBoxesAfterNms.reserve(numScores); |
| 243 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 244 | std::vector<unsigned int> selectedClasses; |
| 245 | |
| 246 | for (unsigned int c = 0; c < desc.m_NumClasses; ++c) |
| 247 | { |
| 248 | // For each boxes, get scores of the boxes for the class c. |
| 249 | for (unsigned int i = 0; i < numBoxes; ++i) |
| 250 | { |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 251 | classScores[i] = decodedScores[i * numClassesWithBg + c + 1]; |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 252 | } |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 253 | std::vector<unsigned int> selectedIndices = NonMaxSuppression(numBoxes, |
| 254 | boxCorners, |
| 255 | classScores, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 256 | desc.m_NmsScoreThreshold, |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 257 | desc.m_DetectionsPerClass, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 258 | desc.m_NmsIouThreshold); |
| 259 | |
| 260 | for (unsigned int i = 0; i < selectedIndices.size(); ++i) |
| 261 | { |
| 262 | selectedBoxesAfterNms.push_back(selectedIndices[i]); |
| 263 | selectedScoresAfterNms.push_back(classScores[selectedIndices[i]]); |
| 264 | selectedClasses.push_back(c); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Select max detection numbers of the highest score across all classes |
| 269 | unsigned int numSelected = boost::numeric_cast<unsigned int>(selectedBoxesAfterNms.size()); |
| 270 | unsigned int numOutput = std::min(desc.m_MaxDetections, numSelected); |
| 271 | |
| 272 | // Sort the max scores among the selected indices. |
| 273 | std::vector<unsigned int> outputIndices = GenerateRangeK(numSelected); |
| 274 | TopKSort(numOutput, outputIndices.data(), selectedScoresAfterNms.data(), numSelected); |
| 275 | |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 276 | AllocateOutputData(detectionBoxesInfo.GetShape()[1], numOutput, boxCorners, outputIndices, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 277 | selectedBoxesAfterNms, selectedClasses, selectedScoresAfterNms, |
| 278 | detectionBoxes, detectionScores, detectionClasses, numDetections); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | // Perform Fast NMS. |
| 283 | // Select max scores of boxes and perform NMS on max scores, |
| 284 | // select max detection numbers of the highest score |
| 285 | unsigned int numClassesPerBox = std::min(desc.m_MaxClassesPerDetection, desc.m_NumClasses); |
| 286 | std::vector<float> maxScores; |
| 287 | std::vector<unsigned int>boxIndices; |
| 288 | std::vector<unsigned int>maxScoreClasses; |
| 289 | |
| 290 | for (unsigned int box = 0; box < numBoxes; ++box) |
| 291 | { |
| 292 | unsigned int scoreIndex = box * numClassesWithBg + 1; |
| 293 | |
| 294 | // Get the max scores of the box. |
| 295 | std::vector<unsigned int> maxScoreIndices = GenerateRangeK(desc.m_NumClasses); |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 296 | TopKSort(numClassesPerBox, maxScoreIndices.data(), |
| 297 | decodedScores.data() + scoreIndex, desc.m_NumClasses); |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 298 | |
| 299 | for (unsigned int i = 0; i < numClassesPerBox; ++i) |
| 300 | { |
Aron Virginas-Tar | 6331f91 | 2019-06-03 17:10:02 +0100 | [diff] [blame] | 301 | maxScores.push_back(decodedScores[scoreIndex + maxScoreIndices[i]]); |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 302 | maxScoreClasses.push_back(maxScoreIndices[i]); |
| 303 | boxIndices.push_back(box); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Perform NMS on max scores |
| 308 | std::vector<unsigned int> selectedIndices = NonMaxSuppression(numBoxes, boxCorners, maxScores, |
| 309 | desc.m_NmsScoreThreshold, |
| 310 | desc.m_MaxDetections, |
| 311 | desc.m_NmsIouThreshold); |
| 312 | |
| 313 | unsigned int numSelected = boost::numeric_cast<unsigned int>(selectedIndices.size()); |
| 314 | unsigned int numOutput = std::min(desc.m_MaxDetections, numSelected); |
| 315 | |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 316 | AllocateOutputData(detectionBoxesInfo.GetShape()[1], numOutput, boxCorners, selectedIndices, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 317 | boxIndices, maxScoreClasses, maxScores, |
| 318 | detectionBoxes, detectionScores, detectionClasses, numDetections); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | } // namespace armnn |