Merge pull request #12264 from dkurt:dnn_remove_forward_method
* Remove a forward method in dnn::Layer * Add a test * Fix tests * Mark multiple dnn::Layer::finalize methods as deprecated * Replace back dnn's inputBlobs to vector of pointers * Remove Layer::forward_fallback from CV_OCL_RUN scopes
This commit is contained in:
committed by
Alexander Alekhin
parent
8e8315abfd
commit
d486204a0d
@@ -6,7 +6,8 @@
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
#include <opencv2/core/ocl.hpp>
|
||||
#include <opencv2/core/opencl/ocl_defs.hpp>
|
||||
#include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
|
||||
|
||||
namespace opencv_test { namespace {
|
||||
@@ -87,9 +88,13 @@ public:
|
||||
return Ptr<Layer>(new FirstCustomLayer(params));
|
||||
}
|
||||
|
||||
virtual void forward(InputArrayOfArrays, OutputArrayOfArrays, OutputArrayOfArrays) CV_OVERRIDE {}
|
||||
virtual void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat>& internals) CV_OVERRIDE
|
||||
void forward(InputArrayOfArrays, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
|
||||
|
||||
std::vector<Mat> outputs;
|
||||
outputs_arr.getMatVector(outputs);
|
||||
outputs[0].setTo(1);
|
||||
}
|
||||
};
|
||||
@@ -104,9 +109,13 @@ public:
|
||||
return Ptr<Layer>(new SecondCustomLayer(params));
|
||||
}
|
||||
|
||||
virtual void forward(InputArrayOfArrays, OutputArrayOfArrays, OutputArrayOfArrays) CV_OVERRIDE {}
|
||||
virtual void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat>& internals) CV_OVERRIDE
|
||||
void forward(InputArrayOfArrays, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays) CV_OVERRIDE
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
|
||||
|
||||
std::vector<Mat> outputs;
|
||||
outputs_arr.getMatVector(outputs);
|
||||
outputs[0].setTo(2);
|
||||
}
|
||||
};
|
||||
@@ -178,4 +187,125 @@ INSTANTIATE_TEST_CASE_P(/**/, setInput, Combine(
|
||||
dnnBackendsAndTargets()
|
||||
));
|
||||
|
||||
class CustomLayerWithDeprecatedForward CV_FINAL : public Layer
|
||||
{
|
||||
public:
|
||||
CustomLayerWithDeprecatedForward(const LayerParams ¶ms) : Layer(params) {}
|
||||
|
||||
static Ptr<Layer> create(LayerParams& params)
|
||||
{
|
||||
return Ptr<Layer>(new CustomLayerWithDeprecatedForward(params));
|
||||
}
|
||||
|
||||
virtual void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat> &internals) CV_OVERRIDE
|
||||
{
|
||||
CV_Assert_N(inputs[0]->depth() == CV_32F, outputs[0].depth() == CV_32F);
|
||||
cv::add(*inputs[0], 0.5f, outputs[0]);
|
||||
}
|
||||
};
|
||||
|
||||
class CustomLayerWithDeprecatedForwardAndFallback CV_FINAL : public Layer
|
||||
{
|
||||
public:
|
||||
CustomLayerWithDeprecatedForwardAndFallback(const LayerParams ¶ms) : Layer(params) {}
|
||||
|
||||
static Ptr<Layer> create(LayerParams& params)
|
||||
{
|
||||
return Ptr<Layer>(new CustomLayerWithDeprecatedForwardAndFallback(params));
|
||||
}
|
||||
|
||||
void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals) CV_OVERRIDE
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
|
||||
|
||||
CV_OCL_RUN(preferableTarget == DNN_TARGET_OPENCL || preferableTarget == DNN_TARGET_OPENCL_FP16,
|
||||
forward_ocl(inputs, outputs, internals));
|
||||
|
||||
Layer::forward_fallback(inputs, outputs, internals);
|
||||
}
|
||||
|
||||
virtual void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat> &internals) CV_OVERRIDE
|
||||
{
|
||||
CV_Assert_N(inputs[0]->depth() == CV_32F, outputs[0].depth() == CV_32F);
|
||||
cv::add(*inputs[0], 0.5f, outputs[0]);
|
||||
}
|
||||
|
||||
#ifdef HAVE_OPENCL
|
||||
bool forward_ocl(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr)
|
||||
{
|
||||
if (inputs_arr.depth() != CV_32F)
|
||||
return false;
|
||||
|
||||
std::vector<UMat> inputs;
|
||||
std::vector<UMat> outputs;
|
||||
inputs_arr.getUMatVector(inputs);
|
||||
outputs_arr.getUMatVector(outputs);
|
||||
cv::add(inputs[0], 0.5f, outputs[0]);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef testing::TestWithParam<tuple<Backend, Target> > DeprecatedForward;
|
||||
TEST_P(DeprecatedForward, CustomLayer)
|
||||
{
|
||||
const int backend = get<0>(GetParam());
|
||||
const int target = get<1>(GetParam());
|
||||
|
||||
Mat inp(5, 5, CV_32FC1);
|
||||
randu(inp, -1.0f, 1.0f);
|
||||
inp = blobFromImage(inp);
|
||||
|
||||
CV_DNN_REGISTER_LAYER_CLASS(CustomType, CustomLayerWithDeprecatedForward);
|
||||
try
|
||||
{
|
||||
LayerParams lp;
|
||||
Net net;
|
||||
net.addLayerToPrev("testLayer", "CustomType", lp);
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
net.setInput(inp);
|
||||
Mat out = net.forward();
|
||||
normAssert(out, inp + 0.5f, "", 2e-4, 7e-4);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LayerFactory::unregisterLayer("CustomType");
|
||||
throw;
|
||||
}
|
||||
LayerFactory::unregisterLayer("CustomType");
|
||||
}
|
||||
|
||||
TEST_P(DeprecatedForward, CustomLayerWithFallback)
|
||||
{
|
||||
const int backend = get<0>(GetParam());
|
||||
const int target = get<1>(GetParam());
|
||||
|
||||
Mat inp(5, 5, CV_32FC1);
|
||||
randu(inp, -1.0f, 1.0f);
|
||||
inp = blobFromImage(inp);
|
||||
|
||||
CV_DNN_REGISTER_LAYER_CLASS(CustomType, CustomLayerWithDeprecatedForwardAndFallback);
|
||||
try
|
||||
{
|
||||
LayerParams lp;
|
||||
Net net;
|
||||
net.addLayerToPrev("testLayer", "CustomType", lp);
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
net.setInput(inp);
|
||||
Mat out = net.forward();
|
||||
normAssert(out, inp + 0.5f, "", 2e-4, 7e-4);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LayerFactory::unregisterLayer("CustomType");
|
||||
throw;
|
||||
}
|
||||
LayerFactory::unregisterLayer("CustomType");
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, DeprecatedForward, dnnBackendsAndTargets());
|
||||
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user