From b304730225ce7ac9dcaa4baa178bf05d5be5de2e Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 12 Jan 2022 03:46:13 +0000 Subject: [PATCH 01/19] dnn: fix API - explicit ctors, const methods --- modules/dnn/include/opencv2/dnn/dict.hpp | 14 +- modules/dnn/include/opencv2/dnn/dnn.hpp | 33 +++-- modules/dnn/src/dnn.cpp | 153 ++++++++++---------- modules/dnn/src/dnn_common.hpp | 4 +- modules/dnn/src/layers/recurrent_layers.cpp | 2 +- 5 files changed, 113 insertions(+), 93 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/dict.hpp b/modules/dnn/include/opencv2/dnn/dict.hpp index 60c2aa5ddb..fb0ef3296c 100644 --- a/modules/dnn/include/opencv2/dnn/dict.hpp +++ b/modules/dnn/include/opencv2/dnn/dict.hpp @@ -60,13 +60,13 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN struct CV_EXPORTS_W DictValue { DictValue(const DictValue &r); - DictValue(bool i) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i ? 1 : 0; } //!< Constructs integer scalar - DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i; } //!< Constructs integer scalar - CV_WRAP DictValue(int i) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i; } //!< Constructs integer scalar - DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = p; } //!< Constructs integer scalar - CV_WRAP DictValue(double p) : type(Param::REAL), pd(new AutoBuffer) { (*pd)[0] = p; } //!< Constructs floating point scalar - CV_WRAP DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer) { (*ps)[0] = s; } //!< Constructs string scalar - DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer) { (*ps)[0] = s; } //!< @overload + explicit DictValue(bool i) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i ? 1 : 0; } //!< Constructs integer scalar + explicit DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i; } //!< Constructs integer scalar + CV_WRAP explicit DictValue(int i) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = i; } //!< Constructs integer scalar + explicit DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = p; } //!< Constructs integer scalar + CV_WRAP explicit DictValue(double p) : type(Param::REAL), pd(new AutoBuffer) { (*pd)[0] = p; } //!< Constructs floating point scalar + CV_WRAP explicit DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer) { (*ps)[0] = s; } //!< Constructs string scalar + explicit DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer) { (*ps)[0] = s; } //!< @overload template static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index 34afc4147f..8af42ea4f4 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -125,7 +125,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN class BackendNode { public: - BackendNode(int backendId); + explicit BackendNode(int backendId); virtual ~BackendNode(); //!< Virtual destructor to make polymorphism. @@ -259,18 +259,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN * Each layer input and output can be labeled to easily identify them using "%[.output_name]" notation. * This method maps label of input blob to its index into input vector. */ - virtual int inputNameToIndex(String inputName); + virtual int inputNameToIndex(String inputName); // FIXIT const /** @brief Returns index of output blob in output array. * @see inputNameToIndex() */ - CV_WRAP virtual int outputNameToIndex(const String& outputName); + CV_WRAP virtual int outputNameToIndex(const String& outputName); // FIXIT const /** * @brief Ask layer if it support specific backend for doing computations. * @param[in] backendId computation backend identifier. * @see Backend */ - virtual bool supportBackend(int backendId); + virtual bool supportBackend(int backendId); // FIXIT const /** * @brief Returns Halide backend node. @@ -442,18 +442,29 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN /** @brief Converts string name of the layer to the integer identifier. * @returns id of the layer, or -1 if the layer wasn't found. */ - CV_WRAP int getLayerId(const String &layer); + CV_WRAP int getLayerId(const String &layer) const; CV_WRAP std::vector getLayerNames() const; - /** @brief Container for strings and integers. */ + /** @brief Container for strings and integers. + * + * @deprecated Use getLayerId() with int result. + */ typedef DictValue LayerId; /** @brief Returns pointer to layer with specified id or name which the network use. */ - CV_WRAP Ptr getLayer(LayerId layerId); + CV_WRAP Ptr getLayer(int layerId) const; + /** @overload + * @deprecated Use int getLayerId(const String &layer) + */ + CV_WRAP inline Ptr getLayer(const String& layerName) const { return getLayer(getLayerId(layerName)); } + /** @overload + * @deprecated to be removed + */ + CV_WRAP Ptr getLayer(const LayerId& layerId) const; /** @brief Returns pointers to input layers of specific layer. */ - std::vector > getLayerInputs(LayerId layerId); // FIXIT: CV_WRAP + std::vector > getLayerInputs(int layerId) const; // FIXIT: CV_WRAP /** @brief Connects output of the first layer to input of the second layer. * @param outPin descriptor of the first layer output. @@ -587,14 +598,16 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN * @note If shape of the new blob differs from the previous shape, * then the following forward pass may fail. */ - CV_WRAP void setParam(LayerId layer, int numParam, const Mat &blob); + CV_WRAP void setParam(int layer, int numParam, const Mat &blob); + CV_WRAP inline void setParam(const String& layerName, int numParam, const Mat &blob) { return setParam(getLayerId(layerName), numParam, blob); } /** @brief Returns parameter blob of the layer. * @param layer name or id of the layer. * @param numParam index of the layer parameter in the Layer::blobs array. * @see Layer::blobs */ - CV_WRAP Mat getParam(LayerId layer, int numParam = 0); + CV_WRAP Mat getParam(int layer, int numParam = 0) const; + CV_WRAP inline Mat getParam(const String& layerName, int numParam = 0) const { return getParam(getLayerId(layerName), numParam); } /** @brief Returns indexes of layers with unconnected outputs. */ diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 28bcde4827..4a3f6cdb60 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -846,11 +846,11 @@ public: // layer blob. int numReferences(const LayerPin& lp) { - std::map::iterator mapIt = reuseMap.find(lp); + std::map::const_iterator mapIt = reuseMap.find(lp); CV_Assert(mapIt != reuseMap.end()); LayerPin memHost = mapIt->second; - std::map::iterator refIt = refCounter.find(memHost); + std::map::const_iterator refIt = refCounter.find(memHost); CV_Assert(refIt != refCounter.end()); return refIt->second; } @@ -878,7 +878,7 @@ public: // Decrease references counter to allocated memory inside specific blob. void releaseReference(const LayerPin& lp) { - std::map::iterator mapIt = reuseMap.find(lp); + std::map::const_iterator mapIt = reuseMap.find(lp); CV_Assert(mapIt != reuseMap.end()); std::map::iterator refIt = refCounter.find(mapIt->second); @@ -902,8 +902,8 @@ public: Mat bestBlob; LayerPin bestBlobPin; - std::map::iterator hostIt; - std::map::iterator refIt; + std::map::const_iterator hostIt; + std::map::const_iterator refIt; const int targetTotal = total(shape); int bestBlobTotal = INT_MAX; @@ -915,7 +915,7 @@ public: // it might be used as output. if (refIt != refCounter.end() && refIt->second == 0) { - Mat& unusedBlob = hostIt->second; + const Mat& unusedBlob = hostIt->second; if (unusedBlob.total() >= targetTotal && unusedBlob.total() < bestBlobTotal) { @@ -1097,7 +1097,7 @@ detail::NetImplBase::NetImplBase() // nothing } -std::string detail::NetImplBase::getDumpFileNameBase() +std::string detail::NetImplBase::getDumpFileNameBase() const { std::string dumpFileNameBase = cv::format("ocv_dnn_net_%05d_%02d", networkId, networkDumpCounter++); return dumpFileNameBase; @@ -1148,7 +1148,6 @@ struct Net::Impl : public detail::NetImplBase bool fusion; bool isAsync; std::vector layersTimings; - Mat output_blob; Ptr wrap(Mat& host) { @@ -1207,7 +1206,7 @@ struct Net::Impl : public detail::NetImplBase std::vector< std::reference_wrapper > compileList; compileList.reserve(64); for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); ++it) { - LayerData &ld = it->second; + LayerData& ld = it->second; Ptr layer = ld.layerInstance; if (layer->supportBackend(DNN_BACKEND_HALIDE) && !ld.skip) { @@ -1371,19 +1370,19 @@ struct Net::Impl : public detail::NetImplBase } } - int getLayerId(const String &layerName) + int getLayerId(const String &layerName) const { - std::map::iterator it = layerNameToId.find(layerName); + std::map::const_iterator it = layerNameToId.find(layerName); return (it != layerNameToId.end()) ? it->second : -1; } - int getLayerId(int id) + int getLayerId(int id) const { - MapIdToLayerData::iterator it = layers.find(id); + MapIdToLayerData::const_iterator it = layers.find(id); return (it != layers.end()) ? id : -1; } - int getLayerId(DictValue &layerDesc) + int getLayerId(DictValue &layerDesc) const { if (layerDesc.isInt()) return getLayerId(layerDesc.get()); @@ -1394,23 +1393,23 @@ struct Net::Impl : public detail::NetImplBase return -1; } - String getLayerName(int id) + String getLayerName(int id) const { - MapIdToLayerData::iterator it = layers.find(id); + MapIdToLayerData::const_iterator it = layers.find(id); return (it != layers.end()) ? it->second.name : "(unknown layer)"; } - LayerData& getLayerData(int id) + LayerData& getLayerData(int id) const { - MapIdToLayerData::iterator it = layers.find(id); + MapIdToLayerData::const_iterator it = layers.find(id); if (it == layers.end()) CV_Error(Error::StsObjectNotFound, format("Layer with requested id=%d not found", id)); - return it->second; + return const_cast(it->second); } - LayerData& getLayerData(const String &layerName) + LayerData& getLayerData(const String &layerName) const { int id = getLayerId(layerName); @@ -1420,7 +1419,7 @@ struct Net::Impl : public detail::NetImplBase return getLayerData(id); } - LayerData& getLayerData(const DictValue &layerDesc) + LayerData& getLayerData(const DictValue &layerDesc) const { CV_Assert(layerDesc.isInt() || layerDesc.isString()); if (layerDesc.isInt()) @@ -1446,14 +1445,14 @@ struct Net::Impl : public detail::NetImplBase ld.inputBlobsId[inNum] = from; } - int resolvePinOutputName(LayerData &ld, const String &outName) + int resolvePinOutputName(LayerData &ld, const String &outName) const { if (outName.empty()) return 0; return ld.getLayerInstance()->outputNameToIndex(outName); } - LayerPin getPinByAlias(const String &layerName) + LayerPin getPinByAlias(const String &layerName) const { LayerPin pin; pin.lid = (layerName.empty()) ? 0 : getLayerId(layerName); @@ -1464,13 +1463,17 @@ struct Net::Impl : public detail::NetImplBase return pin; } - std::vector getLayerOutPins(const String &layerName) + std::vector getLayerOutPins(const String &layerName) const { int lid = (layerName.empty()) ? 0 : getLayerId(layerName); - std::vector pins; + MapIdToLayerData::const_iterator it = layers.find(lid); + if (it == layers.end()) + CV_Error_(Error::StsOutOfRange, ("Layer #%d is not valid", lid)); + const size_t nOutputs = it->second.outputBlobs.size(); - for (int i = 0; i < layers[lid].outputBlobs.size(); i++) + std::vector pins; + for (int i = 0; i < nOutputs; i++) { pins.push_back(LayerPin(lid, i)); } @@ -1920,12 +1923,11 @@ struct Net::Impl : public detail::NetImplBase CV_TRACE_FUNCTION(); CV_Assert_N(preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, haveInfEngine()); - MapIdToLayerData::iterator it; Ptr net; - for (it = layers.begin(); it != layers.end(); ++it) + for (MapIdToLayerData::const_iterator it = layers.begin(); it != layers.end(); ++it) { - LayerData &ld = it->second; + const LayerData& ld = it->second; if (ld.id == 0) { CV_Assert((netInputLayer->outNames.empty() && ld.outputBlobsWrappers.size() == 1) || @@ -1961,9 +1963,9 @@ struct Net::Impl : public detail::NetImplBase InfEngineNgraphNet& ienet = *ieNode->net; ienet.reset(); - for (it = layers.begin(); it != layers.end(); ++it) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); ++it) { - LayerData &ld = it->second; + LayerData& ld = it->second; if (ld.id == 0) { for (int i = 0; i < ld.inputBlobsWrappers.size(); ++i) @@ -2005,9 +2007,9 @@ struct Net::Impl : public detail::NetImplBase // Build Inference Engine networks from sets of layers that support this // backend. Split a whole model on several Inference Engine networks if // some of layers are not implemented. - for (it = layers.begin(); it != layers.end(); ++it) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); ++it) { - LayerData &ld = it->second; + LayerData& ld = it->second; if (ld.id == 0 && ld.skip) continue; @@ -2256,7 +2258,7 @@ struct Net::Impl : public detail::NetImplBase ld.inputLayersId.insert(ld.inputBlobsId[i].lid); //allocate parents - for (set::iterator i = ld.inputLayersId.begin(); i != ld.inputLayersId.end(); i++) + for (set::const_iterator i = ld.inputLayersId.begin(); i != ld.inputLayersId.end(); i++) allocateLayer(*i, layersShapes); //bind inputs @@ -2348,8 +2350,7 @@ struct Net::Impl : public detail::NetImplBase // we try to embed this activation into the convolution and disable separate execution of the activation std::set pinsToKeep(blobsToKeep_.begin(), blobsToKeep_.end()); - MapIdToLayerData::iterator it; - for (it = layers.begin(); it != layers.end(); it++) + for (MapIdToLayerData::const_iterator it = layers.begin(); it != layers.end(); it++) { int lid = it->first; LayerData& ld = layers[lid]; @@ -2705,8 +2706,7 @@ struct Net::Impl : public detail::NetImplBase { CV_TRACE_FUNCTION(); - MapIdToLayerData::iterator it; - for (it = layers.begin(); it != layers.end(); it++) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); it++) it->second.flag = 0; CV_Assert(!layers[0].outputBlobs.empty()); @@ -2730,7 +2730,7 @@ struct Net::Impl : public detail::NetImplBase // Fake references to input blobs. for (int i = 0; i < layers[0].outputBlobs.size(); ++i) blobManager.addReference(LayerPin(0, i)); - for (it = layers.begin(); it != layers.end(); ++it) + for (MapIdToLayerData::const_iterator it = layers.begin(); it != layers.end(); ++it) { const LayerData& ld = it->second; blobManager.addReferences(ld.inputBlobsId); @@ -2741,7 +2741,7 @@ struct Net::Impl : public detail::NetImplBase blobManager.addReference(blobsToKeep_[i]); } - for (it = layers.begin(); it != layers.end(); it++) + for (MapIdToLayerData::const_iterator it = layers.begin(); it != layers.end(); it++) { int lid = it->first; allocateLayer(lid, layersShapes); @@ -2762,7 +2762,7 @@ struct Net::Impl : public detail::NetImplBase TickMeter tm; tm.start(); - std::map >::iterator it = ld.backendNodes.find(preferableBackend); + std::map >::const_iterator it = ld.backendNodes.find(preferableBackend); if (preferableBackend == DNN_BACKEND_OPENCV || it == ld.backendNodes.end() || it->second.empty()) { if (isAsync) @@ -2959,8 +2959,7 @@ struct Net::Impl : public detail::NetImplBase if (clearFlags) { - MapIdToLayerData::iterator it; - for (it = layers.begin(); it != layers.end(); it++) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); it++) it->second.flag = 0; } @@ -2969,8 +2968,7 @@ struct Net::Impl : public detail::NetImplBase return; //forward parents - MapIdToLayerData::iterator it; - for (it = layers.begin(); it != layers.end() && (it->second.id < ld.id); ++it) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end() && (it->second.id < ld.id); ++it) { LayerData &ld = it->second; if (ld.flag) @@ -3032,7 +3030,7 @@ struct Net::Impl : public detail::NetImplBase for(int i = 0; i < inputLayerIds.size(); i++) { int layerId = inputLayerIds[i].lid; - LayersShapesMap::iterator it = + LayersShapesMap::const_iterator it = inOutShapes.find(layerId); if(it == inOutShapes.end() || it->second.out.empty()) @@ -3115,7 +3113,7 @@ struct Net::Impl : public detail::NetImplBase inOutShapes.clear(); inOutShapes[0].in = netInputShapes; //insert shape for first input layer - for (MapIdToLayerData::iterator it = layers.begin(); + for (MapIdToLayerData::const_iterator it = layers.begin(); it != layers.end(); it++) { getLayerShapesRecursively(it->first, inOutShapes); @@ -3155,12 +3153,11 @@ struct Net::Impl : public detail::NetImplBase CV_LOG_DEBUG(NULL, toString(inputShapes, "Network input shapes")); LayersShapesMap layersShapes; layersShapes[0].in = inputShapes; - for (MapIdToLayerData::iterator it = layers.begin(); - it != layers.end(); it++) + for (MapIdToLayerData::iterator it = layers.begin(); it != layers.end(); it++) { int layerId = it->first; LayerData& layerData = it->second; - std::vector& inputLayerIds = layerData.inputBlobsId; + const std::vector& inputLayerIds = layerData.inputBlobsId; LayerShapes& layerShapes = layersShapes[layerId]; CV_LOG_DEBUG(NULL, "layer " << layerId << ": [" << layerData.type << "]:(" << layerData.name << ") with inputs.size=" << inputLayerIds.size()); if (layerShapes.in.empty()) @@ -3170,7 +3167,7 @@ struct Net::Impl : public detail::NetImplBase const LayerPin& inputPin = inputLayerIds[i]; int inputLayerId = inputPin.lid; CV_LOG_DEBUG(NULL, " input[" << i << "] " << inputLayerId << ":" << inputPin.oid << " as [" << layers[inputLayerId].type << "]:(" << layers[inputLayerId].name << ")"); - LayersShapesMap::iterator inputIt = layersShapes.find(inputLayerId); + LayersShapesMap::const_iterator inputIt = layersShapes.find(inputLayerId); if (inputIt == layersShapes.end() || inputIt->second.out.empty()) { getLayerShapesRecursively(inputLayerId, layersShapes); @@ -3187,19 +3184,23 @@ struct Net::Impl : public detail::NetImplBase CV_LOG_DEBUG(NULL, "updateLayersShapes() - DONE"); } - LayerPin getLatestLayerPin(const std::vector& pins) + LayerPin getLatestLayerPin(const std::vector& pins) const { return *std::max_element(pins.begin(), pins.end()); } - Mat getBlob(const LayerPin& pin) + Mat getBlob(const LayerPin& pin) const { CV_TRACE_FUNCTION(); if (!pin.valid()) CV_Error(Error::StsObjectNotFound, "Requested blob not found"); - LayerData &ld = layers[pin.lid]; + MapIdToLayerData::const_iterator it = layers.find(pin.lid); + if (it == layers.end()) + CV_Error_(Error::StsOutOfRange, ("Layer #%d is not valid (output #%d requested)", pin.lid, pin.oid)); + + const LayerData &ld = it->second; if ((size_t)pin.oid >= ld.outputBlobs.size()) { CV_Error(Error::StsOutOfRange, format("Layer \"%s\" produce only %d outputs, " @@ -3215,6 +3216,7 @@ struct Net::Impl : public detail::NetImplBase if (ld.outputBlobs[pin.oid].depth() == CV_16S) { + Mat output_blob; convertFp16(ld.outputBlobs[pin.oid], output_blob); return output_blob; } @@ -3222,7 +3224,7 @@ struct Net::Impl : public detail::NetImplBase return ld.outputBlobs[pin.oid]; } - Mat getBlob(String outputName) + Mat getBlob(String outputName) const { return getBlob(getPinByAlias(outputName)); } @@ -3282,9 +3284,9 @@ struct Net::Impl : public detail::NetImplBase Net createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNet); #endif - string dump(); + string dump() const; - void dumpNetworkToFile() + void dumpNetworkToFile() const { #ifndef OPENCV_DNN_DISABLE_NETWORK_AUTO_DUMP string dumpFileNameBase = getDumpFileNameBase(); @@ -3945,7 +3947,7 @@ void Net::setInput(InputArray blob, const String& name, double scalefactor, cons impl->netWasAllocated = impl->netWasAllocated && oldShape; } -Mat Net::getParam(LayerId layer, int numParam) +Mat Net::getParam(int layer, int numParam) const { LayerData &ld = impl->getLayerData(layer); std::vector &layerBlobs = ld.getLayerInstance()->blobs; @@ -3953,7 +3955,7 @@ Mat Net::getParam(LayerId layer, int numParam) return layerBlobs[numParam]; } -void Net::setParam(LayerId layer, int numParam, const Mat &blob) +void Net::setParam(int layer, int numParam, const Mat &blob) { LayerData &ld = impl->getLayerData(layer); @@ -3963,7 +3965,7 @@ void Net::setParam(LayerId layer, int numParam, const Mat &blob) layerBlobs[numParam] = blob; } -int Net::getLayerId(const String &layer) +int Net::getLayerId(const String &layer) const { return impl->getLayerId(layer); } @@ -4006,7 +4008,7 @@ String Net::dump() return impl->dump(); } -string Net::Impl::dump() +string Net::Impl::dump() const { bool hasInput = !netInputLayer->inputsData.empty(); @@ -4266,13 +4268,18 @@ void Net::dumpToFile(const String& path) { file.close(); } -Ptr Net::getLayer(LayerId layerId) +Ptr Net::getLayer(int layerId) const +{ + LayerData &ld = impl->getLayerData(layerId); + return ld.getLayerInstance(); +} +Ptr Net::getLayer(const LayerId& layerId) const { LayerData &ld = impl->getLayerData(layerId); return ld.getLayerInstance(); } -std::vector > Net::getLayerInputs(LayerId layerId) +std::vector > Net::getLayerInputs(int layerId) const { LayerData &ld = impl->getLayerData(layerId); @@ -4291,7 +4298,7 @@ std::vector Net::getLayerNames() const std::vector res; res.reserve(impl->layers.size()); - Impl::MapIdToLayerData::iterator it; + Impl::MapIdToLayerData::const_iterator it; for (it = impl->layers.begin(); it != impl->layers.end(); it++) { if (it->second.id) //skip Data layer @@ -4310,11 +4317,11 @@ std::vector Net::getUnconnectedOutLayers() const { std::vector layersIds; - Impl::MapIdToLayerData::iterator it; + Impl::MapIdToLayerData::const_iterator it; for (it = impl->layers.begin(); it != impl->layers.end(); it++) { int lid = it->first; - LayerData &ld = it->second; + const LayerData &ld = it->second; if (ld.requiredOutputs.size() == 0) layersIds.push_back(lid); @@ -4414,13 +4421,13 @@ int64 Net::getFLOPS(const MatShape& netInputShape) const int64 Net::getFLOPS(const int layerId, const std::vector& netInputShapes) const { - Impl::MapIdToLayerData::iterator layer = impl->layers.find(layerId); + Impl::MapIdToLayerData::const_iterator layer = impl->layers.find(layerId); CV_Assert(layer != impl->layers.end()); LayerShapes shapes; impl->getLayerShapes(netInputShapes, layerId, shapes); - return layer->second.getLayerInstance()->getFLOPS(shapes.in, shapes.out); + return const_cast(layer->second).getLayerInstance()->getFLOPS(shapes.in, shapes.out); } int64 Net::getFLOPS(const int layerId, @@ -4434,7 +4441,7 @@ void Net::getLayerTypes(std::vector& layersTypes) const layersTypes.clear(); std::map layers; - for (Impl::MapIdToLayerData::iterator it = impl->layers.begin(); + for (Impl::MapIdToLayerData::const_iterator it = impl->layers.begin(); it != impl->layers.end(); it++) { if (layers.find(it->second.type) == layers.end()) @@ -4442,7 +4449,7 @@ void Net::getLayerTypes(std::vector& layersTypes) const layers[it->second.type]++; } - for (std::map::iterator it = layers.begin(); + for (std::map::const_iterator it = layers.begin(); it != layers.end(); it++) { layersTypes.push_back(it->first); @@ -4452,7 +4459,7 @@ void Net::getLayerTypes(std::vector& layersTypes) const int Net::getLayersCount(const String& layerType) const { int count = 0; - for (Impl::MapIdToLayerData::iterator it = impl->layers.begin(); + for (Impl::MapIdToLayerData::const_iterator it = impl->layers.begin(); it != impl->layers.end(); it++) { if (it->second.type == layerType) @@ -4467,7 +4474,7 @@ void Net::getMemoryConsumption(const int layerId, { CV_TRACE_FUNCTION(); - Impl::MapIdToLayerData::iterator layer = impl->layers.find(layerId); + Impl::MapIdToLayerData::const_iterator layer = impl->layers.find(layerId); CV_Assert(layer != impl->layers.end()); weights = blobs = 0; @@ -4535,7 +4542,7 @@ void Net::getMemoryConsumption(const std::vector& netInputShapes, for(int i = 0; i < layerIds.size(); i++) { int w = 0, b = 0; - Impl::MapIdToLayerData::iterator layer = impl->layers.find(layerIds[i]); + Impl::MapIdToLayerData::const_iterator layer = impl->layers.find(layerIds[i]); CV_Assert(layer != impl->layers.end()); for(int j = 0; j < layer->second.params.blobs.size(); j++) diff --git a/modules/dnn/src/dnn_common.hpp b/modules/dnn/src/dnn_common.hpp index 7360031801..572de180e9 100644 --- a/modules/dnn/src/dnn_common.hpp +++ b/modules/dnn/src/dnn_common.hpp @@ -19,12 +19,12 @@ namespace detail { struct NetImplBase { const int networkId; // network global identifier - int networkDumpCounter; // dump counter + mutable int networkDumpCounter; // dump counter int dumpLevel; // level of information dumps (initialized through OPENCV_DNN_NETWORK_DUMP parameter) NetImplBase(); - std::string getDumpFileNameBase(); + std::string getDumpFileNameBase() const; }; } // namespace detail diff --git a/modules/dnn/src/layers/recurrent_layers.cpp b/modules/dnn/src/layers/recurrent_layers.cpp index 21dafa142d..f37498ed09 100644 --- a/modules/dnn/src/layers/recurrent_layers.cpp +++ b/modules/dnn/src/layers/recurrent_layers.cpp @@ -184,7 +184,7 @@ public: CV_Assert(!reverse || !bidirectional); // read activations - DictValue activations = params.get("activations", ""); + DictValue activations = params.get("activations", DictValue(String())); if (activations.size() == 1) // if activations wasn't specified use default { f_activation = sigmoid; From eca2d92791a0a697df9f459a23625e8f4758c71f Mon Sep 17 00:00:00 2001 From: Vadim Levin Date: Wed, 19 Jan 2022 00:07:50 +0300 Subject: [PATCH 02/19] fix: submodules creation and registration - Add special case handling when submodule has the same name as parent - `PyDict_SetItemString` doesn't steal reference, so reference count should be explicitly decremented to transfer object life-time ownership - Add sanity checks for module registration input --- .../include/opencv2/core/bindings_utils.hpp | 6 + modules/python/src2/cv2.cpp | 199 ++++++++++++++---- modules/python/test/test_misc.py | 15 ++ 3 files changed, 184 insertions(+), 36 deletions(-) diff --git a/modules/core/include/opencv2/core/bindings_utils.hpp b/modules/core/include/opencv2/core/bindings_utils.hpp index 67efdcdac2..f091606c4a 100644 --- a/modules/core/include/opencv2/core/bindings_utils.hpp +++ b/modules/core/include/opencv2/core/bindings_utils.hpp @@ -213,6 +213,12 @@ AsyncArray testAsyncException() return p.getArrayResult(); } +namespace nested { +CV_WRAP static inline bool testEchoBooleanFunction(bool flag) { + return flag; +} +} // namespace nested + //! @} // core_utils } // namespace cv::utils diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 3241b4f5e8..d4f1a5aa3a 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -2100,45 +2100,151 @@ struct ConstDef long long val; }; -static void init_submodule(PyObject * root, const char * name, PyMethodDef * methods, ConstDef * consts) -{ - // traverse and create nested submodules - std::string s = name; - size_t i = s.find('.'); - while (i < s.length() && i != std::string::npos) - { - size_t j = s.find('.', i); - if (j == std::string::npos) - j = s.length(); - std::string short_name = s.substr(i, j-i); - std::string full_name = s.substr(0, j); - i = j+1; +static inline bool strStartsWith(const std::string& str, const std::string& prefix) { + return prefix.empty() || \ + (str.size() >= prefix.size() && std::memcmp(str.data(), prefix.data(), prefix.size()) == 0); +} - PyObject * d = PyModule_GetDict(root); - PyObject * submod = PyDict_GetItemString(d, short_name.c_str()); - if (submod == NULL) +static inline bool strEndsWith(const std::string& str, char symbol) { + return !str.empty() && str[str.size() - 1] == symbol; +} + +/** + * \brief Creates a submodule of the `root`. Missing parents submodules + * are created as needed. If name equals to parent module name than + * borrowed reference to parent module is returned (no reference counting + * are done). + * Submodule lifetime is managed by the parent module. + * If nested submodules are created than the lifetime is managed by the + * predecessor submodule in a list. + * + * \param parent_module Parent module object. + * \param name Submodule name. + * \return borrowed reference to the created submodule. + * If any of submodules can't be created than NULL is returned. + */ +static PyObject* createSubmodule(PyObject* parent_module, const std::string& name) +{ + if (!parent_module) { - submod = PyImport_AddModule(full_name.c_str()); - PyDict_SetItemString(d, short_name.c_str(), submod); + return PyErr_Format(PyExc_ImportError, + "Bindings generation error. " + "Parent module is NULL during the submodule '%s' creation", + name.c_str() + ); + } + if (strEndsWith(name, '.')) + { + return PyErr_Format(PyExc_ImportError, + "Bindings generation error. " + "Submodule can't end with a dot. Got: %s", name.c_str() + ); } - if (short_name != "") - root = submod; - } + const std::string parent_name = PyModule_GetName(parent_module); - // populate module's dict - PyObject * d = PyModule_GetDict(root); - for (PyMethodDef * m = methods; m->ml_name != NULL; ++m) - { - PyObject * method_obj = PyCFunction_NewEx(m, NULL, NULL); - PyDict_SetItemString(d, m->ml_name, method_obj); - Py_DECREF(method_obj); - } - for (ConstDef * c = consts; c->name != NULL; ++c) - { - PyDict_SetItemString(d, c->name, PyLong_FromLongLong(c->val)); - } + /// Special case handling when caller tries to register a submodule of the parent module with + /// the same name + if (name == parent_name) { + return parent_module; + } + if (!strStartsWith(name, parent_name)) + { + return PyErr_Format(PyExc_ImportError, + "Bindings generation error. " + "Submodule name should always start with a parent module name. " + "Parent name: %s. Submodule name: %s", parent_name.c_str(), + name.c_str() + ); + } + + size_t submodule_name_end = name.find('.', parent_name.size() + 1); + /// There is no intermediate submodules in the provided name + if (submodule_name_end == std::string::npos) + { + submodule_name_end = name.size(); + } + + PyObject* submodule = parent_module; + + for (size_t submodule_name_start = parent_name.size() + 1; + submodule_name_start < name.size(); ) + { + const std::string submodule_name = name.substr(submodule_name_start, + submodule_name_end - submodule_name_start); + + const std::string full_submodule_name = name.substr(0, submodule_name_end); + + + PyObject* parent_module_dict = PyModule_GetDict(submodule); + /// If submodule already exists it can be found in the parent module dictionary, + /// otherwise it should be added to it. + submodule = PyDict_GetItemString(parent_module_dict, + submodule_name.c_str()); + if (!submodule) + { + submodule = PyImport_AddModule(full_submodule_name.c_str()); + if (PyDict_SetItemString(parent_module_dict, submodule_name.c_str(), submodule) < 0) { + Py_CLEAR(submodule); + return PyErr_Format(PyExc_ImportError, + "Can't register a submodule '%s' (full name: '%s')", + submodule_name.c_str(), full_submodule_name.c_str() + ); + } + /// PyDict_SetItemString doesn't steal a reference so the reference counter + /// of the submodule should be decremented to bind submodule lifetime to the + /// parent module + Py_DECREF(submodule); + } + + submodule_name_start = submodule_name_end + 1; + + submodule_name_end = name.find('.', submodule_name_start); + if (submodule_name_end == std::string::npos) { + submodule_name_end = name.size(); + } + } + return submodule; +} + +static bool init_submodule(PyObject * root, const char * name, PyMethodDef * methods, ConstDef * consts) +{ + // traverse and create nested submodules + PyObject* submodule = createSubmodule(root, name); + if (!submodule) + { + return false; + } + // populate module's dict + PyObject * d = PyModule_GetDict(submodule); + for (PyMethodDef * m = methods; m->ml_name != NULL; ++m) + { + PyObject * method_obj = PyCFunction_NewEx(m, NULL, NULL); + if (PyDict_SetItemString(d, m->ml_name, method_obj) < 0) + { + PyErr_Format(PyExc_ImportError, + "Can't register function %s in module: %s", m->ml_name, name + ); + Py_CLEAR(method_obj); + return false; + } + Py_DECREF(method_obj); + } + for (ConstDef * c = consts; c->name != NULL; ++c) + { + PyObject* const_obj = PyLong_FromLongLong(c->val); + if (PyDict_SetItemString(d, c->name, const_obj) < 0) + { + PyErr_Format(PyExc_ImportError, + "Can't register constant %s in module %s", c->name, name + ); + Py_CLEAR(const_obj); + return false; + } + Py_DECREF(const_obj); + } + return true; } #include "pyopencv_generated_modules_content.h" @@ -2146,7 +2252,10 @@ static void init_submodule(PyObject * root, const char * name, PyMethodDef * met static bool init_body(PyObject * m) { #define CVPY_MODULE(NAMESTR, NAME) \ - init_submodule(m, MODULESTR NAMESTR, methods_##NAME, consts_##NAME) + if (!init_submodule(m, MODULESTR NAMESTR, methods_##NAME, consts_##NAME)) \ + { \ + return false; \ + } #include "pyopencv_generated_modules.h" #undef CVPY_MODULE @@ -2163,7 +2272,13 @@ static bool init_body(PyObject * m) PyObject* d = PyModule_GetDict(m); - PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION)); + PyObject* version_obj = PyString_FromString(CV_VERSION); + if (PyDict_SetItemString(d, "__version__", version_obj) < 0) { + PyErr_SetString(PyExc_ImportError, "Can't update module version"); + Py_CLEAR(version_obj); + return false; + } + Py_DECREF(version_obj); PyObject *opencv_error_dict = PyDict_New(); PyDict_SetItemString(opencv_error_dict, "file", Py_None); @@ -2177,7 +2292,18 @@ static bool init_body(PyObject * m) PyDict_SetItemString(d, "error", opencv_error); -#define PUBLISH(I) PyDict_SetItemString(d, #I, PyInt_FromLong(I)) +#define PUBLISH_(I, var_name, type_obj) \ + PyObject* type_obj = PyInt_FromLong(I); \ + if (PyDict_SetItemString(d, var_name, type_obj) < 0) \ + { \ + PyErr_SetString(PyExc_ImportError, "Can't register " var_name " constant"); \ + Py_CLEAR(type_obj); \ + return false; \ + } \ + Py_DECREF(type_obj); + +#define PUBLISH(I) PUBLISH_(I, #I, I ## _obj) + PUBLISH(CV_8U); PUBLISH(CV_8UC1); PUBLISH(CV_8UC2); @@ -2213,6 +2339,7 @@ static bool init_body(PyObject * m) PUBLISH(CV_64FC2); PUBLISH(CV_64FC3); PUBLISH(CV_64FC4); +#undef PUBLISH_ #undef PUBLISH return true; diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index de7af1d350..f9e75e8fe4 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from __future__ import print_function +import sys import ctypes from functools import partial from collections import namedtuple @@ -596,6 +597,20 @@ class Arguments(NewOpenCVTests): self.assertTrue(isinstance(rr, tuple), msg=type(rrv)) self.assertEqual(len(rr), 3) + def test_nested_function_availability(self): + self.assertTrue(hasattr(cv.utils, "nested"), + msg="Module is not generated for nested namespace") + self.assertTrue(hasattr(cv.utils.nested, "testEchoBooleanFunction"), + msg="Function in nested module is not available") + self.assertEqual(sys.getrefcount(cv.utils.nested), 2, + msg="Nested submodule lifetime should be managed by " + "the parent module so the reference count should be " + "2, because `getrefcount` temporary increases it.") + for flag in (True, False): + self.assertEqual(flag, cv.utils.nested.testEchoBooleanFunction(flag), + msg="Function in nested module returns wrong result") + + class SamplesFindFile(NewOpenCVTests): def test_ExistedFile(self): From 82818e7324fc212f37960c3c1e7e1ab99d4c8cc2 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 20 Jan 2022 02:27:57 +0000 Subject: [PATCH 03/19] cmake(highgui): update handling of OpenGL libraries --- modules/highgui/CMakeLists.txt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt index 416fb5a315..b2ed5f9f4e 100644 --- a/modules/highgui/CMakeLists.txt +++ b/modules/highgui/CMakeLists.txt @@ -67,6 +67,9 @@ if(HAVE_QT) set(qt_deps Core Gui Widgets Test Concurrent) if(HAVE_QT_OPENGL) list(APPEND qt_deps OpenGL) + if(OPENGL_LIBRARIES) + list(APPEND HIGHGUI_LIBRARIES "${OPENGL_LIBRARIES}") + endif() endif() foreach(dt_dep ${qt_deps}) @@ -76,8 +79,11 @@ if(HAVE_QT) endforeach() else() ocv_assert(QT_VERSION_MAJOR EQUAL 4) - if (HAVE_QT_OPENGL) + if(HAVE_QT_OPENGL) set(QT_USE_QTOPENGL TRUE) + if(OPENGL_LIBRARIES) + list(APPEND HIGHGUI_LIBRARIES "${OPENGL_LIBRARIES}") + endif() endif() include(${QT_USE_FILE}) @@ -130,6 +136,9 @@ elseif(HAVE_WIN32UI) if(OpenCV_ARCH STREQUAL "ARM64") list(APPEND HIGHGUI_LIBRARIES "comdlg32" "advapi32") endif() + if(HAVE_OPENGL AND OPENGL_LIBRARIES) + list(APPEND HIGHGUI_LIBRARIES "${OPENGL_LIBRARIES}") + endif() elseif(HAVE_GTK OR HAVE_GTK3) list(APPEND highgui_srcs ${CMAKE_CURRENT_LIST_DIR}/src/window_gtk.cpp) elseif(HAVE_CARBON) @@ -163,10 +172,6 @@ if(APPLE) add_apple_compiler_options(${the_module}) endif() -if(HAVE_WIN32UI AND HAVE_OPENGL AND OPENGL_LIBRARIES) - ocv_target_link_libraries(${the_module} PRIVATE "${OPENGL_LIBRARIES}") -endif() - if(MSVC AND NOT BUILD_SHARED_LIBS AND BUILD_WITH_STATIC_CRT) set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /NODEFAULTLIB:libcmt.lib /DEBUG") endif() From 51e8af9e5f02fb30170fc90ca4e7302038308c3d Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sun, 26 Dec 2021 09:47:25 +0000 Subject: [PATCH 04/19] cmake(link): add '-Wl,--no-undefined' - avoid missing of necessary library dependencies --- cmake/OpenCVCompilerOptions.cmake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmake/OpenCVCompilerOptions.cmake b/cmake/OpenCVCompilerOptions.cmake index d444085ea3..90f1896c30 100644 --- a/cmake/OpenCVCompilerOptions.cmake +++ b/cmake/OpenCVCompilerOptions.cmake @@ -360,6 +360,22 @@ if(NOT OPENCV_SKIP_LINK_AS_NEEDED) endif() endif() +# Apply "-Wl,--no-undefined" linker flags: https://github.com/opencv/opencv/pull/21347 +if(NOT OPENCV_SKIP_LINK_NO_UNDEFINED) + if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2")) + set(_option "-Wl,--no-undefined") + set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056 + ocv_check_compiler_flag(CXX "" HAVE_LINK_NO_UNDEFINED) + set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}") + if(HAVE_LINK_NO_UNDEFINED) + set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}") + set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}") + set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} ${_option}") + endif() + endif() +endif() + # combine all "extra" options if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}") From 17b2d92a3d71afbe04982776c34d1bb88fea2112 Mon Sep 17 00:00:00 2001 From: Smirnov Egor Date: Fri, 21 Jan 2022 12:31:46 +0300 Subject: [PATCH 05/19] add optional outputs support and fix graph links --- modules/dnn/src/onnx/onnx_importer.cpp | 82 ++++++++++++++++++-------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 47ec830313..736f3a27de 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -455,7 +455,11 @@ void ONNXImporter::addLayer(LayerParams& layerParams, int id = dstNet.addLayer(layerParams.name, layerParams.type, layerParams); for (int i = 0; i < node_proto.output_size(); ++i) { - layer_id.insert(std::make_pair(node_proto.output(i), LayerInfo(id, i))); + const std::string& output_name = node_proto.output(i); + if (!output_name.empty()) + { + layer_id.insert(std::make_pair(output_name, LayerInfo(id, i))); + } } std::vector layerInpShapes, layerOutShapes, layerInternalShapes; @@ -478,7 +482,11 @@ void ONNXImporter::addLayer(LayerParams& layerParams, layer->getMemoryShapes(layerInpShapes, 0, layerOutShapes, layerInternalShapes); for (int i = 0; i < node_proto.output_size() && i < (int)layerOutShapes.size(); ++i) { - outShapes[node_proto.output(i)] = layerOutShapes[i]; + const std::string& output_name = node_proto.output(i); + if (!output_name.empty()) + { + outShapes[node_proto.output(i)] = layerOutShapes[i]; + } } } @@ -678,10 +686,30 @@ void ONNXImporter::populateNet() CV_LOG_DEBUG(NULL, "DNN/ONNX: import completed!"); } +const std::string& extractNodeName(const opencv_onnx::NodeProto& node_proto) +{ + if (node_proto.has_name() && !node_proto.name().empty()) + { + return node_proto.name(); + } + for (int i = 0; i < node_proto.output_size(); ++i) + { + const std::string& name = node_proto.output(i); + // There are two ways to leave an optional input or output unspecified: + // the first, available only for trailing inputs and outputs, is to simply not provide that input; + // the second method is to use an empty string in place of an input or output name. + if (!name.empty()) + { + return name; + } + } + CV_Error(Error::StsAssert, "Couldn't deduce Node name."); +} + void ONNXImporter::handleNode(const opencv_onnx::NodeProto& node_proto) { CV_Assert(node_proto.output_size() >= 1); - std::string name = node_proto.output(0); + const std::string& name = extractNodeName(node_proto); const std::string& layer_type = node_proto.op_type(); const std::string& layer_type_domain = node_proto.has_domain() ? node_proto.domain() : std::string(); if (!layer_type_domain.empty() && layer_type_domain != "ai.onnx") @@ -802,6 +830,7 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node { opencv_onnx::NodeProto node_proto = node_proto_; const std::string& layer_type = node_proto.op_type(); + const std::string output_name = node_proto.output(0); CV_Assert(node_proto.input_size() == 1); layerParams.type = "Pooling"; @@ -922,7 +951,7 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node layerParams.set("dim", DictValue::arrayInt(&targetShape[0], targetShape.size())); node_proto.set_input(0, node_proto.output(0)); - node_proto.set_output(0, layerParams.name); + node_proto.set_output(0, output_name); } else if (!layerParams.has("axes") && (layer_type == "ReduceMean" || layer_type == "ReduceSum" || layer_type == "ReduceMax")) { @@ -955,7 +984,7 @@ void ONNXImporter::parseReduce(LayerParams& layerParams, const opencv_onnx::Node layerParams.set("dim", DictValue::arrayInt(targetShape.data(), targetShape.size())); node_proto.set_input(0, node_proto.output(0)); - node_proto.set_output(0, layerParams.name); + node_proto.set_output(0, output_name); } addLayer(layerParams, node_proto); } @@ -1045,7 +1074,7 @@ void ONNXImporter::parseSlice(LayerParams& layerParams, const opencv_onnx::NodeP { Mat flipped; flip(inp, flipped, 0); - addConstant(layerParams.name, flipped); + addConstant(node_proto.output(0), flipped); return; } } @@ -1065,7 +1094,7 @@ void ONNXImporter::parseSlice(LayerParams& layerParams, const opencv_onnx::NodeP inputs.push_back(inp); runLayer(layerParams, inputs, sliced); CV_Assert(sliced.size() == 1); - addConstant(layerParams.name, sliced[0]); + addConstant(node_proto.output(0), sliced[0]); return; } addLayer(layerParams, node_proto); @@ -1130,7 +1159,7 @@ void ONNXImporter::parseBias(LayerParams& layerParams, const opencv_onnx::NodePr Mat blob_1 = getBlob(node_proto, 1); CV_Assert(blob_0.size == blob_1.size); Mat output = isSub ? (blob_0 - blob_1) : (blob_0 + blob_1); - addConstant(layerParams.name, output); + addConstant(node_proto.output(0), output); return; } else if (is_const_0 || is_const_1) @@ -1244,12 +1273,13 @@ void ONNXImporter::parseConstant(LayerParams& layerParams, const opencv_onnx::No { CV_Assert(node_proto.input_size() == 0); CV_Assert(layerParams.blobs.size() == 1); - addConstant(layerParams.name, layerParams.blobs[0]); + addConstant(node_proto.output(0), layerParams.blobs[0]); } void ONNXImporter::parseLSTM(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto_) { opencv_onnx::NodeProto node_proto = node_proto_; + const std::string output_name = node_proto.output(0); LayerParams lstmParams = layerParams; lstmParams.name += "/lstm"; @@ -1331,7 +1361,7 @@ void ONNXImporter::parseLSTM(LayerParams& layerParams, const opencv_onnx::NodePr layerParams.type = "Reshape"; layerParams.set("dim", DictValue::arrayInt(&lstmShape[0], lstmShape.size())); node_proto.set_input(0, lstmParams.name); // redirect input to LSTM - node_proto.set_output(0, layerParams.name); // keep origin LSTM's name + node_proto.set_output(0, output_name); // keep origin LSTM's name addLayer(layerParams, node_proto); } @@ -1573,6 +1603,7 @@ void ONNXImporter::parseMul(LayerParams& layerParams, const opencv_onnx::NodePro { opencv_onnx::NodeProto node_proto = node_proto_; const std::string& layer_type = node_proto.op_type(); + const std::string output_name = node_proto.output(0); CV_Assert(node_proto.input_size() == 2); bool isDiv = layer_type == "Div"; @@ -1657,7 +1688,7 @@ void ONNXImporter::parseMul(LayerParams& layerParams, const opencv_onnx::NodePro if (inp0.dims == 1 && inp1.dims == 1) out.dims = 1; // to workaround dims == 1 - addConstant(layerParams.name, out); + addConstant(output_name, out); return; } else if (outShapes[node_proto.input(0)] == outShapes[node_proto.input(1)]) @@ -1673,7 +1704,7 @@ void ONNXImporter::parseMul(LayerParams& layerParams, const opencv_onnx::NodePro opencv_onnx::NodeProto proto; proto.add_input(node_proto.input(1)); proto.add_input(node_proto.input(0)); - proto.add_output(layerParams.name); + proto.add_output(output_name); node_proto = proto; } @@ -1851,7 +1882,7 @@ void ONNXImporter::parseTranspose(LayerParams& layerParams, const opencv_onnx::N std::vector inputs(1, getBlob(node_proto, 0)), transposed; runLayer(layerParams, inputs, transposed); CV_Assert(transposed.size() == 1); - addConstant(layerParams.name, transposed[0]); + addConstant(node_proto.output(0), transposed[0]); return; } addLayer(layerParams, node_proto); @@ -1903,7 +1934,7 @@ void ONNXImporter::parseSqueeze(LayerParams& layerParams, const opencv_onnx::Nod Mat inp = getBlob(node_proto, 0); Mat out = inp.reshape(1, outShape); out.dims = outShape.size(); // to workaround dims == 1 - addConstant(layerParams.name, out); + addConstant(node_proto.output(0), out); return; } addLayer(layerParams, node_proto); @@ -1930,7 +1961,7 @@ void ONNXImporter::parseFlatten(LayerParams& layerParams, const opencv_onnx::Nod } Mat output = input.reshape(1, 2, out_size); - addConstant(layerParams.name, output); + addConstant(node_proto.output(0), output); return; } IterShape_t shapeIt = outShapes.find(node_proto.input(0)); @@ -2002,7 +2033,7 @@ void ONNXImporter::parseUnsqueeze(LayerParams& layerParams, const opencv_onnx::N } Mat out = input.reshape(0, dims); - addConstant(layerParams.name, out); + addConstant(node_proto.output(0), out); return; } @@ -2039,6 +2070,7 @@ void ONNXImporter::parseExpand(LayerParams& layerParams, const opencv_onnx::Node CV_CheckEQ(node_proto.input_size(), 2, ""); const std::string& input0 = node_proto.input(0); const std::string& input1 = node_proto.input(1); + const std::string output_name = node_proto.output(0); Mat newShapeMat = getBlob(input1); MatShape targetShape(newShapeMat.ptr(), newShapeMat.ptr() + newShapeMat.total()); @@ -2108,7 +2140,7 @@ void ONNXImporter::parseExpand(LayerParams& layerParams, const opencv_onnx::Node input = input.reshape(0, total(inpShape, 0, broadcast_axes[0])); Mat output = cv::repeat(input, 1, targetShape[broadcast_axes[0]]); output = output.reshape(0, targetShape); - addConstant(layerParams.name, output); + addConstant(output_name, output); return; } @@ -2138,7 +2170,7 @@ void ONNXImporter::parseExpand(LayerParams& layerParams, const opencv_onnx::Node layerParams.set("axis", broadcast_axes[0]); layerParams.type = "Concat"; - node_proto.set_output(0, layerParams.name); + node_proto.set_output(0, output_name); } else if (broadcast_axes.empty()) { @@ -2163,7 +2195,7 @@ void ONNXImporter::parseReshape(LayerParams& layerParams, const opencv_onnx::Nod if (layer_id.find(node_proto.input(0)) == layer_id.end()) { std::vector inputs(1, getBlob(node_proto, 0)), outputs; runLayer(layerParams, inputs, outputs); - addConstant(layerParams.name, outputs[0]); + addConstant(node_proto.output(0), outputs[0]); return; } } @@ -2177,7 +2209,7 @@ void ONNXImporter::parseReshape(LayerParams& layerParams, const opencv_onnx::Nod if (layer_id.find(node_proto.input(0)) == layer_id.end()) { Mat input = getBlob(node_proto, 0); Mat out = input.reshape(0, dim); - addConstant(layerParams.name, out); + addConstant(node_proto.output(0), out); return; } replaceLayerParam(layerParams, "shape", "dim"); @@ -2229,7 +2261,7 @@ void ONNXImporter::parseShape(LayerParams& layerParams, const opencv_onnx::NodeP CV_LOG_ERROR(NULL, "DNN/ONNX(Shape): dynamic 'zero' shapes are not supported, input " << toString(inpShape, node_proto.input(0))); CV_Assert(!isDynamicShape); // not supported } - addConstant(layerParams.name, shapeMat); + addConstant(node_proto.output(0), shapeMat); } void ONNXImporter::parseCast(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto) @@ -2253,7 +2285,7 @@ void ONNXImporter::parseCast(LayerParams& layerParams, const opencv_onnx::NodePr Mat dst; blob.convertTo(dst, type); dst.dims = blob.dims; - addConstant(layerParams.name, dst); + addConstant(node_proto.output(0), dst); return; } else @@ -2281,7 +2313,7 @@ void ONNXImporter::parseConstantFill(LayerParams& layerParams, const opencv_onnx for (int i = 0; i < inpShape.size(); i++) CV_CheckGT(inpShape[i], 0, ""); Mat tensor(inpShape.size(), &inpShape[0], depth, Scalar(fill_value)); - addConstant(layerParams.name, tensor); + addConstant(node_proto.output(0), tensor); } void ONNXImporter::parseGather(LayerParams& layerParams, const opencv_onnx::NodeProto& node_proto_) @@ -2309,7 +2341,7 @@ void ONNXImporter::parseGather(LayerParams& layerParams, const opencv_onnx::Node } else { out.dims = 1; } - addConstant(layerParams.name, out); + addConstant(node_proto.output(0), out); return; } else @@ -2403,7 +2435,7 @@ void ONNXImporter::parseConcat(LayerParams& layerParams, const opencv_onnx::Node runLayer(layerParams, inputs, concatenated); CV_Assert(concatenated.size() == 1); - addConstant(layerParams.name, concatenated[0]); + addConstant(node_proto.output(0), concatenated[0]); return; } else From 302d14adefb98e02c2110eaa898cf04adb5564cd Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 22 Jan 2022 11:48:44 +0000 Subject: [PATCH 06/19] build: fix GCC12 compilation --- modules/core/src/persistence_base64.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/persistence_base64.cpp b/modules/core/src/persistence_base64.cpp index 6a32ab5450..3191372b35 100644 --- a/modules/core/src/persistence_base64.cpp +++ b/modules/core/src/persistence_base64.cpp @@ -164,7 +164,7 @@ size_t base64_decode(char const * src, char * dst, size_t off, size_t cnt) bool base64_valid(uint8_t const * src, size_t off, size_t cnt) { /* check parameters */ - if (src == 0 || src + off == 0) + if (src == 0) return false; if (cnt == 0U) cnt = std::strlen(reinterpret_cast(src)); From 2b5bb02817ba2b3abdc96b1da9b347aa6a4bc84f Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Fri, 14 Jan 2022 23:42:37 +0300 Subject: [PATCH 07/19] Update imgcodecs.hpp --- .../imgcodecs/include/opencv2/imgcodecs.hpp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index 41fcc79674..01d46cba7c 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -96,16 +96,16 @@ enum ImwriteFlags { IMWRITE_EXR_TYPE = (3 << 4) + 0, /* 48 */ //!< override EXR storage type (FLOAT (FP32) is default) IMWRITE_WEBP_QUALITY = 64, //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used. IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format - IMWRITE_TIFF_RESUNIT = 256,//!< For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values. - IMWRITE_TIFF_XDPI = 257,//!< For TIFF, use to specify the X direction DPI. - IMWRITE_TIFF_YDPI = 258, //!< For TIFF, use to specify the Y direction DPI. - IMWRITE_TIFF_COMPRESSION = 259 //!< For TIFF, use to specify the image compression scheme. See libtiff for integer constants corresponding to compression formats. Note, for images whose depth is CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified by this flag; LZW compression is the default. + IMWRITE_TIFF_RESUNIT = 256,//!< For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values. + IMWRITE_TIFF_XDPI = 257,//!< For TIFF, use to specify the X direction DPI. + IMWRITE_TIFF_YDPI = 258,//!< For TIFF, use to specify the Y direction DPI. + IMWRITE_TIFF_COMPRESSION = 259 //!< For TIFF, use to specify the image compression scheme. See libtiff for integer constants corresponding to compression formats. Note, for images whose depth is CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified by this flag; LZW compression is the default. }; enum ImwriteEXRTypeFlags { /*IMWRITE_EXR_TYPE_UNIT = 0, //!< not supported */ - IMWRITE_EXR_TYPE_HALF = 1, //!< store as HALF (FP16) - IMWRITE_EXR_TYPE_FLOAT = 2 //!< store as FP32 (default) + IMWRITE_EXR_TYPE_HALF = 1, //!< store as HALF (FP16) + IMWRITE_EXR_TYPE_FLOAT = 2 //!< store as FP32 (default) }; //! Imwrite PNG specific flags used to tune the compression algorithm. @@ -124,14 +124,14 @@ enum ImwritePNGFlags { IMWRITE_PNG_STRATEGY_FIXED = 4 //!< Using this value prevents the use of dynamic Huffman codes, allowing for a simpler decoder for special applications. }; -//! Imwrite PAM specific tupletype flags used to define the 'TUPETYPE' field of a PAM file. +//! Imwrite PAM specific tupletype flags used to define the 'TUPLETYPE' field of a PAM file. enum ImwritePAMFlags { - IMWRITE_PAM_FORMAT_NULL = 0, - IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, - IMWRITE_PAM_FORMAT_GRAYSCALE = 2, + IMWRITE_PAM_FORMAT_NULL = 0, + IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1, + IMWRITE_PAM_FORMAT_GRAYSCALE = 2, IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3, - IMWRITE_PAM_FORMAT_RGB = 4, - IMWRITE_PAM_FORMAT_RGB_ALPHA = 5, + IMWRITE_PAM_FORMAT_RGB = 4, + IMWRITE_PAM_FORMAT_RGB_ALPHA = 5 }; //! @} imgcodecs_flags @@ -191,8 +191,8 @@ CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR ); The function imreadmulti loads a multi-page image from the specified file into a vector of Mat objects. @param filename Name of file to be loaded. +@param mats A vector of Mat objects holding each page. @param flags Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. -@param mats A vector of Mat objects holding each page, if more than one. @sa cv::imread */ CV_EXPORTS_W bool imreadmulti(const String& filename, CV_OUT std::vector& mats, int flags = IMREAD_ANYCOLOR); From 2647902feed03bfffd30d7289048a0ba5acc14b3 Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Sat, 22 Jan 2022 11:27:01 +0300 Subject: [PATCH 08/19] Update python samples --- .../python/camera_calibration_show_extrinsics.py | 15 ++++++++++++++- samples/python/common.py | 2 +- samples/python/digits.py | 1 + samples/python/digits_video.py | 2 +- samples/python/facedetect.py | 6 +++--- samples/python/qrcode.py | 2 ++ samples/python/text_skewness_correction.py | 10 ++++++---- 7 files changed, 28 insertions(+), 10 deletions(-) diff --git a/samples/python/camera_calibration_show_extrinsics.py b/samples/python/camera_calibration_show_extrinsics.py index d676691f15..0ee2a19b68 100755 --- a/samples/python/camera_calibration_show_extrinsics.py +++ b/samples/python/camera_calibration_show_extrinsics.py @@ -1,5 +1,18 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- + +''' +Plot camera calibration extrinsics. + +usage: + camera_calibration_show_extrinsics.py [--calibration ] [--cam_width] [--cam_height] [--scale_focal] [--patternCentric ] + +default values: + --calibration : left_intrinsics.yml + --cam_width : 0.064/2 + --cam_height : 0.048/2 + --scale_focal : 40 + --patternCentric : True +''' # Python 2/3 compatibility from __future__ import print_function diff --git a/samples/python/common.py b/samples/python/common.py index 85cda62cd4..e7ad478b88 100755 --- a/samples/python/common.py +++ b/samples/python/common.py @@ -222,7 +222,7 @@ def mosaic(w, imgs): pad = np.zeros_like(img0) imgs = it.chain([img0], imgs) rows = grouper(w, imgs, pad) - return np.vstack(map(np.hstack, rows)) + return np.vstack(list(map(np.hstack, rows))) def getsize(img): h, w = img.shape[:2] diff --git a/samples/python/digits.py b/samples/python/digits.py index e5d8ceb59a..25db411f94 100755 --- a/samples/python/digits.py +++ b/samples/python/digits.py @@ -191,3 +191,4 @@ if __name__ == '__main__': model.save('digits_svm.dat') cv.waitKey(0) + cv.destroyAllWindows() diff --git a/samples/python/digits_video.py b/samples/python/digits_video.py index 7b07831643..237e40a9b2 100755 --- a/samples/python/digits_video.py +++ b/samples/python/digits_video.py @@ -29,7 +29,7 @@ def main(): src = sys.argv[1] except: src = 0 - cap = video.create_capture(src) + cap = video.create_capture(src, fallback='synth:bg={}:noise=0.05'.format(cv.samples.findFile('sudoku.png'))) classifier_fn = 'digits_svm.dat' if not os.path.exists(classifier_fn): diff --git a/samples/python/facedetect.py b/samples/python/facedetect.py index 488c92d5e5..248206a7cd 100755 --- a/samples/python/facedetect.py +++ b/samples/python/facedetect.py @@ -39,13 +39,13 @@ def main(): except: video_src = 0 args = dict(args) - cascade_fn = args.get('--cascade', "data/haarcascades/haarcascade_frontalface_alt.xml") - nested_fn = args.get('--nested-cascade', "data/haarcascades/haarcascade_eye.xml") + cascade_fn = args.get('--cascade', "haarcascades/haarcascade_frontalface_alt.xml") + nested_fn = args.get('--nested-cascade', "haarcascades/haarcascade_eye.xml") cascade = cv.CascadeClassifier(cv.samples.findFile(cascade_fn)) nested = cv.CascadeClassifier(cv.samples.findFile(nested_fn)) - cam = create_capture(video_src, fallback='synth:bg={}:noise=0.05'.format(cv.samples.findFile('samples/data/lena.jpg'))) + cam = create_capture(video_src, fallback='synth:bg={}:noise=0.05'.format(cv.samples.findFile('lena.jpg'))) while True: _ret, img = cam.read() diff --git a/samples/python/qrcode.py b/samples/python/qrcode.py index b3253f96c6..21b1a59073 100644 --- a/samples/python/qrcode.py +++ b/samples/python/qrcode.py @@ -245,4 +245,6 @@ def main(): if __name__ == '__main__': + print(__doc__) main() + cv.destroyAllWindows() diff --git a/samples/python/text_skewness_correction.py b/samples/python/text_skewness_correction.py index c8ee33b39d..c3e97a333b 100644 --- a/samples/python/text_skewness_correction.py +++ b/samples/python/text_skewness_correction.py @@ -15,7 +15,7 @@ import argparse def main(): parser = argparse.ArgumentParser() - parser.add_argument("-i", "--image", required=True, help="path to input image file") + parser.add_argument("-i", "--image", default="imageTextR.png", help="path to input image file") args = vars(parser.parse_args()) # load the image from disk @@ -37,9 +37,9 @@ def main(): coords = cv.findNonZero(thresh) angle = cv.minAreaRect(coords)[-1] # the `cv.minAreaRect` function returns values in the - # range [-90, 0) if the angle is less than -45 we need to add 90 to it - if angle < -45: - angle = (90 + angle) + # range [0, 90) if the angle is more than 45 we need to subtract 90 from it + if angle > 45: + angle = (angle - 90) (h, w) = image.shape[:2] center = (w // 2, h // 2) @@ -55,4 +55,6 @@ def main(): if __name__ == "__main__": + print(__doc__) main() + cv.destroyAllWindows() From d1b533d399df7bbd686ecb2289c08e4ef8fd1ba4 Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Mon, 24 Jan 2022 11:23:34 +0300 Subject: [PATCH 09/19] Disable -Wreturn-type-c-linkage under clang-cl clang-cl defines both __clang__ and _MSC_VER, yet uses `#pragma GCC` to disable certain diagnostics. At the time `-Wreturn-type-c-linkage` was reported by clang-cl. This PR fixes this behavior by reordering defines. --- modules/core/include/opencv2/core/core_c.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/core/include/opencv2/core/core_c.h b/modules/core/include/opencv2/core/core_c.h index 95a98cfcd0..76141be80a 100644 --- a/modules/core/include/opencv2/core/core_c.h +++ b/modules/core/include/opencv2/core/core_c.h @@ -48,16 +48,19 @@ #include "opencv2/core/types_c.h" #ifdef __cplusplus -# ifdef _MSC_VER -/* disable warning C4190: 'function' has C-linkage specified, but returns UDT 'typename' - which is incompatible with C +/* disable MSVC warning C4190 / clang-cl -Wreturn-type-c-linkage: + 'function' has C-linkage specified, but returns UDT 'typename' + which is incompatible with C It is OK to disable it because we only extend few plain structures with C++ constructors for simpler interoperability with C++ API of the library */ -# pragma warning(disable:4190) -# elif defined __clang__ && __clang_major__ >= 3 +# if defined(__clang__) + // handle clang on Linux and clang-cl (i. e. clang on Windows) first # pragma GCC diagnostic ignored "-Wreturn-type-c-linkage" +# elif defined(_MSC_VER) + // then handle MSVC +# pragma warning(disable:4190) # endif #endif From abb5c9fd926b23b8b0644d587c0234222e121c91 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Tue, 25 Jan 2022 11:37:39 +0100 Subject: [PATCH 10/19] Fix undefined behavior in line drawing. Left shift of negative values is undefined. --- modules/imgproc/src/drawing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index 8bbe4fa0e3..65735bd9c3 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -655,7 +655,7 @@ Line2( Mat& img, Point2l pt1, Point2l pt2, const void* color) pt1.y ^= pt2.y & j; x_step = XY_ONE; - y_step = (dy << XY_SHIFT) / (ax | 1); + y_step = dy * (1 << XY_SHIFT) / (ax | 1); ecount = (int)((pt2.x - pt1.x) >> XY_SHIFT); } else @@ -668,7 +668,7 @@ Line2( Mat& img, Point2l pt1, Point2l pt2, const void* color) pt2.y ^= pt1.y & i; pt1.y ^= pt2.y & i; - x_step = (dx << XY_SHIFT) / (ay | 1); + x_step = dx * (1 << XY_SHIFT) / (ay | 1); y_step = XY_ONE; ecount = (int)((pt2.y - pt1.y) >> XY_SHIFT); } From 5d9ea394ba8056be70788996fcb154a5e2c5dd2b Mon Sep 17 00:00:00 2001 From: pkubaj Date: Tue, 25 Jan 2022 13:35:22 +0000 Subject: [PATCH 11/19] Fix VSX detection on FreeBSD hwcap should actually be long. --- modules/core/src/system.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 01ed10a7fd..e8c5c20d89 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -621,7 +621,7 @@ struct HWFeatures } } #elif (defined __ppc64__ || defined __PPC64__) && defined __FreeBSD__ - unsigned int hwcap = 0; + unsigned long hwcap = 0; elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap)); if (hwcap & PPC_FEATURE_HAS_VSX) { elf_aux_info(AT_HWCAP2, &hwcap, sizeof(hwcap)); From 30ff9c6775f3449dcf015eb9c7da26be3e51191d Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 26 Jan 2022 04:34:51 +0000 Subject: [PATCH 12/19] cmake: don't force -Werror=... - improve compatibility with further compiler versions - warnings are not errors by default --- cmake/OpenCVCompilerOptions.cmake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmake/OpenCVCompilerOptions.cmake b/cmake/OpenCVCompilerOptions.cmake index 90f1896c30..bbfd889690 100644 --- a/cmake/OpenCVCompilerOptions.cmake +++ b/cmake/OpenCVCompilerOptions.cmake @@ -119,12 +119,12 @@ if(CV_GCC OR CV_CLANG) # we want. add_extra_compiler_option(-Wall) endif() - add_extra_compiler_option(-Werror=return-type) - add_extra_compiler_option(-Werror=non-virtual-dtor) - add_extra_compiler_option(-Werror=address) - add_extra_compiler_option(-Werror=sequence-point) + add_extra_compiler_option(-Wreturn-type) + add_extra_compiler_option(-Wnon-virtual-dtor) + add_extra_compiler_option(-Waddress) + add_extra_compiler_option(-Wsequence-point) add_extra_compiler_option(-Wformat) - add_extra_compiler_option(-Werror=format-security -Wformat) + add_extra_compiler_option(-Wformat-security -Wformat) add_extra_compiler_option(-Wmissing-declarations) add_extra_compiler_option(-Wmissing-prototypes) add_extra_compiler_option(-Wstrict-prototypes) From 123519165d7a9f3ff204a4505a9d571af6096a66 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 11 Jan 2022 03:06:43 +0000 Subject: [PATCH 13/19] core: FP denormals hints support --- .../opencv2/core/utils/fp_control.private.hpp | 29 +++++++ .../opencv2/core/utils/fp_control_utils.hpp | 69 ++++++++++++++++ modules/core/src/system.cpp | 79 +++++++++++++++++++ modules/core/test/test_misc.cpp | 65 +++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 modules/core/include/opencv2/core/utils/fp_control.private.hpp create mode 100644 modules/core/include/opencv2/core/utils/fp_control_utils.hpp diff --git a/modules/core/include/opencv2/core/utils/fp_control.private.hpp b/modules/core/include/opencv2/core/utils/fp_control.private.hpp new file mode 100644 index 0000000000..12ee363dd8 --- /dev/null +++ b/modules/core/include/opencv2/core/utils/fp_control.private.hpp @@ -0,0 +1,29 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#ifndef OPENCV_CORE_FP_CONTROL_UTILS_PRIVATE_HPP +#define OPENCV_CORE_FP_CONTROL_UTILS_PRIVATE_HPP + +#include "fp_control_utils.hpp" + +#if OPENCV_SUPPORTS_FP_DENORMALS_HINT == 0 + // disabled +#elif defined(OPENCV_IMPL_FP_HINTS) + // custom +#elif defined(OPENCV_IMPL_FP_HINTS_X86) + // custom +#elif defined(__SSE__) || defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1) + #include + #define OPENCV_IMPL_FP_HINTS_X86 1 + #define OPENCV_IMPL_FP_HINTS 1 +#endif + +#ifndef OPENCV_IMPL_FP_HINTS +#define OPENCV_IMPL_FP_HINTS 0 +#endif +#ifndef OPENCV_IMPL_FP_HINTS_X86 +#define OPENCV_IMPL_FP_HINTS_X86 0 +#endif + +#endif // OPENCV_CORE_FP_CONTROL_UTILS_PRIVATE_HPP diff --git a/modules/core/include/opencv2/core/utils/fp_control_utils.hpp b/modules/core/include/opencv2/core/utils/fp_control_utils.hpp new file mode 100644 index 0000000000..930bc5d367 --- /dev/null +++ b/modules/core/include/opencv2/core/utils/fp_control_utils.hpp @@ -0,0 +1,69 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#ifndef OPENCV_CORE_FP_CONTROL_UTILS_HPP +#define OPENCV_CORE_FP_CONTROL_UTILS_HPP + +namespace cv { + +namespace details { + +struct FPDenormalsModeState +{ + uint32_t reserved[16]; // 64-bytes +}; // FPDenormalsModeState + +CV_EXPORTS void setFPDenormalsIgnoreHint(bool ignore, CV_OUT FPDenormalsModeState& state); +CV_EXPORTS int saveFPDenormalsState(CV_OUT FPDenormalsModeState& state); +CV_EXPORTS bool restoreFPDenormalsState(const FPDenormalsModeState& state); + +class FPDenormalsIgnoreHintScope +{ +public: + inline explicit FPDenormalsIgnoreHintScope(bool ignore = true) + { + details::setFPDenormalsIgnoreHint(ignore, saved_state); + } + + inline explicit FPDenormalsIgnoreHintScope(const FPDenormalsModeState& state) + { + details::saveFPDenormalsState(saved_state); + details::restoreFPDenormalsState(state); + } + + inline ~FPDenormalsIgnoreHintScope() + { + details::restoreFPDenormalsState(saved_state); + } + +protected: + FPDenormalsModeState saved_state; +}; // FPDenormalsIgnoreHintScope + +class FPDenormalsIgnoreHintScopeNOOP +{ +public: + inline FPDenormalsIgnoreHintScopeNOOP(bool ignore = true) { CV_UNUSED(ignore); } + inline FPDenormalsIgnoreHintScopeNOOP(const FPDenormalsModeState& state) { CV_UNUSED(state); } + inline ~FPDenormalsIgnoreHintScopeNOOP() { } +}; // FPDenormalsIgnoreHintScopeNOOP + +} // namespace details + + +// Should depend on target compilation architecture only +// Note: previously added archs should NOT be removed to preserve ABI compatibility +#if defined(OPENCV_SUPPORTS_FP_DENORMALS_HINT) + // preserve configuration overloading through ports +#elif defined(__i386__) || defined(__x86_64__) || defined(_M_X64) || defined(_X86_) +typedef details::FPDenormalsIgnoreHintScope FPDenormalsIgnoreHintScope; +#define OPENCV_SUPPORTS_FP_DENORMALS_HINT 1 +#else +#define OPENCV_SUPPORTS_FP_DENORMALS_HINT 0 +typedef details::FPDenormalsIgnoreHintScopeNOOP FPDenormalsIgnoreHintScope; +#endif + +} // namespace cv + +#endif // OPENCV_CORE_FP_CONTROL_UTILS_HPP diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 01ed10a7fd..41d00e73b6 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -53,6 +53,9 @@ #include #include +#include +#include + #ifndef OPENCV_WITH_THREAD_SANITIZER #if defined(__clang__) && defined(__has_feature) #if __has_feature(thread_sanitizer) @@ -2733,6 +2736,82 @@ void setUseIPP_NE(bool flag) } // namespace ipp + +namespace details { + +#if OPENCV_IMPL_FP_HINTS_X86 +#ifndef _MM_DENORMALS_ZERO_ON // requires pmmintrin.h (SSE3) +#define _MM_DENORMALS_ZERO_ON 0x0040 +#endif +#ifndef _MM_DENORMALS_ZERO_MASK // requires pmmintrin.h (SSE3) +#define _MM_DENORMALS_ZERO_MASK 0x0040 +#endif +#endif + +void setFPDenormalsIgnoreHint(bool ignore, CV_OUT FPDenormalsModeState& state) +{ +#if OPENCV_IMPL_FP_HINTS_X86 + unsigned mask = _MM_FLUSH_ZERO_MASK; + unsigned value = ignore ? _MM_FLUSH_ZERO_ON : 0; + if (featuresEnabled.have[CPU_SSE3]) + { + mask |= _MM_DENORMALS_ZERO_MASK; + value |= ignore ? _MM_DENORMALS_ZERO_ON : 0; + } + const unsigned old_flags = _mm_getcsr(); + const unsigned old_value = old_flags & mask; + unsigned flags = (old_flags & ~mask) | value; + CV_LOG_DEBUG(NULL, "core: update FP mxcsr flags = " << cv::format("0x%08x", flags)); + // save state + state.reserved[0] = (uint32_t)mask; + state.reserved[1] = (uint32_t)old_value; + _mm_setcsr(flags); +#else + CV_UNUSED(ignore); CV_UNUSED(state); +#endif +} + +int saveFPDenormalsState(CV_OUT FPDenormalsModeState& state) +{ +#if OPENCV_IMPL_FP_HINTS_X86 + unsigned mask = _MM_FLUSH_ZERO_MASK; + if (featuresEnabled.have[CPU_SSE3]) + { + mask |= _MM_DENORMALS_ZERO_MASK; + } + const unsigned old_flags = _mm_getcsr(); + const unsigned old_value = old_flags & mask; + // save state + state.reserved[0] = (uint32_t)mask; + state.reserved[1] = (uint32_t)old_value; + return 2; +#else + CV_UNUSED(state); + return 0; +#endif +} + +bool restoreFPDenormalsState(const FPDenormalsModeState& state) +{ +#if OPENCV_IMPL_FP_HINTS_X86 + const unsigned mask = (unsigned)state.reserved[0]; + CV_DbgAssert(mask != 0); // invalid state (ensure that state is properly saved earlier) + const unsigned value = (unsigned)state.reserved[1]; + CV_DbgCheck((int)value, value == (value & mask), "invalid SSE FP state"); + const unsigned old_flags = _mm_getcsr(); + unsigned flags = (old_flags & ~mask) | value; + CV_LOG_DEBUG(NULL, "core: restore FP mxcsr flags = " << cv::format("0x%08x", flags)); + _mm_setcsr(flags); + return true; +#else + CV_UNUSED(state); + return false; +#endif +} + +} // namespace details + + } // namespace cv #ifdef HAVE_TEGRA_OPTIMIZATION diff --git a/modules/core/test/test_misc.cpp b/modules/core/test/test_misc.cpp index d9e9119230..e00544fdf4 100644 --- a/modules/core/test/test_misc.cpp +++ b/modules/core/test/test_misc.cpp @@ -3,6 +3,15 @@ // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" +#include "opencv2/core/utils/logger.hpp" + +#include + +#ifdef CV_CXX11 +#include +#include +#endif + namespace opencv_test { namespace { TEST(Core_OutputArrayCreate, _1997) @@ -242,6 +251,62 @@ TEST(Core_Parallel, propagate_exceptions) }, cv::Exception); } +class FPDenormalsHintCheckerParallelLoopBody : public cv::ParallelLoopBody +{ +public: + FPDenormalsHintCheckerParallelLoopBody() + : isOK(true) + { + state_values_to_check = cv::details::saveFPDenormalsState(base_state); + } + ~FPDenormalsHintCheckerParallelLoopBody() {} + void operator()(const cv::Range& r) const + { + CV_UNUSED(r); + cv::details::FPDenormalsModeState state; + if (cv::details::saveFPDenormalsState(state)) + { + for (int i = 0; i < state_values_to_check; ++i) + { + if (base_state.reserved[i] != state.reserved[i]) + { + CV_LOG_ERROR(NULL, cv::format("FP state[%d] mismatch: base=0x%08x thread=0x%08x", i, base_state.reserved[i], state.reserved[i])); + isOK = false; + cv::details::restoreFPDenormalsState(base_state); + } + } + } + else + { + // FP state is not supported + // no checks + } +#ifdef CV_CXX11 + std::this_thread::sleep_for(std::chrono::milliseconds(100)); +#endif + } + + cv::details::FPDenormalsModeState base_state; + int state_values_to_check; + + mutable bool isOK; +}; + +TEST(Core_Parallel, propagate_fp_denormals_ignore_hint) +{ + int nThreads = std::max(1, cv::getNumThreads()) * 3; + for (int i = 0; i < 4; ++i) + { + SCOPED_TRACE(cv::format("Case=%d: FP denormals ignore hint: %s\n", i, ((i & 1) != 0) ? "enable" : "disable")); + FPDenormalsIgnoreHintScope fp_denormals_scope((i & 1) != 0); + FPDenormalsHintCheckerParallelLoopBody job; + ASSERT_NO_THROW({ + parallel_for_(cv::Range(0, nThreads), job); + }); + EXPECT_TRUE(job.isOK); + } +} + TEST(Core_Version, consistency) { // this test verifies that OpenCV version loaded in runtime From b1d484f827e02e4df7030e67cfc2af0ccdb28716 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 24 Jan 2022 05:39:45 +0000 Subject: [PATCH 14/19] core(parallel): propagate FP denormals mode --- modules/core/src/parallel.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/modules/core/src/parallel.cpp b/modules/core/src/parallel.cpp index d1454457b7..95df5454c2 100644 --- a/modules/core/src/parallel.cpp +++ b/modules/core/src/parallel.cpp @@ -142,6 +142,9 @@ #include "opencv2/core/detail/exception_ptr.hpp" // CV__EXCEPTION_PTR = 1 if std::exception_ptr is available +#include +#include + using namespace cv; namespace cv { @@ -191,6 +194,9 @@ namespace { // propagate main thread state rng = cv::theRNG(); +#if OPENCV_SUPPORTS_FP_DENORMALS_HINT && OPENCV_IMPL_FP_HINTS + details::saveFPDenormalsState(fp_denormals_base_state); +#endif #ifdef OPENCV_TRACE traceRootRegion = CV_TRACE_NS::details::getCurrentRegion(); @@ -271,6 +277,11 @@ namespace { } } } + +#if OPENCV_SUPPORTS_FP_DENORMALS_HINT && OPENCV_IMPL_FP_HINTS + details::FPDenormalsModeState fp_denormals_base_state; +#endif + private: ParallelLoopBodyWrapperContext(const ParallelLoopBodyWrapperContext&); // disabled ParallelLoopBodyWrapperContext& operator=(const ParallelLoopBodyWrapperContext&); // disabled @@ -307,6 +318,9 @@ namespace { // propagate main thread state cv::theRNG() = ctx.rng; +#if OPENCV_SUPPORTS_FP_DENORMALS_HINT && OPENCV_IMPL_FP_HINTS + FPDenormalsIgnoreHintScope fp_denormals_scope(ctx.fp_denormals_base_state); +#endif cv::Range r; cv::Range wholeRange = ctx.wholeRange; From 70b0274c8e73991748a3cbbefd78363fa0042def Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 26 Jan 2022 05:00:47 +0000 Subject: [PATCH 15/19] dnn: apply hint to ignore denormals processing --- modules/dnn/src/caffe/caffe_importer.cpp | 4 ++++ modules/dnn/src/darknet/darknet_importer.cpp | 3 +++ modules/dnn/src/dnn.cpp | 13 +++++++++++++ modules/dnn/src/layers/convolution_layer.cpp | 11 ----------- modules/dnn/src/onnx/onnx_importer.cpp | 4 ++++ modules/dnn/src/tensorflow/tf_importer.cpp | 3 +++ modules/dnn/src/torch/torch_importer.cpp | 5 +++++ 7 files changed, 32 insertions(+), 11 deletions(-) diff --git a/modules/dnn/src/caffe/caffe_importer.cpp b/modules/dnn/src/caffe/caffe_importer.cpp index 0b6c0a6e38..03dff96464 100644 --- a/modules/dnn/src/caffe/caffe_importer.cpp +++ b/modules/dnn/src/caffe/caffe_importer.cpp @@ -53,6 +53,8 @@ #include "caffe_io.hpp" #endif +#include + namespace cv { namespace dnn { CV__DNN_EXPERIMENTAL_NS_BEGIN @@ -88,6 +90,8 @@ MatShape parseBlobShape(const caffe::BlobShape& _input_shape) class CaffeImporter { + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + caffe::NetParameter net; caffe::NetParameter netBinary; diff --git a/modules/dnn/src/darknet/darknet_importer.cpp b/modules/dnn/src/darknet/darknet_importer.cpp index 282b37277c..5d28dbd2e2 100644 --- a/modules/dnn/src/darknet/darknet_importer.cpp +++ b/modules/dnn/src/darknet/darknet_importer.cpp @@ -51,6 +51,7 @@ #include "darknet_io.hpp" +#include namespace cv { namespace dnn { @@ -61,6 +62,8 @@ namespace class DarknetImporter { + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + darknet::NetParameter net; public: diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 28bcde4827..52428a9898 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -55,6 +55,8 @@ #include #include +#include + #include #include @@ -3504,6 +3506,9 @@ Net Net::readFromModelOptimizer(const String& xml, const String& bin) CV_UNUSED(xml); CV_UNUSED(bin); CV_Error(Error::StsError, "Build OpenCV with Inference Engine to enable loading models from Model Optimizer."); #else + + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + #if INF_ENGINE_VER_MAJOR_LE(INF_ENGINE_RELEASE_2019R3) InferenceEngine::CNNNetReader reader; reader.ReadNetwork(xml); @@ -3540,6 +3545,8 @@ Net Net::readFromModelOptimizer( CV_Error(Error::StsError, "Build OpenCV with Inference Engine to enable loading models from Model Optimizer."); #else + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + #if INF_ENGINE_VER_MAJOR_LE(INF_ENGINE_RELEASE_2019R3) InferenceEngine::CNNNetReader reader; @@ -3639,6 +3646,7 @@ Mat Net::forward(const String& outputName) { CV_TRACE_FUNCTION(); CV_Assert(!empty()); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; String layerName = outputName; @@ -3660,6 +3668,7 @@ AsyncArray Net::forwardAsync(const String& outputName) { CV_TRACE_FUNCTION(); CV_Assert(!empty()); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; #ifdef CV_CXX11 String layerName = outputName; @@ -3691,6 +3700,7 @@ void Net::forward(OutputArrayOfArrays outputBlobs, const String& outputName) { CV_TRACE_FUNCTION(); CV_Assert(!empty()); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; String layerName = outputName; @@ -3769,6 +3779,7 @@ void Net::forward(OutputArrayOfArrays outputBlobs, const std::vector& outBlobNames) { CV_TRACE_FUNCTION(); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; std::vector pins; for (int i = 0; i < outBlobNames.size(); i++) @@ -3796,6 +3807,7 @@ void Net::forward(std::vector >& outputBlobs, const std::vector& outBlobNames) { CV_TRACE_FUNCTION(); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; std::vector pins; for (int i = 0; i < outBlobNames.size(); i++) @@ -3886,6 +3898,7 @@ void Net::setInput(InputArray blob, const String& name, double scalefactor, cons { CV_TRACE_FUNCTION(); CV_TRACE_ARG_VALUE(name, "name", name.c_str()); + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; LayerPin pin; pin.lid = 0; diff --git a/modules/dnn/src/layers/convolution_layer.cpp b/modules/dnn/src/layers/convolution_layer.cpp index f5c158453d..e160cfeebe 100644 --- a/modules/dnn/src/layers/convolution_layer.cpp +++ b/modules/dnn/src/layers/convolution_layer.cpp @@ -1629,13 +1629,6 @@ public: CV_TRACE_FUNCTION(); CV_TRACE_ARG_VALUE(name, "name", name.c_str()); -#if CV_SSE3 - uint32_t ftzMode = _MM_GET_FLUSH_ZERO_MODE(); - uint32_t dazMode = _MM_GET_DENORMALS_ZERO_MODE(); - _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); - _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); -#endif - CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget), forward_ocl(inputs_arr, outputs_arr, internals_arr)) @@ -1748,10 +1741,6 @@ public: ParallelConv::run(inputs[0], outputs[0], weightsMat, biasvec, reluslope, kernel_size, strides, pads_begin, pads_end, dilations, activ.get(), ngroups, nstripes); } -#if CV_SSE3 - _MM_SET_FLUSH_ZERO_MODE(ftzMode); - _MM_SET_DENORMALS_ZERO_MODE(dazMode); -#endif } virtual int64 getFLOPS(const std::vector &inputs, diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 47ec830313..f68b1ccf2f 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -8,6 +8,8 @@ #include "../precomp.hpp" #include +#include + #include #undef CV_LOG_STRIP_LEVEL #define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE + 1 @@ -40,6 +42,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN class ONNXImporter { + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + opencv_onnx::ModelProto model_proto; struct LayerInfo { int layerId; diff --git a/modules/dnn/src/tensorflow/tf_importer.cpp b/modules/dnn/src/tensorflow/tf_importer.cpp index 8cbe1c4b23..43ab1a93ac 100644 --- a/modules/dnn/src/tensorflow/tf_importer.cpp +++ b/modules/dnn/src/tensorflow/tf_importer.cpp @@ -11,6 +11,8 @@ Implementation of Tensorflow models parser #include "../precomp.hpp" +#include + #include #include #undef CV_LOG_STRIP_LEVEL @@ -509,6 +511,7 @@ void ExcludeLayer(tensorflow::GraphDef& net, const int layer_index, const int in class TFImporter { + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; public: TFImporter(Net& net, const char *model, const char *config = NULL); TFImporter(Net& net, const char *dataModel, size_t lenModel, diff --git a/modules/dnn/src/torch/torch_importer.cpp b/modules/dnn/src/torch/torch_importer.cpp index 5dd9e3e290..f158bc12c2 100644 --- a/modules/dnn/src/torch/torch_importer.cpp +++ b/modules/dnn/src/torch/torch_importer.cpp @@ -40,6 +40,9 @@ //M*/ #include "../precomp.hpp" + +#include + #include #include #include @@ -106,6 +109,8 @@ static inline bool endsWith(const String &str, const char *substr) struct TorchImporter { + FPDenormalsIgnoreHintScope fp_denormals_ignore_scope; + typedef std::map > TensorsMap; Net net; From ef85b24a78a4ac7a2e679fe019481cd9c1affd5b Mon Sep 17 00:00:00 2001 From: Vadim Levin Date: Thu, 27 Jan 2022 12:04:56 +0300 Subject: [PATCH 16/19] fix: wrong reference counter after module initialization --- modules/python/src2/cv2.cpp | 14 +++++++++----- modules/python/test/test_misc.py | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index d4f1a5aa3a..a82086f315 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -2184,18 +2184,22 @@ static PyObject* createSubmodule(PyObject* parent_module, const std::string& nam submodule_name.c_str()); if (!submodule) { + /// Populates global modules dictionary and returns borrowed reference to it submodule = PyImport_AddModule(full_submodule_name.c_str()); + if (!submodule) + { + /// Return `PyImport_AddModule` NULL with an exception set on failure. + return NULL; + } + /// Populates parent module dictionary. Submodule lifetime should be managed + /// by the global modules dictionary and parent module dictionary, so Py_DECREF after + /// successfull call to the `PyDict_SetItemString` is redundant. if (PyDict_SetItemString(parent_module_dict, submodule_name.c_str(), submodule) < 0) { - Py_CLEAR(submodule); return PyErr_Format(PyExc_ImportError, "Can't register a submodule '%s' (full name: '%s')", submodule_name.c_str(), full_submodule_name.c_str() ); } - /// PyDict_SetItemString doesn't steal a reference so the reference counter - /// of the submodule should be decremented to bind submodule lifetime to the - /// parent module - Py_DECREF(submodule); } submodule_name_start = submodule_name_end + 1; diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index f9e75e8fe4..41e5c6ba4b 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -602,10 +602,18 @@ class Arguments(NewOpenCVTests): msg="Module is not generated for nested namespace") self.assertTrue(hasattr(cv.utils.nested, "testEchoBooleanFunction"), msg="Function in nested module is not available") - self.assertEqual(sys.getrefcount(cv.utils.nested), 2, - msg="Nested submodule lifetime should be managed by " - "the parent module so the reference count should be " - "2, because `getrefcount` temporary increases it.") + + # Nested submodule is managed by the global submodules dictionary + # and parent native module + expected_ref_count = 2 + + # `getrefcount` temporary increases reference counter by 1 + actual_ref_count = sys.getrefcount(cv.utils.nested) - 1 + + self.assertEqual(actual_ref_count, expected_ref_count, + msg="Nested submodule reference counter has wrong value\n" + "Expected: {}. Actual: {}".format(expected_ref_count, actual_ref_count)) + for flag in (True, False): self.assertEqual(flag, cv.utils.nested.testEchoBooleanFunction(flag), msg="Function in nested module returns wrong result") From b5b52afd35789acf62f3d8714073e8f3481e002e Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Fri, 28 Jan 2022 16:35:47 +0100 Subject: [PATCH 17/19] Merge pull request #21527 from vrabaud:3.4_msan * Fix wrong MSAN errors. Because Fortran is called in Lapack, MSAN does not think the memory has been written even though it is the case. MSAN does no support well cross-language memory analysis. * Make a dedicated check. --- modules/core/src/hal_internal.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/core/src/hal_internal.cpp b/modules/core/src/hal_internal.cpp index cbe02780d2..8b74a35361 100644 --- a/modules/core/src/hal_internal.cpp +++ b/modules/core/src/hal_internal.cpp @@ -239,6 +239,21 @@ lapack_SVD(fptype* a, size_t a_step, fptype *w, fptype* u, size_t u_step, fptype else if(typeid(fptype) == typeid(double)) OCV_LAPACK_FUNC(dgesdd)(mode, &m, &n, (double*)a, &lda, (double*)w, (double*)u, &ldu, (double*)vt, &ldv, (double*)buffer, &lwork, iworkBuf, info); +#if defined(__clang__) && defined(__has_feature) +#if __has_feature(memory_sanitizer) + // Make sure MSAN sees the memory as having been written. + // MSAN does not think it has been written because a different language was called. + __msan_unpoison(a, a_step * n); + __msan_unpoison(buffer, sizeof(fptype) * (lwork + 1)); + if (u) + __msan_unpoison(u, u_step * m); + if (vt) + __msan_unpoison(vt, v_step * n); + if (w) + __msan_unpoison(w, sizeof(fptype) * std::min(m, n)); +#endif // __has_feature(memory_sanitizer) +#endif // defined(__clang__) && defined(__has_feature) + if(!(flags & CV_HAL_SVD_NO_UV)) transpose_square_inplace(vt, ldv, n); From 85719a0a5d0280835ccd363049eeed84d877d0b9 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 29 Jan 2022 19:11:58 +0000 Subject: [PATCH 18/19] dnn: support outputs registration under new names - fixed ONNX importer --- modules/dnn/include/opencv2/dnn/dnn.hpp | 16 +++++ modules/dnn/src/dnn.cpp | 88 +++++++++++++++++++++---- modules/dnn/src/onnx/onnx_importer.cpp | 23 +++++++ 3 files changed, 113 insertions(+), 14 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index 8af42ea4f4..8c9b32cdd7 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -489,6 +489,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN */ void connect(int outLayerId, int outNum, int inpLayerId, int inpNum); + /** @brief Registers network output with name + * + * Function may create additional 'Identity' layer. + * + * @param outputName identifier of the output + * @param layerId identifier of the second layer + * @param outputPort number of the second layer input + * + * @returns index of bound layer (the same as layerId or newly created) + */ + int registerOutput(const std::string& outputName, int layerId, int outputPort); + /** @brief Sets outputs names of the network input pseudo layer. * * Each net always has special own the network input pseudo layer with id=0. @@ -610,10 +622,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN CV_WRAP inline Mat getParam(const String& layerName, int numParam = 0) const { return getParam(getLayerId(layerName), numParam); } /** @brief Returns indexes of layers with unconnected outputs. + * + * FIXIT: Rework API to registerOutput() approach, deprecate this call */ CV_WRAP std::vector getUnconnectedOutLayers() const; /** @brief Returns names of layers with unconnected outputs. + * + * FIXIT: Rework API to registerOutput() approach, deprecate this call */ CV_WRAP std::vector getUnconnectedOutLayersNames() const; diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 43b2a72199..245a2c5c1d 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -1135,6 +1135,7 @@ struct Net::Impl : public detail::NetImplBase std::vector blobsToKeep; MapIdToLayerData layers; std::map layerNameToId; + std::map outputNameToId; // use registerOutput() to populate outputs BlobManager blobManager; int preferableBackend; int preferableTarget; @@ -1483,6 +1484,23 @@ struct Net::Impl : public detail::NetImplBase return pins; } + int addLayer(const String &name, const String &type, LayerParams ¶ms) + { + if (getLayerId(name) >= 0) + { + CV_Error(Error::StsBadArg, "Layer \"" + name + "\" already into net"); + return -1; + } + + int id = ++lastLayerId; + layerNameToId.insert(std::make_pair(name, id)); + layers.insert(std::make_pair(id, LayerData(id, name, type, params))); + if (params.get("has_dynamic_shapes", false)) + hasDynamicShapes = true; + + return id; + } + void connect(int outLayerId, int outNum, int inLayerId, int inNum) { CV_Assert(outLayerId < inLayerId); @@ -1492,6 +1510,39 @@ struct Net::Impl : public detail::NetImplBase addLayerInput(ldInp, inNum, LayerPin(outLayerId, outNum)); ldOut.requiredOutputs.insert(outNum); ldOut.consumers.push_back(LayerPin(inLayerId, outNum)); + + CV_LOG_VERBOSE(NULL, 0, "DNN: connect(" << outLayerId << ":" << outNum << " ==> " << inLayerId << ":" << inNum << ")"); + } + + int registerOutput(const std::string& outputName, int layerId, int outputPort) + { + int checkLayerId = getLayerId(outputName); + if (checkLayerId >= 0) + { + if (checkLayerId == layerId) + { + if (outputPort == 0) + { + // layer name correlates with its output name + CV_LOG_DEBUG(NULL, "DNN: register output='" << outputName << "': reuse layer with the same name and id=" << layerId << " to be linked"); + outputNameToId.insert(std::make_pair(outputName, layerId)); + return checkLayerId; + } + } + CV_Error_(Error::StsBadArg, ("Layer with name='%s' already exists id=%d (to be linked with %d:%d)", outputName.c_str(), checkLayerId, layerId, outputPort)); + } +#if 0 // TODO + if (outputPort == 0) + // make alias only, need to adopt getUnconnectedOutLayers() call +#endif + LayerParams outputLayerParams; + outputLayerParams.name = outputName; + outputLayerParams.type = "Identity"; + int outputLayerId = addLayer(outputLayerParams.name, outputLayerParams.type, outputLayerParams); + connect(layerId, outputPort, outputLayerId, 0); + CV_LOG_DEBUG(NULL, "DNN: register output='" << outputName << "' id=" << outputLayerId << " defined as " << layerId << ":" << outputPort); + outputNameToId.insert(std::make_pair(outputName, outputLayerId)); + return outputLayerId; } void initBackend(const std::vector& blobsToKeep_) @@ -3599,20 +3650,8 @@ Net::~Net() int Net::addLayer(const String &name, const String &type, LayerParams ¶ms) { CV_TRACE_FUNCTION(); - - if (impl->getLayerId(name) >= 0) - { - CV_Error(Error::StsBadArg, "Layer \"" + name + "\" already into net"); - return -1; - } - - int id = ++impl->lastLayerId; - impl->layerNameToId.insert(std::make_pair(name, id)); - impl->layers.insert(std::make_pair(id, LayerData(id, name, type, params))); - if (params.get("has_dynamic_shapes", false)) - impl->hasDynamicShapes = true; - - return id; + CV_Assert(impl); + return impl->addLayer(name, type, params); } int Net::addLayerToPrev(const String &name, const String &type, LayerParams ¶ms) @@ -3644,6 +3683,13 @@ void Net::connect(String _outPin, String _inPin) impl->connect(outPin.lid, outPin.oid, inpPin.lid, inpPin.oid); } +int Net::registerOutput(const std::string& outputName, int layerId, int outputPort) +{ + CV_TRACE_FUNCTION(); + CV_Assert(impl); + return impl->registerOutput(outputName, layerId, outputPort); +} + Mat Net::forward(const String& outputName) { CV_TRACE_FUNCTION(); @@ -4328,8 +4374,22 @@ bool Net::empty() const std::vector Net::getUnconnectedOutLayers() const { + CV_TRACE_FUNCTION(); + CV_Assert(impl); + std::vector layersIds; + // registerOutput() flow + const std::map& outputNameToId = impl->outputNameToId; + if (!outputNameToId.empty()) + { + for (std::map::const_iterator it = outputNameToId.begin(); it != outputNameToId.end(); ++it) + { + layersIds.push_back(it->second); + } + return layersIds; + } + Impl::MapIdToLayerData::const_iterator it; for (it = impl->layers.begin(); it != impl->layers.end(); it++) { diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index 9f7d8461ca..1ab2b35cd0 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -130,6 +130,7 @@ protected: std::map layer_id; typedef std::map::iterator IterLayerId_t; + typedef std::map::const_iterator ConstIterLayerId_t; void handleNode(const opencv_onnx::NodeProto& node_proto); @@ -687,9 +688,31 @@ void ONNXImporter::populateNet() handleNode(node_proto); } + // register outputs + for (int i = 0; i < graph_proto.output_size(); ++i) + { + const std::string& output_name = graph_proto.output(i).name(); + if (output_name.empty()) + { + CV_LOG_ERROR(NULL, "DNN/ONNX: can't register output without name: " << i); + continue; + } + ConstIterLayerId_t layerIt = layer_id.find(output_name); + if (layerIt == layer_id.end()) + { + CV_LOG_ERROR(NULL, "DNN/ONNX: can't find layer for output name: '" << output_name << "'. Does model imported properly?"); + continue; + } + + const LayerInfo& li = layerIt->second; + int outputId = dstNet.registerOutput(output_name, li.layerId, li.outputId); CV_UNUSED(outputId); + // no need to duplicate message from engine: CV_LOG_DEBUG(NULL, "DNN/ONNX: registered output='" << output_name << "' with id=" << outputId); + } + CV_LOG_DEBUG(NULL, "DNN/ONNX: import completed!"); } +static const std::string& extractNodeName(const opencv_onnx::NodeProto& node_proto) { if (node_proto.has_name() && !node_proto.name().empty()) From a7e6a1059c3400c913e8de67d5abff2e2aa7e621 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 29 Jan 2022 19:58:46 +0000 Subject: [PATCH 19/19] dnn(test): fix outputs handling in ONNX conformance - ONNX output is 1 tensor per defined output instead of N tensors from outputs of "output" layer --- modules/dnn/test/test_onnx_conformance.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/dnn/test/test_onnx_conformance.cpp b/modules/dnn/test/test_onnx_conformance.cpp index 71ac4bef19..0b146b3eb4 100644 --- a/modules/dnn/test/test_onnx_conformance.cpp +++ b/modules/dnn/test/test_onnx_conformance.cpp @@ -1171,10 +1171,10 @@ TEST_P(Test_ONNX_conformance, Layer_Test) } std::vector layerNames = net.getUnconnectedOutLayersNames(); - std::vector< std::vector > outputs_; + std::vector outputs; try { - net.forward(outputs_, layerNames); + net.forward(outputs, layerNames); } catch (...) { @@ -1182,8 +1182,7 @@ TEST_P(Test_ONNX_conformance, Layer_Test) applyTestTag(CV_TEST_TAG_DNN_ERROR_FORWARD); throw; } - ASSERT_GE(outputs_.size(), 1); - const std::vector& outputs = outputs_[0]; + ASSERT_GE(outputs.size(), 1); if (checkLayersFallbacks && checkFallbacks(net)) {