Skip to content

Commit ec321e6

Browse files
committed
Removed usage of std::map in DetectionOutput layer
1 parent 82ec76c commit ec321e6

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

modules/dnn/src/layers/detection_output_layer.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
218218
_shareLocation, &allLocationPredictions);
219219

220220
// Retrieve all confidences.
221-
std::vector<std::map<int, std::vector<float> > > allConfidenceScores;
221+
std::vector<std::vector<std::vector<float> > > allConfidenceScores;
222222
GetConfidenceScores(confidenceData, num, numPriors, _numClasses,
223223
&allConfidenceScores);
224224

@@ -240,7 +240,7 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
240240
for (int i = 0; i < num; ++i)
241241
{
242242
const LabelBBox& decodeBBoxes = allDecodedBBoxes[i];
243-
const std::map<int, std::vector<float> >& confidenceScores =
243+
const std::vector<std::vector<float> >& confidenceScores =
244244
allConfidenceScores[i];
245245
std::map<int, std::vector<int> > indices;
246246
int numDetections = 0;
@@ -251,13 +251,13 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
251251
// Ignore background class.
252252
continue;
253253
}
254-
if (confidenceScores.find(c) == confidenceScores.end())
254+
if (confidenceScores.size() <= c)
255255
{
256256
// Something bad happened if there are no predictions for current label.
257257
util::make_error<int>("Could not find confidence predictions for label ", c);
258258
}
259259

260-
const std::vector<float>& scores = confidenceScores.find(c)->second;
260+
const std::vector<float>& scores = confidenceScores[c];
261261
int label = _shareLocation ? -1 : c;
262262
if (decodeBBoxes.find(label) == decodeBBoxes.end())
263263
{
@@ -279,13 +279,13 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
279279
{
280280
int label = it->first;
281281
const std::vector<int>& labelIndices = it->second;
282-
if (confidenceScores.find(label) == confidenceScores.end())
282+
if (confidenceScores.size() <= label)
283283
{
284284
// Something bad happened for current label.
285285
util::make_error<int>("Could not find location predictions for label ", label);
286286
continue;
287287
}
288-
const std::vector<float>& scores = confidenceScores.find(label)->second;
288+
const std::vector<float>& scores = confidenceScores[label];
289289
for (size_t j = 0; j < labelIndices.size(); ++j)
290290
{
291291
size_t idx = labelIndices[j];
@@ -328,20 +328,20 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
328328
int count = 0;
329329
for (int i = 0; i < num; ++i)
330330
{
331-
const std::map<int, std::vector<float> >& confidenceScores =
331+
const std::vector<std::vector<float> >& confidenceScores =
332332
allConfidenceScores[i];
333333
const LabelBBox& decodeBBoxes = allDecodedBBoxes[i];
334334
for (std::map<int, std::vector<int> >::iterator it = allIndices[i].begin();
335335
it != allIndices[i].end(); ++it)
336336
{
337337
int label = it->first;
338-
if (confidenceScores.find(label) == confidenceScores.end())
338+
if (confidenceScores.size() <= label)
339339
{
340340
// Something bad happened if there are no predictions for current label.
341341
util::make_error<int>("Could not find confidence predictions for label ", label);
342342
continue;
343343
}
344-
const std::vector<float>& scores = confidenceScores.find(label)->second;
344+
const std::vector<float>& scores = confidenceScores[label];
345345
int locLabel = _shareLocation ? -1 : label;
346346
if (decodeBBoxes.find(locLabel) == decodeBBoxes.end())
347347
{
@@ -641,13 +641,14 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
641641
// confidence prediction for an image.
642642
void GetConfidenceScores(const float* confData, const int num,
643643
const int numPredsPerClass, const int numClasses,
644-
std::vector<std::map<int, std::vector<float> > >* confPreds)
644+
std::vector<std::vector<std::vector<float> > >* confPreds)
645645
{
646646
confPreds->clear();
647647
confPreds->resize(num);
648648
for (int i = 0; i < num; ++i)
649649
{
650-
std::map<int, std::vector<float> >& labelScores = (*confPreds)[i];
650+
std::vector<std::vector<float> >& labelScores = (*confPreds)[i];
651+
labelScores.resize(numClasses);
651652
for (int p = 0; p < numPredsPerClass; ++p)
652653
{
653654
int startIdx = p * numClasses;

modules/dnn/src/layers/softmax_layer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ class SoftMaxLayerImpl : public SoftmaxLayer
123123

124124
for (size_t cnDim = 0; cnDim < channels; cnDim++)
125125
{
126+
const int offset = srcOffset + cnDim * cnStep;
126127
for (size_t i = 0; i < innerSize; i++)
127-
dstPtr[srcOffset + cnDim * cnStep + i] = srcPtr[srcOffset + cnDim * cnStep + i] - bufPtr[bufOffset + i];
128+
dstPtr[offset + i] = srcPtr[offset + i] - bufPtr[bufOffset + i];
128129
}
129130
}
130131

@@ -141,22 +142,25 @@ class SoftMaxLayerImpl : public SoftmaxLayer
141142

142143
for (size_t cnDim = 0; cnDim < channels; cnDim++)
143144
{
145+
const int offset = srcOffset + cnDim * cnStep;
144146
for (size_t i = 0; i < innerSize; i++)
145-
bufPtr[bufOffset + i] += dstPtr[srcOffset + cnDim * cnStep + i];
147+
bufPtr[bufOffset + i] += dstPtr[offset + i];
146148
}
147149

148150
//divide by computed sum
149151
for (size_t cnDim = 0; cnDim < channels; cnDim++)
150152
{
153+
const int offset = srcOffset + cnDim * cnStep;
151154
for (size_t i = 0; i < innerSize; i++)
152-
dstPtr[srcOffset + cnDim * cnStep + i] /= bufPtr[bufOffset + i];
155+
dstPtr[offset + i] /= bufPtr[bufOffset + i];
153156
}
154157
if (logSoftMax)
155158
{
156159
for (size_t cnDim = 0; cnDim < channels; cnDim++)
157160
{
161+
const int offset = srcOffset + cnDim * cnStep;
158162
for (size_t i = 0; i < innerSize; i++)
159-
dstPtr[srcOffset + cnDim * cnStep + i] = log(dstPtr[srcOffset + cnDim * cnStep + i]);
163+
dstPtr[offset + i] = log(dstPtr[offset + i]);
160164
}
161165
}
162166
}

0 commit comments

Comments
 (0)