diff options
Diffstat (limited to 'thirdparty/linux/include/opencv2/dnn')
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/all_layers.hpp | 423 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/blob.hpp | 341 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/blob.inl.hpp | 533 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/dict.hpp | 143 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/dnn.hpp | 350 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/dnn.inl.hpp | 351 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/layer.hpp | 148 | ||||
-rw-r--r-- | thirdparty/linux/include/opencv2/dnn/shape_utils.hpp | 137 |
8 files changed, 2426 insertions, 0 deletions
diff --git a/thirdparty/linux/include/opencv2/dnn/all_layers.hpp b/thirdparty/linux/include/opencv2/dnn/all_layers.hpp new file mode 100644 index 0000000..9d26b35 --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/all_layers.hpp @@ -0,0 +1,423 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_ALL_LAYERS_HPP__ +#define __OPENCV_DNN_DNN_ALL_LAYERS_HPP__ +#include <opencv2/dnn.hpp> + +namespace cv +{ +namespace dnn +{ +//! @addtogroup dnn +//! @{ + +/** @defgroup dnnLayerList Partial List of Implemented Layers + @{ + This subsection of dnn module contains information about bult-in layers and their descriptions. + + Classes listed here, in fact, provides C++ API for creating intances of bult-in layers. + In addition to this way of layers instantiation, there is a more common factory API (see @ref dnnLayerFactory), it allows to create layers dynamically (by name) and register new ones. + You can use both API, but factory API is less convinient for native C++ programming and basically designed for use inside importers (see @ref Importer, @ref createCaffeImporter(), @ref createTorchImporter()). + + Bult-in layers partially reproduce functionality of corresponding Caffe and Torch7 layers. + In partuclar, the following layers and Caffe @ref Importer were tested to reproduce <a href="http://caffe.berkeleyvision.org/tutorial/layers.html">Caffe</a> functionality: + - Convolution + - Deconvolution + - Pooling + - InnerProduct + - TanH, ReLU, Sigmoid, BNLL, Power, AbsVal + - Softmax + - Reshape, Flatten, Slice, Split + - LRN + - MVN + - Dropout (since it does nothing on forward pass -)) +*/ + + //! LSTM recurrent layer + class CV_EXPORTS_W LSTMLayer : public Layer + { + public: + /** Creates instance of LSTM layer */ + static CV_WRAP Ptr<LSTMLayer> create(); + + /** Set trained weights for LSTM layer. + LSTM behavior on each step is defined by current input, previous output, previous cell state and learned weights. + + Let @f$x_t@f$ be current input, @f$h_t@f$ be current output, @f$c_t@f$ be current state. + Than current output and current cell state is computed as follows: + @f{eqnarray*}{ + h_t &= o_t \odot tanh(c_t), \\ + c_t &= f_t \odot c_{t-1} + i_t \odot g_t, \\ + @f} + where @f$\odot@f$ is per-element multiply operation and @f$i_t, f_t, o_t, g_t@f$ is internal gates that are computed using learned wights. + + Gates are computed as follows: + @f{eqnarray*}{ + i_t &= sigmoid&(W_{xi} x_t + W_{hi} h_{t-1} + b_i), \\ + f_t &= sigmoid&(W_{xf} x_t + W_{hf} h_{t-1} + b_f), \\ + o_t &= sigmoid&(W_{xo} x_t + W_{ho} h_{t-1} + b_o), \\ + g_t &= tanh &(W_{xg} x_t + W_{hg} h_{t-1} + b_g), \\ + @f} + where @f$W_{x?}@f$, @f$W_{h?}@f$ and @f$b_{?}@f$ are learned weights represented as matrices: + @f$W_{x?} \in R^{N_h \times N_x}@f$, @f$W_{h?} \in R^{N_h \times N_h}@f$, @f$b_? \in R^{N_h}@f$. + + For simplicity and performance purposes we use @f$ W_x = [W_{xi}; W_{xf}; W_{xo}, W_{xg}] @f$ + (i.e. @f$W_x@f$ is vertical contacentaion of @f$ W_{x?} @f$), @f$ W_x \in R^{4N_h \times N_x} @f$. + The same for @f$ W_h = [W_{hi}; W_{hf}; W_{ho}, W_{hg}], W_h \in R^{4N_h \times N_h} @f$ + and for @f$ b = [b_i; b_f, b_o, b_g]@f$, @f$b \in R^{4N_h} @f$. + + @param Wh is matrix defining how previous output is transformed to internal gates (i.e. according to abovemtioned notation is @f$ W_h @f$) + @param Wx is matrix defining how current input is transformed to internal gates (i.e. according to abovemtioned notation is @f$ W_x @f$) + @param b is bias vector (i.e. according to abovemtioned notation is @f$ b @f$) + */ + CV_WRAP virtual void setWeights(const Blob &Wh, const Blob &Wx, const Blob &b) = 0; + + /** @brief Specifies shape of output blob which will be [[`T`], `N`] + @p outTailShape. + * @details If this parameter is empty or unset then @p outTailShape = [`Wh`.size(0)] will be used, + * where `Wh` is parameter from setWeights(). + */ + CV_WRAP virtual void setOutShape(const BlobShape &outTailShape = BlobShape::empty()) = 0; + + /** @brief Set @f$ h_{t-1} @f$ value that will be used in next forward() calls. + * @details By-default @f$ h_{t-1} @f$ is inited by zeros and updated after each forward() call. + */ + CV_WRAP virtual void setH(const Blob &H) = 0; + /** @brief Returns current @f$ h_{t-1} @f$ value (deep copy). */ + CV_WRAP virtual Blob getH() const = 0; + + /** @brief Set @f$ c_{t-1} @f$ value that will be used in next forward() calls. + * @details By-default @f$ c_{t-1} @f$ is inited by zeros and updated after each forward() call. + */ + CV_WRAP virtual void setC(const Blob &C) = 0; + /** @brief Returns current @f$ c_{t-1} @f$ value (deep copy). */ + CV_WRAP virtual Blob getC() const = 0; + + /** @brief Specifies either interpet first dimension of input blob as timestamp dimenion either as sample. + * + * If flag is set to true then shape of input blob will be interpeted as [`T`, `N`, `[data dims]`] where `T` specifies number of timpestamps, `N` is number of independent streams. + * In this case each forward() call will iterate through `T` timestamps and update layer's state `T` times. + * + * If flag is set to false then shape of input blob will be interpeted as [`N`, `[data dims]`]. + * In this case each forward() call will make one iteration and produce one timestamp with shape [`N`, `[out dims]`]. + */ + CV_WRAP virtual void setUseTimstampsDim(bool use = true) = 0; + + /** @brief If this flag is set to true then layer will produce @f$ c_t @f$ as second output. + * @details Shape of the second output is the same as first output. + */ + CV_WRAP virtual void setProduceCellOutput(bool produce = false) = 0; + + /** In common case it use single input with @f$x_t@f$ values to compute output(s) @f$h_t@f$ (and @f$c_t@f$). + * @param input should contain packed values @f$x_t@f$ + * @param output contains computed outputs: @f$h_t@f$ (and @f$c_t@f$ if setProduceCellOutput() flag was set to true). + * + * If setUseTimstampsDim() is set to true then @p input[0] should has at least two dimensions with the following shape: [`T`, `N`, `[data dims]`], + * where `T` specifies number of timpestamps, `N` is number of independent streams (i.e. @f$ x_{t_0 + t}^{stream} @f$ is stored inside @p input[0][t, stream, ...]). + * + * If setUseTimstampsDim() is set to fase then @p input[0] should contain single timestamp, its shape should has form [`N`, `[data dims]`] with at least one dimension. + * (i.e. @f$ x_{t}^{stream} @f$ is stored inside @p input[0][stream, ...]). + */ + void forward(std::vector<Blob*> &input, std::vector<Blob> &output); + + int inputNameToIndex(String inputName); + + int outputNameToIndex(String outputName); + }; + + //! Classical recurrent layer + class CV_EXPORTS_W RNNLayer : public Layer + { + public: + /** Creates instance of RNNLayer */ + static CV_WRAP Ptr<RNNLayer> create(); + + /** Setups learned weights. + + Recurrent-layer behavior on each step is defined by current input @f$ x_t @f$, previous state @f$ h_t @f$ and learned weights as follows: + @f{eqnarray*}{ + h_t &= tanh&(W_{hh} h_{t-1} + W_{xh} x_t + b_h), \\ + o_t &= tanh&(W_{ho} h_t + b_o), + @f} + + @param Wxh is @f$ W_{xh} @f$ matrix + @param bh is @f$ b_{h} @f$ vector + @param Whh is @f$ W_{hh} @f$ matrix + @param Who is @f$ W_{xo} @f$ matrix + @param bo is @f$ b_{o} @f$ vector + */ + CV_WRAP virtual void setWeights(const Blob &Wxh, const Blob &bh, const Blob &Whh, const Blob &Who, const Blob &bo) = 0; + + /** @brief If this flag is set to true then layer will produce @f$ h_t @f$ as second output. + * @details Shape of the second output is the same as first output. + */ + CV_WRAP virtual void setProduceHiddenOutput(bool produce = false) = 0; + + /** Accepts two inputs @f$x_t@f$ and @f$h_{t-1}@f$ and compute two outputs @f$o_t@f$ and @f$h_t@f$. + + @param input should contain packed input @f$x_t@f$. + @param output should contain output @f$o_t@f$ (and @f$h_t@f$ if setProduceHiddenOutput() is set to true). + + @p input[0] should have shape [`T`, `N`, `data_dims`] where `T` and `N` is number of timestamps and number of independent samples of @f$x_t@f$ respectively. + + @p output[0] will have shape [`T`, `N`, @f$N_o@f$], where @f$N_o@f$ is number of rows in @f$ W_{xo} @f$ matrix. + + If setProduceHiddenOutput() is set to true then @p output[1] will contain a Blob with shape [`T`, `N`, @f$N_h@f$], where @f$N_h@f$ is number of rows in @f$ W_{hh} @f$ matrix. + */ + void forward(std::vector<Blob*> &input, std::vector<Blob> &output); + }; + + class CV_EXPORTS_W BaseConvolutionLayer : public Layer + { + public: + + CV_PROP_RW Size kernel, stride, pad, dilation, adjustPad; + CV_PROP_RW String padMode; + }; + + class CV_EXPORTS_W ConvolutionLayer : public BaseConvolutionLayer + { + public: + + static CV_WRAP Ptr<BaseConvolutionLayer> create(Size kernel = Size(3, 3), Size stride = Size(1, 1), Size pad = Size(0, 0), Size dilation = Size(1, 1)); + }; + + class CV_EXPORTS_W DeconvolutionLayer : public BaseConvolutionLayer + { + public: + + static CV_WRAP Ptr<BaseConvolutionLayer> create(Size kernel = Size(3, 3), Size stride = Size(1, 1), Size pad = Size(0, 0), Size dilation = Size(1, 1), Size adjustPad = Size()); + }; + + class CV_EXPORTS_W LRNLayer : public Layer + { + public: + + enum Type + { + CHANNEL_NRM, + SPATIAL_NRM + }; + CV_PROP_RW int type; + + CV_PROP_RW int size; + CV_PROP_RW double alpha, beta, bias; + CV_PROP_RW bool normBySize; + + static CV_WRAP Ptr<LRNLayer> create(int type = LRNLayer::CHANNEL_NRM, int size = 5, + double alpha = 1, double beta = 0.75, double bias = 1, + bool normBySize = true); + }; + + class CV_EXPORTS_W PoolingLayer : public Layer + { + public: + + enum Type + { + MAX, + AVE, + STOCHASTIC + }; + + CV_PROP_RW int type; + CV_PROP_RW Size kernel, stride, pad; + CV_PROP_RW bool globalPooling; + CV_PROP_RW String padMode; + + static CV_WRAP Ptr<PoolingLayer> create(int type = PoolingLayer::MAX, Size kernel = Size(2, 2), + Size stride = Size(1, 1), Size pad = Size(0, 0), + const cv::String& padMode = ""); + static CV_WRAP Ptr<PoolingLayer> createGlobal(int type = PoolingLayer::MAX); + }; + + class CV_EXPORTS_W SoftmaxLayer : public Layer + { + public: + + static CV_WRAP Ptr<SoftmaxLayer> create(int axis = 1); + }; + + class CV_EXPORTS_W InnerProductLayer : public Layer + { + public: + CV_PROP_RW int axis; + + static CV_WRAP Ptr<InnerProductLayer> create(int axis = 1); + }; + + class CV_EXPORTS_W MVNLayer : public Layer + { + public: + CV_PROP_RW double eps; + CV_PROP_RW bool normVariance, acrossChannels; + + static CV_WRAP Ptr<MVNLayer> create(bool normVariance = true, bool acrossChannels = false, double eps = 1e-9); + }; + + /* Reshaping */ + + class CV_EXPORTS_W ReshapeLayer : public Layer + { + public: + CV_PROP_RW BlobShape newShapeDesc; + CV_PROP_RW Range newShapeRange; + + static CV_WRAP Ptr<ReshapeLayer> create(const BlobShape &newShape, Range applyingRange = Range::all(), + bool enableReordering = false); + }; + + class CV_EXPORTS_W ConcatLayer : public Layer + { + public: + int axis; + + static CV_WRAP Ptr<ConcatLayer> create(int axis = 1); + }; + + class CV_EXPORTS_W SplitLayer : public Layer + { + public: + int outputsCount; //!< Number of copies that will be produced (is ignored when negative). + + static CV_WRAP Ptr<SplitLayer> create(int outputsCount = -1); + }; + + class CV_EXPORTS_W SliceLayer : public Layer + { + public: + CV_PROP_RW int axis; + CV_PROP std::vector<int> sliceIndices; + + static CV_WRAP Ptr<SliceLayer> create(int axis); + static CV_WRAP Ptr<SliceLayer> create(int axis, const std::vector<int> &sliceIndices); + }; + + /* Activations */ + + class CV_EXPORTS_W ReLULayer : public Layer + { + public: + CV_PROP_RW double negativeSlope; + + static CV_WRAP Ptr<ReLULayer> create(double negativeSlope = 0); + }; + + class CV_EXPORTS_W ChannelsPReLULayer : public Layer + { + public: + static CV_WRAP Ptr<ChannelsPReLULayer> create(); + }; + + class CV_EXPORTS_W TanHLayer : public Layer + { + public: + static CV_WRAP Ptr<TanHLayer> create(); + }; + + class CV_EXPORTS_W SigmoidLayer : public Layer + { + public: + static CV_WRAP Ptr<SigmoidLayer> create(); + }; + + class CV_EXPORTS_W BNLLLayer : public Layer + { + public: + static CV_WRAP Ptr<BNLLLayer> create(); + }; + + class CV_EXPORTS_W AbsLayer : public Layer + { + public: + static CV_WRAP Ptr<AbsLayer> create(); + }; + + class CV_EXPORTS_W PowerLayer : public Layer + { + public: + CV_PROP_RW double power, scale, shift; + + static CV_WRAP Ptr<PowerLayer> create(double power = 1, double scale = 1, double shift = 0); + }; + + /* Layers using in semantic segmentation */ + + class CV_EXPORTS_W CropLayer : public Layer + { + public: + CV_PROP int startAxis; + CV_PROP std::vector<int> offset; + + static Ptr<CropLayer> create(int start_axis, const std::vector<int> &offset); + }; + + class CV_EXPORTS_W EltwiseLayer : public Layer + { + public: + enum EltwiseOp + { + PROD = 0, + SUM = 1, + MAX = 2, + }; + + static Ptr<EltwiseLayer> create(EltwiseOp op, const std::vector<int> &coeffs); + }; + + class CV_EXPORTS_W BatchNormLayer : public Layer + { + public: + static CV_WRAP Ptr<BatchNormLayer> create(float eps, bool has_weights, bool has_bias); + }; + + class CV_EXPORTS_W MaxUnpoolLayer : public Layer + { + public: + static CV_WRAP Ptr<MaxUnpoolLayer> create(Size unpoolSize); + }; + +//! @} +//! @} + +} +} +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/blob.hpp b/thirdparty/linux/include/opencv2/dnn/blob.hpp new file mode 100644 index 0000000..71e929d --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/blob.hpp @@ -0,0 +1,341 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_BLOB_HPP__ +#define __OPENCV_DNN_DNN_BLOB_HPP__ +#include <opencv2/core.hpp> +#include <vector> +#include <ostream> +#include <iostream> + +namespace cv +{ +namespace dnn +{ +//! @addtogroup dnn +//! @{ + + /** @brief Lightweight class for storing and processing a shape of blob (or anything else). */ + struct CV_EXPORTS_W BlobShape + { + BlobShape(); //!< Creates [1, 1, 1, 1] shape @todo Make more clearer behavior. + explicit BlobShape(int s0); //!< Creates 1-dim shape [@p s0] + BlobShape(int s0, int s1); //!< @overload + BlobShape(int s0, int s1, int s2); //!< @overload + BlobShape(int num, int cn, int rows, int cols); //!< Creates 4-dim shape [@p num, @p cn, @p rows, @p cols] + + //! Creates n-dim shape from the @p sizes array; if @p sizes is NULL then shape will contain unspecified data + BlobShape(int ndims, const int *sizes); + BlobShape(const std::vector<int> &sizes); //!< Creates n-dim shape from the @p sizes vector + template<int n> + BlobShape(const Vec<int, n> &shape); //!< Creates n-dim shape from @ref cv::Vec + + //! Creates n-dim shape and fill its by @p fill + static BlobShape all(int ndims, int fill = 1); + + /** @brief Returns number of dimensions. */ + int dims() const; + + /** @brief Returns reference to the size of the specified @p axis. + * + * Negative @p axis is supported, in this case a counting starts from the last axis, + * i. e. -1 corresponds to last axis. + * If non-existing axis was passed then an error will be generated. + */ + int &size(int axis); + + /** @brief Returns the size of the specified @p axis. + * @see size() + */ + int size(int axis) const; + + int operator[](int axis) const; //!< Does the same thing as size(axis). + int &operator[](int axis); //!< Does the same thing as size(int) const. + + /** @brief Returns the size of the specified @p axis. + * + * Does the same thing as size(int) const, but if non-existing axis will be passed then 1 will be returned, + * therefore this function always finishes successfully. + */ + int xsize(int axis) const; + + /** @brief Converts @p axis index to canonical format (where 0 <= @p axis < dims()). */ + int canonicalAxis(int axis) const; + + /** @brief Returns the product of all sizes of axes. */ + ptrdiff_t total() const; + + /** @brief Computes the product of sizes of axes among the specified axes range [@p startAxis; @p endAxis). + * @details Negative axis indexing can be used. @sa Blob::total(int,int) + */ + ptrdiff_t total(int startAxis, int endAxis = INT_MAX) const; + + /** @brief Constructs new shape from axes in range [@p startAxis; @p endAxis). + * @details Negative axis indexing can be used. @sa Blob::total(int,int) + */ + BlobShape slice(int startAxis, int endAxis = INT_MAX) const; + + /** @brief Returns pointer to the first element of continuous size array. */ + const int *ptr() const; + /** @overload */ + int *ptr(); + + bool equal(const BlobShape &other) const; //!< Checks equality of two shapes. + bool operator== (const BlobShape &r) const; //!< @sa equal() + + BlobShape operator+ (const BlobShape &r) const; //!< Contacenates two shapes. + + static BlobShape like(const Mat &m); //!< Returns shape of passed Mat. + static BlobShape like(const UMat &m); //!< Returns shape of passed UMat. + + static BlobShape empty(); //!< Returns empty shape []. + bool isEmpty() const; //!< Returns true if shape is empty (i.e []). + +#ifdef CV_CXX_MOVE_SEMANTICS + //TBD +#endif + + private: + cv::AutoBuffer<int,4> sz; + }; + + + /** @brief This class provides methods for continuous n-dimensional CPU and GPU array processing. + * + * The class is realized as a wrapper over @ref cv::Mat and @ref cv::UMat. + * It will support methods for switching and logical synchronization between CPU and GPU. + */ + class CV_EXPORTS_W Blob + { + public: + Blob(); + + /** @brief Constructs blob with specified @p shape and @p type. */ + explicit Blob(const BlobShape &shape, int type = CV_32F, int allocFlags = ALLOC_MAT); + + /** @brief Constructs Blob from existing Mat or UMat. */ + Blob(InputArray data); + + /** @brief Constructs 4-dimensional blob (so-called batch) from image or array of images. + * @param image 2-dimensional multi-channel or 3-dimensional single-channel image (or array of such images) + * @param dstCn specifies size of second axis of ouptut blob + */ + static Blob fromImages(InputArray image, int dstCn = -1); + + /** @brief Works like Blob::fromImages() but in-place. */ + void batchFromImages(InputArray image, int dstCn = -1); + + /** @brief Creates blob with specified @p shape and @p type. */ + void create(const BlobShape &shape, int type = CV_32F, int allocFlags = ALLOC_MAT); + + /** @brief Creates blob from Mat or UMat without copying the data. + * @details If in is Mat then Mat data is populated, otherwise - UMat. + */ + void fill(InputArray in); + + /** @brief Creates blob from user data. + * @details If @p deepCopy is false then CPU data will not be allocated. + */ + void fill(const BlobShape &shape, int type, void *data, bool deepCopy = true); + + /** @brief Sets @p value to the last used data (if @p allocFlags = -1). + * @details If @p allocFlags != -1 then destination data (Mat or UMat) is determined by flags from AllocFlag enum like in create(). + */ + void setTo(InputArray value, int allocFlags = -1); + + Mat& matRef(bool writeOnly = true); //!< Returns reference to cv::Mat, containing blob data. + const Mat& matRefConst() const; //!< Returns reference to cv::Mat, containing blob data, for read-only purposes. + UMat &umatRef(bool writeOnly = true); //!< Returns reference to cv::UMat, containing blob data. + const UMat &umatRefConst() const; //!< Returns reference to cv::UMat, containing blob data, for read-only purposes. + + template<typename XMat> + XMat &getRef(bool writeOnly = true); + template<typename XMat> + const XMat &getRefConst() const; + + void updateMat(bool syncData = true) const; //!< Actualizes data stored inside Mat of Blob; if @p syncData is false then only shape will be actualized. + void updateUMat(bool syncData = true) const; //!< Actualizes data stored inside Mat of Blob; if @p syncData is false then only shape will be actualized. + void sync() const; //!< Updates Mat and UMat of Blob. + + /** @brief Returns number of blob dimensions. */ + int dims() const; + + /** @brief Returns the size of the specified @p axis. + * + * Negative @p axis is supported, in this case a counting starts from the last axis, + * i. e. -1 corresponds to last axis. + * If non-existing axis was passed then an error will be generated. + */ + int size(int axis) const; + + /** @brief Returns the size of the specified @p axis. + * + * Does the same thing as size(int) const, but if non-existing axis will be passed then 1 will be returned, + * therefore this function always finishes successfully. + */ + int xsize(int axis) const; + + /** @brief Computes the product of sizes of axes among the specified axes range [@p startAxis; @p endAxis). + * @param startAxis the first axis to include in the range. + * @param endAxis the first axis to exclude from the range. + * @details Negative axis indexing can be used. + */ + size_t total(int startAxis = 0, int endAxis = INT_MAX) const; + + /** @brief Converts @p axis index to canonical format (where 0 <= @p axis < dims()). */ + int canonicalAxis(int axis) const; + + /** @brief Returns shape of the blob. */ + BlobShape shape() const; + + /** @brief Checks equality of two blobs shapes. */ + bool equalShape(const Blob &other) const; + + /** @brief Returns slice of first two dimensions. + * @details The behaviour is similar to the following numpy code: blob[n, cn, ...] + */ + Mat getPlane(int n, int cn); + + /** @brief Returns slice of first dimension. + * @details The behaviour is similar to getPlane(), but returns all + * channels * rows * cols values, corresponding to the n-th value + * of the first dimension. + */ + Mat getPlanes(int n); + + /* Shape getters of 4-dimensional blobs. */ + int cols() const; //!< Returns size of the fourth axis blob. + int rows() const; //!< Returns size of the thrid axis blob. + int channels() const; //!< Returns size of the second axis blob. + int num() const; //!< Returns size of the first axis blob. + Size size2() const; //!< Returns cv::Size(cols(), rows()) + Vec4i shape4() const; //!< Returns shape of first four blob axes. + + /** @brief Returns linear index of the element with specified coordinates in the blob. + * + * If @p n < dims() then unspecified coordinates will be filled by zeros. + * If @p n > dims() then extra coordinates will be ignored. + */ + template<int n> + size_t offset(const Vec<int, n> &pos) const; + /** @overload */ + size_t offset(int n = 0, int cn = 0, int row = 0, int col = 0) const; + + /* CPU pointer getters */ + /** @brief Returns pointer to the blob element with the specified position, stored in CPU memory. + * + * @p n correspond to the first axis, @p cn - to the second, etc. + * If dims() > 4 then unspecified coordinates will be filled by zeros. + * If dims() < 4 then extra coordinates will be ignored. + */ + uchar *ptr(int n = 0, int cn = 0, int row = 0, int col = 0); + /** @overload */ + template<typename Type> + Type *ptr(int n = 0, int cn = 0, int row = 0, int col = 0); + /** @overload ptr<float>() */ + float *ptrf(int n = 0, int cn = 0, int row = 0, int col = 0); + //TODO: add const ptr methods + + /** @brief Shares data from other @p blob. + * @returns *this + */ + Blob &shareFrom(const Blob &blob); + + /** @brief Changes shape of the blob without copying the data. + * @returns *this + */ + Blob &reshape(const BlobShape &shape); + + /** @brief Changes shape of the blob without copying the data. + * @returns shallow copy of original blob with new shape. + */ + Blob reshaped(const BlobShape &newShape) const; + + int type() const; //!< Returns type of the blob. + int elemSize() const; //!< Returns size of single element in bytes. + int getState() const; //!< Returns current state of the blob, @see DataState. + + private: + const int *sizes() const; + +# define CV_DNN_UMAT //DBG +#ifdef HAVE_OPENCL +# define CV_DNN_UMAT +#endif + +#ifdef CV_DNN_UMAT +# define CV_DNN_UMAT_ONLY(expr) (expr) +#else +# define CV_DNN_UMAT_ONLY(expr) +#endif + +#ifndef CV_DNN_UMAT + Mat m; +#else + mutable Mat m; + mutable UMat um; + mutable uchar state; +#endif + +public: + enum DataState + { + UNINITIALIZED = 0, + HEAD_AT_MAT = 1 << 0, + HEAD_AT_UMAT = 1 << 1, + SYNCED = HEAD_AT_MAT | HEAD_AT_UMAT + }; + + enum AllocFlag + { + ALLOC_MAT = HEAD_AT_MAT, + ALLOC_UMAT = HEAD_AT_UMAT, + ALLOC_BOTH = SYNCED + }; + }; + +//! @} +} +} + +#include "blob.inl.hpp" + +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/blob.inl.hpp b/thirdparty/linux/include/opencv2/dnn/blob.inl.hpp new file mode 100644 index 0000000..b7f741e --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/blob.inl.hpp @@ -0,0 +1,533 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_BLOB_INL_HPP__ +#define __OPENCV_DNN_DNN_BLOB_INL_HPP__ +#include "blob.hpp" + +namespace cv +{ +namespace dnn +{ + +inline BlobShape::BlobShape() +{ + sz.allocate(4); + for (size_t i = 0; i < sz.size(); i++) + sz[i] = 1; +} + +inline BlobShape BlobShape::all(int ndims, int fill) +{ + CV_Assert(ndims >= 0); + BlobShape res; + res.sz.allocate(ndims); + for (int i = 0; i < ndims; i++) + res.sz[i] = fill; + return res; +} + +inline BlobShape::BlobShape(int ndims, const int *sizes) : sz( (size_t)std::max(ndims, 0) ) +{ + CV_Assert(ndims >= 0); + if (!sizes) + return; + for (int i = 0; i < ndims; i++) + sz[i] = sizes[i]; +} + +inline BlobShape::BlobShape(int s0) : sz(1) +{ + sz[0] = s0; +} + +inline BlobShape::BlobShape(int s0, int s1) : sz(2) +{ + sz[0] = s0; + sz[1] = s1; +} + +inline BlobShape::BlobShape(int s0, int s1, int s2) : sz(3) +{ + sz[0] = s0; + sz[1] = s1; + sz[2] = s2; +} + +inline BlobShape::BlobShape(int num, int cn, int rows, int cols) : sz(4) +{ + sz[0] = num; + sz[1] = cn; + sz[2] = rows; + sz[3] = cols; +} + +inline BlobShape::BlobShape(const std::vector<int> &sizes) : sz( sizes.size() ) +{ + for (int i = 0; i < (int)sizes.size(); i++) + sz[i] = sizes[i]; +} + +template<int n> +inline BlobShape::BlobShape(const Vec<int, n> &shape) : sz(n) +{ + for (int i = 0; i < n; i++) + sz[i] = shape[i]; +} + +inline int BlobShape::dims() const +{ + return (int)sz.size(); +} + +inline int BlobShape::xsize(int axis) const +{ + if (axis < -dims() || axis >= dims()) + return 1; + + return sz[(axis < 0) ? axis + dims() : axis]; +} + +inline int BlobShape::size(int axis) const +{ + CV_Assert(-dims() <= axis && axis < dims()); + return sz[(axis < 0) ? axis + dims() : axis]; +} + +inline int &BlobShape::size(int axis) +{ + CV_Assert(-dims() <= axis && axis < dims()); + return sz[(axis < 0) ? axis + dims() : axis]; +} + +inline int BlobShape::operator[] (int axis) const +{ + CV_Assert(-dims() <= axis && axis < dims()); + return sz[(axis < 0) ? axis + dims() : axis]; +} + +inline int &BlobShape::operator[] (int axis) +{ + CV_Assert(-dims() <= axis && axis < dims()); + return sz[(axis < 0) ? axis + dims() : axis]; +} + +inline int BlobShape::canonicalAxis(int axis) const +{ + CV_Assert(-dims() <= axis && axis < dims()); + return (axis < 0) ? axis + dims() : axis; +} + +inline ptrdiff_t BlobShape::total() const +{ + if (dims() == 0) + return 0; + + ptrdiff_t res = 1; + for (int i = 0; i < dims(); i++) + res *= sz[i]; + return res; +} + +inline ptrdiff_t BlobShape::total(int startAxis, int endAxis) const +{ + if (isEmpty()) + return 0; + + if (endAxis == INT_MAX) + endAxis = dims(); + else if (endAxis < 0) + endAxis += dims(); + startAxis = (startAxis < 0) ? startAxis + dims() : startAxis; + CV_Assert(0 <= startAxis && startAxis <= endAxis && endAxis <= dims()); + + ptrdiff_t res = 1; + for (int i = startAxis; i < endAxis; i++) + res *= sz[i]; + return res; +} + +inline BlobShape BlobShape::slice(int startAxis, int endAxis) const +{ + if (isEmpty()) + return BlobShape::empty(); + + if (endAxis == INT_MAX) + endAxis = dims(); + else if (endAxis < 0) + endAxis += dims(); + startAxis = (startAxis < 0) ? startAxis + dims() : startAxis; + CV_Assert(0 <= startAxis && startAxis <= endAxis && endAxis <= dims()); + + BlobShape res(endAxis - startAxis, (const int*)NULL); + for (int i = startAxis; i < endAxis; i++) + res[i - startAxis] = sz[i]; + return res; +} + +inline const int *BlobShape::ptr() const +{ + return sz; +} + +inline int *BlobShape::ptr() +{ + return sz; +} + +inline bool BlobShape::equal(const BlobShape &other) const +{ + if (this->dims() != other.dims()) + return false; + + for (int i = 0; i < other.dims(); i++) + { + if (sz[i] != other.sz[i]) + return false; + } + + return true; +} + +inline bool BlobShape::operator==(const BlobShape &r) const +{ + return this->equal(r); +} + +inline BlobShape BlobShape::like(const Mat &m) +{ + return BlobShape(m.dims, (const int*)m.size); +} + +inline BlobShape BlobShape::like(const UMat &m) +{ + return BlobShape(m.dims, (const int*)m.size); +} + +inline BlobShape BlobShape::empty() +{ + return BlobShape(0, (const int*)NULL); +} + +inline bool BlobShape::isEmpty() const +{ + return dims() == 0; +} + +inline BlobShape BlobShape::operator+(const BlobShape &r) const +{ + BlobShape newShape(this->dims() + r.dims(), (int*)NULL); + for (int i = 0; i < this->dims(); i++) + newShape[i] = (*this)[i]; + for (int i = 0; i < r.dims(); i++) + newShape[this->dims() + i] = r[i]; + return newShape; +} + +CV_EXPORTS std::ostream &operator<< (std::ostream &stream, const BlobShape &shape); + +///////////////////////////////////////////////////////////////////// + +#ifndef CV_DNN_UMAT +# define CV_DNN_SWITCH_MU(cpu_expr, gpu_expr) (cpu_expr) +#else +# define CV_DNN_SWITCH_MU(cpu_expr, gpu_expr) ((state == HEAD_AT_UMAT) ? (gpu_expr) : (cpu_expr)) +#endif + + +inline int Blob::dims() const +{ + return CV_DNN_SWITCH_MU(m.dims, um.dims); +} + +inline const int * Blob::sizes() const +{ + return CV_DNN_SWITCH_MU((const int*)m.size, (const int*)um.size); +} + +inline int Blob::type() const +{ + return CV_DNN_SWITCH_MU(m.type(), um.type()); +} + +template<int n> +inline size_t Blob::offset(const Vec<int, n> &pos) const +{ + const MatStep &step = CV_DNN_SWITCH_MU(m.step, um.step); + size_t ofs = 0; + int i; + for (i = 0; i < std::min(n, dims()); i++) + { + CV_DbgAssert(pos[i] >= 0 && pos[i] < size(i)); + ofs += step[i] * pos[i]; + } + for (; i < dims(); i++) + CV_DbgAssert(pos[i] == 0); + CV_DbgAssert(ofs % elemSize() == 0); + return ofs / elemSize(); +} + +inline int Blob::canonicalAxis(int axis) const +{ + CV_Assert(-dims() <= axis && axis < dims()); + return (axis < 0) ? axis + dims() : axis; +} + +inline int Blob::xsize(int axis) const +{ + if (axis < -dims() || axis >= dims()) + return 1; + + return sizes()[(axis < 0) ? axis + dims() : axis]; +} + +inline int Blob::size(int axis) const +{ + CV_Assert(-dims() <= axis && axis < dims()); + return sizes()[(axis < 0) ? axis + dims() : axis]; +} + +inline size_t Blob::total(int startAxis, int endAxis) const +{ + if (startAxis < 0) + startAxis += dims(); + + if (endAxis == INT_MAX) + endAxis = dims(); + else if (endAxis < 0) + endAxis += dims(); + + CV_Assert(0 <= startAxis && startAxis <= endAxis && endAxis <= dims()); + + size_t cnt = 1; //fix: assume that slice isn't empty + for (int i = startAxis; i < endAxis; i++) + cnt *= (size_t)sizes()[i]; + + return cnt; +} + +inline size_t Blob::offset(int n, int cn, int row, int col) const +{ + return offset(Vec4i(n, cn, row, col)); +} + +inline float *Blob::ptrf(int n, int cn, int row, int col) +{ + return matRef(false).ptr<float>() + offset(n, cn, row, col); +} + +inline uchar *Blob::ptr(int n, int cn, int row, int col) +{ + Mat &mat = matRef(false); + return mat.ptr() + mat.elemSize() * offset(n, cn, row, col); +} + +template<typename Dtype> +inline Dtype* Blob::ptr(int n, int cn, int row, int col) +{ + CV_Assert(type() == cv::DataDepth<Dtype>::value); + return (Dtype*) ptr(n, cn, row, col); +} + +inline BlobShape Blob::shape() const +{ + return BlobShape(dims(), sizes()); +} + +inline bool Blob::equalShape(const Blob &other) const +{ + if (this->dims() != other.dims()) + return false; + + for (int i = 0; i < dims(); i++) + { + if (this->sizes()[i] != other.sizes()[i]) + return false; + } + return true; +} + +inline Mat& Blob::matRef(bool writeOnly) +{ +#ifdef CV_DNN_UMAT + updateMat(!writeOnly); + state = HEAD_AT_MAT; +#else + (void)writeOnly; +#endif + return m; +} + +inline const Mat& Blob::matRefConst() const +{ + CV_DNN_UMAT_ONLY( updateMat() ); + return m; +} + +inline UMat &Blob::umatRef(bool writeOnly) +{ +#ifndef CV_DNN_UMAT + CV_Error(Error::GpuNotSupported, ""); + (void)writeOnly; + return *(new UMat()); +#else + updateUMat(!writeOnly); + state = HEAD_AT_UMAT; + return um; +#endif +} + +inline const UMat &Blob::umatRefConst() const +{ +#ifndef CV_DNN_UMAT + CV_Error(Error::GpuNotSupported, ""); + return *(new UMat()); +#else + updateUMat(); + return um; +#endif +} + +template<> +inline Mat &Blob::getRef<Mat>(bool writeOnly) +{ + return matRef(writeOnly); +} + +template<> +inline UMat &Blob::getRef<UMat>(bool writeOnly) +{ + return umatRef(writeOnly); +} + +template<> +inline const Mat &Blob::getRefConst<Mat>() const +{ + return matRefConst(); +} + +template<> +inline const UMat &Blob::getRefConst<UMat>() const +{ + return umatRefConst(); +} + +inline Mat Blob::getPlane(int n, int cn) +{ + CV_Assert(dims() > 2); + return Mat(dims() - 2, sizes() + 2, type(), ptr(n, cn)); +} + +inline Mat Blob::getPlanes(int n) +{ + CV_Assert(dims() > 3); + return Mat(dims() - 1, sizes() + 1, type(), ptr(n)); +} + +inline int Blob::cols() const +{ + return xsize(3); +} + +inline int Blob::rows() const +{ + return xsize(2); +} + +inline int Blob::channels() const +{ + return xsize(1); +} + +inline int Blob::num() const +{ + return xsize(0); +} + +inline Size Blob::size2() const +{ + return Size(cols(), rows()); +} + +inline Blob &Blob::shareFrom(const Blob &blob) +{ + this->m = blob.m; +#ifdef CV_DNN_UMAT + this->um = blob.um; + this->state = blob.state; +#endif + return *this; +} + +inline Blob &Blob::reshape(const BlobShape &newShape) +{ + if (!m.empty()) m = m.reshape(1, newShape.dims(), newShape.ptr()); +#ifdef CV_DNN_UMAT + if (!um.empty()) um = um.reshape(1, newShape.dims(), newShape.ptr()); +#endif + return *this; +} + +inline Blob Blob::reshaped(const BlobShape &newShape) const +{ + Blob res(*this); //also, res.shareFrom(*this) could be used + res.reshape(newShape); + return res; +} + +inline int Blob::elemSize() const +{ + return CV_ELEM_SIZE(type()); +} + +inline int Blob::getState() const +{ +#ifdef CV_DNN_UMAT + return this->state; +#else + return m.empty() ? UNINITIALIZED : HEAD_AT_MAT; +#endif +} + +} +} + +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/dict.hpp b/thirdparty/linux/include/opencv2/dnn/dict.hpp new file mode 100644 index 0000000..f7cd0f2 --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/dict.hpp @@ -0,0 +1,143 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_DICT_HPP__ +#define __OPENCV_DNN_DNN_DICT_HPP__ + +#include <opencv2/core.hpp> +#include <map> +#include <ostream> + +namespace cv +{ +namespace dnn +{ +//! @addtogroup dnn +//! @{ + +/** @brief This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64. + * @todo Maybe int64 is useless because double type exactly stores at least 2^52 integers. + */ +struct DictValue +{ + DictValue(const DictValue &r); + DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar + DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar + DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar + DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar + DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar + DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload + + template<typename TypeIter> + static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array + template<typename TypeIter> + static DictValue arrayReal(TypeIter begin, int size); //!< Constructs floating point array + template<typename TypeIter> + static DictValue arrayString(TypeIter begin, int size); //!< Constructs array of strings + + template<typename T> + T get(int idx = -1) const; //!< Tries to convert array element with specified index to requested type and returns its. + + int size() const; + + bool isInt() const; + bool isString() const; + bool isReal() const; + + DictValue &operator=(const DictValue &r); + + friend std::ostream &operator<<(std::ostream &stream, const DictValue &dictv); + + ~DictValue(); + +private: + + int type; + + union + { + AutoBuffer<int64, 1> *pi; + AutoBuffer<double, 1> *pd; + AutoBuffer<String, 1> *ps; + void *pv; + }; + + DictValue(int _type, void *_p) : type(_type), pv(_p) {} + void release(); +}; + +/** @brief This class implements name-value dictionary, values are instances of DictValue. */ +class CV_EXPORTS Dict +{ + typedef std::map<String, DictValue> _Dict; + _Dict dict; + +public: + + //! Checks a presence of the @p key in the dictionary. + bool has(const String &key) const; + + //! If the @p key in the dictionary then returns pointer to its value, else returns NULL. + DictValue *ptr(const String &key); + + //! If the @p key in the dictionary then returns its value, else an error will be generated. + const DictValue &get(const String &key) const; + + /** @overload */ + template <typename T> + T get(const String &key) const; + + //! If the @p key in the dictionary then returns its value, else returns @p defaultValue. + template <typename T> + T get(const String &key, const T &defaultValue) const; + + //! Sets new @p value for the @p key, or adds new key-value pair into the dictionary. + template<typename T> + const T &set(const String &key, const T &value); + + friend std::ostream &operator<<(std::ostream &stream, const Dict &dict); +}; + +//! @} +} +} + +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/dnn.hpp b/thirdparty/linux/include/opencv2/dnn/dnn.hpp new file mode 100644 index 0000000..41d975b --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/dnn.hpp @@ -0,0 +1,350 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_HPP__ +#define __OPENCV_DNN_DNN_HPP__ + +#include <vector> +#include <opencv2/core.hpp> +#include <opencv2/dnn/dict.hpp> +#include <opencv2/dnn/blob.hpp> + +namespace cv +{ +namespace dnn //! This namespace is used for dnn module functionlaity. +{ +//! @addtogroup dnn +//! @{ + + /** @brief Initialize dnn module and built-in layers. + * + * This function automatically called on most of OpenCV builds, + * but you need to call it manually on some specific configurations (iOS for example). + */ + CV_EXPORTS_W void initModule(); + + /** @brief This class provides all data needed to initialize layer. + * + * It includes dictionary with scalar params (which can be readed by using Dict interface), + * blob params #blobs and optional meta information: #name and #type of layer instance. + */ + class CV_EXPORTS LayerParams : public Dict + { + public: + //TODO: Add ability to name blob params + std::vector<Blob> blobs; //!< List of learned parameters stored as blobs. + + String name; //!< Name of the layer instance (optional, can be used internal purposes). + String type; //!< Type name which was used for creating layer by layer factory (optional). + }; + + /** @brief This interface class allows to build new Layers - are building blocks of networks. + * + * Each class, derived from Layer, must implement allocate() methods to declare own outputs and forward() to compute outputs. + * Also before using the new layer into networks you must register your layer by using one of @ref dnnLayerFactory "LayerFactory" macros. + */ + class CV_EXPORTS_W Layer + { + public: + + //! List of learned parameters must be stored here to allow read them by using Net::getParam(). + CV_PROP_RW std::vector<Blob> blobs; + + /** @brief Allocates internal buffers and output blobs with respect to the shape of inputs. + * @param[in] input vector of already allocated input blobs + * @param[out] output vector of output blobs, which must be allocated + * + * This method must create each produced blob according to shape of @p input blobs and internal layer params. + * If this method is called first time then @p output vector consists from empty blobs and its size determined by number of output connections. + * This method can be called multiple times if size of any @p input blob was changed. + */ + virtual void allocate(const std::vector<Blob*> &input, std::vector<Blob> &output) = 0; + + /** @brief Given the @p input blobs, computes the output @p blobs. + * @param[in] input the input blobs. + * @param[out] output allocated output blobs, which will store results of the computation. + */ + virtual void forward(std::vector<Blob*> &input, std::vector<Blob> &output) = 0; + + /** @brief @overload */ + CV_WRAP void allocate(const std::vector<Blob> &inputs, CV_OUT std::vector<Blob> &outputs); + + /** @brief @overload */ + CV_WRAP std::vector<Blob> allocate(const std::vector<Blob> &inputs); + + /** @brief @overload */ + CV_WRAP void forward(const std::vector<Blob> &inputs, CV_IN_OUT std::vector<Blob> &outputs); + + /** @brief Allocates layer and computes output. */ + CV_WRAP void run(const std::vector<Blob> &inputs, CV_OUT std::vector<Blob> &outputs); + + /** @brief Returns index of input blob into the input array. + * @param inputName label of input blob + * + * Each layer input and output can be labeled to easily identify them using "%<layer_name%>[.output_name]" notation. + * This method maps label of input blob to its index into input vector. + */ + virtual int inputNameToIndex(String inputName); + /** @brief Returns index of output blob in output array. + * @see inputNameToIndex() + */ + virtual int outputNameToIndex(String outputName); + + CV_PROP String name; //!< Name of the layer instance, can be used for logging or other internal purposes. + CV_PROP String type; //!< Type name which was used for creating layer by layer factory. + + Layer(); + explicit Layer(const LayerParams ¶ms); //!< Initializes only #name, #type and #blobs fields. + void setParamsFrom(const LayerParams ¶ms); //!< Initializes only #name, #type and #blobs fields. + virtual ~Layer(); + }; + + /** @brief This class allows to create and manipulate comprehensive artificial neural networks. + * + * Neural network is presented as directed acyclic graph (DAG), where vertices are Layer instances, + * and edges specify relationships between layers inputs and outputs. + * + * Each network layer has unique integer id and unique string name inside its network. + * LayerId can store either layer name or layer id. + * + * This class supports reference counting of its instances, i. e. copies point to the same instance. + */ + class CV_EXPORTS_W_SIMPLE Net + { + public: + + CV_WRAP Net(); //!< Default constructor. + CV_WRAP ~Net(); //!< Destructor frees the net only if there aren't references to the net anymore. + + /** Returns true if there are no layers in the network. */ + CV_WRAP bool empty() const; + + /** @brief Adds new layer to the net. + * @param name unique name of the adding layer. + * @param type typename of the adding layer (type must be registered in LayerRegister). + * @param params parameters which will be used to initialize the creating layer. + * @returns unique identifier of created layer, or -1 if a failure will happen. + */ + int addLayer(const String &name, const String &type, LayerParams ¶ms); + /** @brief Adds new layer and connects its first input to the first output of previously added layer. + * @see addLayer() + */ + int addLayerToPrev(const String &name, const String &type, LayerParams ¶ms); + + /** @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 std::vector<String> getLayerNames() const; + + /** @brief Container for strings and integers. */ + typedef DictValue LayerId; + + /** @brief Returns pointer to layer with specified name which the network use. */ + CV_WRAP Ptr<Layer> getLayer(LayerId layerId); + + /** @brief Delete layer for the network (not implemented yet) */ + CV_WRAP void deleteLayer(LayerId layer); + + /** @brief Connects output of the first layer to input of the second layer. + * @param outPin descriptor of the first layer output. + * @param inpPin descriptor of the second layer input. + * + * Descriptors have the following template <DFN><layer_name>[.input_number]</DFN>: + * - the first part of the template <DFN>layer_name</DFN> is sting name of the added layer. + * If this part is empty then the network input pseudo layer will be used; + * - the second optional part of the template <DFN>input_number</DFN> + * is either number of the layer input, either label one. + * If this part is omitted then the first layer input will be used. + * + * @see setNetInputs(), Layer::inputNameToIndex(), Layer::outputNameToIndex() + */ + CV_WRAP void connect(String outPin, String inpPin); + + /** @brief Connects #@p outNum output of the first layer to #@p inNum input of the second layer. + * @param outLayerId identifier of the first layer + * @param inpLayerId identifier of the second layer + * @param outNum number of the first layer output + * @param inpNum number of the second layer input + */ + void connect(int outLayerId, int outNum, int inpLayerId, int inpNum); + + /** @brief Sets outputs names of the network input pseudo layer. + * + * Each net always has special own the network input pseudo layer with id=0. + * This layer stores the user blobs only and don't make any computations. + * In fact, this layer provides the only way to pass user data into the network. + * As any other layer, this layer can label its outputs and this function provides an easy way to do this. + */ + CV_WRAP void setNetInputs(const std::vector<String> &inputBlobNames); + + /** @brief Initializes and allocates all layers. */ + CV_WRAP void allocate(); + + /** @brief Runs forward pass to compute output of layer @p toLayer. + * @details By default runs forward pass for the whole network. + */ + CV_WRAP void forward(LayerId toLayer = String()); + /** @brief Runs forward pass to compute output of layer @p toLayer, but computations start from @p startLayer */ + void forward(LayerId startLayer, LayerId toLayer); + /** @overload */ + void forward(const std::vector<LayerId> &startLayers, const std::vector<LayerId> &toLayers); + + //TODO: + /** @brief Optimized forward. + * @warning Not implemented yet. + * @details Makes forward only those layers which weren't changed after previous forward(). + */ + void forwardOpt(LayerId toLayer); + /** @overload */ + void forwardOpt(const std::vector<LayerId> &toLayers); + + /** @brief Sets the new value for the layer output blob + * @param outputName descriptor of the updating layer output blob. + * @param blob new blob. + * @see connect(String, String) to know format of the descriptor. + * @note If updating blob is not empty then @p blob must have the same shape, + * because network reshaping is not implemented yet. + */ + CV_WRAP void setBlob(String outputName, const Blob &blob); + + /** @brief Returns the layer output blob. + * @param outputName the descriptor of the returning layer output blob. + * @see connect(String, String) + */ + CV_WRAP Blob getBlob(String outputName); + + /** @brief Sets the new value for the learned param of the layer. + * @param layer name or id of the layer. + * @param numParam index of the layer parameter in the Layer::blobs array. + * @param blob the new value. + * @see Layer::blobs + * @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 Blob &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 Blob getParam(LayerId layer, int numParam = 0); + + /** @brief Returns indexes of layers with unconnected outputs. + */ + CV_WRAP std::vector<int> getUnconnectedOutLayers() const; + private: + + struct Impl; + Ptr<Impl> impl; + }; + + /** @brief Small interface class for loading trained serialized models of different dnn-frameworks. */ + class CV_EXPORTS_W Importer + { + public: + + /** @brief Adds loaded layers into the @p net and sets connections between them. */ + CV_WRAP virtual void populateNet(Net net) = 0; + + virtual ~Importer(); + }; + + /** @brief Creates the importer of <a href="http://caffe.berkeleyvision.org">Caffe</a> framework network. + * @param prototxt path to the .prototxt file with text description of the network architecture. + * @param caffeModel path to the .caffemodel file with learned network. + * @returns Pointer to the created importer, NULL in failure cases. + */ + CV_EXPORTS_W Ptr<Importer> createCaffeImporter(const String &prototxt, const String &caffeModel = String()); + + /** @brief Reads a network model stored in Caffe model files. + * @details This is shortcut consisting from createCaffeImporter and Net::populateNet calls. + */ + CV_EXPORTS_W Net readNetFromCaffe(const String &prototxt, const String &caffeModel = String()); + + /** @brief Creates the importer of <a href="http://www.tensorflow.org">TensorFlow</a> framework network. + * @param model path to the .pb file with binary protobuf description of the network architecture. + * @returns Pointer to the created importer, NULL in failure cases. + */ + CV_EXPORTS Ptr<Importer> createTensorflowImporter(const String &model); + + /** @brief Creates the importer of <a href="http://torch.ch">Torch7</a> framework network. + * @param filename path to the file, dumped from Torch by using torch.save() function. + * @param isBinary specifies whether the network was serialized in ascii mode or binary. + * @returns Pointer to the created importer, NULL in failure cases. + * + * @warning Torch7 importer is experimental now, you need explicitly set CMake `opencv_dnn_BUILD_TORCH_IMPORTER` flag to compile its. + * + * @note Ascii mode of Torch serializer is more preferable, because binary mode extensively use `long` type of C language, + * which has various bit-length on different systems. + * + * The loading file must contain serialized <a href="https://github.com/torch/nn/blob/master/doc/module.md">nn.Module</a> object + * with importing network. Try to eliminate a custom objects from serialazing data to avoid importing errors. + * + * List of supported layers (i.e. object instances derived from Torch nn.Module class): + * - nn.Sequential + * - nn.Parallel + * - nn.Concat + * - nn.Linear + * - nn.SpatialConvolution + * - nn.SpatialMaxPooling, nn.SpatialAveragePooling + * - nn.ReLU, nn.TanH, nn.Sigmoid + * - nn.Reshape + * + * Also some equivalents of these classes from cunn, cudnn, and fbcunn may be successfully imported. + */ + CV_EXPORTS_W Ptr<Importer> createTorchImporter(const String &filename, bool isBinary = true); + + /** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework. + * @warning This function has the same limitations as createTorchImporter(). + */ + CV_EXPORTS_W Blob readTorchBlob(const String &filename, bool isBinary = true); + +//! @} +} +} + +#include <opencv2/dnn/layer.hpp> +#include <opencv2/dnn/dnn.inl.hpp> + +#endif /* __OPENCV_DNN_DNN_HPP__ */ diff --git a/thirdparty/linux/include/opencv2/dnn/dnn.inl.hpp b/thirdparty/linux/include/opencv2/dnn/dnn.inl.hpp new file mode 100644 index 0000000..a272044 --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/dnn.inl.hpp @@ -0,0 +1,351 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_INL_HPP__ +#define __OPENCV_DNN_DNN_INL_HPP__ + +#include <opencv2/dnn.hpp> + +namespace cv +{ +namespace dnn +{ + +template<typename TypeIter> +DictValue DictValue::arrayInt(TypeIter begin, int size) +{ + DictValue res(Param::INT, new AutoBuffer<int64, 1>(size)); + for (int j = 0; j < size; begin++, j++) + (*res.pi)[j] = *begin; + return res; +} + +template<typename TypeIter> +DictValue DictValue::arrayReal(TypeIter begin, int size) +{ + DictValue res(Param::REAL, new AutoBuffer<double, 1>(size)); + for (int j = 0; j < size; begin++, j++) + (*res.pd)[j] = *begin; + return res; +} + +template<typename TypeIter> +DictValue DictValue::arrayString(TypeIter begin, int size) +{ + DictValue res(Param::STRING, new AutoBuffer<String, 1>(size)); + for (int j = 0; j < size; begin++, j++) + (*res.ps)[j] = *begin; + return res; +} + +template<> +inline DictValue DictValue::get<DictValue>(int idx) const +{ + CV_Assert(idx == -1); + return *this; +} + +template<> +inline int64 DictValue::get<int64>(int idx) const +{ + CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size())); + idx = (idx == -1) ? 0 : idx; + + if (type == Param::INT) + { + return (*pi)[idx]; + } + else if (type == Param::REAL) + { + double doubleValue = (*pd)[idx]; + + double fracpart, intpart; + fracpart = std::modf(doubleValue, &intpart); + CV_Assert(fracpart == 0.0); + + return (int64)doubleValue; + } + else + { + CV_Assert(isInt() || isReal()); + return 0; + } +} + +template<> +inline int DictValue::get<int>(int idx) const +{ + return (int)get<int64>(idx); +} + +template<> +inline unsigned DictValue::get<unsigned>(int idx) const +{ + return (unsigned)get<int64>(idx); +} + +template<> +inline bool DictValue::get<bool>(int idx) const +{ + return (get<int64>(idx) != 0); +} + +template<> +inline double DictValue::get<double>(int idx) const +{ + CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size())); + idx = (idx == -1) ? 0 : idx; + + if (type == Param::REAL) + { + return (*pd)[idx]; + } + else if (type == Param::INT) + { + return (double)(*pi)[idx]; + } + else + { + CV_Assert(isReal() || isInt()); + return 0; + } +} + +template<> +inline float DictValue::get<float>(int idx) const +{ + return (float)get<double>(idx); +} + +template<> +inline String DictValue::get<String>(int idx) const +{ + CV_Assert(isString()); + CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size())); + return (*ps)[(idx == -1) ? 0 : idx]; +} + +inline void DictValue::release() +{ + switch (type) + { + case Param::INT: + delete pi; + break; + case Param::STRING: + delete ps; + break; + case Param::REAL: + delete pd; + break; + } +} + +inline DictValue::~DictValue() +{ + release(); +} + +inline DictValue & DictValue::operator=(const DictValue &r) +{ + if (&r == this) + return *this; + + if (r.type == Param::INT) + { + AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi); + release(); + pi = tmp; + } + else if (r.type == Param::STRING) + { + AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps); + release(); + ps = tmp; + } + else if (r.type == Param::REAL) + { + AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd); + release(); + pd = tmp; + } + + type = r.type; + + return *this; +} + +inline DictValue::DictValue(const DictValue &r) +{ + type = r.type; + + if (r.type == Param::INT) + pi = new AutoBuffer<int64, 1>(*r.pi); + else if (r.type == Param::STRING) + ps = new AutoBuffer<String, 1>(*r.ps); + else if (r.type == Param::REAL) + pd = new AutoBuffer<double, 1>(*r.pd); +} + +inline bool DictValue::isString() const +{ + return (type == Param::STRING); +} + +inline bool DictValue::isInt() const +{ + return (type == Param::INT); +} + +inline bool DictValue::isReal() const +{ + return (type == Param::REAL || type == Param::INT); +} + +inline int DictValue::size() const +{ + switch (type) + { + case Param::INT: + return (int)pi->size(); + break; + case Param::STRING: + return (int)ps->size(); + break; + case Param::REAL: + return (int)pd->size(); + break; + default: + CV_Error(Error::StsInternal, ""); + return -1; + } +} + +inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv) +{ + int i; + + if (dictv.isInt()) + { + for (i = 0; i < dictv.size() - 1; i++) + stream << dictv.get<int64>(i) << ", "; + stream << dictv.get<int64>(i); + } + else if (dictv.isReal()) + { + for (i = 0; i < dictv.size() - 1; i++) + stream << dictv.get<double>(i) << ", "; + stream << dictv.get<double>(i); + } + else if (dictv.isString()) + { + for (i = 0; i < dictv.size() - 1; i++) + stream << "\"" << dictv.get<String>(i) << "\", "; + stream << dictv.get<String>(i); + } + + return stream; +} + +///////////////////////////////////////////////////////////////// + +inline bool Dict::has(const String &key) const +{ + return dict.count(key) != 0; +} + +inline DictValue *Dict::ptr(const String &key) +{ + _Dict::iterator i = dict.find(key); + return (i == dict.end()) ? NULL : &i->second; +} + +inline const DictValue &Dict::get(const String &key) const +{ + _Dict::const_iterator i = dict.find(key); + if (i == dict.end()) + CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary"); + return i->second; +} + +template <typename T> +inline T Dict::get(const String &key) const +{ + return this->get(key).get<T>(); +} + +template <typename T> +inline T Dict::get(const String &key, const T &defaultValue) const +{ + _Dict::const_iterator i = dict.find(key); + + if (i != dict.end()) + return i->second.get<T>(); + else + return defaultValue; +} + +template<typename T> +inline const T &Dict::set(const String &key, const T &value) +{ + _Dict::iterator i = dict.find(key); + + if (i != dict.end()) + i->second = DictValue(value); + else + dict.insert(std::make_pair(key, DictValue(value))); + + return value; +} + +inline std::ostream &operator<<(std::ostream &stream, const Dict &dict) +{ + Dict::_Dict::const_iterator it; + for (it = dict.dict.begin(); it != dict.dict.end(); it++) + stream << it->first << " : " << it->second << "\n"; + + return stream; +} + +} +} + +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/layer.hpp b/thirdparty/linux/include/opencv2/dnn/layer.hpp new file mode 100644 index 0000000..e051041 --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/layer.hpp @@ -0,0 +1,148 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_LAYER_HPP__ +#define __OPENCV_DNN_LAYER_HPP__ +#include <opencv2/dnn.hpp> + +namespace cv +{ +namespace dnn +{ +//! @addtogroup dnn +//! @{ +//! +//! @defgroup dnnLayerFactory Utilities for New Layers Registration +//! @{ + +/** @brief %Layer factory allows to create instances of registered layers. */ +class CV_EXPORTS LayerFactory +{ +public: + + //! Each Layer class must provide this function to the factory + typedef Ptr<Layer>(*Constuctor)(LayerParams ¶ms); + + //! Registers the layer class with typename @p type and specified @p constructor. + static void registerLayer(const String &type, Constuctor constructor); + + //! Unregisters registered layer with specified type name. + static void unregisterLayer(const String &type); + + /** @brief Creates instance of registered layer. + * @param type type name of creating layer. + * @param params parameters which will be used for layer initialization. + */ + static Ptr<Layer> createLayerInstance(const String &type, LayerParams& params); + +private: + LayerFactory(); + + struct Impl; + static Ptr<Impl> impl(); +}; + +/** @brief Registers layer constructor in runtime. +* @param type string, containing type name of the layer. +* @param constuctorFunc pointer to the function of type LayerRegister::Constuctor, which creates the layer. +* @details This macros must be placed inside the function code. +*/ +#define REG_RUNTIME_LAYER_FUNC(type, constuctorFunc) \ + cv::dnn::LayerFactory::registerLayer(#type, constuctorFunc); + +/** @brief Registers layer class in runtime. + * @param type string, containing type name of the layer. + * @param class C++ class, derived from Layer. + * @details This macros must be placed inside the function code. + */ +#define REG_RUNTIME_LAYER_CLASS(type, class) \ + cv::dnn::LayerFactory::registerLayer(#type, _layerDynamicRegisterer<class>); + +/** @brief Registers layer constructor on module load time. +* @param type string, containing type name of the layer. +* @param constuctorFunc pointer to the function of type LayerRegister::Constuctor, which creates the layer. +* @details This macros must be placed outside the function code. +*/ +#define REG_STATIC_LAYER_FUNC(type, constuctorFunc) \ +static cv::dnn::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, constuctorFunc); + +/** @brief Registers layer class on module load time. + * @param type string, containing type name of the layer. + * @param class C++ class, derived from Layer. + * @details This macros must be placed outside the function code. + */ +#define REG_STATIC_LAYER_CLASS(type, class) \ +Ptr<Layer> __LayerStaticRegisterer_func_##type(LayerParams ¶ms) \ + { return Ptr<Layer>(new class(params)); } \ +static _LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, __LayerStaticRegisterer_func_##type); + + +//! @} +//! @} + + +template<typename LayerClass> +Ptr<Layer> _layerDynamicRegisterer(LayerParams ¶ms) +{ + return Ptr<Layer>(new LayerClass(params)); +} + +//allows automatically register created layer on module load time +class _LayerStaticRegisterer +{ + String type; +public: + + _LayerStaticRegisterer(const String &layerType, LayerFactory::Constuctor layerConstuctor) + { + this->type = layerType; + LayerFactory::registerLayer(layerType, layerConstuctor); + } + + ~_LayerStaticRegisterer() + { + LayerFactory::unregisterLayer(type); + } +}; + +} +} +#endif diff --git a/thirdparty/linux/include/opencv2/dnn/shape_utils.hpp b/thirdparty/linux/include/opencv2/dnn/shape_utils.hpp new file mode 100644 index 0000000..f52e5b9 --- /dev/null +++ b/thirdparty/linux/include/opencv2/dnn/shape_utils.hpp @@ -0,0 +1,137 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_DNN_DNN_SHAPE_UTILS_HPP__ +#define __OPENCV_DNN_DNN_SHAPE_UTILS_HPP__ + +#include <opencv2/core.hpp> +#include <ostream> + +namespace cv { +namespace dnn { + +//Useful shortcut +typedef BlobShape Shape; + +inline std::ostream &operator<< (std::ostream &s, cv::Range &r) +{ + return s << "[" << r.start << ", " << r.end << ")"; +} + +//Reshaping +//TODO: add -1 specifier for automatic size inferring + +template<typename Mat> +void reshape(Mat &m, const BlobShape &shape) +{ + m = m.reshape(1, shape.dims(), shape.ptr()); +} + +template<typename Mat> +Mat reshaped(const Mat &m, const BlobShape &shape) +{ + return m.reshape(1, shape.dims(), shape.ptr()); +} + + +//Slicing + +struct _Range : public cv::Range +{ + _Range(const Range &r) : cv::Range(r) {} + _Range(int start, int size = 1) : cv::Range(start, start + size) {} +}; + +template<typename Mat> +Mat slice(const Mat &m, const _Range &r0) +{ + //CV_Assert(m.dims >= 1); + cv::AutoBuffer<cv::Range, 4> ranges(m.dims); + for (int i = 1; i < m.dims; i++) + ranges[i] = Range::all(); + ranges[0] = r0; + return m(&ranges[0]); +} + +template<typename Mat> +Mat slice(const Mat &m, const _Range &r0, const _Range &r1) +{ + CV_Assert(m.dims >= 2); + cv::AutoBuffer<cv::Range, 4> ranges(m.dims); + for (int i = 2; i < m.dims; i++) + ranges[i] = Range::all(); + ranges[0] = r0; + ranges[1] = r1; + return m(&ranges[0]); +} + +template<typename Mat> +Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2) +{ + CV_Assert(m.dims <= 3); + cv::AutoBuffer<cv::Range, 4> ranges(m.dims); + for (int i = 3; i < m.dims; i++) + ranges[i] = Range::all(); + ranges[0] = r0; + ranges[1] = r1; + ranges[2] = r2; + return m(&ranges[0]); +} + +template<typename Mat> +Mat slice(const Mat &m, const _Range &r0, const _Range &r1, const _Range &r2, const _Range &r3) +{ + CV_Assert(m.dims <= 4); + cv::AutoBuffer<cv::Range, 4> ranges(m.dims); + for (int i = 4; i < m.dims; i++) + ranges[i] = Range::all(); + ranges[0] = r0; + ranges[1] = r1; + ranges[2] = r2; + ranges[3] = r3; + return m(&ranges[0]); +} + +BlobShape computeShapeByReshapeMask(const BlobShape &srcShape, const BlobShape &maskShape, Range srcRange = Range::all()); + +} +} +#endif |