mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Merge pull request #19322 from TolyaTalamanov:at/python-callbacks
[G-API] Introduce cv.gin/cv.descr_of for python * Implement cv.gin/cv.descr_of * Fix macos build * Fix gcomputation tests * Add test * Add using to a void exceeded length for windows build * Add using to a void exceeded length for windows build * Fix comments to review * Fix comments to review * Update from latest master * Avoid graph compilation to obtain in/out info * Fix indentation * Fix comments to review * Avoid using default in switches * Post output meta for giebackend
This commit is contained in:
committed by
GitHub
parent
7bcb51eded
commit
eb82ba36a3
@@ -23,6 +23,31 @@
|
||||
#include "compiler/gmodelbuilder.hpp"
|
||||
#include "compiler/gcompiler.hpp"
|
||||
#include "compiler/gcompiled_priv.hpp"
|
||||
#include "compiler/gstreaming_priv.hpp"
|
||||
|
||||
static cv::GTypesInfo collectInfo(const cv::gimpl::GModel::ConstGraph& g,
|
||||
const std::vector<ade::NodeHandle>& nhs) {
|
||||
cv::GTypesInfo info;
|
||||
info.reserve(nhs.size());
|
||||
|
||||
ade::util::transform(nhs, std::back_inserter(info), [&g](const ade::NodeHandle& nh) {
|
||||
const auto& data = g.metadata(nh).get<cv::gimpl::Data>();
|
||||
return cv::GTypeInfo{data.shape, data.kind, data.ctor};
|
||||
});
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
// NB: This function is used to collect graph input/output info.
|
||||
// Needed for python bridge to unpack inputs and constructs outputs properly.
|
||||
static cv::GraphInfo::Ptr collectGraphInfo(const cv::GComputation::Priv& priv)
|
||||
{
|
||||
auto g = cv::gimpl::GCompiler::makeGraph(priv);
|
||||
cv::gimpl::GModel::ConstGraph cgr(*g);
|
||||
auto in_info = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().in_nhs);
|
||||
auto out_info = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().out_nhs);
|
||||
return cv::GraphInfo::Ptr(new cv::GraphInfo{std::move(in_info), std::move(out_info)});
|
||||
}
|
||||
|
||||
// cv::GComputation private implementation /////////////////////////////////////
|
||||
// <none>
|
||||
@@ -105,8 +130,37 @@ cv::GStreamingCompiled cv::GComputation::compileStreaming(GMetaArgs &&metas, GCo
|
||||
|
||||
cv::GStreamingCompiled cv::GComputation::compileStreaming(GCompileArgs &&args)
|
||||
{
|
||||
// NB: Used by python bridge
|
||||
if (!m_priv->m_info)
|
||||
{
|
||||
m_priv->m_info = collectGraphInfo(*m_priv);
|
||||
}
|
||||
|
||||
cv::gimpl::GCompiler comp(*this, {}, std::move(args));
|
||||
return comp.compileStreaming();
|
||||
auto compiled = comp.compileStreaming();
|
||||
|
||||
compiled.priv().setInInfo(m_priv->m_info->inputs);
|
||||
compiled.priv().setOutInfo(m_priv->m_info->outputs);
|
||||
|
||||
return compiled;
|
||||
}
|
||||
|
||||
cv::GStreamingCompiled cv::GComputation::compileStreaming(const cv::detail::ExtractMetaCallback &callback,
|
||||
GCompileArgs &&args)
|
||||
{
|
||||
// NB: Used by python bridge
|
||||
if (!m_priv->m_info)
|
||||
{
|
||||
m_priv->m_info = collectGraphInfo(*m_priv);
|
||||
}
|
||||
|
||||
auto ins = callback(m_priv->m_info->inputs);
|
||||
cv::gimpl::GCompiler comp(*this, std::move(ins), std::move(args));
|
||||
auto compiled = comp.compileStreaming();
|
||||
compiled.priv().setInInfo(m_priv->m_info->inputs);
|
||||
compiled.priv().setOutInfo(m_priv->m_info->outputs);
|
||||
|
||||
return compiled;
|
||||
}
|
||||
|
||||
// FIXME: Introduce similar query/test method for GMetaArgs as a building block
|
||||
@@ -172,50 +226,25 @@ void cv::GComputation::apply(const std::vector<cv::Mat> &ins,
|
||||
}
|
||||
|
||||
// NB: This overload is called from python code
|
||||
cv::GRunArgs cv::GComputation::apply(GRunArgs &&ins, GCompileArgs &&args)
|
||||
cv::GRunArgs cv::GComputation::apply(const cv::detail::ExtractArgsCallback &callback,
|
||||
GCompileArgs &&args)
|
||||
{
|
||||
recompile(descr_of(ins), std::move(args));
|
||||
// NB: Used by python bridge
|
||||
if (!m_priv->m_info)
|
||||
{
|
||||
m_priv->m_info = collectGraphInfo(*m_priv);
|
||||
}
|
||||
|
||||
const auto& out_info = m_priv->m_lastCompiled.priv().outInfo();
|
||||
auto ins = callback(m_priv->m_info->inputs);
|
||||
recompile(descr_of(ins), std::move(args));
|
||||
|
||||
GRunArgs run_args;
|
||||
GRunArgsP outs;
|
||||
run_args.reserve(out_info.size());
|
||||
outs.reserve(out_info.size());
|
||||
run_args.reserve(m_priv->m_info->outputs.size());
|
||||
outs.reserve(m_priv->m_info->outputs.size());
|
||||
|
||||
cv::detail::constructGraphOutputs(m_priv->m_info->outputs, run_args, outs);
|
||||
|
||||
for (auto&& info : out_info)
|
||||
{
|
||||
switch (info.shape)
|
||||
{
|
||||
case cv::GShape::GMAT:
|
||||
{
|
||||
run_args.emplace_back(cv::Mat{});
|
||||
outs.emplace_back(&cv::util::get<cv::Mat>(run_args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GSCALAR:
|
||||
{
|
||||
run_args.emplace_back(cv::Scalar{});
|
||||
outs.emplace_back(&cv::util::get<cv::Scalar>(run_args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GARRAY:
|
||||
{
|
||||
switch (info.kind)
|
||||
{
|
||||
case cv::detail::OpaqueKind::CV_POINT2F:
|
||||
run_args.emplace_back(cv::detail::VectorRef{std::vector<cv::Point2f>{}});
|
||||
outs.emplace_back(cv::util::get<cv::detail::VectorRef>(run_args.back()));
|
||||
break;
|
||||
default:
|
||||
util::throw_error(std::logic_error("Unsupported kind for GArray"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
util::throw_error(std::logic_error("Only cv::GMat and cv::GScalar are supported for python output"));
|
||||
}
|
||||
}
|
||||
m_priv->m_lastCompiled(std::move(ins), std::move(outs));
|
||||
return run_args;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,13 @@
|
||||
|
||||
namespace cv {
|
||||
|
||||
struct GraphInfo
|
||||
{
|
||||
using Ptr = std::shared_ptr<GraphInfo>;
|
||||
cv::GTypesInfo inputs;
|
||||
cv::GTypesInfo outputs;
|
||||
};
|
||||
|
||||
class GComputation::Priv
|
||||
{
|
||||
public:
|
||||
@@ -36,9 +43,10 @@ public:
|
||||
, Dump // A deserialized graph
|
||||
>;
|
||||
|
||||
GCompiled m_lastCompiled;
|
||||
GMetaArgs m_lastMetas; // TODO: make GCompiled remember its metas?
|
||||
Shape m_shape;
|
||||
GCompiled m_lastCompiled;
|
||||
GMetaArgs m_lastMetas; // TODO: make GCompiled remember its metas?
|
||||
Shape m_shape;
|
||||
GraphInfo::Ptr m_info; // NB: Used by python bridge
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -31,3 +31,48 @@ cv::GRunArg& cv::GRunArg::operator= (cv::GRunArg &&arg) {
|
||||
meta = std::move(arg.meta);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// NB: Construct GRunArgsP based on passed info and store the memory in passed cv::GRunArgs.
|
||||
// Needed for python bridge, because in case python user doesn't pass output arguments to apply.
|
||||
void cv::detail::constructGraphOutputs(const cv::GTypesInfo &out_info,
|
||||
cv::GRunArgs &args,
|
||||
cv::GRunArgsP &outs)
|
||||
{
|
||||
for (auto&& info : out_info)
|
||||
{
|
||||
switch (info.shape)
|
||||
{
|
||||
case cv::GShape::GMAT:
|
||||
{
|
||||
args.emplace_back(cv::Mat{});
|
||||
outs.emplace_back(&cv::util::get<cv::Mat>(args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GSCALAR:
|
||||
{
|
||||
args.emplace_back(cv::Scalar{});
|
||||
outs.emplace_back(&cv::util::get<cv::Scalar>(args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GARRAY:
|
||||
{
|
||||
cv::detail::VectorRef ref;
|
||||
util::get<cv::detail::ConstructVec>(info.ctor)(ref);
|
||||
args.emplace_back(ref);
|
||||
outs.emplace_back(cv::util::get<cv::detail::VectorRef>(args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GOPAQUE:
|
||||
{
|
||||
cv::detail::OpaqueRef ref;
|
||||
util::get<cv::detail::ConstructOpaque>(info.ctor)(ref);
|
||||
args.emplace_back(ref);
|
||||
outs.emplace_back(ref);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
util::throw_error(std::logic_error("Unsupported output shape for python"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -707,7 +707,10 @@ static void PostOutputs(InferenceEngine::InferRequest &request,
|
||||
auto& out_mat = ctx->outMatR(i);
|
||||
IE::Blob::Ptr this_blob = request.GetBlob(ctx->uu.params.output_names[i]);
|
||||
copyFromIE(this_blob, out_mat);
|
||||
ctx->out.post(ctx->output(i));
|
||||
auto output = ctx->output(i);
|
||||
ctx->out.meta(output, cv::GRunArg::Meta{});
|
||||
ctx->out.post(std::move(output));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -904,7 +907,9 @@ struct InferList: public cv::detail::KernelTag {
|
||||
// NB: In case there is no input data need to post output anyway
|
||||
if (in_roi_vec.empty()) {
|
||||
for (auto i : ade::util::iota(ctx->uu.params.num_out)) {
|
||||
ctx->out.post(ctx->output(i));
|
||||
auto output = ctx->output(i);
|
||||
ctx->out.meta(output, cv::GRunArg::Meta{});
|
||||
ctx->out.post(std::move(output));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -940,7 +945,9 @@ struct InferList: public cv::detail::KernelTag {
|
||||
}
|
||||
|
||||
for (auto i : ade::util::iota(ctx->uu.params.num_out)) {
|
||||
ctx->out.post(ctx->output(i));
|
||||
auto output = ctx->output(i);
|
||||
ctx->out.meta(output, cv::GRunArg::Meta{});
|
||||
ctx->out.post(std::move(output));
|
||||
}
|
||||
},
|
||||
[](InferenceEngine::InferRequest &) { /* do nothing */ }
|
||||
@@ -1049,7 +1056,9 @@ struct InferList2: public cv::detail::KernelTag {
|
||||
const auto list_size = ctx->inArg<cv::detail::VectorRef>(1u).size();
|
||||
if (list_size == 0u) {
|
||||
for (auto i : ade::util::iota(ctx->uu.params.num_out)) {
|
||||
ctx->out.post(ctx->output(i));
|
||||
auto output = ctx->output(i);
|
||||
ctx->out.meta(output, cv::GRunArg::Meta{});
|
||||
ctx->out.post(std::move(output));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1103,7 +1112,9 @@ struct InferList2: public cv::detail::KernelTag {
|
||||
}
|
||||
|
||||
for (auto i : ade::util::iota(ctx->uu.params.num_out)) {
|
||||
ctx->out.post(ctx->output(i));
|
||||
auto output = ctx->output(i);
|
||||
ctx->out.meta(output, cv::GRunArg::Meta{});
|
||||
ctx->out.post(std::move(output));
|
||||
}
|
||||
},
|
||||
[](InferenceEngine::InferRequest &) { /* do nothing */ }
|
||||
|
||||
@@ -38,10 +38,6 @@ class GAPI_EXPORTS GCompiled::Priv
|
||||
GMetaArgs m_outMetas; // inferred by compiler
|
||||
std::unique_ptr<cv::gimpl::GExecutor> m_exec;
|
||||
|
||||
// NB: Used by python wrapper to clarify input/output types
|
||||
GTypesInfo m_out_info;
|
||||
GTypesInfo m_in_info;
|
||||
|
||||
void checkArgs(const cv::gimpl::GRuntimeArgs &args) const;
|
||||
|
||||
public:
|
||||
@@ -59,12 +55,6 @@ public:
|
||||
const GMetaArgs& outMetas() const;
|
||||
|
||||
const cv::gimpl::GModel::Graph& model() const;
|
||||
|
||||
void setOutInfo(const GTypesInfo& info) { m_out_info = std::move(info); }
|
||||
const GTypesInfo& outInfo() const { return m_out_info; }
|
||||
|
||||
void setInInfo(const GTypesInfo& info) { m_in_info = std::move(info); }
|
||||
const GTypesInfo& inInfo() const { return m_in_info; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -422,19 +422,6 @@ void cv::gimpl::GCompiler::compileIslands(ade::Graph &g, const cv::GCompileArgs
|
||||
GIslandModel::compileIslands(gim, g, args);
|
||||
}
|
||||
|
||||
static cv::GTypesInfo collectInfo(const cv::gimpl::GModel::ConstGraph& g,
|
||||
const std::vector<ade::NodeHandle>& nhs) {
|
||||
cv::GTypesInfo info;
|
||||
info.reserve(nhs.size());
|
||||
|
||||
ade::util::transform(nhs, std::back_inserter(info), [&g](const ade::NodeHandle& nh) {
|
||||
const auto& data = g.metadata(nh).get<cv::gimpl::Data>();
|
||||
return cv::GTypeInfo{data.shape, data.kind};
|
||||
});
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
cv::GCompiled cv::gimpl::GCompiler::produceCompiled(GPtr &&pg)
|
||||
{
|
||||
// This is the final compilation step. Here:
|
||||
@@ -454,23 +441,15 @@ cv::GCompiled cv::gimpl::GCompiler::produceCompiled(GPtr &&pg)
|
||||
// ...before call to produceCompiled();
|
||||
|
||||
GModel::ConstGraph cgr(*pg);
|
||||
|
||||
const auto &outMetas = GModel::ConstGraph(*pg).metadata()
|
||||
.get<OutputMeta>().outMeta;
|
||||
std::unique_ptr<GExecutor> pE(new GExecutor(std::move(pg)));
|
||||
// FIXME: select which executor will be actually used,
|
||||
// make GExecutor abstract.
|
||||
std::unique_ptr<GExecutor> pE(new GExecutor(std::move(pg)));
|
||||
|
||||
GCompiled compiled;
|
||||
compiled.priv().setup(m_metas, outMetas, std::move(pE));
|
||||
|
||||
// NB: Need to store input/output GTypeInfo to allocate output arrays for python bindings
|
||||
auto out_meta = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().out_nhs);
|
||||
auto in_meta = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().in_nhs);
|
||||
|
||||
compiled.priv().setOutInfo(std::move(out_meta));
|
||||
compiled.priv().setInInfo(std::move(in_meta));
|
||||
|
||||
return compiled;
|
||||
}
|
||||
|
||||
@@ -486,16 +465,8 @@ cv::GStreamingCompiled cv::gimpl::GCompiler::produceStreamingCompiled(GPtr &&pg)
|
||||
outMetas = GModel::ConstGraph(*pg).metadata().get<OutputMeta>().outMeta;
|
||||
}
|
||||
|
||||
|
||||
GModel::ConstGraph cgr(*pg);
|
||||
|
||||
// NB: Need to store input/output GTypeInfo to allocate output arrays for python bindings
|
||||
auto out_meta = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().out_nhs);
|
||||
auto in_meta = collectInfo(cgr, cgr.metadata().get<cv::gimpl::Protocol>().in_nhs);
|
||||
|
||||
compiled.priv().setOutInfo(std::move(out_meta));
|
||||
compiled.priv().setInInfo(std::move(in_meta));
|
||||
|
||||
std::unique_ptr<GStreamingExecutor> pE(new GStreamingExecutor(std::move(pg),
|
||||
m_args));
|
||||
if (!m_metas.empty() && !outMetas.empty())
|
||||
|
||||
@@ -96,6 +96,12 @@ cv::GStreamingCompiled::GStreamingCompiled()
|
||||
{
|
||||
}
|
||||
|
||||
// NB: This overload is called from python code
|
||||
void cv::GStreamingCompiled::setSource(const cv::detail::ExtractArgsCallback& callback)
|
||||
{
|
||||
setSource(callback(m_priv->inInfo()));
|
||||
}
|
||||
|
||||
void cv::GStreamingCompiled::setSource(GRunArgs &&ins)
|
||||
{
|
||||
// FIXME: verify these input parameters according to the graph input meta
|
||||
@@ -119,46 +125,13 @@ bool cv::GStreamingCompiled::pull(cv::GRunArgsP &&outs)
|
||||
|
||||
std::tuple<bool, cv::GRunArgs> cv::GStreamingCompiled::pull()
|
||||
{
|
||||
// FIXME: Why it is not @ priv??
|
||||
GRunArgs run_args;
|
||||
GRunArgsP outs;
|
||||
const auto& out_info = m_priv->outInfo();
|
||||
run_args.reserve(out_info.size());
|
||||
outs.reserve(out_info.size());
|
||||
|
||||
for (auto&& info : out_info)
|
||||
{
|
||||
switch (info.shape)
|
||||
{
|
||||
case cv::GShape::GMAT:
|
||||
{
|
||||
run_args.emplace_back(cv::Mat{});
|
||||
outs.emplace_back(&cv::util::get<cv::Mat>(run_args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GSCALAR:
|
||||
{
|
||||
run_args.emplace_back(cv::Scalar{});
|
||||
outs.emplace_back(&cv::util::get<cv::Scalar>(run_args.back()));
|
||||
break;
|
||||
}
|
||||
case cv::GShape::GARRAY:
|
||||
{
|
||||
switch (info.kind)
|
||||
{
|
||||
case cv::detail::OpaqueKind::CV_POINT2F:
|
||||
run_args.emplace_back(cv::detail::VectorRef{std::vector<cv::Point2f>{}});
|
||||
outs.emplace_back(cv::util::get<cv::detail::VectorRef>(run_args.back()));
|
||||
break;
|
||||
default:
|
||||
util::throw_error(std::logic_error("Unsupported kind for GArray"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
util::throw_error(std::logic_error("Only cv::GMat and cv::GScalar are supported for python output"));
|
||||
}
|
||||
}
|
||||
cv::detail::constructGraphOutputs(m_priv->outInfo(), run_args, outs);
|
||||
|
||||
bool is_over = m_priv->pull(std::move(outs));
|
||||
return std::make_tuple(is_over, run_args);
|
||||
|
||||
Reference in New Issue
Block a user