@@ -179,44 +179,62 @@ class PriorBoxLayerImpl : public PriorBoxLayer
179
179
}
180
180
181
181
PriorBoxLayerImpl (const LayerParams ¶ms)
182
- : _boxWidth(0 ), _boxHeight(0 )
183
182
{
184
183
setParamsFrom (params);
185
184
_minSize = getParameter<float >(params, " min_size" , 0 , false , 0 );
186
185
_flip = getParameter<bool >(params, " flip" , 0 , false , true );
187
186
_clip = getParameter<bool >(params, " clip" , 0 , false , true );
188
187
_bboxesNormalized = getParameter<bool >(params, " normalized_bbox" , 0 , false , true );
189
188
190
- _scales.clear ();
191
189
_aspectRatios.clear ();
192
190
193
191
getAspectRatios (params);
194
192
getVariance (params);
195
- getParams (" scales" , params, &_scales);
196
- getParams (" width" , params, &_widths);
197
- getParams (" height" , params, &_heights);
198
- _explicitSizes = !_widths.empty ();
199
- CV_Assert (_widths.size () == _heights.size ());
193
+
194
+ _maxSize = -1 ;
195
+ if (params.has (" max_size" ))
196
+ {
197
+ _maxSize = params.get (" max_size" ).get <float >(0 );
198
+ CV_Assert (_maxSize > _minSize);
199
+ }
200
+
201
+ std::vector<float > widths, heights;
202
+ getParams (" width" , params, &widths);
203
+ getParams (" height" , params, &heights);
204
+ _explicitSizes = !widths.empty ();
205
+ CV_Assert (widths.size () == heights.size ());
200
206
201
207
if (_explicitSizes)
202
208
{
203
209
CV_Assert (_aspectRatios.empty (), !params.has (" min_size" ), !params.has (" max_size" ));
204
- _numPriors = _widths.size ();
210
+ _boxWidths = widths;
211
+ _boxHeights = heights;
205
212
}
206
213
else
207
214
{
208
215
CV_Assert (!_aspectRatios.empty (), _minSize > 0 );
209
- _numPriors = _aspectRatios.size () + 1 ; // + 1 for an aspect ratio 1.0
210
- }
216
+ _boxWidths.resize (1 + (_maxSize > 0 ? 1 : 0 ) + _aspectRatios.size ());
217
+ _boxHeights.resize (_boxWidths.size ());
218
+ _boxWidths[0 ] = _boxHeights[0 ] = _minSize;
211
219
212
- _maxSize = -1 ;
213
- if (params.has (" max_size" ))
214
- {
215
- _maxSize = params.get (" max_size" ).get <float >(0 );
216
- CV_Assert (_maxSize > _minSize);
220
+ int i = 1 ;
221
+ if (_maxSize > 0 )
222
+ {
223
+ // second prior: aspect_ratio = 1, size = sqrt(min_size * max_size)
224
+ _boxWidths[i] = _boxHeights[i] = sqrt (_minSize * _maxSize);
225
+ i += 1 ;
226
+ }
217
227
218
- _numPriors += 1 ;
228
+ // rest of priors
229
+ for (size_t r = 0 ; r < _aspectRatios.size (); ++r)
230
+ {
231
+ float arSqrt = sqrt (_aspectRatios[r]);
232
+ _boxWidths[i + r] = _minSize * arSqrt;
233
+ _boxHeights[i + r] = _minSize / arSqrt;
234
+ }
219
235
}
236
+ CV_Assert (_boxWidths.size () == _boxHeights.size ());
237
+ _numPriors = _boxWidths.size ();
220
238
221
239
if (params.has (" step_h" ) || params.has (" step_w" )) {
222
240
CV_Assert (!params.has (" step" ));
@@ -252,8 +270,7 @@ class PriorBoxLayerImpl : public PriorBoxLayer
252
270
virtual bool supportBackend (int backendId)
253
271
{
254
272
return backendId == DNN_BACKEND_DEFAULT ||
255
- backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine () &&
256
- _scales.empty () && !_explicitSizes;
273
+ backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine () && !_explicitSizes;
257
274
}
258
275
259
276
bool getMemoryShapes (const std::vector<MatShape> &inputs,
@@ -307,27 +324,16 @@ class PriorBoxLayerImpl : public PriorBoxLayer
307
324
if (umat_offsetsX.empty ())
308
325
{
309
326
Mat offsetsX (1 , _offsetsX.size (), CV_32FC1, &_offsetsX[0 ]);
310
- Mat offsetsY (1 , _offsetsX.size (), CV_32FC1, &_offsetsY[0 ]);
311
- Mat aspectRatios (1 , _aspectRatios.size (), CV_32FC1, &_aspectRatios[0 ]);
327
+ Mat offsetsY (1 , _offsetsY.size (), CV_32FC1, &_offsetsY[0 ]);
312
328
Mat variance (1 , _variance.size (), CV_32FC1, &_variance[0 ]);
329
+ Mat widths (1 , _boxWidths.size (), CV_32FC1, &_boxWidths[0 ]);
330
+ Mat heights (1 , _boxHeights.size (), CV_32FC1, &_boxHeights[0 ]);
313
331
314
332
offsetsX.copyTo (umat_offsetsX);
315
333
offsetsY.copyTo (umat_offsetsY);
316
- aspectRatios.copyTo (umat_aspectRatios);
317
334
variance.copyTo (umat_variance);
318
-
319
- int real_numPriors = _numPriors >> (_offsetsX.size () - 1 );
320
- if (_scales.empty ())
321
- {
322
- _scales.resize (real_numPriors, 1 .0f );
323
- umat_scales = UMat (1 , &real_numPriors, CV_32F, 1 .0f );
324
- }
325
- else
326
- {
327
- CV_Assert (_scales.size () == real_numPriors);
328
- Mat scales (1 , _scales.size (), CV_32FC1, &_scales[0 ]);
329
- scales.copyTo (umat_scales);
330
- }
335
+ widths.copyTo (umat_widths);
336
+ heights.copyTo (umat_heights);
331
337
}
332
338
333
339
size_t nthreads = _layerHeight * _layerWidth;
@@ -336,19 +342,17 @@ class PriorBoxLayerImpl : public PriorBoxLayer
336
342
kernel.set (0 , (int )nthreads);
337
343
kernel.set (1 , (float )stepX);
338
344
kernel.set (2 , (float )stepY);
339
- kernel.set (3 , (float )_minSize);
340
- kernel.set (4 , (float )_maxSize);
341
- kernel.set (5 , ocl::KernelArg::PtrReadOnly (umat_offsetsX));
342
- kernel.set (6 , ocl::KernelArg::PtrReadOnly (umat_offsetsY));
343
- kernel.set (7 , (int )_offsetsX.size ());
344
- kernel.set (8 , ocl::KernelArg::PtrReadOnly (umat_aspectRatios));
345
- kernel.set (9 , (int )_aspectRatios.size ());
346
- kernel.set (10 , ocl::KernelArg::PtrReadOnly (umat_scales));
347
- kernel.set (11 , ocl::KernelArg::PtrWriteOnly (outputs[0 ]));
348
- kernel.set (12 , (int )_layerHeight);
349
- kernel.set (13 , (int )_layerWidth);
350
- kernel.set (14 , (int )_imageHeight);
351
- kernel.set (15 , (int )_imageWidth);
345
+ kernel.set (3 , ocl::KernelArg::PtrReadOnly (umat_offsetsX));
346
+ kernel.set (4 , ocl::KernelArg::PtrReadOnly (umat_offsetsY));
347
+ kernel.set (5 , (int )_offsetsX.size ());
348
+ kernel.set (6 , ocl::KernelArg::PtrReadOnly (umat_widths));
349
+ kernel.set (7 , ocl::KernelArg::PtrReadOnly (umat_heights));
350
+ kernel.set (8 , (int )_boxWidths.size ());
351
+ kernel.set (9 , ocl::KernelArg::PtrWriteOnly (outputs[0 ]));
352
+ kernel.set (10 , (int )_layerHeight);
353
+ kernel.set (11 , (int )_layerWidth);
354
+ kernel.set (12 , (int )_imageHeight);
355
+ kernel.set (13 , (int )_imageWidth);
352
356
kernel.run (1 , &nthreads, NULL , false );
353
357
354
358
// clip the prior's coordidate such that it is within [0, 1]
@@ -401,12 +405,6 @@ class PriorBoxLayerImpl : public PriorBoxLayer
401
405
402
406
CV_Assert (inputs.size () == 2 );
403
407
404
- size_t real_numPriors = _numPriors >> (_offsetsX.size () - 1 );
405
- if (_scales.empty ())
406
- _scales.resize (real_numPriors, 1 .0f );
407
- else
408
- CV_Assert (_scales.size () == real_numPriors);
409
-
410
408
int _layerWidth = inputs[0 ]->size [3 ];
411
409
int _layerHeight = inputs[0 ]->size [2 ];
412
410
@@ -425,72 +423,15 @@ class PriorBoxLayerImpl : public PriorBoxLayer
425
423
int _outChannelSize = _layerHeight * _layerWidth * _numPriors * 4 ;
426
424
427
425
float * outputPtr = outputs[0 ].ptr <float >();
426
+ float _boxWidth, _boxHeight;
428
427
for (size_t h = 0 ; h < _layerHeight; ++h)
429
428
{
430
429
for (size_t w = 0 ; w < _layerWidth; ++w)
431
430
{
432
- // first prior: aspect_ratio = 1, size = min_size
433
- if (_explicitSizes)
431
+ for (size_t i = 0 ; i < _boxWidths.size (); ++i)
434
432
{
435
- _boxWidth = _widths[0 ] * _scales[0 ];
436
- _boxHeight = _heights[0 ] * _scales[0 ];
437
- if (_bboxesNormalized)
438
- {
439
- _boxWidth *= _imageWidth;
440
- _boxHeight *= _imageHeight;
441
- }
442
- }
443
- else
444
- _boxWidth = _boxHeight = _minSize * _scales[0 ];
445
-
446
- for (int i = 0 ; i < _offsetsX.size (); ++i)
447
- {
448
- float center_x = (w + _offsetsX[i]) * stepX;
449
- float center_y = (h + _offsetsY[i]) * stepY;
450
- outputPtr = addPrior (center_x, center_y, _boxWidth, _boxHeight, _imageWidth,
451
- _imageHeight, _bboxesNormalized, outputPtr);
452
- }
453
- if (_maxSize > 0 )
454
- {
455
- // second prior: aspect_ratio = 1, size = sqrt(min_size * max_size)
456
- _boxWidth = _boxHeight = sqrt (_minSize * _maxSize) * _scales[1 ];
457
- for (int i = 0 ; i < _offsetsX.size (); ++i)
458
- {
459
- float center_x = (w + _offsetsX[i]) * stepX;
460
- float center_y = (h + _offsetsY[i]) * stepY;
461
- outputPtr = addPrior (center_x, center_y, _boxWidth, _boxHeight, _imageWidth,
462
- _imageHeight, _bboxesNormalized, outputPtr);
463
- }
464
- }
465
-
466
- // rest of priors
467
- CV_Assert (_aspectRatios.empty () || (_maxSize > 0 ? 2 : 1 ) + _aspectRatios.size () == _scales.size ());
468
- for (size_t r = 0 ; r < _aspectRatios.size (); ++r)
469
- {
470
- float ar = _aspectRatios[r];
471
- float scale = _scales[(_maxSize > 0 ? 2 : 1 ) + r];
472
- _boxWidth = _minSize * sqrt (ar) * scale;
473
- _boxHeight = _minSize / sqrt (ar) * scale;
474
- for (int i = 0 ; i < _offsetsX.size (); ++i)
475
- {
476
- float center_x = (w + _offsetsX[i]) * stepX;
477
- float center_y = (h + _offsetsY[i]) * stepY;
478
- outputPtr = addPrior (center_x, center_y, _boxWidth, _boxHeight, _imageWidth,
479
- _imageHeight, _bboxesNormalized, outputPtr);
480
- }
481
- }
482
-
483
- // rest of sizes
484
- CV_Assert (_widths.empty () || _widths.size () == _scales.size ());
485
- for (size_t i = 1 ; i < _widths.size (); ++i)
486
- {
487
- _boxWidth = _widths[i] * _scales[i];
488
- _boxHeight = _heights[i] * _scales[i];
489
- if (_bboxesNormalized)
490
- {
491
- _boxWidth *= _imageWidth;
492
- _boxHeight *= _imageHeight;
493
- }
433
+ _boxWidth = _boxWidths[i];
434
+ _boxHeight = _boxHeights[i];
494
435
for (int j = 0 ; j < _offsetsX.size (); ++j)
495
436
{
496
437
float center_x = (w + _offsetsX[j]) * stepX;
@@ -591,24 +532,21 @@ class PriorBoxLayerImpl : public PriorBoxLayer
591
532
float _minSize;
592
533
float _maxSize;
593
534
594
- float _boxWidth;
595
- float _boxHeight;
596
-
597
535
float _stepX, _stepY;
598
536
599
537
std::vector<float > _aspectRatios;
600
538
std::vector<float > _variance;
601
- std::vector<float > _scales;
602
- std::vector<float > _widths;
603
- std::vector<float > _heights;
604
539
std::vector<float > _offsetsX;
605
540
std::vector<float > _offsetsY;
541
+ // Precomputed final widhts and heights based on aspect ratios or explicit sizes.
542
+ std::vector<float > _boxWidths;
543
+ std::vector<float > _boxHeights;
606
544
607
545
#ifdef HAVE_OPENCL
608
546
UMat umat_offsetsX;
609
547
UMat umat_offsetsY;
610
- UMat umat_aspectRatios ;
611
- UMat umat_scales ;
548
+ UMat umat_widths ;
549
+ UMat umat_heights ;
612
550
UMat umat_variance;
613
551
#endif
614
552
0 commit comments