1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

JavaScript bindings for dnn module

This commit is contained in:
Dmitry Kurtaev
2017-12-02 23:52:35 +03:00
parent 6185f7209e
commit f503515082
14 changed files with 326 additions and 57 deletions
+14 -9
View File
@@ -52,22 +52,27 @@ namespace dnn
class EltwiseLayerImpl : public EltwiseLayer
{
public:
EltwiseOp op;
enum EltwiseOp
{
PROD = 0,
SUM = 1,
MAX = 2,
} op;
std::vector<float> coeffs;
EltwiseLayerImpl(const LayerParams& params)
{
setParamsFrom(params);
op = EltwiseLayer::SUM;
op = SUM;
if (params.has("operation"))
{
String operation = params.get<String>("operation").toLowerCase();
if (operation == "prod")
op = EltwiseLayer::PROD;
op = PROD;
else if (operation == "sum")
op = EltwiseLayer::SUM;
op = SUM;
else if (operation == "max")
op = EltwiseLayer::MAX;
op = MAX;
else
CV_Error(cv::Error::StsBadArg, "Unknown operaticon type \"" + operation + "\"");
}
@@ -122,7 +127,7 @@ public:
int channels;
size_t planeSize;
EltwiseInvoker() : srcs(0), nsrcs(0), dst(0), coeffs(0), op(EltwiseLayer::PROD), nstripes(0), activ(0), channels(0), planeSize(0) {}
EltwiseInvoker() : srcs(0), nsrcs(0), dst(0), coeffs(0), op(PROD), nstripes(0), activ(0), channels(0), planeSize(0) {}
static void run(const Mat** srcs, int nsrcs, Mat& dst,
const std::vector<float>& coeffs, EltwiseOp op,
@@ -150,7 +155,7 @@ public:
CV_Assert(dst.total() == dst.size[0] * p.channels * p.planeSize);
bool simpleCoeffs = true;
if( op == EltwiseLayer::SUM && !coeffs.empty() )
if( op == SUM && !coeffs.empty() )
{
CV_Assert( coeffs.size() == (size_t)nsrcs );
@@ -192,7 +197,7 @@ public:
const float* srcptr0 = srcs[0]->ptr<float>() + globalDelta;
float* dstptr = dstptr0 + globalDelta;
if( op == EltwiseLayer::PROD )
if( op == PROD )
{
for( k = 1; k < n; k++ )
{
@@ -204,7 +209,7 @@ public:
srcptr0 = (const float*)dstptr;
}
}
else if( op == EltwiseLayer::MAX )
else if( op == MAX )
{
for( k = 1; k < n; k++ )
{
+9 -2
View File
@@ -67,9 +67,9 @@ public:
type = -1;
String nrmType = params.get<String>("norm_region", "ACROSS_CHANNELS");
if (nrmType == "ACROSS_CHANNELS")
type = LRNLayer::CHANNEL_NRM;
type = CHANNEL_NRM;
else if (nrmType == "WITHIN_CHANNEL")
type = LRNLayer::SPATIAL_NRM;
type = SPATIAL_NRM;
else
CV_Error(Error::StsBadArg, "Unknown region type \"" + nrmType + "\"");
@@ -397,6 +397,13 @@ public:
}
return flops;
}
private:
enum Type
{
CHANNEL_NRM,
SPATIAL_NRM
};
};
Ptr<LRNLayer> LRNLayer::create(const LayerParams& params)
+18 -11
View File
@@ -63,7 +63,7 @@ class PoolingLayerImpl : public PoolingLayer
public:
PoolingLayerImpl(const LayerParams& params)
{
type = PoolingLayer::MAX;
type = MAX;
computeMaxIdx = true;
globalPooling = false;
@@ -71,11 +71,11 @@ public:
{
String pool = params.get<String>("pool").toLowerCase();
if (pool == "max")
type = PoolingLayer::MAX;
type = MAX;
else if (pool == "ave")
type = PoolingLayer::AVE;
type = AVE;
else if (pool == "stochastic")
type = PoolingLayer::STOCHASTIC;
type = STOCHASTIC;
else
CV_Error(Error::StsBadArg, "Unknown pooling type \"" + pool + "\"");
getPoolingKernelParams(params, kernel.height, kernel.width, globalPooling,
@@ -83,7 +83,7 @@ public:
}
else if (params.has("pooled_w") || params.has("pooled_h") || params.has("spatial_scale"))
{
type = PoolingLayer::ROI;
type = ROI;
}
setParamsFrom(params);
ceilMode = params.get<bool>("ceil_mode", true);
@@ -115,8 +115,7 @@ public:
{
return backendId == DNN_BACKEND_DEFAULT ||
backendId == DNN_BACKEND_HALIDE && haveHalide() &&
(type == PoolingLayer::MAX ||
type == PoolingLayer::AVE && !pad.width && !pad.height);
(type == MAX || type == AVE && !pad.width && !pad.height);
}
#ifdef HAVE_OPENCL
@@ -200,9 +199,9 @@ public:
virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs)
{
if (type == PoolingLayer::MAX)
if (type == MAX)
return initMaxPoolingHalide(inputs);
else if (type == PoolingLayer::AVE)
else if (type == AVE)
return initAvePoolingHalide(inputs);
else
return Ptr<BackendNode>();
@@ -221,7 +220,7 @@ public:
float spatialScale;
PoolingInvoker() : src(0), rois(0), dst(0), mask(0), nstripes(0),
computeMaxIdx(0), poolingType(PoolingLayer::MAX), spatialScale(0) {}
computeMaxIdx(0), poolingType(MAX), spatialScale(0) {}
static void run(const Mat& src, const Mat& rois, Mat& dst, Mat& mask, Size kernel,
Size stride, Size pad, int poolingType, float spatialScale,
@@ -698,7 +697,7 @@ public:
out.height = 1;
out.width = 1;
}
else if (type == PoolingLayer::ROI)
else if (type == ROI)
{
out.height = pooledSize.height;
out.width = pooledSize.width;
@@ -757,6 +756,14 @@ public:
}
return flops;
}
private:
enum Type
{
MAX,
AVE,
STOCHASTIC,
ROI
};
};
Ptr<PoolingLayer> PoolingLayer::create(const LayerParams& params)