From ec321e651fdcc13ccdc16678f2e3a70e9eb97a8e Mon Sep 17 00:00:00 2001 From: Aleksandr Rybnikov Date: Wed, 28 Jun 2017 11:31:38 +0300 Subject: [PATCH] Removed usage of std::map in DetectionOutput layer --- .../dnn/src/layers/detection_output_layer.cpp | 23 ++++++++++--------- modules/dnn/src/layers/softmax_layer.cpp | 12 ++++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/modules/dnn/src/layers/detection_output_layer.cpp b/modules/dnn/src/layers/detection_output_layer.cpp index 463d0a093d..6b449bfea6 100644 --- a/modules/dnn/src/layers/detection_output_layer.cpp +++ b/modules/dnn/src/layers/detection_output_layer.cpp @@ -218,7 +218,7 @@ public: _shareLocation, &allLocationPredictions); // Retrieve all confidences. - std::vector > > allConfidenceScores; + std::vector > > allConfidenceScores; GetConfidenceScores(confidenceData, num, numPriors, _numClasses, &allConfidenceScores); @@ -240,7 +240,7 @@ public: for (int i = 0; i < num; ++i) { const LabelBBox& decodeBBoxes = allDecodedBBoxes[i]; - const std::map >& confidenceScores = + const std::vector >& confidenceScores = allConfidenceScores[i]; std::map > indices; int numDetections = 0; @@ -251,13 +251,13 @@ public: // Ignore background class. continue; } - if (confidenceScores.find(c) == confidenceScores.end()) + if (confidenceScores.size() <= c) { // Something bad happened if there are no predictions for current label. util::make_error("Could not find confidence predictions for label ", c); } - const std::vector& scores = confidenceScores.find(c)->second; + const std::vector& scores = confidenceScores[c]; int label = _shareLocation ? -1 : c; if (decodeBBoxes.find(label) == decodeBBoxes.end()) { @@ -279,13 +279,13 @@ public: { int label = it->first; const std::vector& labelIndices = it->second; - if (confidenceScores.find(label) == confidenceScores.end()) + if (confidenceScores.size() <= label) { // Something bad happened for current label. util::make_error("Could not find location predictions for label ", label); continue; } - const std::vector& scores = confidenceScores.find(label)->second; + const std::vector& scores = confidenceScores[label]; for (size_t j = 0; j < labelIndices.size(); ++j) { size_t idx = labelIndices[j]; @@ -328,20 +328,20 @@ public: int count = 0; for (int i = 0; i < num; ++i) { - const std::map >& confidenceScores = + const std::vector >& confidenceScores = allConfidenceScores[i]; const LabelBBox& decodeBBoxes = allDecodedBBoxes[i]; for (std::map >::iterator it = allIndices[i].begin(); it != allIndices[i].end(); ++it) { int label = it->first; - if (confidenceScores.find(label) == confidenceScores.end()) + if (confidenceScores.size() <= label) { // Something bad happened if there are no predictions for current label. util::make_error("Could not find confidence predictions for label ", label); continue; } - const std::vector& scores = confidenceScores.find(label)->second; + const std::vector& scores = confidenceScores[label]; int locLabel = _shareLocation ? -1 : label; if (decodeBBoxes.find(locLabel) == decodeBBoxes.end()) { @@ -641,13 +641,14 @@ public: // confidence prediction for an image. void GetConfidenceScores(const float* confData, const int num, const int numPredsPerClass, const int numClasses, - std::vector > >* confPreds) + std::vector > >* confPreds) { confPreds->clear(); confPreds->resize(num); for (int i = 0; i < num; ++i) { - std::map >& labelScores = (*confPreds)[i]; + std::vector >& labelScores = (*confPreds)[i]; + labelScores.resize(numClasses); for (int p = 0; p < numPredsPerClass; ++p) { int startIdx = p * numClasses; diff --git a/modules/dnn/src/layers/softmax_layer.cpp b/modules/dnn/src/layers/softmax_layer.cpp index fc80fc1cd5..23a470cd18 100644 --- a/modules/dnn/src/layers/softmax_layer.cpp +++ b/modules/dnn/src/layers/softmax_layer.cpp @@ -123,8 +123,9 @@ public: for (size_t cnDim = 0; cnDim < channels; cnDim++) { + const int offset = srcOffset + cnDim * cnStep; for (size_t i = 0; i < innerSize; i++) - dstPtr[srcOffset + cnDim * cnStep + i] = srcPtr[srcOffset + cnDim * cnStep + i] - bufPtr[bufOffset + i]; + dstPtr[offset + i] = srcPtr[offset + i] - bufPtr[bufOffset + i]; } } @@ -141,22 +142,25 @@ public: for (size_t cnDim = 0; cnDim < channels; cnDim++) { + const int offset = srcOffset + cnDim * cnStep; for (size_t i = 0; i < innerSize; i++) - bufPtr[bufOffset + i] += dstPtr[srcOffset + cnDim * cnStep + i]; + bufPtr[bufOffset + i] += dstPtr[offset + i]; } //divide by computed sum for (size_t cnDim = 0; cnDim < channels; cnDim++) { + const int offset = srcOffset + cnDim * cnStep; for (size_t i = 0; i < innerSize; i++) - dstPtr[srcOffset + cnDim * cnStep + i] /= bufPtr[bufOffset + i]; + dstPtr[offset + i] /= bufPtr[bufOffset + i]; } if (logSoftMax) { for (size_t cnDim = 0; cnDim < channels; cnDim++) { + const int offset = srcOffset + cnDim * cnStep; for (size_t i = 0; i < innerSize; i++) - dstPtr[srcOffset + cnDim * cnStep + i] = log(dstPtr[srcOffset + cnDim * cnStep + i]); + dstPtr[offset + i] = log(dstPtr[offset + i]); } } }