2020-05-26 20:45:55 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
|
|
|
|
#ifndef __OPENCV_DNN_COMMON_HPP__
|
|
|
|
#define __OPENCV_DNN_COMMON_HPP__
|
|
|
|
|
2021-08-20 22:43:47 +08:00
|
|
|
#include <unordered_map>
|
2022-01-18 05:03:43 +08:00
|
|
|
#include <unordered_set>
|
2021-08-20 22:43:47 +08:00
|
|
|
|
2020-05-26 20:45:55 +08:00
|
|
|
#include <opencv2/dnn.hpp>
|
|
|
|
|
|
|
|
namespace cv { namespace dnn {
|
2020-05-29 07:35:11 +08:00
|
|
|
CV__DNN_INLINE_NS_BEGIN
|
2020-05-26 20:45:55 +08:00
|
|
|
#define IS_DNN_OPENCL_TARGET(id) (id == DNN_TARGET_OPENCL || id == DNN_TARGET_OPENCL_FP16)
|
|
|
|
Mutex& getInitializationMutex();
|
|
|
|
void initializeLayerFactory();
|
|
|
|
|
2021-08-20 22:43:47 +08:00
|
|
|
extern bool DNN_DIAGNOSTICS_RUN;
|
|
|
|
extern bool DNN_SKIP_REAL_IMPORT;
|
|
|
|
|
2020-05-26 20:45:55 +08:00
|
|
|
namespace detail {
|
2021-07-30 18:00:13 +08:00
|
|
|
#define CALL_MEMBER_FN(object, ptrToMemFn) ((object).*(ptrToMemFn))
|
2020-05-26 20:45:55 +08:00
|
|
|
|
2021-07-13 17:20:35 +08:00
|
|
|
class NotImplemented : public Layer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static Ptr<Layer> create(const LayerParams ¶ms);
|
|
|
|
|
|
|
|
static void Register();
|
|
|
|
static void unRegister();
|
|
|
|
};
|
|
|
|
|
2021-08-20 22:43:47 +08:00
|
|
|
template <typename Importer, typename ... Args>
|
|
|
|
Net readNet(Args&& ... args)
|
|
|
|
{
|
|
|
|
Net net;
|
|
|
|
Importer importer(net, std::forward<Args>(args)...);
|
|
|
|
return net;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Importer, typename ... Args>
|
|
|
|
Net readNetDiagnostic(Args&& ... args)
|
|
|
|
{
|
|
|
|
Net maybeDebugNet = readNet<Importer>(std::forward<Args>(args)...);
|
|
|
|
if (DNN_DIAGNOSTICS_RUN && !DNN_SKIP_REAL_IMPORT)
|
|
|
|
{
|
|
|
|
// if we just imported the net in diagnostic mode, disable it and import again
|
|
|
|
enableModelDiagnostics(false);
|
|
|
|
Net releaseNet = readNet<Importer>(std::forward<Args>(args)...);
|
|
|
|
enableModelDiagnostics(true);
|
|
|
|
return releaseNet;
|
|
|
|
}
|
|
|
|
return maybeDebugNet;
|
|
|
|
}
|
|
|
|
|
|
|
|
class LayerHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void addMissing(const std::string& name, const std::string& type);
|
|
|
|
bool contains(const std::string& type) const;
|
2022-01-18 05:03:43 +08:00
|
|
|
void printMissing() const;
|
2021-08-20 22:43:47 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
LayerParams getNotImplementedParams(const std::string& name, const std::string& op);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unordered_map<std::string, std::unordered_set<std::string>> layers;
|
|
|
|
};
|
|
|
|
|
2020-05-26 20:45:55 +08:00
|
|
|
struct NetImplBase
|
|
|
|
{
|
|
|
|
const int networkId; // network global identifier
|
2022-01-12 11:46:13 +08:00
|
|
|
mutable int networkDumpCounter; // dump counter
|
2020-05-26 20:45:55 +08:00
|
|
|
int dumpLevel; // level of information dumps (initialized through OPENCV_DNN_NETWORK_DUMP parameter)
|
|
|
|
|
|
|
|
NetImplBase();
|
|
|
|
|
2022-01-12 11:46:13 +08:00
|
|
|
std::string getDumpFileNameBase() const;
|
2020-05-26 20:45:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2021-12-23 07:33:57 +08:00
|
|
|
|
|
|
|
typedef std::vector<MatShape> ShapesVec;
|
|
|
|
|
|
|
|
static inline std::string toString(const ShapesVec& shapes, const std::string& name = std::string())
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
if (!name.empty())
|
|
|
|
ss << name << ' ';
|
|
|
|
ss << '[';
|
|
|
|
for(size_t i = 0, n = shapes.size(); i < n; ++i)
|
|
|
|
ss << ' ' << toString(shapes[i]);
|
|
|
|
ss << " ]";
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::string toString(const Mat& blob, const std::string& name = std::string())
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
if (!name.empty())
|
|
|
|
ss << name << ' ';
|
|
|
|
if (blob.empty())
|
|
|
|
{
|
|
|
|
ss << "<empty>";
|
|
|
|
}
|
|
|
|
else if (blob.dims == 1)
|
|
|
|
{
|
|
|
|
Mat blob_ = blob;
|
|
|
|
blob_.dims = 2; // hack
|
|
|
|
ss << blob_.t();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ss << blob.reshape(1, 1);
|
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2021-12-25 00:35:47 +08:00
|
|
|
|
2020-05-29 07:35:11 +08:00
|
|
|
CV__DNN_INLINE_NS_END
|
2020-05-26 20:45:55 +08:00
|
|
|
}} // namespace
|
|
|
|
|
|
|
|
#endif // __OPENCV_DNN_COMMON_HPP__
|