mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
dnn: fix LayerFactory initialization
This commit is contained in:
+39
-12
@@ -1931,42 +1931,69 @@ bool Layer::getMemoryShapes(const std::vector<MatShape> &inputs,
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct LayerFactory::Impl : public std::map<String, LayerFactory::Constuctor>
|
||||
static Mutex& getLayerFactoryMutex()
|
||||
{
|
||||
};
|
||||
static Mutex* volatile instance = NULL;
|
||||
if (instance == NULL)
|
||||
{
|
||||
cv::AutoLock lock(getInitializationMutex());
|
||||
if (instance == NULL)
|
||||
instance = new Mutex();
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
|
||||
Ptr<LayerFactory::Impl> LayerFactory::impl ()
|
||||
typedef std::map<String, LayerFactory::Constuctor> LayerFactory_Impl;
|
||||
|
||||
static LayerFactory_Impl& getLayerFactoryImpl_()
|
||||
{
|
||||
// allocate on first use
|
||||
static Ptr<LayerFactory::Impl> impl_(new LayerFactory::Impl());
|
||||
return impl_;
|
||||
static LayerFactory_Impl impl;
|
||||
return impl;
|
||||
}
|
||||
|
||||
static LayerFactory_Impl& getLayerFactoryImpl()
|
||||
{
|
||||
static LayerFactory_Impl* volatile instance = NULL;
|
||||
if (instance == NULL)
|
||||
{
|
||||
cv::AutoLock lock(getLayerFactoryMutex());
|
||||
if (instance == NULL)
|
||||
{
|
||||
instance = &getLayerFactoryImpl_();
|
||||
initializeLayerFactory();
|
||||
}
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
|
||||
void LayerFactory::registerLayer(const String &_type, Constuctor constructor)
|
||||
{
|
||||
cv::AutoLock lock(getLayerFactoryMutex());
|
||||
String type = _type.toLowerCase();
|
||||
Impl::iterator it = impl()->find(type);
|
||||
LayerFactory_Impl::const_iterator it = getLayerFactoryImpl().find(type);
|
||||
|
||||
if (it != impl()->end() && it->second != constructor)
|
||||
if (it != getLayerFactoryImpl().end() && it->second != constructor)
|
||||
{
|
||||
CV_Error(cv::Error::StsBadArg, "Layer \"" + type + "\" already was registered");
|
||||
}
|
||||
|
||||
impl()->insert(std::make_pair(type, constructor));
|
||||
getLayerFactoryImpl().insert(std::make_pair(type, constructor));
|
||||
}
|
||||
|
||||
void LayerFactory::unregisterLayer(const String &_type)
|
||||
{
|
||||
cv::AutoLock lock(getLayerFactoryMutex());
|
||||
String type = _type.toLowerCase();
|
||||
impl()->erase(type);
|
||||
getLayerFactoryImpl().erase(type);
|
||||
}
|
||||
|
||||
Ptr<Layer> LayerFactory::createLayerInstance(const String &_type, LayerParams& params)
|
||||
{
|
||||
cv::AutoLock lock(getLayerFactoryMutex());
|
||||
String type = _type.toLowerCase();
|
||||
Impl::const_iterator it = LayerFactory::impl()->find(type);
|
||||
LayerFactory_Impl::const_iterator it = getLayerFactoryImpl().find(type);
|
||||
|
||||
if (it != impl()->end())
|
||||
if (it != getLayerFactoryImpl().end())
|
||||
{
|
||||
return it->second(params);
|
||||
}
|
||||
|
||||
+44
-50
@@ -40,68 +40,62 @@
|
||||
//M*/
|
||||
|
||||
#include "precomp.hpp"
|
||||
#include <opencv2/dnn/layer.details.hpp>
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace dnn
|
||||
{
|
||||
|
||||
struct AutoInitializer
|
||||
static Mutex* __initialization_mutex = NULL;
|
||||
Mutex& getInitializationMutex()
|
||||
{
|
||||
bool status;
|
||||
if (__initialization_mutex == NULL)
|
||||
__initialization_mutex = new Mutex();
|
||||
return *__initialization_mutex;
|
||||
}
|
||||
// force initialization (single-threaded environment)
|
||||
Mutex* __initialization_mutex_initializer = &getInitializationMutex();
|
||||
|
||||
AutoInitializer() : status(false)
|
||||
{
|
||||
initModule();
|
||||
}
|
||||
};
|
||||
|
||||
static AutoInitializer init;
|
||||
|
||||
void initModule()
|
||||
void initializeLayerFactory()
|
||||
{
|
||||
if (init.status)
|
||||
return;
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Slice, SliceLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Split, SplitLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Concat, ConcatLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Reshape, ReshapeLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Flatten, FlattenLayer);
|
||||
|
||||
REG_RUNTIME_LAYER_CLASS(Slice, SliceLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Split, SplitLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Concat, ConcatLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Reshape, ReshapeLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Flatten, FlattenLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Convolution, ConvolutionLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Deconvolution, DeconvolutionLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Pooling, PoolingLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(LRN, LRNLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(InnerProduct, InnerProductLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Softmax, SoftmaxLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(MVN, MVNLayer);
|
||||
|
||||
REG_RUNTIME_LAYER_CLASS(Convolution, ConvolutionLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Deconvolution, DeconvolutionLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Pooling, PoolingLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(LRN, LRNLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(InnerProduct, InnerProductLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Softmax, SoftmaxLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(MVN, MVNLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(ReLU, ReLULayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(ChannelsPReLU, ChannelsPReLULayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Sigmoid, SigmoidLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(TanH, TanHLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(BNLL, BNLLLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(AbsVal, AbsLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Power, PowerLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(BatchNorm, BatchNormLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(MaxUnpool, MaxUnpoolLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Dropout, BlankLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Identity, BlankLayer);
|
||||
|
||||
REG_RUNTIME_LAYER_CLASS(ReLU, ReLULayer);
|
||||
REG_RUNTIME_LAYER_CLASS(ChannelsPReLU, ChannelsPReLULayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Sigmoid, SigmoidLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(TanH, TanHLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(BNLL, BNLLLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(AbsVal, AbsLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Power, PowerLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(BatchNorm, BatchNormLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(MaxUnpool, MaxUnpoolLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Dropout, BlankLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Identity, BlankLayer);
|
||||
|
||||
REG_RUNTIME_LAYER_CLASS(Crop, CropLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Eltwise, EltwiseLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Permute, PermuteLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(PriorBox, PriorBoxLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(DetectionOutput, DetectionOutputLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(NormalizeBBox, NormalizeBBoxLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Normalize, NormalizeBBoxLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Shift, ShiftLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Padding, PaddingLayer);
|
||||
REG_RUNTIME_LAYER_CLASS(Scale, ScaleLayer);
|
||||
|
||||
init.status = true;
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Crop, CropLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Eltwise, EltwiseLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Permute, PermuteLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(PriorBox, PriorBoxLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(DetectionOutput, DetectionOutputLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(NormalizeBBox, NormalizeBBoxLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Normalize, NormalizeBBoxLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Shift, ShiftLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Padding, PaddingLayer);
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Scale, ScaleLayer);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}} //namespace
|
||||
|
||||
@@ -43,3 +43,8 @@
|
||||
#include "cvconfig.h"
|
||||
#include <opencv2/dnn.hpp>
|
||||
#include <opencv2/dnn/all_layers.hpp>
|
||||
|
||||
namespace cv { namespace dnn {
|
||||
Mutex& getInitializationMutex();
|
||||
void initializeLayerFactory();
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user