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

Merge pull request #18127 from smirnov-alexey:as/gapi_serialization

[G-API]: Add GOpaque and GArray serialization support

* Add GOpaque and GArray serialization support

* Address review comments

* Remove holds() method

* Address review comments

* Remove comments

* Align streaming with kind changes

* Fix kind in kernel

* Address review comments
This commit is contained in:
Alexey Smirnov
2020-09-07 20:10:03 +03:00
committed by GitHub
parent 620629593b
commit a3e8c6e866
26 changed files with 648 additions and 228 deletions
+6 -1
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -35,6 +35,11 @@ void cv::detail::GArrayU::setConstructFcn(ConstructVec &&cv)
m_priv->ctor = std::move(cv);
}
void cv::detail::GArrayU::setKind(cv::detail::OpaqueKind kind)
{
m_priv->kind = kind;
}
namespace cv {
std::ostream& operator<<(std::ostream& os, const cv::GArrayDesc &)
{
+6 -1
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) 2019 Intel Corporation
// Copyright (C) 2019-2020 Intel Corporation
#include "precomp.hpp"
@@ -35,6 +35,11 @@ void cv::detail::GOpaqueU::setConstructFcn(ConstructOpaque &&co)
m_priv->ctor = std::move(co);
}
void cv::detail::GOpaqueU::setKind(cv::detail::OpaqueKind kind)
{
m_priv->kind = kind;
}
namespace cv {
std::ostream& operator<<(std::ostream& os, const cv::GOpaqueDesc &)
{
+5 -4
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -14,13 +14,14 @@
cv::GOrigin::GOrigin(GShape s,
const cv::GNode& n,
std::size_t p,
const cv::gimpl::HostCtor c)
: shape(s), node(n), port(p), ctor(c)
const cv::gimpl::HostCtor c,
cv::detail::OpaqueKind k)
: shape(s), node(n), port(p), ctor(c), kind(k)
{
}
cv::GOrigin::GOrigin(GShape s, cv::gimpl::ConstVal v)
: shape(s), node(cv::GNode::Const()), value(v), port(INVALID_PORT)
: shape(s), node(cv::GNode::Const()), value(v), port(INVALID_PORT), kind(cv::detail::OpaqueKind::CV_UNKNOWN)
{
}
+4 -3
View File
@@ -2,8 +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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#ifndef OPENCV_GAPI_GORIGIN_HPP
#define OPENCV_GAPI_GORIGIN_HPP
@@ -30,7 +29,8 @@ struct GOrigin
GOrigin(GShape s,
const GNode& n,
std::size_t p = INVALID_PORT,
const gimpl::HostCtor h = {});
const gimpl::HostCtor h = {},
cv::detail::OpaqueKind kind = cv::detail::OpaqueKind::CV_UNKNOWN);
GOrigin(GShape s, gimpl::ConstVal value);
const GShape shape; // Shape of a produced object
@@ -38,6 +38,7 @@ struct GOrigin
const gimpl::ConstVal value; // Node can have initial constant value, now only scalar is supported
const std::size_t port; // GNode's output number; FIXME: "= max_size" in C++14
gimpl::HostCtor ctor; // FIXME: replace with an interface?
detail::OpaqueKind kind; // primary is needed for GOpaque and GArray
};
namespace detail
+35
View File
@@ -44,6 +44,8 @@ std::vector<char> cv::gapi::serialize(const cv::GRunArgs& ra)
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)
{
cv::GRunArgsP outputs;
@@ -77,3 +79,36 @@ cv::GRunArgsP cv::gapi::bind(cv::GRunArgs &results)
}
return outputs;
}
// FIXME: move it out of s11n to api/
// FIXME: don't we have such function already?
cv::GRunArg cv::gapi::bind(cv::GRunArgP &out)
{
using T = cv::GRunArgP;
switch (out.index())
{
#if !defined(GAPI_STANDALONE)
case T::index_of<cv::UMat*>() :
GAPI_Assert(false && "Please implement this!");
break;
#endif
case T::index_of<cv::detail::VectorRef>() :
return cv::GRunArg(cv::util::get<cv::detail::VectorRef>(out));
case T::index_of<cv::detail::OpaqueRef>() :
return cv::GRunArg(cv::util::get<cv::detail::OpaqueRef>(out));
case T::index_of<cv::Mat*>() :
return cv::GRunArg(*cv::util::get<cv::Mat*>(out));
case T::index_of<cv::Scalar*>() :
return cv::GRunArg(*cv::util::get<cv::Scalar*>(out));
default:
// ...maybe our types were extended
GAPI_Assert(false && "This value type is UNKNOWN!");
break;
}
return cv::GRunArg();
}
@@ -292,25 +292,85 @@ I::IStream& operator >> (I::IStream& is, cv::gapi::wip::IStreamSource::Ptr &)
return is;
}
I::OStream& operator<< (I::OStream& os, const cv::detail::VectorRef &)
namespace
{
GAPI_Assert(false && "Serialization: Unsupported << for cv::detail::VectorRef &");
template<typename Ref, typename T, typename... Ts>
struct putToStream;
template<typename Ref>
struct putToStream<Ref, std::tuple<>>
{
static void put(I::OStream&, const Ref &)
{
GAPI_Assert(false && "Unsupported type for GArray/GOpaque serialization");
}
};
template<typename Ref, typename T, typename... Ts>
struct putToStream<Ref, std::tuple<T, Ts...>>
{
static void put(I::OStream& os, const Ref &r)
{
if (r.getKind() == cv::detail::GOpaqueTraits<T>::kind) {
os << r.template rref<T>();
} else {
putToStream<Ref, std::tuple<Ts...> >::put(os, r);
}
}
};
template<typename Ref, typename T, typename... Ts>
struct getFromStream;
template<typename Ref>
struct getFromStream<Ref, std::tuple<>>
{
static void get(I::IStream&, Ref &, cv::detail::OpaqueKind)
{
GAPI_Assert(false && "Unsupported type for GArray/GOpaque deserialization");
}
};
template<typename Ref, typename T, typename... Ts>
struct getFromStream<Ref, std::tuple<T, Ts...>>
{
static void get(I::IStream& is, Ref &r, cv::detail::OpaqueKind kind) {
if (kind == cv::detail::GOpaqueTraits<T>::kind) {
r.template reset<T>();
auto& val = r.template wref<T>();
is >> val;
} else {
getFromStream<Ref, std::tuple<Ts...> >::get(is, r, kind);
}
}
};
}
I::OStream& operator<< (I::OStream& os, const cv::detail::VectorRef& ref)
{
os << ref.getKind();
putToStream<cv::detail::VectorRef, cv::detail::GOpaqueTraitsArrayTypes>::put(os, ref);
return os;
}
I::IStream& operator >> (I::IStream& is, cv::detail::VectorRef &)
I::IStream& operator >> (I::IStream& is, cv::detail::VectorRef& ref)
{
GAPI_Assert(false && "Serialization: Unsupported >> for cv::detail::VectorRef &");
cv::detail::OpaqueKind kind;
is >> kind;
getFromStream<cv::detail::VectorRef, cv::detail::GOpaqueTraitsArrayTypes>::get(is, ref, kind);
return is;
}
I::OStream& operator<< (I::OStream& os, const cv::detail::OpaqueRef &)
I::OStream& operator<< (I::OStream& os, const cv::detail::OpaqueRef& ref)
{
GAPI_Assert(false && "Serialization: Unsupported << for cv::detail::OpaqueRef &");
os << ref.getKind();
putToStream<cv::detail::OpaqueRef, cv::detail::GOpaqueTraitsOpaqueTypes>::put(os, ref);
return os;
}
I::IStream& operator >> (I::IStream& is, cv::detail::OpaqueRef &)
I::IStream& operator >> (I::IStream& is, cv::detail::OpaqueRef& ref)
{
GAPI_Assert(false && "Serialization: Unsupported >> for cv::detail::OpaqueRef &");
cv::detail::OpaqueKind kind;
is >> kind;
getFromStream<cv::detail::OpaqueRef, cv::detail::GOpaqueTraitsOpaqueTypes>::get(is, ref, kind);
return is;
}
// Enums and structures
@@ -350,7 +410,6 @@ I::IStream& operator>> (I::IStream& is, cv::gimpl::Data::Storage &s) {
return get_enum<cv::gimpl::Data::Storage>(is, s);
}
I::OStream& operator<< (I::OStream& os, const cv::GArg &arg) {
// Only GOBJREF and OPAQUE_VAL kinds can be serialized/deserialized
GAPI_Assert( arg.kind == cv::detail::ArgKind::OPAQUE_VAL
@@ -376,6 +435,7 @@ I::OStream& operator<< (I::OStream& os, const cv::GArg &arg) {
}
return os;
}
I::IStream& operator>> (I::IStream& is, cv::GArg &arg) {
is >> arg.kind >> arg.opaque_kind;
@@ -447,12 +507,50 @@ I::IStream& operator>> (I::IStream& is, cv::gimpl::Op &op) {
I::OStream& operator<< (I::OStream& os, const cv::gimpl::Data &d) {
// FIXME: HostCtor is not stored here!!
// FIXME: Storage may be incorrect for subgraph-to-graph process
return os << d.shape << d.rc << d.meta << d.storage;
return os << d.shape << d.rc << d.meta << d.storage << d.kind;
}
namespace
{
template<typename Ref, typename T, typename... Ts>
struct initCtor;
template<typename Ref>
struct initCtor<Ref, std::tuple<>>
{
static void init(cv::gimpl::Data&)
{
GAPI_Assert(false && "Unsupported type for GArray/GOpaque deserialization");
}
};
template<typename Ref, typename T, typename... Ts>
struct initCtor<Ref, std::tuple<T, Ts...>>
{
static void init(cv::gimpl::Data& d) {
if (d.kind == cv::detail::GOpaqueTraits<T>::kind) {
static std::function<void(Ref&)> ctor = [](Ref& ref){ref.template reset<T>();};
d.ctor = ctor;
} else {
initCtor<Ref, std::tuple<Ts...> >::init(d);
}
}
};
} // anonymous namespace
I::IStream& operator>> (I::IStream& is, cv::gimpl::Data &d) {
// FIXME: HostCtor is not stored here!!
// FIXME: Storage may be incorrect for subgraph-to-graph process
return is >> d.shape >> d.rc >> d.meta >> d.storage;
is >> d.shape >> d.rc >> d.meta >> d.storage >> d.kind;
if (d.shape == cv::GShape::GARRAY)
{
initCtor<cv::detail::VectorRef, cv::detail::GOpaqueTraitsArrayTypes>::init(d);
}
else if (d.shape == cv::GShape::GOPAQUE)
{
initCtor<cv::detail::OpaqueRef, cv::detail::GOpaqueTraitsOpaqueTypes>::init(d);
}
return is;
}
@@ -478,6 +576,14 @@ void serialize( I::OStream& os
, const ade::Graph &g
, const std::vector<ade::NodeHandle> &nodes) {
cv::gimpl::GModel::ConstGraph cg(g);
serialize(os, g, cg.metadata().get<cv::gimpl::Protocol>(), nodes);
}
void serialize( I::OStream& os
, const ade::Graph &g
, const cv::gimpl::Protocol &p
, const std::vector<ade::NodeHandle> &nodes) {
cv::gimpl::GModel::ConstGraph cg(g);
GSerialized s;
for (auto &nh : nodes) {
switch (cg.metadata(nh).get<NodeType>().t)
@@ -488,7 +594,7 @@ void serialize( I::OStream& os
}
}
s.m_counter = cg.metadata().get<cv::gimpl::DataObjectCounter>();
s.m_proto = cg.metadata().get<cv::gimpl::Protocol>();
s.m_proto = p;
os << s.m_ops << s.m_datas << s.m_counter << s.m_proto;
}
@@ -121,7 +121,6 @@ GAPI_EXPORTS I::IStream& operator>> (I::IStream& is, cv::GArg &arg);
//I::OStream& operator<< (I::OStream& os, const cv::GRunArg &arg);
//I::IStream& operator>> (I::IStream& is, cv::GRunArg &arg);
GAPI_EXPORTS I::OStream& operator<< (I::OStream& os, const cv::GKernel &k);
GAPI_EXPORTS I::IStream& operator>> (I::IStream& is, cv::GKernel &k);
@@ -167,6 +166,14 @@ GAPI_EXPORTS void serialize( I::OStream& os
, const ade::Graph &g
, const std::vector<ade::NodeHandle> &nodes);
// The top-level serialization routine.
// Note it is just a single function which takes a GModel and a list of nodes
// and writes the data to the stream (recursively)
GAPI_EXPORTS void serialize( I::OStream& os
, const ade::Graph &g
, const cv::gimpl::Protocol &p
, const std::vector<ade::NodeHandle> &nodes);
// The top-level deserialization routineS.
// Unfortunately the deserialization is a two-step process:
// 1. First we decode a stream into some intermediate representation
@@ -194,7 +194,7 @@ void cv::gimpl::GCPUExecutable::run(std::vector<InObj> &&input_objs,
{
const auto &desc = gm.metadata(nh).get<Data>();
if ( desc.storage == Data::Storage::INTERNAL
if ( desc.storage == Data::Storage::INTERNAL // FIXME: to reconsider
&& !util::holds_alternative<util::monostate>(desc.ctor))
{
// FIXME: Note that compile-time constant data objects (like
+4 -4
View File
@@ -614,7 +614,7 @@ struct InferList2: public cv::detail::KernelTag {
GAPI_Assert(util::holds_alternative<cv::GArrayDesc>(mm)
&& "Non-array inputs are not supported");
if (op.k.inSpecs[idx] == cv::detail::ArgSpec::RECT) {
if (op.k.inKinds[idx] == cv::detail::OpaqueKind::CV_RECT) {
// This is a cv::Rect -- configure the IE preprocessing
ii->setPrecision(toIE(meta_0.depth));
ii->getPreProcess().setResizeAlgorithm(IE::RESIZE_BILINEAR);
@@ -622,7 +622,7 @@ struct InferList2: public cv::detail::KernelTag {
// This is a cv::GMat (equals to: cv::Mat)
// Just validate that it is really the type
// (other types are prohibited here)
GAPI_Assert(op.k.inSpecs[idx] == cv::detail::ArgSpec::GMAT);
GAPI_Assert(op.k.inKinds[idx] == cv::detail::OpaqueKind::CV_MAT);
}
idx++; // NB: Never forget to increment the counter
}
@@ -666,11 +666,11 @@ struct InferList2: public cv::detail::KernelTag {
GAPI_Assert(this_vec.size() == list_size);
// Prepare input {{{
IE::Blob::Ptr this_blob;
if (this_vec.spec() == cv::detail::TypeSpec::RECT) {
if (this_vec.getKind() == cv::detail::OpaqueKind::CV_RECT) {
// ROI case - create an ROI blob
const auto &vec = this_vec.rref<cv::Rect>();
this_blob = IE::make_shared_blob(blob_0, toIE(vec[list_idx]));
} else if (this_vec.spec() == cv::detail::TypeSpec::MAT) {
} else if (this_vec.getKind() == cv::detail::OpaqueKind::CV_MAT) {
// Mat case - create a regular blob
// FIXME: NOW Assume Mats are always BLOBS (not
// images)
+4 -3
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -54,7 +54,7 @@ ade::NodeHandle GModel::mkDataNode(GModel::Graph &g, const GOrigin& origin)
// associated host-type constructor (e.g. when the array is
// somewhere in the middle of the graph).
auto ctor_copy = origin.ctor;
g.metadata(data_h).set(Data{origin.shape, id, meta, ctor_copy, storage});
g.metadata(data_h).set(Data{origin.shape, id, meta, ctor_copy, origin.kind, storage});
return data_h;
}
@@ -67,8 +67,9 @@ ade::NodeHandle GModel::mkDataNode(GModel::Graph &g, const GShape shape)
GMetaArg meta;
HostCtor ctor;
Data::Storage storage = Data::Storage::INTERNAL; // By default, all objects are marked INTERNAL
cv::detail::OpaqueKind kind = cv::detail::OpaqueKind::CV_UNKNOWN;
g.metadata(data_h).set(Data{shape, id, meta, ctor, storage});
g.metadata(data_h).set(Data{shape, id, meta, ctor, kind, storage});
return data_h;
}
+3 -1
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#ifndef OPENCV_GAPI_GMODEL_HPP
@@ -26,6 +26,7 @@
#include <opencv2/gapi/garg.hpp>
#include <opencv2/gapi/gkernel.hpp>
#include <opencv2/gapi/gcommon.hpp>
#include "compiler/gobjref.hpp"
#include "compiler/gislandmodel.hpp"
@@ -71,6 +72,7 @@ struct Data
int rc;
GMetaArg meta;
HostCtor ctor; // T-specific helper to deal with unknown types in our code
cv::detail::OpaqueKind kind; // FIXME: is needed to store GArray/GOpaque type
// FIXME: Why rc+shape+meta is not represented as RcDesc here?
enum class Storage: int
+2 -2
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
////////////////////////////////////////////////////////////////////////////////
@@ -139,7 +139,7 @@ cv::gimpl::Unrolled cv::gimpl::unrollExpr(const GProtoArgs &ins,
std::size_t port = ade::util::index(it);
GShape shape = ade::util::value(it);
GOrigin org { shape, node, port};
GOrigin org { shape, node, port, {}, origin.kind };
origins.insert(org);
}
-1
View File
@@ -55,7 +55,6 @@ namespace detail
template<> struct GTypeTraits<cv::gimpl::RcDesc>
{
static constexpr const ArgKind kind = ArgKind::GOBJREF;
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
};
}
+16 -13
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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -94,18 +94,21 @@ void cv::gimpl::passes::inferMeta(ade::passes::PassContext &ctx, bool meta_is_in
GAPI_Assert(gr.metadata(output_nh).get<NodeType>().t == NodeType::DATA);
auto &output_meta = gr.metadata(output_nh).get<Data>().meta;
if (!meta_is_initialized && !util::holds_alternative<util::monostate>(output_meta))
{
GAPI_LOG_INFO(NULL,
"!!! Output object has an initialized meta - "
"how it is possible today?" << std::endl; );
if (output_meta != out_metas.at(output_port))
{
util::throw_error(std::logic_error("Fatal: meta mismatch"));
// FIXME: New exception type?
// FIXME: More details!
}
}
cv::util::suppress_unused_warning(meta_is_initialized);
// FIXME: calling compile() with meta the second time when cannot reshape will lead to error below
//if (!meta_is_initialized && !util::holds_alternative<util::monostate>(output_meta))
//{
// GAPI_LOG_INFO(NULL,
// "!!! Output object has an initialized meta - "
// "how it is possible today?" << std::endl; );
// if (output_meta != out_metas.at(output_port))
// {
// util::throw_error(std::logic_error("Fatal: meta mismatch"));
// // FIXME: New exception type?
// // FIXME: More details!
// }
//}
// Store meta in graph
output_meta = out_metas.at(output_port);
}