From dadb1473c10529f85bf43788fba14bd89d2ac0f5 Mon Sep 17 00:00:00 2001 From: Liubov Batanina Date: Mon, 29 Apr 2019 10:29:34 +0300 Subject: [PATCH] Add BatchNorm3d layer --- modules/dnn/src/layers/batch_norm_layer.cpp | 7 +++++-- modules/dnn/test/test_onnx_importer.cpp | 7 +++++++ modules/dnn/test/test_tf_importer.cpp | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/dnn/src/layers/batch_norm_layer.cpp b/modules/dnn/src/layers/batch_norm_layer.cpp index 4c69c247c4..6c1fc6ef29 100644 --- a/modules/dnn/src/layers/batch_norm_layer.cpp +++ b/modules/dnn/src/layers/batch_norm_layer.cpp @@ -29,6 +29,8 @@ class BatchNormLayerImpl CV_FINAL : public BatchNormLayer public: Mat weights_, bias_; UMat umat_weight, umat_bias; + mutable int dims; + BatchNormLayerImpl(const LayerParams& params) { @@ -142,6 +144,7 @@ public: std::vector &outputs, std::vector &internals) const CV_OVERRIDE { + dims = inputs[0].size(); if (!useGlobalStats && inputs[0][0] != 1) CV_Error(Error::StsNotImplemented, "Batch normalization in training mode with batch size > 1"); Layer::getMemoryShapes(inputs, requiredOutputs, outputs, internals); @@ -150,9 +153,9 @@ public: virtual bool supportBackend(int backendId) CV_OVERRIDE { - return backendId == DNN_BACKEND_OPENCV || + return (backendId == DNN_BACKEND_OPENCV && (dims == 4 || dims == 2)) || (backendId == DNN_BACKEND_HALIDE && haveHalide()) || - (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine()); + (backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine() && (preferableTarget == DNN_TARGET_CPU || dims == 4)); } #ifdef HAVE_OPENCL diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index f1b0a81e8e..10402a3dda 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -167,6 +167,13 @@ TEST_P(Test_ONNX_layers, BatchNormalization) testONNXModels("batch_norm"); } +TEST_P(Test_ONNX_layers, BatchNormalization3D) +{ + if (backend != DNN_BACKEND_INFERENCE_ENGINE || target != DNN_TARGET_CPU) + throw SkipTestException("Only DLIE backend on CPU is supported"); + testONNXModels("batch_norm_3d"); +} + TEST_P(Test_ONNX_layers, Transpose) { if (backend == DNN_BACKEND_INFERENCE_ENGINE && diff --git a/modules/dnn/test/test_tf_importer.cpp b/modules/dnn/test/test_tf_importer.cpp index e662da53e7..64a56ee94f 100644 --- a/modules/dnn/test/test_tf_importer.cpp +++ b/modules/dnn/test/test_tf_importer.cpp @@ -188,6 +188,13 @@ TEST_P(Test_TensorFlow_layers, batch_norm) runTensorFlowNet("mvn_batch_norm_1x1"); } +TEST_P(Test_TensorFlow_layers, batch_norm3D) +{ + if (backend != DNN_BACKEND_INFERENCE_ENGINE || target != DNN_TARGET_CPU) + throw SkipTestException("Only DLIE backend on CPU is supported"); + runTensorFlowNet("batch_norm3d"); +} + TEST_P(Test_TensorFlow_layers, slim_batch_norm) { if (backend == DNN_BACKEND_INFERENCE_ENGINE)