Skip to content

[WIP] G-API: Wrap GOpaque#18900

Closed
TolyaTalamanov wants to merge 2 commits intoopencv:masterfrom
TolyaTalamanov:at/wrap-gopaque
Closed

[WIP] G-API: Wrap GOpaque#18900
TolyaTalamanov wants to merge 2 commits intoopencv:masterfrom
TolyaTalamanov:at/wrap-gopaque

Conversation

@TolyaTalamanov
Copy link
Copy Markdown
Contributor

@TolyaTalamanov TolyaTalamanov commented Nov 23, 2020

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Build configuration

Xforce_builders_only=linux,docs
force_builders=Custom,Custom Win,Custom Mac
build_gapi_standalone:Linux x64=ade-0.1.1f
build_gapi_standalone:Win64=ade-0.1.1f
build_gapi_standalone:Mac=ade-0.1.1f
build_gapi_standalone:Linux x64 Debug=ade-0.1.1f

Xbuild_image:Custom=centos:7
Xbuildworker:Custom=linux-1
build_gapi_standalone:Custom=ade-0.1.1f

build_image:Custom=ubuntu-openvino-2021.1.0:20.04
build_image:Custom Win=openvino-2021.1.0
build_image:Custom Mac=openvino-2021.1.0

test_modules:Custom=gapi
test_modules:Custom Win=gapi
test_modules:Custom Mac=gapi

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

{
args.emplace_back(reinterpret_cast<pyopencv_GMat_t*>(item)->v);
}
else if (PyObject_TypeCheck(item, reinterpret_cast<PyTypeObject*>(pyopencv_GArrayP2f_TypePtr)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since each instance for GArray and GOpaque is a separate object we need to have a huge if here, we can make a separate object for Python that will contain all types for GArray and GOpaque and will be created like this:
g_in = GArray(cv.detail.OpaqueKind.RECT).
I didn't do this because OpaqueKind is located in the internal namespace detail, let's discuss it locally

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant

@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 27, 2020

@TolyaTalamanov Please rebase this PR

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@dkurt @smirnov-alexey Could you take a look, please ?

@TolyaTalamanov TolyaTalamanov force-pushed the at/wrap-gopaque branch 2 times, most recently from ea95239 to 6c6180c Compare December 16, 2020 10:23
return GRunArgsP{ GRunArgP(detail::wrap_host_helper<Ts>::wrap_out(args))... };
}

// NB: Used by python wrapper
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should it be placed ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in gcommon?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to address this?

};

using GArrayP2f = GArray<cv::Point2f>;
using GRects = GArray<cv::Rect>;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to implement wrapper for GArray & GOpaque to avoid making using for every GArray & GOpaque type ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need using everywhere? And why does it bother you? I don't think you need an extra wrapper here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is impossible to wrap template object into python, so I just make using and wrap it in shadow_gapi.hpp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


// NB: Used by python wrapper
using GSize = GOpaque<cv::Size>;
using GRect = GOpaque<cv::Rect>;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to implement wrapper for GArray & GOpaque to avoid making using for every GArray & GOpaque type ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


// NB: Python wrapper generate T_U for T<U>
// This behavior is only observed for inputs
using GOpaque_Size = cv::GOpaque<cv::Size>;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be fixed on python code generator side ?

Copy link
Copy Markdown
Contributor

@smirnov-alexey smirnov-alexey Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what do you mean by using using. Does this behavior block us?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it can be fixed like this

{
args.emplace_back(reinterpret_cast<pyopencv_GArrayP2f_t*>(item)->v.strip());
}
else if (PyObject_TypeCheck(item, reinterpret_cast<PyTypeObject*>(pyopencv_GSize_TypePtr)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really ugly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@smirnov-alexey @dmatveev Could you check that ?

struct GTypeInfo;
using GTypesInfo = std::vector<GTypeInfo>;

using ExtractArgsCallback = std::function<cv::GRunArgs(const cv::GTypesInfo& info)>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it relevant to garg.hpp at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, maybe we need a special place to move python stuff to there

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put a FIXME here (mentioning where it is actually required)


# G-API
g_in = cv.GMat()
g_out = cv.gapi.boundingRect(g_in)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does this work without defining GOpaque<Size> as GSize ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it's needed to wrap GOpaque<Size> into python somehow

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant
use cv.GOpaqueT(cv.gapi.CV_SIZE)

comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))

actual = comp.apply(cv.gin(points))
print('actual = ', actual)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also.

I want to create a graph which has GOpaque<cv::Rect> among of its inputs. How does it looks like in Python?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

g_in = cv.GRect()

BTW, need to remove this print

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant

@TolyaTalamanov TolyaTalamanov force-pushed the at/wrap-gopaque branch 2 times, most recently from 8f1e0e6 to 4b85848 Compare December 28, 2020 12:50
}

template<>
bool pyopencv_to_generic_vec<bool>(PyObject* obj, std::vector<bool>& value, const ArgInfo& info)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek Do you mind if I add this specialization ?

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@dmatveev @smirnov-alexey Could you have a look ?

@smirnov-alexey
Copy link
Copy Markdown
Contributor

@TolyaTalamanov I would recommend to switch the build to openvino 2021.01 due to red tests

}
}

cv::detail::GOpaqueU strip() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I would rename that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ? What's your suggestion ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit harsh word but maybe it's just me:) You could use unpack or extract

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, don't think that it's really big effort to change that. Let's wait for the @dmatveev comments.

Copy link
Copy Markdown
Contributor

@smirnov-alexey smirnov-alexey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some minor comments left but overall looks good

Copy link
Copy Markdown
Contributor

@dmatveev dmatveev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much changes here. Type lists are constantly duplicated. The graph compilation flow is not clear now. No description provided to the MR.

If possible, I'd break it into smaller parts for an easier review. With such massive changes it is quite easy to break old contracts.

struct GTypeInfo;
using GTypesInfo = std::vector<GTypeInfo>;

using ExtractArgsCallback = std::function<cv::GRunArgs(const cv::GTypesInfo& info)>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put a FIXME here (mentioning where it is actually required)


/// @private -- Exclude this function from OpenCV documentation
GAPI_WRAP GRunArgs apply(GRunArgs &&ins, GCompileArgs &&args = {});
GAPI_WRAP GRunArgs apply(const cv::ExtractArgsCallback& callback, GCompileArgs &&args = {});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cv::ExtractArgsCallback shouldn't be in cv::. cv::detail:: is better.

*/
GAPI_WRAP void setSource(GRunArgs &&ins);
void setSource(GRunArgs &&ins);
GAPI_WRAP void setSource(const cv::ExtractArgsCallback& callback);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate these two, and make this 2nd setSource "private" for doxygen.

CV_SIZE,
CV_RECT,
CV_SCALAR,
CV_MAT,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare this list of CV_XXX / type pairs once, and avoid all the code duplication below.


struct GAPI_EXPORTS_W_SIMPLE GArrayT
{
using Storage = MakeVariantType< cv::GArray
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is cv::GArray here?

{
if (PyObject_TypeCheck(obj, reinterpret_cast<PyTypeObject*>(pyopencv_GOpaqueT_TypePtr)))
{
auto& opaque = reinterpret_cast<pyopencv_GOpaqueT_t*>(obj)->v;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??


auto ins = callback(m_priv->m_lastCompiled.priv().inInfo());

// NB: After getting the arguments, recompile the graph with the correct meta
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double compilation may be costly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..especially when dealing with NNs.

util::throw_error(std::logic_error("Unsupported kind for GArray"));
}
cv::detail::VectorRef ref;
util::get<cv::detail::ConstructVec>(info.ctor)(ref);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if any other function in this file/class operates on the same low level of abstraction - it is all built on some higher-level entities. Please keep the abstraction level.

// FIXME: select which executor will be actually used,
// make GExecutor abstract.
pE.reset(new GExecutor(std::move(pg)));
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the above comment still valid after your changes?

std::unique_ptr<ade::Graph> pG = generateGraph();
runPasses(*pG);
compileIslands(*pG);
if (!m_metas.empty())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can compilation go a wrong way now? What if I call a regular compile() (like in e.g. v4.2) with an empty metadata vector - what will happen?

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

Was split and merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants