From 62feeec31d6ae06c8e63a0bc8d7d370266d1a61a Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Mon, 8 Jun 2015 19:41:11 +0300 Subject: [PATCH 1/4] fixed compilation witn C++11 compiler --- modules/dnn/CMakeLists.txt | 4 ++-- modules/dnn/include/opencv2/dnn.hpp | 19 ++++++++++--------- modules/dnn/include/opencv2/dnn/dict.hpp | 10 +++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index c72b3fb79..e3005f658 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -3,7 +3,7 @@ set(OPENCV_MODULE_IS_PART_OF_WORLD OFF) option(WITH_PROTOBUF "Build with libprotobuf" ON) if(NOT WITH_PROTOBUF) - message(FATAL_ERROR "libprotobuf is required for dnn module") + message(FATAL_ERROR "libprotobuf is required for dnn module") endif() find_package( Protobuf REQUIRED ) @@ -27,4 +27,4 @@ ocv_create_module() target_link_libraries(opencv_dnn ${PROTOBUF_LIBRARIES}) ocv_add_accuracy_tests() ocv_add_perf_tests() -ocv_add_samples() \ No newline at end of file +ocv_add_samples() diff --git a/modules/dnn/include/opencv2/dnn.hpp b/modules/dnn/include/opencv2/dnn.hpp index 9668acd63..436972777 100644 --- a/modules/dnn/include/opencv2/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn.hpp @@ -17,10 +17,8 @@ namespace dnn class LayerParams; //wrapper over cv::Mat and cv::UMat - CV_EXPORTS class Blob + class CV_EXPORTS Blob { - Mat m; - public: Blob(); Blob(InputArray in); @@ -44,16 +42,19 @@ namespace dnn int num() const; Vec4i shape() const; + + private: + Mat m; }; - CV_EXPORTS class LayerParams : public Dict + class CV_EXPORTS LayerParams : public Dict { public: std::vector learnedBlobs; }; - CV_EXPORTS class LayerRegister + class CV_EXPORTS LayerRegister { public: @@ -73,7 +74,7 @@ namespace dnn }; //this class allows to build new Layers - CV_EXPORTS class Layer + class CV_EXPORTS Layer { public: @@ -103,7 +104,7 @@ namespace dnn //TODO: maybe eliminate all int ids and replace them by string names //Proxy class for different formats //Each format importer must populate it - CV_EXPORTS class NetConfiguration + class CV_EXPORTS NetConfiguration { public: @@ -140,7 +141,7 @@ namespace dnn }; - CV_EXPORTS class Net + class CV_EXPORTS Net { public: @@ -157,7 +158,7 @@ namespace dnn virtual void forward(int layer, std::vector > &layerOutputs) = 0; }; - CV_EXPORTS class Importer + class CV_EXPORTS Importer { public: diff --git a/modules/dnn/include/opencv2/dnn/dict.hpp b/modules/dnn/include/opencv2/dnn/dict.hpp index 6740670ac..cd8be085c 100644 --- a/modules/dnn/include/opencv2/dnn/dict.hpp +++ b/modules/dnn/include/opencv2/dnn/dict.hpp @@ -35,7 +35,7 @@ struct DictValue DictValue &operator=(const DictValue &r); ~DictValue(); - + private: void release(); }; @@ -57,14 +57,14 @@ public: } template - const T &get(const String &name, const T &default) const + const T &get(const String &name, const T &default_value) const { _Dict::const_iterator i = dict.find(name); if (i != dict.end()) return i->second.get(); else - return default; + return default_value; } template @@ -154,7 +154,7 @@ inline DictValue & DictValue::operator=(const DictValue &r) { if (&r == this) return *this; - + release(); //how to copy anonymous union without memcpy? @@ -175,4 +175,4 @@ inline DictValue::DictValue(const DictValue &r) } } -} \ No newline at end of file +} From 42b36fb34620f4c5e35337691cd388df14493f8a Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Mon, 8 Jun 2015 19:48:32 +0300 Subject: [PATCH 2/4] more compilation --- modules/dnn/CMakeLists.txt | 2 ++ modules/dnn/include/opencv2/dnn.hpp | 4 ++-- modules/dnn/include/opencv2/dnn/dnn.inl.hpp | 11 ++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index e3005f658..8e6400853 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -14,6 +14,8 @@ else() message(FATAL_ERROR "Could not find PROTOBUF Compiler") endif() +include_directories(src/caffe) + file(GLOB PROTO_FILES src/*.proto) PROTOBUF_GENERATE_CPP(PROTO_HDRS PROTO_SRCS ${PROTO_FILES}) diff --git a/modules/dnn/include/opencv2/dnn.hpp b/modules/dnn/include/opencv2/dnn.hpp index 436972777..ad19c9060 100644 --- a/modules/dnn/include/opencv2/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn.hpp @@ -108,7 +108,7 @@ namespace dnn { public: - CV_EXPORTS static Ptr create(); + static Ptr create(); int addLayer(const String &name, const String &type); @@ -145,7 +145,7 @@ namespace dnn { public: - CV_EXPORTS static Ptr create(Ptr config); + static Ptr create(Ptr config); virtual ~Net() = 0; diff --git a/modules/dnn/include/opencv2/dnn/dnn.inl.hpp b/modules/dnn/include/opencv2/dnn/dnn.inl.hpp index 917da3852..054dbc701 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.inl.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.inl.hpp @@ -7,26 +7,23 @@ namespace cv { namespace dnn { - inline - Mat& Blob::getMatRef() + inline Mat& Blob::getMatRef() { return m; } - inline - const Mat& Blob::getMatRef() const + inline const Mat& Blob::getMatRef() const { return m; } - inline - Mat Blob::getMat() + inline Mat Blob::getMat() { return m; } - Mat Blob::getMat(int num, int channel) + inline Mat Blob::getMat(int num, int channel) { CV_Assert(false); return Mat(); From 10c3a1378a24c06ec6a3ab5ef952f214a7a3fc5b Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Mon, 8 Jun 2015 20:00:37 +0300 Subject: [PATCH 3/4] umbrealla header --- modules/dnn/CMakeLists.txt | 4 +- modules/dnn/include/opencv2/dnn.hpp | 174 +---------------------- modules/dnn/include/opencv2/dnn/dnn.hpp | 177 ++++++++++++++++++++++++ modules/dnn/src/precomp.hpp | 2 +- 4 files changed, 185 insertions(+), 172 deletions(-) create mode 100644 modules/dnn/include/opencv2/dnn/dnn.hpp diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index 8e6400853..7391f6426 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -14,11 +14,11 @@ else() message(FATAL_ERROR "Could not find PROTOBUF Compiler") endif() -include_directories(src/caffe) - file(GLOB PROTO_FILES src/*.proto) PROTOBUF_GENERATE_CPP(PROTO_HDRS PROTO_SRCS ${PROTO_FILES}) +include_directories(include src/caffe) + ocv_add_module(dnn opencv_imgproc opencv_core opencv_highgui WRAP python matlab) ocv_glob_module_sources(${PROTO_SRCS} ${PROTO_HDRS}) ocv_source_group("Src\\protobuf" FILES ${PROTO_SRCS} ${PROTO_HDRS}) diff --git a/modules/dnn/include/opencv2/dnn.hpp b/modules/dnn/include/opencv2/dnn.hpp index ad19c9060..626433b18 100644 --- a/modules/dnn/include/opencv2/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn.hpp @@ -1,177 +1,13 @@ #ifndef __OPENCV_DNN_HPP__ #define __OPENCV_DNN_HPP__ -#include -#include -#include -#include "dnn/dict.hpp" +// This is an umbrealla header to include into you project. +// We are free to change headers layout in dnn subfolder, so please include +// this header for future compartibility -namespace cv -{ -namespace dnn -{ - class Layer; - class NetConfiguration; - class Net; - class Blob; - class LayerParams; +#include - //wrapper over cv::Mat and cv::UMat - class CV_EXPORTS Blob - { - public: - Blob(); - Blob(InputArray in); - void fill(InputArray in); - void fill(int ndims, const int *sizes, int type, void *data, bool deepCopy = true); - void create(int ndims, const int *sizes, int type = CV_32F); +#endif /* __OPENCV_DNN_HPP__ */ - bool empty() const; - Mat& getMatRef(); - const Mat& getMatRef() const; - Mat getMat(); - Mat getMat(int num, int channel); - - int cols() const; - int rows() const; - Size size() const; - - int channels() const; - int num() const; - - Vec4i shape() const; - - private: - Mat m; - }; - - class CV_EXPORTS LayerParams : public Dict - { - public: - - std::vector learnedBlobs; - }; - - class CV_EXPORTS LayerRegister - { - public: - - typedef Layer* (*Constuctor)(); - - static void registerLayer(const String &type, Constuctor constructor); - - static void unregisterLayer(const String &type); - - static Ptr createLayerInstance(const String &type); - - private: - LayerRegister(); - LayerRegister(const LayerRegister &lr); - - static std::map registeredLayers; - }; - - //this class allows to build new Layers - class CV_EXPORTS Layer - { - public: - - //TODO: this field must be declared as public if we want support possibility to change these params in runtime - std::vector learnedParams; - - virtual ~Layer(); - - //type of Layer - virtual String type() const; - - //setUp calls once (think that it's constructor) - virtual void setUp(LayerParams ¶ms); - - //after setUp the following two function must be able to return values - virtual int getNumInputs(); - virtual int getNumOutputs(); - - //maybe useless function - //shape of output blobs must be adjusted with respect to shape of input blobs - virtual void adjustShape(const std::vector &inputs, std::vector &outputs); - - virtual void forward(std::vector &inputs, std::vector &outputs); - }; - - //TODO: divide NetConfiguration interface and implementation, hide internal data - //TODO: maybe eliminate all int ids and replace them by string names - //Proxy class for different formats - //Each format importer must populate it - class CV_EXPORTS NetConfiguration - { - public: - - static Ptr create(); - - int addLayer(const String &name, const String &type); - - void deleteLayer(int layerId); - - void setLayerParams(int layerId, LayerParams ¶ms); - - //each output of each layer can be labeled by unique string label (as in Caffe) - //if label not specified then %layer_name%:c_%N% will be used - void setLayerOutputLabels(int layerId, const std::vector &outputNames); - - //version #1 - void addConnection(int fromLayer, int fromLayerOutput, int toLayer, int toLayerInput); - - //or maybe version #2 - inline int getBlobId(int layerId, int inputOutputNumber) - { - return (layerId << 16) + inputOutputNumber; - } - - void addConnection(int outputId, int inputId); - - void addConnections(const std::vector &outputIds, const std::vector &inputIds); - - private: - - int lastLayerId; - std::map< int, Ptr > layers; - std::map< int, std::vector > layerOutputLabels; - }; - - - class CV_EXPORTS Net - { - public: - - static Ptr create(Ptr config); - - virtual ~Net() = 0; - - virtual int getBlobId(int layerId, int outputId) = 0; - - virtual int getBlobId(const String &blobName) = 0; - - virtual void forward(std::vector< int, Ptr > &inputBlobs, std::vector > &outputBlobs) = 0; - - virtual void forward(int layer, std::vector > &layerOutputs) = 0; - }; - - class CV_EXPORTS Importer - { - public: - - virtual void populateNetConfiguration(Ptr config) = 0; - - virtual ~Importer(); - }; - - CV_EXPORTS Ptr createCaffeImporter(const String &prototxt, const String &caffeModel); - -} -} - -#include "dnn/dnn.inl.hpp" - -#endif diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp new file mode 100644 index 000000000..7a58c29bd --- /dev/null +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -0,0 +1,177 @@ +#ifndef __OPENCV_DNN_DNN_HPP__ +#define __OPENCV_DNN_DNN_HPP__ + +#include +#include +#include +#include + +namespace cv +{ +namespace dnn +{ + class Layer; + class NetConfiguration; + class Net; + class Blob; + class LayerParams; + + //wrapper over cv::Mat and cv::UMat + class CV_EXPORTS Blob + { + public: + Blob(); + Blob(InputArray in); + + void fill(InputArray in); + void fill(int ndims, const int *sizes, int type, void *data, bool deepCopy = true); + void create(int ndims, const int *sizes, int type = CV_32F); + + bool empty() const; + + Mat& getMatRef(); + const Mat& getMatRef() const; + Mat getMat(); + Mat getMat(int num, int channel); + + int cols() const; + int rows() const; + Size size() const; + + int channels() const; + int num() const; + + Vec4i shape() const; + + private: + Mat m; + }; + + class CV_EXPORTS LayerParams : public Dict + { + public: + + std::vector learnedBlobs; + }; + + class CV_EXPORTS LayerRegister + { + public: + + typedef Layer* (*Constuctor)(); + + static void registerLayer(const String &type, Constuctor constructor); + + static void unregisterLayer(const String &type); + + static Ptr createLayerInstance(const String &type); + + private: + LayerRegister(); + LayerRegister(const LayerRegister &lr); + + static std::map registeredLayers; + }; + + //this class allows to build new Layers + class CV_EXPORTS Layer + { + public: + + //TODO: this field must be declared as public if we want support possibility to change these params in runtime + std::vector learnedParams; + + virtual ~Layer(); + + //type of Layer + virtual String type() const; + + //setUp calls once (think that it's constructor) + virtual void setUp(LayerParams ¶ms); + + //after setUp the following two function must be able to return values + virtual int getNumInputs(); + virtual int getNumOutputs(); + + //maybe useless function + //shape of output blobs must be adjusted with respect to shape of input blobs + virtual void adjustShape(const std::vector &inputs, std::vector &outputs); + + virtual void forward(std::vector &inputs, std::vector &outputs); + }; + + //TODO: divide NetConfiguration interface and implementation, hide internal data + //TODO: maybe eliminate all int ids and replace them by string names + //Proxy class for different formats + //Each format importer must populate it + class CV_EXPORTS NetConfiguration + { + public: + + static Ptr create(); + + int addLayer(const String &name, const String &type); + + void deleteLayer(int layerId); + + void setLayerParams(int layerId, LayerParams ¶ms); + + //each output of each layer can be labeled by unique string label (as in Caffe) + //if label not specified then %layer_name%:c_%N% will be used + void setLayerOutputLabels(int layerId, const std::vector &outputNames); + + //version #1 + void addConnection(int fromLayer, int fromLayerOutput, int toLayer, int toLayerInput); + + //or maybe version #2 + inline int getBlobId(int layerId, int inputOutputNumber) + { + return (layerId << 16) + inputOutputNumber; + } + + void addConnection(int outputId, int inputId); + + void addConnections(const std::vector &outputIds, const std::vector &inputIds); + + private: + + int lastLayerId; + std::map< int, Ptr > layers; + std::map< int, std::vector > layerOutputLabels; + }; + + + class CV_EXPORTS Net + { + public: + + static Ptr create(Ptr config); + + virtual ~Net() = 0; + + virtual int getBlobId(int layerId, int outputId) = 0; + + virtual int getBlobId(const String &blobName) = 0; + + virtual void forward(std::vector< int, Ptr > &inputBlobs, std::vector > &outputBlobs) = 0; + + virtual void forward(int layer, std::vector > &layerOutputs) = 0; + }; + + class CV_EXPORTS Importer + { + public: + + virtual void populateNetConfiguration(Ptr config) = 0; + + virtual ~Importer(); + }; + + CV_EXPORTS Ptr createCaffeImporter(const String &prototxt, const String &caffeModel); + +} +} + +#include + +#endif /* __OPENCV_DNN_DNN_HPP__ */ diff --git a/modules/dnn/src/precomp.hpp b/modules/dnn/src/precomp.hpp index 630af81d2..7dcb7282e 100644 --- a/modules/dnn/src/precomp.hpp +++ b/modules/dnn/src/precomp.hpp @@ -1,2 +1,2 @@ #include -#include \ No newline at end of file +#include From eb958463c9c6a0c194d7eb9117ed142cc05b6d2e Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Mon, 8 Jun 2015 20:26:47 +0300 Subject: [PATCH 4/4] some warnings disabled --- modules/dnn/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index 7391f6426..d1eb3161c 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -14,6 +14,8 @@ else() message(FATAL_ERROR "Could not find PROTOBUF Compiler") endif() +ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses) + file(GLOB PROTO_FILES src/*.proto) PROTOBUF_GENERATE_CPP(PROTO_HDRS PROTO_SRCS ${PROTO_FILES})