diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt new file mode 100644 index 000000000..f85139bea --- /dev/null +++ b/modules/dnn/CMakeLists.txt @@ -0,0 +1,5 @@ +set(the_description "Deep neural netork module. Allow load models and make forward pass") +set(OPENCV_MODULE_IS_PART_OF_WORLD OFF) +ocv_define_module(dnn opencv_imgproc opencv_core opencv_highgui WRAP python) + +target_link_libraries(opencv_dnn) diff --git a/modules/dnn/include/opencv2/dnn.hpp b/modules/dnn/include/opencv2/dnn.hpp new file mode 100644 index 000000000..74e1fd753 --- /dev/null +++ b/modules/dnn/include/opencv2/dnn.hpp @@ -0,0 +1,171 @@ +#ifndef __OPENCV_DNN_HPP__ +#define __OPENCV_DNN_HPP__ + +#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 Blob : public _InputOutputArray + { + public: + Blob(Mat &in); + Blob(const Mat &in); + Blob(UMat &in); + Blob(const UMat &in); + + int width() const; //cols + int height() const; //rows + int channels() const; + int num() const; + + Vec4i size() const; + }; + + class LayerParams + { + struct Value + { + int type; + + union + { + int i; + double d; + String *s; + Mat *m; + } data; + }; + + //TODO: maybe this mechanism was realized somewhere in OpenCV? + std::map params; + + public: + + std::vector learnedWeights; + + template + T get(const String &name); + }; + + //this class allows to build new Layers + class Layer : public Algorithm + { + public: + //Layer registration routines + + typedef Layer* (*Constuctor)(); + + static void registerLayer(const String &type, Constuctor constructor); + + static void unregisterLayer(const String &type); + + static Ptr createLayerInstance(const String &type); + + 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); + + private: + + static std::map registeredLayers; + }; + + //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 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> layers; + std::map> layerOutputLabels; + }; + + + class Net + { + public: + + static Ptr create(Ptr config); + + virtual int getBlobId(int layerId, int outputId); + + virtual int getBlobId(const String &blobName); + + virtual void forward(std::vector> &inputBlobs, std::vector> &outputBlobs); + + virtual void forward(int layer, std::vector> &layerOutputs); + }; + + class Importer + { + public: + + virtual void populateNetConfiguration(Ptr config) = 0; + + virtual ~Importer(); + }; + +} +} + +#endif \ No newline at end of file diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp new file mode 100644 index 000000000..81dd812ed --- /dev/null +++ b/modules/dnn/src/dnn.cpp @@ -0,0 +1,28 @@ +#include "opencv2/dnn.hpp" + +namespace cv +{ +namespace dnn +{ + + Blob::Blob(Mat &in) : _InputOutputArray(in) + { + + } + + Blob::Blob(const Mat &in) : _InputOutputArray(in) + { + + } + + Blob::Blob(UMat &in) : _InputOutputArray(in) + { + + } + + Blob::Blob(const UMat &in) : _InputOutputArray(in) + { + + } +} +} \ No newline at end of file