Skip to content

Commit c5faa9a

Browse files
committed
Merge pull request opencv#9013 from arrybn:ssd_last_layers_optim
2 parents bbb14d3 + ec321e6 commit c5faa9a

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
@@ -219,7 +219,7 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
219219
_shareLocation, &allLocationPredictions);
220220

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

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

261-
const std::vector<float>& scores = confidenceScores.find(c)->second;
261+
const std::vector<float>& scores = confidenceScores[c];
262262
int label = _shareLocation ? -1 : c;
263263
if (decodeBBoxes.find(label) == decodeBBoxes.end())
264264
{
@@ -280,13 +280,13 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
280280
{
281281
int label = it->first;
282282
const std::vector<int>& labelIndices = it->second;
283-
if (confidenceScores.find(label) == confidenceScores.end())
283+
if (confidenceScores.size() <= label)
284284
{
285285
// Something bad happened for current label.
286286
util::make_error<int>("Could not find location predictions for label ", label);
287287
continue;
288288
}
289-
const std::vector<float>& scores = confidenceScores.find(label)->second;
289+
const std::vector<float>& scores = confidenceScores[label];
290290
for (size_t j = 0; j < labelIndices.size(); ++j)
291291
{
292292
size_t idx = labelIndices[j];
@@ -329,20 +329,20 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
329329
int count = 0;
330330
for (int i = 0; i < num; ++i)
331331
{
332-
const std::map<int, std::vector<float> >& confidenceScores =
332+
const std::vector<std::vector<float> >& confidenceScores =
333333
allConfidenceScores[i];
334334
const LabelBBox& decodeBBoxes = allDecodedBBoxes[i];
335335
for (std::map<int, std::vector<int> >::iterator it = allIndices[i].begin();
336336
it != allIndices[i].end(); ++it)
337337
{
338338
int label = it->first;
339-
if (confidenceScores.find(label) == confidenceScores.end())
339+
if (confidenceScores.size() <= label)
340340
{
341341
// Something bad happened if there are no predictions for current label.
342342
util::make_error<int>("Could not find confidence predictions for label ", label);
343343
continue;
344344
}
345-
const std::vector<float>& scores = confidenceScores.find(label)->second;
345+
const std::vector<float>& scores = confidenceScores[label];
346346
int locLabel = _shareLocation ? -1 : label;
347347
if (decodeBBoxes.find(locLabel) == decodeBBoxes.end())
348348
{
@@ -642,13 +642,14 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
642642
// confidence prediction for an image.
643643
void GetConfidenceScores(const float* confData, const int num,
644644
const int numPredsPerClass, const int numClasses,
645-
std::vector<std::map<int, std::vector<float> > >* confPreds)
645+
std::vector<std::vector<std::vector<float> > >* confPreds)
646646
{
647647
confPreds->clear();
648648
confPreds->resize(num);
649649
for (int i = 0; i < num; ++i)
650650
{
651-
std::map<int, std::vector<float> >& labelScores = (*confPreds)[i];
651+
std::vector<std::vector<float> >& labelScores = (*confPreds)[i];
652+
labelScores.resize(numClasses);
652653
for (int p = 0; p < numPredsPerClass; ++p)
653654
{
654655
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
@@ -124,8 +124,9 @@ class SoftMaxLayerImpl : public SoftmaxLayer
124124

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

@@ -142,22 +143,25 @@ class SoftMaxLayerImpl : public SoftmaxLayer
142143

143144
for (size_t cnDim = 0; cnDim < channels; cnDim++)
144145
{
146+
const int offset = srcOffset + cnDim * cnStep;
145147
for (size_t i = 0; i < innerSize; i++)
146-
bufPtr[bufOffset + i] += dstPtr[srcOffset + cnDim * cnStep + i];
148+
bufPtr[bufOffset + i] += dstPtr[offset + i];
147149
}
148150

149151
//divide by computed sum
150152
for (size_t cnDim = 0; cnDim < channels; cnDim++)
151153
{
154+
const int offset = srcOffset + cnDim * cnStep;
152155
for (size_t i = 0; i < innerSize; i++)
153-
dstPtr[srcOffset + cnDim * cnStep + i] /= bufPtr[bufOffset + i];
156+
dstPtr[offset + i] /= bufPtr[bufOffset + i];
154157
}
155158
if (logSoftMax)
156159
{
157160
for (size_t cnDim = 0; cnDim < channels; cnDim++)
158161
{
162+
const int offset = srcOffset + cnDim * cnStep;
159163
for (size_t i = 0; i < innerSize; i++)
160-
dstPtr[srcOffset + cnDim * cnStep + i] = log(dstPtr[srcOffset + cnDim * cnStep + i]);
164+
dstPtr[offset + i] = log(dstPtr[offset + i]);
161165
}
162166
}
163167
}

0 commit comments

Comments
 (0)