mirror of https://github.com/opencv/opencv.git
Merge pull request #18339 from rgarnov:rg/rmat_integration
[GAPI] RMat integration into the framework * RMat integration * Added initialization of input mat in GArray initialization tests * Fixed klocwork warnings in RMat tests, changed argument order in EXPECT_EQpull/18494/head
parent
199687a1c5
commit
5224d016e9
31 changed files with 659 additions and 229 deletions
@ -0,0 +1,23 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#include <opencv2/gapi/rmat.hpp> |
||||
|
||||
using View = cv::RMat::View; |
||||
|
||||
// There is an issue with default generated operator=(View&&) on Mac:
|
||||
// it doesn't nullify m_cb of a moved object
|
||||
View& View::operator=(View&& v) { |
||||
m_desc = v.m_desc; |
||||
m_data = v.m_data; |
||||
m_step = v.m_step; |
||||
m_cb = v.m_cb; |
||||
v.m_desc = {}; |
||||
v.m_data = nullptr; |
||||
v.m_step = 0u; |
||||
v.m_cb = nullptr; |
||||
return *this; |
||||
} |
@ -0,0 +1,170 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#include "../test_precomp.hpp" |
||||
#include <opencv2/gapi/rmat.hpp> |
||||
#include "rmat_test_common.hpp" |
||||
|
||||
#include <opencv2/gapi/fluid/imgproc.hpp> |
||||
#include <opencv2/gapi/cpu/imgproc.hpp> |
||||
|
||||
namespace opencv_test |
||||
{ |
||||
|
||||
// This test set takes RMat type as a template parameter and launces simple
|
||||
// blur(isl1) -> blur(isl2) computation passing RMat as input, as output
|
||||
// and both input and output
|
||||
template<typename RMatAdapterT> |
||||
struct RMatIntTestBase { |
||||
cv::Mat in_mat; |
||||
cv::Mat out_mat; |
||||
cv::Mat out_mat_ref; |
||||
cv::GComputation comp; |
||||
bool inCallbackCalled; |
||||
bool outCallbackCalled; |
||||
|
||||
static constexpr int w = 8; |
||||
static constexpr int h = 8; |
||||
|
||||
RMatIntTestBase() |
||||
: in_mat(h, w, CV_8UC1) |
||||
, out_mat(h, w, CV_8UC1) |
||||
, out_mat_ref(h, w, CV_8UC1) |
||||
, comp([](){ |
||||
cv::GMat in; |
||||
auto tmp = cv::gapi::blur(in, {3,3}); |
||||
auto out = cv::gapi::blur(tmp, {3,3}); |
||||
cv::gapi::island("test", cv::GIn(in), cv::GOut(tmp)); |
||||
return cv::GComputation(in, out); |
||||
}) |
||||
, inCallbackCalled(false) |
||||
, outCallbackCalled(false) { |
||||
cv::randu(in_mat, cv::Scalar::all(127), cv::Scalar::all(40)); |
||||
} |
||||
|
||||
void check() { |
||||
comp.apply(in_mat, out_mat_ref); |
||||
EXPECT_EQ(0, cvtest::norm(out_mat_ref, out_mat, NORM_INF)); |
||||
} |
||||
|
||||
RMat createRMat(cv::Mat& mat, bool& callbackCalled) { |
||||
return {cv::make_rmat<RMatAdapterT>(mat, callbackCalled)}; |
||||
} |
||||
}; |
||||
|
||||
template<typename RMatAdapterT> |
||||
struct RMatIntTest : public RMatIntTestBase<RMatAdapterT> |
||||
{ |
||||
template<typename In, typename Out> |
||||
void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) { |
||||
for (int i = 0; i < 2; i++) { |
||||
EXPECT_FALSE(this->inCallbackCalled); |
||||
EXPECT_FALSE(this->outCallbackCalled); |
||||
auto compile_args_copy = compile_args; |
||||
this->comp.apply(cv::gin(in), cv::gout(out), std::move(compile_args_copy)); |
||||
EXPECT_FALSE(this->inCallbackCalled); |
||||
if (std::is_same<RMat,Out>::value) { |
||||
EXPECT_TRUE(this->outCallbackCalled); |
||||
} else { |
||||
EXPECT_FALSE(this->outCallbackCalled); |
||||
} |
||||
this->outCallbackCalled = false; |
||||
} |
||||
this->check(); |
||||
} |
||||
}; |
||||
|
||||
template<typename RMatAdapterT> |
||||
struct RMatIntTestStreaming : public RMatIntTestBase<RMatAdapterT> |
||||
{ |
||||
template <typename M> |
||||
cv::GMatDesc getDesc(const M& m) { return cv::descr_of(m); } |
||||
|
||||
void checkOutput(const cv::Mat&) { this->check(); } |
||||
|
||||
void checkOutput(const RMat& rm) { |
||||
auto view = rm.access(RMat::Access::R); |
||||
this->out_mat = cv::Mat(view.size(), view.type(), view.ptr()); |
||||
this->check(); |
||||
} |
||||
|
||||
template<typename In, typename Out> |
||||
void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) { |
||||
auto sc = this->comp.compileStreaming(getDesc(in), std::move(compile_args)); |
||||
|
||||
sc.setSource(cv::gin(in)); |
||||
sc.start(); |
||||
|
||||
std::size_t frame = 0u; |
||||
constexpr std::size_t num_frames = 10u; |
||||
EXPECT_FALSE(this->inCallbackCalled); |
||||
EXPECT_FALSE(this->outCallbackCalled); |
||||
while (sc.pull(cv::gout(out)) && frame < num_frames) { |
||||
frame++; |
||||
this->checkOutput(out); |
||||
EXPECT_FALSE(this->inCallbackCalled); |
||||
EXPECT_FALSE(this->outCallbackCalled); |
||||
} |
||||
EXPECT_EQ(num_frames, frame); |
||||
} |
||||
}; |
||||
|
||||
struct OcvKernels { |
||||
cv::gapi::GKernelPackage kernels() { return cv::gapi::imgproc::cpu::kernels(); } |
||||
}; |
||||
struct FluidKernels { |
||||
cv::gapi::GKernelPackage kernels() { return cv::gapi::imgproc::fluid::kernels(); } |
||||
}; |
||||
|
||||
struct RMatIntTestCpuRef : public |
||||
RMatIntTest<RMatAdapterRef>, OcvKernels {}; |
||||
struct RMatIntTestCpuCopy : public |
||||
RMatIntTest<RMatAdapterCopy>, OcvKernels {}; |
||||
struct RMatIntTestCpuRefStreaming : public |
||||
RMatIntTestStreaming<RMatAdapterRef>, OcvKernels {}; |
||||
struct RMatIntTestCpuCopyStreaming : public |
||||
RMatIntTestStreaming<RMatAdapterCopy>, OcvKernels {}; |
||||
struct RMatIntTestCpuRefFluid : public |
||||
RMatIntTest<RMatAdapterRef>, FluidKernels {}; |
||||
struct RMatIntTestCpuCopyFluid : public |
||||
RMatIntTest<RMatAdapterCopy>, FluidKernels {}; |
||||
struct RMatIntTestCpuRefStreamingFluid : public |
||||
RMatIntTestStreaming<RMatAdapterRef>, FluidKernels {}; |
||||
struct RMatIntTestCpuCopyStreamingFluid : public |
||||
RMatIntTestStreaming<RMatAdapterCopy>, FluidKernels {}; |
||||
|
||||
template<typename T> |
||||
struct RMatIntTypedTest : public ::testing::Test, public T {}; |
||||
|
||||
using RMatIntTestTypes = ::testing::Types< RMatIntTestCpuRef |
||||
, RMatIntTestCpuCopy |
||||
, RMatIntTestCpuRefStreaming |
||||
, RMatIntTestCpuCopyStreaming |
||||
, RMatIntTestCpuRefFluid |
||||
, RMatIntTestCpuCopyFluid |
||||
, RMatIntTestCpuRefStreamingFluid |
||||
, RMatIntTestCpuCopyStreamingFluid |
||||
>; |
||||
|
||||
TYPED_TEST_CASE(RMatIntTypedTest, RMatIntTestTypes); |
||||
|
||||
TYPED_TEST(RMatIntTypedTest, In) { |
||||
auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled); |
||||
this->run(in_rmat, this->out_mat, cv::compile_args(this->kernels())); |
||||
} |
||||
|
||||
TYPED_TEST(RMatIntTypedTest, Out) { |
||||
auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled); |
||||
this->run(this->in_mat, out_rmat, cv::compile_args(this->kernels())); |
||||
} |
||||
|
||||
TYPED_TEST(RMatIntTypedTest, InOut) { |
||||
auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled); |
||||
auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled); |
||||
this->run(in_rmat, out_rmat, cv::compile_args(this->kernels())); |
||||
} |
||||
|
||||
} // namespace opencv_test
|
@ -0,0 +1,61 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_RMAT_TESTS_COMMON_HPP |
||||
#define OPENCV_GAPI_RMAT_TESTS_COMMON_HPP |
||||
|
||||
#include "../test_precomp.hpp" |
||||
#include <opencv2/gapi/rmat.hpp> |
||||
|
||||
namespace opencv_test { |
||||
class RMatAdapterRef : public RMat::Adapter { |
||||
cv::Mat& m_mat; |
||||
bool& m_callbackCalled; |
||||
public: |
||||
RMatAdapterRef(cv::Mat& m, bool& callbackCalled) |
||||
: m_mat(m), m_callbackCalled(callbackCalled) |
||||
{} |
||||
virtual RMat::View access(RMat::Access access) override { |
||||
if (access == RMat::Access::W) { |
||||
return RMat::View(cv::descr_of(m_mat), m_mat.data, m_mat.step, |
||||
[this](){ |
||||
EXPECT_FALSE(m_callbackCalled); |
||||
m_callbackCalled = true; |
||||
}); |
||||
} else { |
||||
return RMat::View(cv::descr_of(m_mat), m_mat.data, m_mat.step); |
||||
} |
||||
} |
||||
virtual cv::GMatDesc desc() const override { return cv::descr_of(m_mat); } |
||||
}; |
||||
|
||||
class RMatAdapterCopy : public RMat::Adapter { |
||||
cv::Mat& m_deviceMat; |
||||
cv::Mat m_hostMat; |
||||
bool& m_callbackCalled; |
||||
|
||||
public: |
||||
RMatAdapterCopy(cv::Mat& m, bool& callbackCalled) |
||||
: m_deviceMat(m), m_hostMat(m.clone()), m_callbackCalled(callbackCalled) |
||||
{} |
||||
virtual RMat::View access(RMat::Access access) override { |
||||
if (access == RMat::Access::W) { |
||||
return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, m_hostMat.step, |
||||
[this](){ |
||||
EXPECT_FALSE(m_callbackCalled); |
||||
m_callbackCalled = true; |
||||
m_hostMat.copyTo(m_deviceMat); |
||||
}); |
||||
} else { |
||||
m_deviceMat.copyTo(m_hostMat); |
||||
return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, m_hostMat.step); |
||||
} |
||||
} |
||||
virtual cv::GMatDesc desc() const override { return cv::descr_of(m_hostMat); } |
||||
}; |
||||
} // namespace opencv_test
|
||||
|
||||
#endif // OPENCV_GAPI_RMAT_TESTS_COMMON_HPP
|
Loading…
Reference in new issue