mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge pull request #22037 from xiong-jie-y:py_gapi_add_state_kernel
Add stateful kernel to python G-API
This commit is contained in:
@@ -6,26 +6,25 @@
|
||||
|
||||
#include <ade/util/zip_range.hpp> // zip_range, indexed
|
||||
|
||||
#include "compiler/gmodel.hpp"
|
||||
#include <opencv2/gapi/garg.hpp>
|
||||
#include <opencv2/gapi/util/throw.hpp> // throw_error
|
||||
#include <opencv2/gapi/python/python.hpp>
|
||||
|
||||
#include "api/gbackend_priv.hpp"
|
||||
#include "backends/common/gbackend.hpp"
|
||||
|
||||
cv::gapi::python::GPythonKernel::GPythonKernel(cv::gapi::python::Impl run)
|
||||
: m_run(run)
|
||||
cv::gapi::python::GPythonKernel::GPythonKernel(cv::gapi::python::Impl runf,
|
||||
cv::gapi::python::Setup setupf)
|
||||
: run(runf), setup(setupf), is_stateful(setup != nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
cv::GRunArgs cv::gapi::python::GPythonKernel::operator()(const cv::gapi::python::GPythonContext& ctx)
|
||||
{
|
||||
return m_run(ctx);
|
||||
}
|
||||
|
||||
cv::gapi::python::GPythonFunctor::GPythonFunctor(const char* id,
|
||||
const cv::gapi::python::GPythonFunctor::Meta &meta,
|
||||
const cv::gapi::python::Impl& impl)
|
||||
: gapi::GFunctor(id), impl_{GPythonKernel{impl}, meta}
|
||||
const cv::gapi::python::GPythonFunctor::Meta& meta,
|
||||
const cv::gapi::python::Impl& impl,
|
||||
const cv::gapi::python::Setup& setup)
|
||||
: gapi::GFunctor(id), impl_{GPythonKernel{impl, setup}, meta}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -68,6 +67,7 @@ class GPythonExecutable final: public cv::gimpl::GIslandExecutable
|
||||
virtual cv::RMat allocate(const cv::GMatDesc&) const override { return {}; }
|
||||
|
||||
virtual bool canReshape() const override { return true; }
|
||||
virtual void handleNewStream() override;
|
||||
virtual void reshape(ade::Graph&, const cv::GCompileArgs&) override {
|
||||
// Do nothing here
|
||||
}
|
||||
@@ -80,6 +80,7 @@ public:
|
||||
cv::gimpl::GModel::ConstGraph m_gm;
|
||||
cv::gapi::python::GPythonKernel m_kernel;
|
||||
ade::NodeHandle m_op;
|
||||
cv::GArg m_node_state;
|
||||
|
||||
cv::GTypesInfo m_out_info;
|
||||
cv::GMetaArgs m_in_metas;
|
||||
@@ -153,6 +154,15 @@ static void writeBack(cv::GRunArg& arg, cv::GRunArgP& out)
|
||||
}
|
||||
}
|
||||
|
||||
void GPythonExecutable::handleNewStream()
|
||||
{
|
||||
if (!m_kernel.is_stateful)
|
||||
return;
|
||||
|
||||
m_node_state = m_kernel.setup(cv::gimpl::GModel::collectInputMeta(m_gm, m_op),
|
||||
m_gm.metadata(m_op).get<cv::gimpl::Op>().args);
|
||||
}
|
||||
|
||||
void GPythonExecutable::run(std::vector<InObj> &&input_objs,
|
||||
std::vector<OutObj> &&output_objs)
|
||||
{
|
||||
@@ -165,9 +175,15 @@ void GPythonExecutable::run(std::vector<InObj> &&input_objs,
|
||||
std::back_inserter(inputs),
|
||||
std::bind(&packArg, std::ref(m_res), _1));
|
||||
|
||||
cv::gapi::python::GPythonContext ctx{inputs, m_in_metas, m_out_info, /*state*/{}};
|
||||
|
||||
cv::gapi::python::GPythonContext ctx{inputs, m_in_metas, m_out_info};
|
||||
auto outs = m_kernel(ctx);
|
||||
// NB: For stateful kernel add state to its execution context
|
||||
if (m_kernel.is_stateful)
|
||||
{
|
||||
ctx.m_state = cv::optional<cv::GArg>(m_node_state);
|
||||
}
|
||||
|
||||
auto outs = m_kernel.run(ctx);
|
||||
|
||||
for (auto&& it : ade::util::zip(outs, output_objs))
|
||||
{
|
||||
@@ -225,6 +241,12 @@ GPythonExecutable::GPythonExecutable(const ade::Graph& g,
|
||||
m_op = *it;
|
||||
m_kernel = cag.metadata(m_op).get<PythonUnit>().kernel;
|
||||
|
||||
// If kernel is stateful then prepare storage for its state.
|
||||
if (m_kernel.is_stateful)
|
||||
{
|
||||
m_node_state = cv::GArg{ };
|
||||
}
|
||||
|
||||
// Ensure this the only op in the graph
|
||||
if (std::any_of(it+1, nodes.end(), is_op))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user