Merge pull request #19617 from smirnov-alexey:as/extend_kernel_package_api

G-API: Extend GKernelPackage and serialization API

* Extend GKernelPackage API

* Adding tests

* Extend serialization API

* Address review comments
This commit is contained in:
Alexey Smirnov 2021-03-10 18:58:34 +03:00 committed by GitHub
parent 04d907fb97
commit ddd2447192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 7 deletions

View File

@ -2,7 +2,7 @@
// 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) 2018-2020 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
#ifndef OPENCV_GAPI_GKERNEL_HPP
@ -517,6 +517,13 @@ namespace gapi {
*/
const std::vector<GTransform>& get_transformations() const;
/**
* @brief Returns vector of kernel ids included in the package
*
* @return vector of kernel ids included in the package
*/
std::vector<std::string> get_kernel_ids() const;
/**
* @brief Test if a particular kernel _implementation_ KImpl is
* included in this kernel package.
@ -606,6 +613,18 @@ namespace gapi {
includeHelper<KImpl>();
}
/**
* @brief Adds a new kernel based on it's backend and id into the kernel package
*
* @param backend backend associated with the kernel
* @param kernel_id a name/id of the kernel
*/
void include(const cv::gapi::GBackend& backend, const std::string& kernel_id)
{
removeAPI(kernel_id);
m_id_kernels[kernel_id] = std::make_pair(backend, GKernelImpl{{}, {}});
}
/**
* @brief Lists all backends which are included into package
*

View File

@ -2,7 +2,7 @@
// 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
// Copyright (C) 2020-2021 Intel Corporation
#ifndef OPENCV_GAPI_S11N_HPP
#define OPENCV_GAPI_S11N_HPP
@ -24,6 +24,8 @@ namespace detail {
GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector<char> &p);
GAPI_EXPORTS std::vector<std::string> getVectorOfStrings(const std::vector<char> &p);
template<typename... Types>
cv::GCompileArgs getCompileArgs(const std::vector<char> &p);
@ -42,6 +44,7 @@ T deserialize(const std::vector<char> &p);
GAPI_EXPORTS std::vector<char> serialize(const cv::GCompileArgs&);
GAPI_EXPORTS std::vector<char> serialize(const cv::GMetaArgs&);
GAPI_EXPORTS std::vector<char> serialize(const cv::GRunArgs&);
GAPI_EXPORTS std::vector<char> serialize(const std::vector<std::string>&);
template<> inline
cv::GComputation deserialize(const std::vector<char> &p) {
@ -58,6 +61,11 @@ cv::GRunArgs deserialize(const std::vector<char> &p) {
return detail::getRunArgs(p);
}
template<> inline
std::vector<std::string> deserialize(const std::vector<char> &p) {
return detail::getVectorOfStrings(p);
}
template<typename T, typename... Types> inline
typename std::enable_if<std::is_same<T, GCompileArgs>::value, GCompileArgs>::
type deserialize(const std::vector<char> &p) {

View File

@ -2,7 +2,7 @@
// 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) 2018-2019 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
#include "precomp.hpp"
@ -55,6 +55,16 @@ const std::vector<cv::GTransform> &cv::gapi::GKernelPackage::get_transformations
return m_transformations;
}
std::vector<std::string> cv::gapi::GKernelPackage::get_kernel_ids() const
{
std::vector<std::string> ids;
for (auto &&id : m_id_kernels)
{
ids.emplace_back(id.first);
}
return ids;
}
cv::gapi::GKernelPackage cv::gapi::combine(const GKernelPackage &lhs,
const GKernelPackage &rhs)
{

View File

@ -2,7 +2,7 @@
// 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
// Copyright (C) 2020-2021 Intel Corporation
#include <opencv2/gapi/s11n.hpp>
#include <opencv2/gapi/garg.hpp>
@ -30,6 +30,11 @@ cv::GRunArgs cv::gapi::detail::getRunArgs(const std::vector<char> &p) {
return run_args_deserialize(is);
}
std::vector<std::string> cv::gapi::detail::getVectorOfStrings(const std::vector<char> &p) {
cv::gapi::s11n::ByteMemoryInStream is(p);
return vector_of_strings_deserialize(is);
}
std::vector<char> cv::gapi::serialize(const cv::GMetaArgs& ma)
{
cv::gapi::s11n::ByteMemoryOutStream os;
@ -51,6 +56,13 @@ std::vector<char> cv::gapi::serialize(const cv::GCompileArgs& ca)
return os.data();
}
std::vector<char> cv::gapi::serialize(const std::vector<std::string>& vs)
{
cv::gapi::s11n::ByteMemoryOutStream os;
serialize(os, vs);
return os.data();
}
// FIXME: This function should move from S11N to GRunArg-related entities.
// it has nothing to do with the S11N as it is
cv::GRunArgsP cv::gapi::bind(cv::GRunArgs &results)

View File

@ -2,7 +2,7 @@
// 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
// Copyright (C) 2020-2021 Intel Corporation
#include <set> // set
#include <map> // map
@ -906,6 +906,9 @@ GAPI_EXPORTS void serialize(IOStream& os, const cv::GMetaArgs &ma) {
GAPI_EXPORTS void serialize(IOStream& os, const cv::GRunArgs &ra) {
os << ra;
}
GAPI_EXPORTS void serialize(IOStream& os, const std::vector<std::string> &vs) {
os << vs;
}
GAPI_EXPORTS GMetaArgs meta_args_deserialize(IIStream& is) {
GMetaArgs s;
is >> s;
@ -916,6 +919,11 @@ GAPI_EXPORTS GRunArgs run_args_deserialize(IIStream& is) {
is >> s;
return s;
}
GAPI_EXPORTS std::vector<std::string> vector_of_strings_deserialize(IIStream& is) {
std::vector<std::string> s;
is >> s;
return s;
}
} // namespace s11n
} // namespace gapi

View File

@ -5,7 +5,7 @@
// 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
// Copyright (C) 2020-2021 Intel Corporation
#include <iostream>
#include <fstream>
@ -217,8 +217,10 @@ GAPI_EXPORTS std::unique_ptr<IIStream> getInStream(const std::vector<char> &p);
GAPI_EXPORTS void serialize(IOStream& os, const cv::GCompileArgs &ca);
GAPI_EXPORTS void serialize(IOStream& os, const cv::GMetaArgs &ma);
GAPI_EXPORTS void serialize(IOStream& os, const cv::GRunArgs &ra);
GAPI_EXPORTS void serialize(IOStream& os, const std::vector<std::string> &vs);
GAPI_EXPORTS GMetaArgs meta_args_deserialize(IIStream& is);
GAPI_EXPORTS GRunArgs run_args_deserialize(IIStream& is);
GAPI_EXPORTS std::vector<std::string> vector_of_strings_deserialize(IIStream& is);
} // namespace s11n
} // namespace gapi

View File

@ -2,8 +2,9 @@
// 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) 2018 Intel Corporation
// Copyright (C) 2018-2021 Intel Corporation
#include <algorithm>
#include "test_precomp.hpp"
#include "gapi_mock_kernels.hpp"
@ -146,6 +147,29 @@ TEST(KernelPackage, Includes)
EXPECT_FALSE(pkg.includes<J::Qux>());
}
TEST(KernelPackage, Include)
{
namespace J = Jupiter;
auto pkg = cv::gapi::kernels();
pkg.include(J::backend(), "test.kernels.foo");
pkg.include(J::backend(), "test.kernels.bar");
EXPECT_TRUE (pkg.includes<J::Foo>());
EXPECT_TRUE (pkg.includes<J::Bar>());
}
TEST(KernelPackage, GetIds)
{
namespace J = Jupiter;
auto pkg = cv::gapi::kernels();
pkg.include(J::backend(), "test.kernels.foo");
pkg.include(J::backend(), "test.kernels.bar");
pkg.include<J::Baz>();
auto ids = pkg.get_kernel_ids();
EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.foo"));
EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.bar"));
EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.baz"));
}
TEST(KernelPackage, IncludesAPI)
{
namespace J = Jupiter;

View File

@ -571,6 +571,16 @@ TEST_F(S11N_Basic, Test_Bind_RunArgs_MatScalar) {
}
}
TEST_F(S11N_Basic, Test_Vector_Of_Strings) {
std::vector<std::string> vs{"hello", "world", "42"};
const std::vector<char> ser = cv::gapi::serialize(vs);
auto des = cv::gapi::deserialize<std::vector<std::string>>(ser);
EXPECT_EQ("hello", des[0]);
EXPECT_EQ("world", des[1]);
EXPECT_EQ("42", des[2]);
}
TEST_F(S11N_Basic, Test_RunArg_RMat) {
cv::Mat mat = cv::Mat::eye(cv::Size(128, 64), CV_8UC3);
cv::RMat rmat = cv::make_rmat<MyRMatAdapter>(mat, 42, "It actually works");