Skip to content

Commit e7d62d6

Browse files
committed
Merge pull request opencv#10126 from alalek:dnn_issue_10125
2 parents f8ad289 + 9db5cbf commit e7d62d6

File tree

5 files changed

+91
-82
lines changed

5 files changed

+91
-82
lines changed

modules/core/include/opencv2/core/mat.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ class CV_EXPORTS _OutputArray : public _InputArray
357357

358358
void assign(const UMat& u) const;
359359
void assign(const Mat& m) const;
360+
361+
void assign(const std::vector<UMat>& v) const;
362+
void assign(const std::vector<Mat>& v) const;
360363
};
361364

362365

modules/core/src/matrix.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,82 @@ void _OutputArray::assign(const Mat& m) const
30533053
}
30543054

30553055

3056+
void _OutputArray::assign(const std::vector<UMat>& v) const
3057+
{
3058+
int k = kind();
3059+
if (k == STD_VECTOR_UMAT)
3060+
{
3061+
std::vector<UMat>& this_v = *(std::vector<UMat>*)obj;
3062+
CV_Assert(this_v.size() == v.size());
3063+
3064+
for (size_t i = 0; i < v.size(); i++)
3065+
{
3066+
const UMat& m = v[i];
3067+
UMat& this_m = this_v[i];
3068+
if (this_m.u != NULL && this_m.u == m.u)
3069+
continue; // same object (see dnn::Layer::forward_fallback)
3070+
m.copyTo(this_m);
3071+
}
3072+
}
3073+
else if (k == STD_VECTOR_MAT)
3074+
{
3075+
std::vector<Mat>& this_v = *(std::vector<Mat>*)obj;
3076+
CV_Assert(this_v.size() == v.size());
3077+
3078+
for (size_t i = 0; i < v.size(); i++)
3079+
{
3080+
const UMat& m = v[i];
3081+
Mat& this_m = this_v[i];
3082+
if (this_m.u != NULL && this_m.u == m.u)
3083+
continue; // same object (see dnn::Layer::forward_fallback)
3084+
m.copyTo(this_m);
3085+
}
3086+
}
3087+
else
3088+
{
3089+
CV_Error(Error::StsNotImplemented, "");
3090+
}
3091+
}
3092+
3093+
3094+
void _OutputArray::assign(const std::vector<Mat>& v) const
3095+
{
3096+
int k = kind();
3097+
if (k == STD_VECTOR_UMAT)
3098+
{
3099+
std::vector<UMat>& this_v = *(std::vector<UMat>*)obj;
3100+
CV_Assert(this_v.size() == v.size());
3101+
3102+
for (size_t i = 0; i < v.size(); i++)
3103+
{
3104+
const Mat& m = v[i];
3105+
UMat& this_m = this_v[i];
3106+
if (this_m.u != NULL && this_m.u == m.u)
3107+
continue; // same object (see dnn::Layer::forward_fallback)
3108+
m.copyTo(this_m);
3109+
}
3110+
}
3111+
else if (k == STD_VECTOR_MAT)
3112+
{
3113+
std::vector<Mat>& this_v = *(std::vector<Mat>*)obj;
3114+
CV_Assert(this_v.size() == v.size());
3115+
3116+
for (size_t i = 0; i < v.size(); i++)
3117+
{
3118+
const Mat& m = v[i];
3119+
Mat& this_m = this_v[i];
3120+
if (this_m.u != NULL && this_m.u == m.u)
3121+
continue; // same object (see dnn::Layer::forward_fallback)
3122+
m.copyTo(this_m);
3123+
}
3124+
}
3125+
else
3126+
{
3127+
CV_Error(Error::StsNotImplemented, "");
3128+
}
3129+
}
3130+
3131+
30563132
static _InputOutputArray _none;
30573133
InputOutputArray noArray() { return _none; }
30583134

modules/dnn/src/dnn.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,10 @@ void Layer::forward_fallback(InputArrayOfArrays inputs_arr, OutputArrayOfArrays
23812381
inputs[i] = &inpvec[i];
23822382

23832383
this->forward(inputs, outputs, internals);
2384+
2385+
// sync results back
2386+
outputs_arr.assign(outputs);
2387+
internals_arr.assign(internals);
23842388
}
23852389

23862390
void Layer::run(const std::vector<Mat> &inputs, std::vector<Mat> &outputs, std::vector<Mat> &internals)

modules/dnn/src/layers/detection_output_layer.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -211,92 +211,11 @@ class DetectionOutputLayerImpl : public DetectionOutputLayer
211211
return false;
212212
}
213213

214-
#ifdef HAVE_OPENCL
215-
bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
216-
{
217-
std::vector<Mat> inpvec;
218-
std::vector<Mat> outputs;
219-
220-
inputs_arr.getMatVector(inpvec);
221-
outputs_arr.getMatVector(outputs);
222-
223-
std::vector<Mat*> inputs(inpvec.size());
224-
for (size_t i = 0; i < inpvec.size(); i++)
225-
inputs[i] = &inpvec[i];
226-
227-
std::vector<LabelBBox> allDecodedBBoxes;
228-
std::vector<std::vector<std::vector<float> > > allConfidenceScores;
229-
230-
int num = inputs[0]->size[0];
231-
232-
// extract predictions from input layers
233-
{
234-
int numPriors = inputs[2]->size[2] / 4;
235-
236-
const float* locationData = inputs[0]->ptr<float>();
237-
const float* confidenceData = inputs[1]->ptr<float>();
238-
const float* priorData = inputs[2]->ptr<float>();
239-
240-
// Retrieve all location predictions
241-
std::vector<LabelBBox> allLocationPredictions;
242-
GetLocPredictions(locationData, num, numPriors, _numLocClasses,
243-
_shareLocation, _locPredTransposed, allLocationPredictions);
244-
245-
// Retrieve all confidences
246-
GetConfidenceScores(confidenceData, num, numPriors, _numClasses, allConfidenceScores);
247-
248-
// Retrieve all prior bboxes
249-
std::vector<util::NormalizedBBox> priorBBoxes;
250-
std::vector<std::vector<float> > priorVariances;
251-
GetPriorBBoxes(priorData, numPriors, priorBBoxes, priorVariances);
252-
253-
// Decode all loc predictions to bboxes
254-
DecodeBBoxesAll(allLocationPredictions, priorBBoxes, priorVariances, num,
255-
_shareLocation, _numLocClasses, _backgroundLabelId,
256-
_codeType, _varianceEncodedInTarget, false, allDecodedBBoxes);
257-
}
258-
259-
size_t numKept = 0;
260-
std::vector<std::map<int, std::vector<int> > > allIndices;
261-
for (int i = 0; i < num; ++i)
262-
{
263-
numKept += processDetections_(allDecodedBBoxes[i], allConfidenceScores[i], allIndices);
264-
}
265-
266-
if (numKept == 0)
267-
{
268-
// Set confidences to zeros.
269-
Range ranges[] = {Range::all(), Range::all(), Range::all(), Range(2, 3)};
270-
outputs[0](ranges).setTo(0);
271-
return true;
272-
}
273-
int outputShape[] = {1, 1, (int)numKept, 7};
274-
Mat mat(4, outputShape, CV_32F);
275-
float* outputsData = mat.ptr<float>();
276-
277-
size_t count = 0;
278-
for (int i = 0; i < num; ++i)
279-
{
280-
count += outputDetections_(i, &outputsData[count * 7],
281-
allDecodedBBoxes[i], allConfidenceScores[i],
282-
allIndices[i]);
283-
}
284-
UMat& output = outputs_arr.getUMatRef(0);
285-
output = mat.getUMat(ACCESS_READ);
286-
CV_Assert(count == numKept);
287-
return true;
288-
}
289-
#endif
290-
291214
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
292215
{
293216
CV_TRACE_FUNCTION();
294217
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
295218

296-
CV_OCL_RUN((preferableTarget == DNN_TARGET_OPENCL) &&
297-
OCL_PERFORMANCE_CHECK(ocl::Device::getDefault().isIntel()),
298-
forward_ocl(inputs_arr, outputs_arr, internals_arr))
299-
300219
Layer::forward_fallback(inputs_arr, outputs_arr, internals_arr);
301220
}
302221

samples/dnn/ssd_mobilenet_object_detection.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const char* params
3737
"{ camera_device | 0 | camera device number }"
3838
"{ video | | video or image for detection}"
3939
"{ out | | path to output video file}"
40-
"{ min_confidence | 0.2 | min confidence }";
40+
"{ min_confidence | 0.2 | min confidence }"
41+
"{ opencl | false | enable OpenCL }"
42+
;
4143

4244
int main(int argc, char** argv)
4345
{
@@ -57,6 +59,11 @@ int main(int argc, char** argv)
5759
dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
5860
//! [Initialize network]
5961

62+
if (parser.get<bool>("opencl"))
63+
{
64+
net.setPreferableTarget(DNN_TARGET_OPENCL);
65+
}
66+
6067
if (net.empty())
6168
{
6269
cerr << "Can't load network by using the following files: " << endl;

0 commit comments

Comments
 (0)