Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Apr 24, 2020
2 parents 15b948d + 8d05dab commit 152e647
Show file tree
Hide file tree
Showing 30 changed files with 1,658 additions and 44 deletions.
2 changes: 1 addition & 1 deletion doc/opencv.bib
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ @inproceedings{Lienhart02
volume = {1},
publisher = {IEEE}
}
@article{Lowe:2004:DIF:993451.996342,
@article{Lowe04,
author = {Lowe, David G.},
title = {Distinctive Image Features from Scale-Invariant Keypoints},
journal = {Int. J. Comput. Vision},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ img1 = cv.imread('box.png',0) # queryImage
img2 = cv.imread('box_in_scene.png',0) # trainImage

# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
Expand Down
4 changes: 2 additions & 2 deletions doc/py_tutorials/py_feature2d/py_matcher/py_matcher.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE) # queryImage
img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage

# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
Expand Down Expand Up @@ -174,7 +174,7 @@ img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE) # queryImage
img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage

# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import cv2 as cv
img = cv.imread('home.jpg')
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)

sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()
kp = sift.detect(gray,None)

img=cv.drawKeypoints(gray,kp,img)
Expand Down Expand Up @@ -151,7 +151,7 @@ Now to calculate the descriptor, OpenCV provides two methods.

We will see the second method:
@code{.py}
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()
kp, des = sift.detectAndCompute(gray,None)
@endcode
Here kp will be a list of keypoints and des is a numpy array of shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Binary descriptors (ORB, BRISK, ...) are matched using the <a href="https://en.w
This distance is equivalent to count the number of different elements for binary strings (population count after applying a XOR operation):
\f[ d_{hamming} \left ( a,b \right ) = \sum_{i=0}^{n-1} \left ( a_i \oplus b_i \right ) \f]

To filter the matches, Lowe proposed in @cite Lowe:2004:DIF:993451.996342 to use a distance ratio test to try to eliminate false matches.
To filter the matches, Lowe proposed in @cite Lowe04 to use a distance ratio test to try to eliminate false matches.
The distance ratio between the two nearest matches of a considered keypoint is computed and it is a good match when this value is below
a threshold. Indeed, this ratio allows helping to discriminate between ambiguous matches (distance ratio between the two nearest neighbors
is close to one) and well discriminated matches. The figure below from the SIFT paper illustrates the probability that a match is correct
Expand Down
11 changes: 11 additions & 0 deletions modules/core/include/opencv2/core/hal/intrin_avx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct v_uint8x32
(char)v22, (char)v23, (char)v24, (char)v25, (char)v26, (char)v27,
(char)v28, (char)v29, (char)v30, (char)v31);
}
/* coverity[uninit_ctor]: suppress warning */
v_uint8x32() {}

uchar get0() const { return (uchar)_v_cvtsi256_si32(val); }
Expand All @@ -184,6 +185,7 @@ struct v_int8x32
v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20,
v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31);
}
/* coverity[uninit_ctor]: suppress warning */
v_int8x32() {}

schar get0() const { return (schar)_v_cvtsi256_si32(val); }
Expand All @@ -205,6 +207,7 @@ struct v_uint16x16
(short)v4, (short)v5, (short)v6, (short)v7, (short)v8, (short)v9,
(short)v10, (short)v11, (short)v12, (short)v13, (short)v14, (short)v15);
}
/* coverity[uninit_ctor]: suppress warning */
v_uint16x16() {}

ushort get0() const { return (ushort)_v_cvtsi256_si32(val); }
Expand All @@ -225,6 +228,7 @@ struct v_int16x16
val = _mm256_setr_epi16(v0, v1, v2, v3, v4, v5, v6, v7,
v8, v9, v10, v11, v12, v13, v14, v15);
}
/* coverity[uninit_ctor]: suppress warning */
v_int16x16() {}

short get0() const { return (short)_v_cvtsi256_si32(val); }
Expand All @@ -243,6 +247,7 @@ struct v_uint32x8
val = _mm256_setr_epi32((unsigned)v0, (unsigned)v1, (unsigned)v2,
(unsigned)v3, (unsigned)v4, (unsigned)v5, (unsigned)v6, (unsigned)v7);
}
/* coverity[uninit_ctor]: suppress warning */
v_uint32x8() {}

unsigned get0() const { return (unsigned)_v_cvtsi256_si32(val); }
Expand All @@ -260,6 +265,7 @@ struct v_int32x8
{
val = _mm256_setr_epi32(v0, v1, v2, v3, v4, v5, v6, v7);
}
/* coverity[uninit_ctor]: suppress warning */
v_int32x8() {}

int get0() const { return _v_cvtsi256_si32(val); }
Expand All @@ -277,6 +283,7 @@ struct v_float32x8
{
val = _mm256_setr_ps(v0, v1, v2, v3, v4, v5, v6, v7);
}
/* coverity[uninit_ctor]: suppress warning */
v_float32x8() {}

float get0() const { return _mm_cvtss_f32(_mm256_castps256_ps128(val)); }
Expand All @@ -291,7 +298,9 @@ struct v_uint64x4
explicit v_uint64x4(__m256i v) : val(v) {}
v_uint64x4(uint64 v0, uint64 v1, uint64 v2, uint64 v3)
{ val = _mm256_setr_epi64x((int64)v0, (int64)v1, (int64)v2, (int64)v3); }
/* coverity[uninit_ctor]: suppress warning */
v_uint64x4() {}

uint64 get0() const
{
#if defined __x86_64__ || defined _M_X64
Expand All @@ -313,6 +322,7 @@ struct v_int64x4
explicit v_int64x4(__m256i v) : val(v) {}
v_int64x4(int64 v0, int64 v1, int64 v2, int64 v3)
{ val = _mm256_setr_epi64x(v0, v1, v2, v3); }
/* coverity[uninit_ctor]: suppress warning */
v_int64x4() {}

int64 get0() const
Expand All @@ -336,6 +346,7 @@ struct v_float64x4
explicit v_float64x4(__m256d v) : val(v) {}
v_float64x4(double v0, double v1, double v2, double v3)
{ val = _mm256_setr_pd(v0, v1, v2, v3); }
/* coverity[uninit_ctor]: suppress warning */
v_float64x4() {}

double get0() const { return _mm_cvtsd_f64(_mm256_castpd256_pd128(val)); }
Expand Down
10 changes: 10 additions & 0 deletions modules/core/include/opencv2/core/hal/intrin_sse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct v_uint8x16
typedef __m128i vector_type;
enum { nlanes = 16 };

/* coverity[uninit_ctor]: suppress warning */
v_uint8x16() {}
explicit v_uint8x16(__m128i v) : val(v) {}
v_uint8x16(uchar v0, uchar v1, uchar v2, uchar v3, uchar v4, uchar v5, uchar v6, uchar v7,
Expand All @@ -100,6 +101,7 @@ struct v_int8x16
typedef __m128i vector_type;
enum { nlanes = 16 };

/* coverity[uninit_ctor]: suppress warning */
v_int8x16() {}
explicit v_int8x16(__m128i v) : val(v) {}
v_int8x16(schar v0, schar v1, schar v2, schar v3, schar v4, schar v5, schar v6, schar v7,
Expand All @@ -125,6 +127,7 @@ struct v_uint16x8
typedef __m128i vector_type;
enum { nlanes = 8 };

/* coverity[uninit_ctor]: suppress warning */
v_uint16x8() {}
explicit v_uint16x8(__m128i v) : val(v) {}
v_uint16x8(ushort v0, ushort v1, ushort v2, ushort v3, ushort v4, ushort v5, ushort v6, ushort v7)
Expand All @@ -147,6 +150,7 @@ struct v_int16x8
typedef __m128i vector_type;
enum { nlanes = 8 };

/* coverity[uninit_ctor]: suppress warning */
v_int16x8() {}
explicit v_int16x8(__m128i v) : val(v) {}
v_int16x8(short v0, short v1, short v2, short v3, short v4, short v5, short v6, short v7)
Expand All @@ -169,6 +173,7 @@ struct v_uint32x4
typedef __m128i vector_type;
enum { nlanes = 4 };

/* coverity[uninit_ctor]: suppress warning */
v_uint32x4() {}
explicit v_uint32x4(__m128i v) : val(v) {}
v_uint32x4(unsigned v0, unsigned v1, unsigned v2, unsigned v3)
Expand All @@ -190,6 +195,7 @@ struct v_int32x4
typedef __m128i vector_type;
enum { nlanes = 4 };

/* coverity[uninit_ctor]: suppress warning */
v_int32x4() {}
explicit v_int32x4(__m128i v) : val(v) {}
v_int32x4(int v0, int v1, int v2, int v3)
Expand All @@ -211,6 +217,7 @@ struct v_float32x4
typedef __m128 vector_type;
enum { nlanes = 4 };

/* coverity[uninit_ctor]: suppress warning */
v_float32x4() {}
explicit v_float32x4(__m128 v) : val(v) {}
v_float32x4(float v0, float v1, float v2, float v3)
Expand All @@ -232,6 +239,7 @@ struct v_uint64x2
typedef __m128i vector_type;
enum { nlanes = 2 };

/* coverity[uninit_ctor]: suppress warning */
v_uint64x2() {}
explicit v_uint64x2(__m128i v) : val(v) {}
v_uint64x2(uint64 v0, uint64 v1)
Expand Down Expand Up @@ -259,6 +267,7 @@ struct v_int64x2
typedef __m128i vector_type;
enum { nlanes = 2 };

/* coverity[uninit_ctor]: suppress warning */
v_int64x2() {}
explicit v_int64x2(__m128i v) : val(v) {}
v_int64x2(int64 v0, int64 v1)
Expand Down Expand Up @@ -286,6 +295,7 @@ struct v_float64x2
typedef __m128d vector_type;
enum { nlanes = 2 };

/* coverity[uninit_ctor]: suppress warning */
v_float64x2() {}
explicit v_float64x2(__m128d v) : val(v) {}
v_float64x2(double v0, double v1)
Expand Down
59 changes: 53 additions & 6 deletions modules/dnn/src/dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,7 +2230,7 @@ struct Net::Impl

auto ieInpNode = inputNodes[i].dynamicCast<InfEngineNgraphNode>();
CV_Assert(oid < ieInpNode->node->get_output_size());
#if INF_ENGINE_VER_MAJOR_GT(2020020000)
#if INF_ENGINE_VER_MAJOR_GT(2020030000)
inputNodes[i] = Ptr<BackendNode>(new InfEngineNgraphNode(ieInpNode->node->get_output_as_single_output_node(oid)));
#else
inputNodes[i] = Ptr<BackendNode>(new InfEngineNgraphNode(ieInpNode->node->get_output_as_single_output_node(oid, false)));
Expand Down Expand Up @@ -3450,6 +3450,8 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
{
CV_TRACE_FUNCTION();

CV_TRACE_REGION("register_inputs");

std::vector<String> inputsNames;
std::vector<MatShape> inp_shapes;
for (auto& it : ieNet.getInputsInfo())
Expand All @@ -3468,6 +3470,8 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
cvNet.setInputShape(inputsNames[inp_id], inp_shapes[inp_id]);
}

CV_TRACE_REGION_NEXT("backendNode");

Ptr<BackendNode> backendNode;
#ifdef HAVE_DNN_NGRAPH
if (DNN_BACKEND_INFERENCE_ENGINE_NGRAPH == getInferenceEngineBackendTypeParam())
Expand All @@ -3489,8 +3493,25 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
#endif
}

CV_TRACE_REGION_NEXT("register_outputs");

#ifdef HAVE_DNN_NGRAPH
auto ngraphFunction = ieNet.getFunction();
#if INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2020_2)
std::list< std::shared_ptr<ngraph::Node> > ngraphOperations;
#else
std::vector< std::shared_ptr<ngraph::Node> > ngraphOperations;
#endif
if (ngraphFunction)
{
ngraphOperations = ngraphFunction->get_ops();
}
#endif

for (auto& it : ieNet.getOutputsInfo())
{
CV_TRACE_REGION("output");

LayerParams lp;
int lid = cvNet.addLayer(it.first, "", lp);

Expand All @@ -3499,15 +3520,38 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
#ifdef HAVE_DNN_NGRAPH
if (DNN_BACKEND_INFERENCE_ENGINE_NGRAPH == getInferenceEngineBackendTypeParam())
{
const auto& outputName = it.first;
Ptr<Layer> cvLayer(new NgraphBackendLayer(ieNet));
cvLayer->name = outputName;
cvLayer->type = "_unknown_";

InferenceEngine::CNNLayerPtr ieLayer = ieNet.getLayerByName(it.first.c_str());
CV_Assert(ieLayer);
if (ngraphFunction)
{
CV_TRACE_REGION("ngraph_function");
bool found = false;
for (const auto& op : ngraphOperations)
{
CV_Assert(op);
if (op->get_friendly_name() == outputName)
{
const std::string typeName = op->get_type_info().name;
cvLayer->type = typeName;
found = true;
break;
}
}
if (!found)
CV_LOG_WARNING(NULL, "DNN/IE: Can't determine output layer type: '" << outputName << "'");
}
else
{
CV_TRACE_REGION("legacy_cnn_layer");
InferenceEngine::CNNLayerPtr ieLayer = ieNet.getLayerByName(it.first.c_str());
CV_Assert(ieLayer);

cvLayer->name = it.first;
cvLayer->type = ieLayer->type;
cvLayer->type = ieLayer->type;
}
ld.layerInstance = cvLayer;

ld.backendNodes[DNN_BACKEND_INFERENCE_ENGINE_NGRAPH] = backendNode;
}
else
Expand All @@ -3532,6 +3576,9 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
for (int i = 0; i < inputsNames.size(); ++i)
cvNet.connect(0, i, lid, i);
}

CV_TRACE_REGION_NEXT("finalize");

cvNet.setPreferableBackend(getInferenceEngineBackendTypeParam());

cvNet.impl->skipInfEngineInit = true;
Expand Down
31 changes: 31 additions & 0 deletions modules/dnn/src/onnx/onnx_graph_simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,36 @@ class ResizeSubgraph2 : public ExtractScalesSubgraph
}
};

class BatchNormalizationSubgraph : public Subgraph
{
public:
BatchNormalizationSubgraph()
{
int input = addNodeToMatch("");
int data1 = addNodeToMatch("Constant");
int data2 = addNodeToMatch("Constant");
int data3 = addNodeToMatch("Constant");
int data4 = addNodeToMatch("Constant");
int shape1 = addNodeToMatch("Constant");
int reshape1 = addNodeToMatch("Reshape", data1, shape1);
int shape2 = addNodeToMatch("Constant");
int reshape2 = addNodeToMatch("Reshape", data2, shape2);
int shape3 = addNodeToMatch("Constant");
int reshape3 = addNodeToMatch("Reshape", data3, shape3);
int shape4 = addNodeToMatch("Constant");
int reshape4 = addNodeToMatch("Reshape", data4, shape4);
int sqrtNode = addNodeToMatch("Sqrt", reshape3);
int A = addNodeToMatch("Constant");
int divNode = addNodeToMatch("Div", A, sqrtNode);
int mul1 = addNodeToMatch("Mul", reshape1, divNode);
int mul2 = addNodeToMatch("Mul", reshape4, mul1);
int sub = addNodeToMatch("Sub", reshape2, mul2);
int mul3 = addNodeToMatch("Mul", input, mul1);
addNodeToMatch("Add", mul3, sub);
setFusedNode("BatchNormalization", input, data1, data2, data4 ,data3);
}
};

void simplifySubgraphs(opencv_onnx::GraphProto& net)
{
std::vector<Ptr<Subgraph> > subgraphs;
Expand All @@ -394,6 +424,7 @@ void simplifySubgraphs(opencv_onnx::GraphProto& net)
subgraphs.push_back(makePtr<NormalizeSubgraph1>());
subgraphs.push_back(makePtr<NormalizeSubgraph2>());
subgraphs.push_back(makePtr<NormalizeSubgraph3>());
subgraphs.push_back(makePtr<BatchNormalizationSubgraph>());

simplifySubgraphs(Ptr<ImportGraphWrapper>(new ONNXGraphWrapper(net)), subgraphs);
}
Expand Down
Loading

0 comments on commit 152e647

Please sign in to comment.