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

Merge remote-tracking branch 'upstream/3.4' into merge-3.4

This commit is contained in:
Alexander Alekhin
2021-10-07 04:27:22 +00:00
7 changed files with 70 additions and 14 deletions
+4 -2
View File
@@ -391,20 +391,22 @@ class dnn_test(NewOpenCVTests):
raise unittest.SkipTest("Missing DNN test files (dnn/onnx/data/{input/output}_hidden_lstm.npy). "
"Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.")
net = cv.dnn.readNet(model)
input = np.load(input_file)
# we have to expand the shape of input tensor because Python bindings cut 3D tensors to 2D
# it should be fixed in future. see : https://github.com/opencv/opencv/issues/19091
# please remove `expand_dims` after that
input = np.expand_dims(input, axis=3)
gold_output = np.load(output_file)
net.setInput(input)
for backend, target in self.dnnBackendsAndTargets:
printParams(backend, target)
net = cv.dnn.readNet(model)
net.setPreferableBackend(backend)
net.setPreferableTarget(target)
net.setInput(input)
real_output = net.forward()
normAssert(self, real_output, gold_output, "", getDefaultThreshold(target))
@@ -19,6 +19,16 @@ CV__DNN_INLINE_NS_BEGIN
using ::google::protobuf::RepeatedField;
using ::google::protobuf::MapPair;
static Mat getTensorContentRef_(const tensorflow::TensorProto& tensor);
static inline
bool isAlignedMat(const Mat& m)
{
int depth = m.depth();
int alignment = CV_ELEM_SIZE1(depth);
return (((size_t)m.data) & (alignment - 1)) == 0;
}
class TFNodeWrapper : public ImportNodeWrapper
{
public:
@@ -719,8 +729,19 @@ public:
{
if (!negativeScales)
{
Mat scales = getTensorContent(inputNodes[1]->attr().at("value").tensor(), /*copy*/false);
scales *= -1;
Mat scalesRef = getTensorContentRef_(inputNodes[1]->attr().at("value").tensor());
// FIXME: This breaks the const guarantees of tensor() by writing to scalesRef
if (isAlignedMat(scalesRef))
{
scalesRef *= -1;
}
else
{
Mat scales = scalesRef.clone() * -1;
CV_Assert(scalesRef.isContinuous());
CV_Assert(scales.isContinuous());
memcpy(scalesRef.data, scales.data, scales.total() * scales.elemSize());
}
}
}
@@ -832,7 +853,8 @@ void RemoveIdentityOps(tensorflow::GraphDef& net)
}
}
Mat getTensorContent(const tensorflow::TensorProto &tensor, bool copy)
// NB: returned Mat::data pointer may be unaligned
Mat getTensorContentRef_(const tensorflow::TensorProto& tensor)
{
const std::string& content = tensor.tensor_content();
Mat m;
@@ -904,7 +926,18 @@ Mat getTensorContent(const tensorflow::TensorProto &tensor, bool copy)
CV_Error(Error::StsError, "Tensor's data type is not supported");
break;
}
return copy ? m.clone() : m;
return m;
}
Mat getTensorContent(const tensorflow::TensorProto& tensor, bool forceCopy)
{
// If necessary clone m to have aligned data pointer
Mat m = getTensorContentRef_(tensor);
if (forceCopy || !isAlignedMat(m))
return m.clone();
else
return m;
}
void releaseTensor(tensorflow::TensorProto* tensor)
@@ -21,7 +21,7 @@ void RemoveIdentityOps(tensorflow::GraphDef& net);
void simplifySubgraphs(tensorflow::GraphDef& net);
Mat getTensorContent(const tensorflow::TensorProto &tensor, bool copy = true);
Mat getTensorContent(const tensorflow::TensorProto& tensor, bool forceCopy = true);
void releaseTensor(tensorflow::TensorProto* tensor);
@@ -124,8 +124,10 @@ void parseTensor(const tensorflow::TensorProto &tensor, Mat &dstBlob)
}
dstBlob.create(shape, CV_32F);
CV_Assert(dstBlob.isContinuous());
Mat tensorContent = getTensorContent(tensor, /*no copy*/false);
CV_Assert(tensorContent.isContinuous());
int size = tensorContent.total();
CV_Assert(size == (int)dstBlob.total());
@@ -2671,8 +2673,10 @@ void TFImporter::kernelFromTensor(const tensorflow::TensorProto &tensor, Mat &ds
out_c = shape[0]; input_c = shape[1];
dstBlob.create(shape, CV_32F);
CV_Assert(dstBlob.isContinuous());
Mat tensorContent = getTensorContent(tensor, /*no copy*/false);
CV_Assert(tensorContent.isContinuous());
int size = tensorContent.total();
CV_Assert(size == (int)dstBlob.total());