diff --git a/modules/dnn/samples/ssd_ilsvrc15_300x300.prototxt b/modules/dnn/samples/ssd_ilsvrc15_300x300.prototxt index 120e78e91..15a0c2aba 100644 --- a/modules/dnn/samples/ssd_ilsvrc15_300x300.prototxt +++ b/modules/dnn/samples/ssd_ilsvrc15_300x300.prototxt @@ -500,7 +500,7 @@ layer { type: "constant" value: 0 } - # dilation: 6 + dilation: 6 } } layer { @@ -750,7 +750,7 @@ layer { } layer { name: "conv4_3_norm" - type: "Normalize" + type: "NormalizeBBox" bottom: "conv4_3" top: "conv4_3_norm" normalize_bbox_param { diff --git a/modules/dnn/samples/ssd_object_detection.cpp b/modules/dnn/samples/ssd_object_detection.cpp index 77567f155..0d12ac53c 100644 --- a/modules/dnn/samples/ssd_object_detection.cpp +++ b/modules/dnn/samples/ssd_object_detection.cpp @@ -9,48 +9,46 @@ using namespace cv::dnn; #include using namespace std; -static const string fcnType = "fcn8s"; - -static void colorizeSegmentation(dnn::Blob& score, - const vector& colors, - cv::Mat& segm) -{ - const int rows = score.rows(); - const int cols = score.cols(); - const int chns = score.channels(); - - cv::Mat maxCl(rows, cols, CV_8UC1); - cv::Mat maxVal(rows, cols, CV_32FC1); - for (int ch = 0; ch < chns; ch++) - { - for (int row = 0; row < rows; row++) - { - const float* ptrScore = score.ptrf(0, ch, row); - uchar* ptrMaxCl = maxCl.ptr(row); - float* ptrMaxVal = maxVal.ptr(row); - for (int col = 0; col < cols; col++) - { - if (ptrScore[col] > ptrMaxVal[col]) - { - ptrMaxVal[col] = ptrScore[col]; - ptrMaxCl[col] = ch; - } - } - } - } - - segm.create(rows, cols, CV_8UC3); - for (int row = 0; row < rows; row++) - { - const uchar* ptrMaxCl = maxCl.ptr(row); - cv::Vec3b* ptrSegm = segm.ptr(row); - for (int col = 0; col < cols; col++) - { - ptrSegm[col] = colors[ptrMaxCl[col]]; - } - } -} +//static void colorizeSegmentation(dnn::Blob& score, +// const vector& colors, +// cv::Mat& segm) +//{ +// const int rows = score.rows(); +// const int cols = score.cols(); +// const int chns = score.channels(); + +// cv::Mat maxCl(rows, cols, CV_8UC1); +// cv::Mat maxVal(rows, cols, CV_32FC1); +// for (int ch = 0; ch < chns; ch++) +// { +// for (int row = 0; row < rows; row++) +// { +// const float* ptrScore = score.ptrf(0, ch, row); +// uchar* ptrMaxCl = maxCl.ptr(row); +// float* ptrMaxVal = maxVal.ptr(row); +// for (int col = 0; col < cols; col++) +// { +// if (ptrScore[col] > ptrMaxVal[col]) +// { +// ptrMaxVal[col] = ptrScore[col]; +// ptrMaxCl[col] = ch; +// } +// } +// } +// } + +// segm.create(rows, cols, CV_8UC3); +// for (int row = 0; row < rows; row++) +// { +// const uchar* ptrMaxCl = maxCl.ptr(row); +// cv::Vec3b* ptrSegm = segm.ptr(row); +// for (int col = 0; col < cols; col++) +// { +// ptrSegm[col] = colors[ptrMaxCl[col]]; +// } +// } +//} const char* about = "This sample uses Single-Shot Detector to detect objects " "from camera\n"; // TODO: link @@ -111,6 +109,7 @@ int main(int argc, char** argv) return 0; } + size_t i = 0; for (;; ) { Mat frame; @@ -118,12 +117,15 @@ int main(int argc, char** argv) if (frame.empty()) break; - //! [Prepare blob] resize(frame, frame, Size(300, 300)); //SSD accepts 300x300 RGB-images dnn::Blob inputBlob = dnn::Blob(frame); //Convert Mat to dnn::Blob image //! [Prepare blob] + std::ostringstream stream; + stream << "folder/" << i << ".jpg"; + imwrite(stream.str(), frame); + //! [Set input blob] net.setBlob(".data", inputBlob); //set the network input //! [Set input blob] @@ -132,20 +134,20 @@ int main(int argc, char** argv) net.forward(); //compute output //! [Make forward pass] - //! [Gather output] - dnn::Blob detection = net.getBlob("detection_out"); +// //! [Gather output] +// dnn::Blob detection = net.getBlob("detection_out"); - // cv::Mat colorize; - // colorizeSegmentation(score, colors, colorize); - // cv::Mat show; - // cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show); - // cv::imshow("show", show); - // cv::waitKey(0); - // return 0; +// // cv::Mat colorize; +// // colorizeSegmentation(score, colors, colorize); +// // cv::Mat show; +// // cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show); +// // cv::imshow("show", show); +// // cv::waitKey(0); +// // return 0; - imshow("frame", frame); - if (waitKey(1) == 27) - break; // stop capturing by pressing ESC +// imshow("frame", frame); +// if (waitKey(1) == 27) +// break; // stop capturing by pressing ESC } camera.release(); diff --git a/modules/dnn/src/caffe/caffe.proto b/modules/dnn/src/caffe/caffe.proto index 6a92803a1..a7bca16ad 100644 --- a/modules/dnn/src/caffe/caffe.proto +++ b/modules/dnn/src/caffe/caffe.proto @@ -404,7 +404,7 @@ message ParamSpec { // NOTE // Update the next available ID when you add a new LayerParameter field. // -// LayerParameter next available layer-specific ID: 138 (last added: detection_output_param) +// LayerParameter next available layer-specific ID: 142 (last added: detection_output_param) message LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the layer type diff --git a/modules/dnn/src/init.cpp b/modules/dnn/src/init.cpp index 03546bb16..7e285a6fd 100644 --- a/modules/dnn/src/init.cpp +++ b/modules/dnn/src/init.cpp @@ -55,6 +55,11 @@ #include "layers/split_layer.hpp" #include "layers/crop_layer.hpp" #include "layers/eltwise_layer.hpp" +#include "layers/flatten_layer.hpp" +#include "layers/permute_layer.hpp" +#include "layers/prior_box_layer.hpp" +#include "layers/detection_output_layer.hpp" +#include "layers/normalize_bbox_layer.hpp" namespace cv { @@ -82,7 +87,7 @@ void initModule() REG_RUNTIME_LAYER_CLASS(Softmax, SoftMaxLayer) REG_RUNTIME_LAYER_CLASS(Split, SplitLayer) REG_RUNTIME_LAYER_CLASS(Reshape, ReshapeLayer) - REG_STATIC_LAYER_FUNC(Flatten, createFlattenLayer) +// REG_STATIC_LAYER_FUNC(Flatten, createFlattenLayer) REG_RUNTIME_LAYER_CLASS(Pooling, PoolingLayer) REG_RUNTIME_LAYER_CLASS(MVN, MVNLayer) REG_RUNTIME_LAYER_CLASS(LRN, LRNLayer) @@ -103,6 +108,12 @@ void initModule() REG_RUNTIME_LAYER_CLASS(Crop, CropLayer) REG_RUNTIME_LAYER_CLASS(Eltwise, EltwiseLayer) + REG_RUNTIME_LAYER_CLASS(Permute, PermuteLayer) + REG_RUNTIME_LAYER_CLASS(Flatten, FlattenLayer) + REG_RUNTIME_LAYER_CLASS(PriorBox, PriorBoxLayer) + REG_RUNTIME_LAYER_CLASS(DetectionOutput, DetectionOutputLayer) + REG_RUNTIME_LAYER_CLASS(NormalizeBBox, NormalizeBBoxLayer) + init.status = true; } diff --git a/modules/dnn/src/layers/convolution_layer.cpp b/modules/dnn/src/layers/convolution_layer.cpp index 03d76dc7b..4934fba37 100644 --- a/modules/dnn/src/layers/convolution_layer.cpp +++ b/modules/dnn/src/layers/convolution_layer.cpp @@ -190,8 +190,9 @@ namespace dnn inpW = inpBlob.cols(); inpCn = inpBlob.channels(); - outH = (inpH + 2 * padH - kerH) / strideH + 1; - outW = (inpW + 2 * padW - kerW) / strideW + 1; + outH = (inpH + 2 * padH - (dilationH * (kerH - 1) + 1)) / strideH + 1; + outW = (inpW + 2 * padW - (dilationW * (kerW - 1) + 1)) / strideW + 1; + outCn = numOutput; topH = outH; topW = outW; topCn = outCn; diff --git a/modules/dnn/src/layers/detection_output_layer.cpp b/modules/dnn/src/layers/detection_output_layer.cpp index 3a18ed0b9..73ef9558c 100644 --- a/modules/dnn/src/layers/detection_output_layer.cpp +++ b/modules/dnn/src/layers/detection_output_layer.cpp @@ -79,65 +79,78 @@ bool SortScorePairDescend(const std::pair& pair1, const std::string DetectionOutputLayer::_layerName = std::string("DetectionOutput"); -DictValue DetectionOutputLayer::getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName) +bool DetectionOutputLayer::getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result) { if (!params.has(parameterName)) { - std::string message = _layerName; - message += " layer parameter does not contain "; - message += parameterName; - message += " index."; - CV_Error(Error::StsBadArg, message); + return false; } - DictValue parameter = params.get(parameterName); - if(parameter.size() != 1) - { - std::string message = parameterName; - message += " field in "; - message += _layerName; - message += " layer parameter is required"; - CV_Error(Error::StsBadArg, message); - } - - return parameter; + result = params.get(parameterName); + return true; } template T DetectionOutputLayer::getParameter(const LayerParams ¶ms, - const std::string ¶meterName, - const size_t &idx) + const std::string ¶meterName, + const size_t &idx, + const bool required, + const T& defaultValue) +{ + DictValue dictValue; + bool success = getParameterDict(params, parameterName, dictValue); + if(!success) + { + if(required) + { + std::string message = _layerName; + message += " layer parameter does not contain "; + message += parameterName; + message += " parameter."; + CV_Error(Error::StsBadArg, message); + } + else + { + return defaultValue; + } + } + return dictValue.get(idx); +} + +void DetectionOutputLayer::getCodeType(LayerParams ¶ms) { - return getParameterDict(params, parameterName).get(idx); + String codeTypeString = params.get("code_type").toLowerCase(); + if (codeTypeString == "corner") + _codeType = caffe::PriorBoxParameter_CodeType_CORNER; + else if (codeTypeString == "center_size") + _codeType = caffe::PriorBoxParameter_CodeType_CENTER_SIZE; + else + _codeType = caffe::PriorBoxParameter_CodeType_CORNER; } DetectionOutputLayer::DetectionOutputLayer(LayerParams ¶ms) : Layer(params) { - _numClasses = getParameter(params, "num_classes"); - _shareLocation = getParameter(params, "share_location"); + _numClasses = getParameter(params, "num_classes"); + _shareLocation = getParameter(params, "share_location"); _numLocClasses = _shareLocation ? 1 : _numClasses; _backgroundLabelId = getParameter(params, "background_label_id"); - _codeType = static_cast(getParameter(params, "code_type")); - _varianceEncodedInTarget = getParameter(params, "variance_encoded_in_target"); + _varianceEncodedInTarget = getParameter(params, "variance_encoded_in_target", 0, false, false); _keepTopK = getParameter(params, "keep_top_k"); - _confidenceThreshold = params.has("confidence_threshold") ? - getParameter(params, "confidence_threshold") : -FLT_MAX; + _confidenceThreshold = getParameter(params, "confidence_threshold", 0, false, -FLT_MAX); + _topK = getParameter(params, "top_k", 0, false, -1); + + getCodeType(params); // Parameters used in nms. _nmsThreshold = getParameter(params, "nms_threshold"); CV_Assert(_nmsThreshold > 0.); - - _topK = -1; - if (params.has("top_k")) - { - _topK = getParameter(params, "top_k"); - } } void DetectionOutputLayer::checkInputs(const std::vector &inputs) { - for (size_t i = 0; i < inputs.size(); i++) + for (size_t i = 1; i < inputs.size(); i++) { for (size_t j = 0; j < _numAxes; j++) { diff --git a/modules/dnn/src/layers/detection_output_layer.hpp b/modules/dnn/src/layers/detection_output_layer.hpp index ddcc03d87..0b28d69a8 100644 --- a/modules/dnn/src/layers/detection_output_layer.hpp +++ b/modules/dnn/src/layers/detection_output_layer.hpp @@ -51,7 +51,7 @@ namespace dnn { class DetectionOutputLayer : public Layer { - int _numClasses; + unsigned _numClasses; bool _shareLocation; int _numLocClasses; @@ -79,13 +79,18 @@ public: void forward(std::vector &inputs, std::vector &outputs); void checkInputs(const std::vector &inputs); + void getCodeType(LayerParams ¶ms); template - T getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx = 0); - - DictValue getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName); + T getParameter(const LayerParams ¶ms, + const std::string ¶meterName, + const size_t &idx = 0, + const bool required = true, + const T& defaultValue = T()); + + bool getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result); typedef std::map > LabelBBox; diff --git a/modules/dnn/src/layers/flatten_layer.cpp b/modules/dnn/src/layers/flatten_layer.cpp index c84912f35..4dd256a5f 100644 --- a/modules/dnn/src/layers/flatten_layer.cpp +++ b/modules/dnn/src/layers/flatten_layer.cpp @@ -52,54 +52,56 @@ namespace dnn const std::string FlattenLayer::_layerName = std::string("Flatten"); -DictValue FlattenLayer::getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName) +bool FlattenLayer::getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result) { if (!params.has(parameterName)) { - std::string message = _layerName; - message += " layer parameter does not contain "; - message += parameterName; - message += " index."; - CV_Error(Error::StsBadArg, message); + return false; } - DictValue parameter = params.get(parameterName); - if(parameter.size() != 1) - { - std::string message = parameterName; - message += " field in "; - message += _layerName; - message += " layer parameter is required"; - CV_Error(Error::StsBadArg, message); - } - - return parameter; + result = params.get(parameterName); + return true; } template T FlattenLayer::getParameter(const LayerParams ¶ms, - const std::string ¶meterName, - const size_t &idx) + const std::string ¶meterName, + const size_t &idx, + const bool required, + const T& defaultValue) { - return getParameterDict(params, parameterName).get(idx); + DictValue dictValue; + bool success = getParameterDict(params, parameterName, dictValue); + if(!success) + { + if(required) + { + std::string message = _layerName; + message += " layer parameter does not contain "; + message += parameterName; + message += " parameter."; + CV_Error(Error::StsBadArg, message); + } + else + { + return defaultValue; + } + } + return dictValue.get(idx); } FlattenLayer::FlattenLayer(LayerParams ¶ms) : Layer(params) { - _startAxis = getParameter(params, "start_axis"); - _endAxis = getParameter(params, "end_axis"); - - if(_endAxis <= 0) - { - _endAxis += _numAxes; - } + _startAxis = getParameter(params, "axis"); + _endAxis = getParameter(params, "end_axis", 0, false, -1); } void FlattenLayer::checkInputs(const std::vector &inputs) { CV_Assert(inputs.size() > 0); - for (size_t i = 0; i < inputs.size(); i++) + for (size_t i = 1; i < inputs.size(); i++) { for (size_t j = 0; j < _numAxes; j++) { @@ -112,29 +114,31 @@ void FlattenLayer::allocate(const std::vector &inputs, std::vector { checkInputs(inputs); + _numAxes = inputs[0]->shape().dims(); + if(_endAxis <= 0) + { + _endAxis += _numAxes; + } + CV_Assert(_startAxis >= 0); + CV_Assert(_endAxis >= _startAxis && _endAxis < (int)_numAxes); + size_t flattenedDimensionSize = 1; for (int i = _startAxis; i <= _endAxis; i++) { flattenedDimensionSize *= inputs[0]->shape()[i]; } - BlobShape outputShape; - size_t interval = _endAxis - _startAxis; - - // imitate flexible number of axes: make first dimension sizes = 1 - for (size_t i = 0; i < interval; i++) - { - outputShape[i] = 1; - } - for (int i = interval; i < _endAxis; i++) + std::vector outputShape; + for (int i = 0; i < _startAxis; i++) { - outputShape[i] = inputs[0]->shape()[i - interval]; + outputShape.push_back(inputs[0]->shape()[i]); } - outputShape[_endAxis] = flattenedDimensionSize; + outputShape.push_back(flattenedDimensionSize); for (size_t i = _endAxis + 1; i < _numAxes; i++) { - outputShape[i] = inputs[0]->shape()[i]; + outputShape.push_back(inputs[0]->shape()[i]); } + CV_Assert(outputShape.size() <= 4); for (size_t i = 0; i < inputs.size(); i++) { diff --git a/modules/dnn/src/layers/flatten_layer.hpp b/modules/dnn/src/layers/flatten_layer.hpp index cb7e35c8e..f940f3989 100644 --- a/modules/dnn/src/layers/flatten_layer.hpp +++ b/modules/dnn/src/layers/flatten_layer.hpp @@ -49,10 +49,10 @@ namespace dnn { class FlattenLayer : public Layer { - size_t _startAxis; + int _startAxis; int _endAxis; - static const size_t _numAxes = 4; + size_t _numAxes; static const std::string _layerName; public: @@ -63,11 +63,14 @@ public: void checkInputs(const std::vector &inputs); template - T getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx = 0); + T getParameter(const LayerParams ¶ms, + const std::string ¶meterName, + const size_t &idx = 0, + const bool required = true, + const T& defaultValue = T()); - DictValue getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName); + bool getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, DictValue &result); }; } } diff --git a/modules/dnn/src/layers/layers_common.cpp b/modules/dnn/src/layers/layers_common.cpp index 24cf842ab..d811a208a 100644 --- a/modules/dnn/src/layers/layers_common.cpp +++ b/modules/dnn/src/layers/layers_common.cpp @@ -72,20 +72,21 @@ bool getParameter(LayerParams ¶ms, const std::string& nameBase, const std::s else { if (params.has(nameAll_)) + { + parameterH = parameterW = params.get(nameAll_); + return true; + } + else { if(hasDefault) { - parameterH = parameterW = params.get(nameAll_, defaultValue); + parameterH = parameterW = defaultValue; + return true; } else { - parameterH = parameterW = params.get(nameAll_); + return false; } - return true; - } - else - { - return false; } } } diff --git a/modules/dnn/src/layers/normalize_bbox_layer.cpp b/modules/dnn/src/layers/normalize_bbox_layer.cpp index 108783c03..13a9cb2a3 100644 --- a/modules/dnn/src/layers/normalize_bbox_layer.cpp +++ b/modules/dnn/src/layers/normalize_bbox_layer.cpp @@ -54,43 +54,49 @@ namespace dnn const std::string NormalizeBBoxLayer::_layerName = std::string("NormalizeBBox"); -DictValue NormalizeBBoxLayer::getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName) +bool NormalizeBBoxLayer::getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result) { if (!params.has(parameterName)) { - std::string message = _layerName; - message += " layer parameter does not contain "; - message += parameterName; - message += " index."; - CV_Error(Error::StsBadArg, message); + return false; } - DictValue parameter = params.get(parameterName); - if(parameter.size() != 1) - { - std::string message = parameterName; - message += " field in "; - message += _layerName; - message += " layer parameter is required"; - CV_Error(Error::StsBadArg, message); - } - - return parameter; + result = params.get(parameterName); + return true; } template T NormalizeBBoxLayer::getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx) + const size_t &idx, + const bool required, + const T& defaultValue) { - return getParameterDict(params, parameterName).get(idx); + DictValue dictValue; + bool success = getParameterDict(params, parameterName, dictValue); + if(!success) + { + if(required) + { + std::string message = _layerName; + message += " layer parameter does not contain "; + message += parameterName; + message += " parameter."; + CV_Error(Error::StsBadArg, message); + } + else + { + return defaultValue; + } + } + return dictValue.get(idx); } - NormalizeBBoxLayer::NormalizeBBoxLayer(LayerParams ¶ms) : Layer(params) { - _eps = getParameter(params, "eps"); + _eps = getParameter(params, "eps", 0, false, 1e-10); _across_spatial = getParameter(params, "across_spatial"); _channel_shared = getParameter(params, "channel_shared"); } @@ -98,7 +104,7 @@ NormalizeBBoxLayer::NormalizeBBoxLayer(LayerParams ¶ms) : Layer(params) void NormalizeBBoxLayer::checkInputs(const std::vector &inputs) { CV_Assert(inputs.size() > 0); - for (size_t i = 0; i < inputs.size(); i++) + for (size_t i = 1; i < inputs.size(); i++) { for (size_t j = 0; j < _numAxes; j++) { diff --git a/modules/dnn/src/layers/normalize_bbox_layer.hpp b/modules/dnn/src/layers/normalize_bbox_layer.hpp index edac1feec..ef2fc884b 100644 --- a/modules/dnn/src/layers/normalize_bbox_layer.hpp +++ b/modules/dnn/src/layers/normalize_bbox_layer.hpp @@ -82,11 +82,15 @@ public: void checkInputs(const std::vector &inputs); template - T getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx = 0); + T getParameter(const LayerParams ¶ms, + const std::string ¶meterName, + const size_t &idx = 0, + const bool required = true, + const T& defaultValue = T()); - DictValue getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName); + bool getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result); }; } } diff --git a/modules/dnn/src/layers/prior_box_layer.cpp b/modules/dnn/src/layers/prior_box_layer.cpp index 817c97473..289f71a70 100644 --- a/modules/dnn/src/layers/prior_box_layer.cpp +++ b/modules/dnn/src/layers/prior_box_layer.cpp @@ -53,42 +53,52 @@ namespace dnn const std::string PriorBoxLayer::_layerName = std::string("PriorBox"); -DictValue PriorBoxLayer::getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName) +bool PriorBoxLayer::getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result) { if (!params.has(parameterName)) { - std::string message = _layerName; - message += " layer parameter does not contain "; - message += parameterName; - message += " index."; - CV_Error(Error::StsBadArg, message); + return false; } - DictValue parameter = params.get(parameterName); - if(parameter.size() != 1) - { - std::string message = parameterName; - message += " field in "; - message += _layerName; - message += " layer parameter is required"; - CV_Error(Error::StsBadArg, message); - } - - return parameter; + result = params.get(parameterName); + return true; } template T PriorBoxLayer::getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx) + const size_t &idx, + const bool required, + const T& defaultValue) { - return getParameterDict(params, parameterName).get(idx); + DictValue dictValue; + bool success = getParameterDict(params, parameterName, dictValue); + if(!success) + { + if(required) + { + std::string message = _layerName; + message += " layer parameter does not contain "; + message += parameterName; + message += " parameter."; + CV_Error(Error::StsBadArg, message); + } + else + { + return defaultValue; + } + } + return dictValue.get(idx); } void PriorBoxLayer::getAspectRatios(const LayerParams ¶ms) { - DictValue aspectRatioParameter = getParameterDict(params, "aspect_ratio"); + DictValue aspectRatioParameter; + bool aspectRatioRetieved = getParameterDict(params, "aspect_ratio", aspectRatioParameter); + CV_Assert(aspectRatioRetieved); + for (int i = 0; i < aspectRatioParameter.size(); ++i) { float aspectRatio = aspectRatioParameter.get(i); @@ -115,7 +125,10 @@ void PriorBoxLayer::getAspectRatios(const LayerParams ¶ms) void PriorBoxLayer::getVariance(const LayerParams ¶ms) { - DictValue varianceParameter = getParameterDict(params, "variance"); + DictValue varianceParameter; + bool varianceParameterRetrieved = getParameterDict(params, "variance", varianceParameter); + CV_Assert(varianceParameterRetrieved); + int varianceSize = varianceParameter.size(); if (varianceSize > 1) { @@ -190,12 +203,9 @@ void PriorBoxLayer::allocate(const std::vector &inputs, std::vector // 2 channels. First channel stores the mean of each prior coordinate. // Second channel stores the variance of each prior coordinate. size_t outChannels = 2; - size_t outHeight = _layerHeight; - size_t outWidth = _layerWidth * _numPriors * 4; - _outChannelSize = _layerHeight * _layerWidth * _numPriors * 4; - outputs[0].create(BlobShape(outNum, outChannels, outHeight, outWidth)); + outputs[0].create(BlobShape(outNum, outChannels, _outChannelSize)); } void PriorBoxLayer::forward(std::vector &inputs, std::vector &outputs) diff --git a/modules/dnn/src/layers/prior_box_layer.hpp b/modules/dnn/src/layers/prior_box_layer.hpp index 13938a607..e398aa165 100644 --- a/modules/dnn/src/layers/prior_box_layer.hpp +++ b/modules/dnn/src/layers/prior_box_layer.hpp @@ -83,11 +83,15 @@ public: void forward(std::vector &inputs, std::vector &outputs); template - T getParameter(const LayerParams ¶ms, const std::string ¶meterName, - const size_t &idx = 0); + T getParameter(const LayerParams ¶ms, + const std::string ¶meterName, + const size_t &idx = 0, + const bool required = true, + const T& defaultValue = T()); - DictValue getParameterDict(const LayerParams ¶ms, - const std::string ¶meterName); + bool getParameterDict(const LayerParams ¶ms, + const std::string ¶meterName, + DictValue& result); void getAspectRatios(const LayerParams ¶ms); void getVariance(const LayerParams ¶ms);