Skip to content

Commit f0c411d

Browse files
authored
Merge pull request opencv#17502 from dmatveev:dm/infer2
* G-API: Introduce a new gapi::infer2 overload + gaze estimation sample * G-API/infer2: Introduced static type checking for infer2 - Also added extra tests on the type check routine * G-API/infer2: Addressed self-review comments in the sample app - Also fix build on Linux; * G-API/infer2: Remove incorrect SetLayout(HWC) + dead code - Also fixed comments in the backend * G-API/infer2: Continue with self-review - Fix warnings/compile errors in gaze estimation - Dropped the use of RTTI (VectorRef::holds()) from the giebackend - Replaced it with a trait-based enums for GArray<T> and std::vector<T> - The enums and traits are temporary and need to be unified with the S11N when it comes * G-API/infer2: Final self-review items - Refactored ROIList test to cover 70% for infer<> and infer2<>; - Fixed the model data discovery routine to be compatible with new OpenVINO; - Hopefully fixed the final issues (warnings) with the sample. * G-API/infer2: address review problems - Fixed typo in comments; - Fixed public (Doxygen) comment on GArray<GMat> input case for infer2; - Made model lookup more flexible to allow new & old OMZ dir layouts. * G-API/infer2: Change the model paths again * G-API/infer2: Change the lookup path for test data * G-API/infer2: use randu instead of imread. CI war is over
1 parent e5e767a commit f0c411d

File tree

14 files changed

+1066
-85
lines changed

14 files changed

+1066
-85
lines changed

modules/gapi/include/opencv2/gapi/garray.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ std::ostream& operator<<(std::ostream& os, const cv::GArrayDesc &desc);
4949

5050
namespace detail
5151
{
52+
// FIXME: This type spec needs to be:
53+
// 1) shared with GOpaque (not needed right now)
54+
// 2) unified with the serialization (S11N, not merged right now).
55+
// Adding it to type traits is problematic due to our header deps
56+
// (which also need to be fixed).
57+
enum class TypeSpec: int {
58+
OPAQUE_SPEC,
59+
MAT,
60+
RECT
61+
};
62+
// FIXME: Reuse the below from "opaque traits" of S11N!
63+
template<typename T> struct GTypeSpec;
64+
template<typename T> struct GTypeSpec
65+
{
66+
static constexpr const TypeSpec spec = TypeSpec::OPAQUE_SPEC;
67+
};
68+
template<> struct GTypeSpec<cv::Mat>
69+
{
70+
static constexpr const TypeSpec spec = TypeSpec::MAT;
71+
};
72+
template<> struct GTypeSpec<cv::Rect>
73+
{
74+
static constexpr const TypeSpec spec = TypeSpec::RECT;
75+
};
76+
5277
// ConstructVec is a callback which stores information about T and is used by
5378
// G-API runtime to construct arrays in host memory (T remains opaque for G-API).
5479
// ConstructVec is carried into G-API internals by GArrayU.
@@ -110,12 +135,15 @@ namespace detail
110135
class BasicVectorRef
111136
{
112137
public:
138+
// These fields are set by the derived class(es)
113139
std::size_t m_elemSize = 0ul;
114140
cv::GArrayDesc m_desc;
141+
TypeSpec m_spec;
115142
virtual ~BasicVectorRef() {}
116143

117144
virtual void mov(BasicVectorRef &ref) = 0;
118145
virtual const void* ptr() const = 0;
146+
virtual std::size_t size() const = 0;
119147
};
120148

121149
template<typename T> class VectorRefT final: public BasicVectorRef
@@ -135,6 +163,7 @@ namespace detail
135163
{
136164
m_elemSize = sizeof(T);
137165
if (vec) m_desc = cv::descr_of(*vec);
166+
m_spec = GTypeSpec<T>::spec;
138167
}
139168

140169
public:
@@ -209,7 +238,9 @@ namespace detail
209238
wref() = std::move(tv->wref());
210239
}
211240

241+
212242
virtual const void* ptr() const override { return &rref(); }
243+
virtual std::size_t size() const override { return rref().size(); }
213244
};
214245

215246
// This class strips type information from VectorRefT<> and makes it usable
@@ -265,8 +296,18 @@ namespace detail
265296
return m_ref->m_desc;
266297
}
267298

299+
std::size_t size() const
300+
{
301+
return m_ref->size();
302+
}
303+
268304
// May be used to uniquely identify this object internally
269305
const void *ptr() const { return m_ref->ptr(); }
306+
307+
TypeSpec spec() const
308+
{
309+
return m_ref->m_spec;
310+
}
270311
};
271312

272313
// Helper (FIXME: work-around?)

modules/gapi/include/opencv2/gapi/gkernel.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace cv {
2828

29+
using GSpecs = std::vector<cv::detail::ArgSpec>;
2930
using GShapes = std::vector<GShape>;
3031

3132
// GKernel describes kernel API to the system
@@ -38,8 +39,11 @@ struct GAPI_EXPORTS GKernel
3839
const std::string name; // kernel ID, defined by its API (signature)
3940
const std::string tag; // some (implementation-specific) tag
4041
const M outMeta; // generic adaptor to API::outMeta(...)
42+
const GSpecs inSpecs; // specs of kernel's inputs (FIXME: below)
4143
const GShapes outShapes; // types (shapes) kernel's outputs
4244
};
45+
// TODO: It's questionable if inSpecs should really be here. Instead,
46+
// this information could come from meta.
4347

4448
// GKernelImpl describes particular kernel implementation to the system
4549
struct GAPI_EXPORTS GKernelImpl
@@ -203,10 +207,15 @@ class GKernelTypeM<K, std::function<std::tuple<R...>(Args...)> >
203207
using InArgs = std::tuple<Args...>;
204208
using OutArgs = std::tuple<R...>;
205209

210+
// TODO: Args&&... here?
206211
static std::tuple<R...> on(Args... args)
207212
{
208-
cv::GCall call(GKernel{K::id(), K::tag(), &K::getOutMeta, {detail::GTypeTraits<R>::shape...}});
209-
call.pass(args...);
213+
cv::GCall call(GKernel{ K::id()
214+
, K::tag()
215+
, &K::getOutMeta
216+
, {detail::GTypeTraits<Args>::spec...}
217+
, {detail::GTypeTraits<R>::shape...}});
218+
call.pass(args...); // TODO: std::forward() here?
210219
return yield(call, typename detail::MkSeq<sizeof...(R)>::type());
211220
}
212221
};
@@ -226,7 +235,11 @@ class GKernelType<K, std::function<R(Args...)> >
226235

227236
static R on(Args... args)
228237
{
229-
cv::GCall call(GKernel{K::id(), K::tag(), &K::getOutMeta, {detail::GTypeTraits<R>::shape}});
238+
cv::GCall call(GKernel{ K::id()
239+
, K::tag()
240+
, &K::getOutMeta
241+
, {detail::GTypeTraits<Args>::spec...}
242+
, {detail::GTypeTraits<R>::shape}});
230243
call.pass(args...);
231244
return detail::Yield<R>::yield(call, 0);
232245
}

modules/gapi/include/opencv2/gapi/gopaque.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ std::ostream& operator<<(std::ostream& os, const cv::GOpaqueDesc &desc);
4646

4747
namespace detail
4848
{
49+
4950
// ConstructOpaque is a callback which stores information about T and is used by
5051
// G-API runtime to construct an object in host memory (T remains opaque for G-API).
5152
// ConstructOpaque is carried into G-API internals by GOpaqueU.

modules/gapi/include/opencv2/gapi/gtype_traits.hpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018 Intel Corporation
5+
// Copyright (C) 2018-2020 Intel Corporation
66

77

88
#ifndef OPENCV_GAPI_GTYPE_TRAITS_HPP
@@ -41,6 +41,32 @@ namespace detail
4141
GOPAQUE, // a cv::GOpaqueU (note - exactly GOpaqueU, not GOpaque<T>!)
4242
};
4343

44+
// This enum captures some information about T in GArray<T> and GOpaque<T>
45+
enum class ArgSpec: int
46+
{
47+
OPAQUE_SPEC, // Unknown, generic, opaque-to-GAPI data type
48+
GMAT, // a GMat
49+
RECT, // a cv::Rect
50+
// NB: Add more types when required
51+
};
52+
53+
// Describe specialization types of interest first
54+
// FIXME: It comes to GArg but ideally it should go to *Desc{}
55+
// type family. Bringing it there is a more massive change though.
56+
template<typename T> struct GSpecTraits;
57+
template<typename T> struct GSpecTraits
58+
{
59+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
60+
};
61+
template<> struct GSpecTraits<cv::GMat>
62+
{
63+
static constexpr const ArgSpec spec = ArgSpec::GMAT;
64+
};
65+
template<> struct GSpecTraits<cv::Rect>
66+
{
67+
static constexpr const ArgSpec spec = ArgSpec::RECT;
68+
};
69+
4470
enum class OpaqueKind: int
4571
{
4672
CV_UNKNOWN, // Unknown, generic, opaque-to-GAPI data type unsupported in graph seriallization
@@ -69,35 +95,44 @@ namespace detail
6995
// cv::GArg to store meta information about types passed into
7096
// operation arguments. Please note that cv::GComputation is
7197
// defined on GProtoArgs, not GArgs!
98+
//
99+
// spec is a type specialization (makes sense for GArray<> and GOpaque<>)
100+
// for the rest, it is just OPAQUE_VAL by default.
72101
template<typename T> struct GTypeTraits;
73102
template<typename T> struct GTypeTraits
74103
{
75104
static constexpr const ArgKind kind = ArgKind::OPAQUE_VAL;
105+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
76106
};
77107
template<> struct GTypeTraits<cv::GMat>
78108
{
79109
static constexpr const ArgKind kind = ArgKind::GMAT;
80110
static constexpr const GShape shape = GShape::GMAT;
111+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
81112
};
82113
template<> struct GTypeTraits<cv::GMatP>
83114
{
84115
static constexpr const ArgKind kind = ArgKind::GMATP;
85116
static constexpr const GShape shape = GShape::GMAT;
117+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
86118
};
87119
template<> struct GTypeTraits<cv::GFrame>
88120
{
89121
static constexpr const ArgKind kind = ArgKind::GFRAME;
90122
static constexpr const GShape shape = GShape::GMAT;
123+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
91124
};
92125
template<> struct GTypeTraits<cv::GScalar>
93126
{
94127
static constexpr const ArgKind kind = ArgKind::GSCALAR;
95128
static constexpr const GShape shape = GShape::GSCALAR;
129+
static constexpr const ArgSpec spec = ArgSpec::OPAQUE_SPEC;
96130
};
97131
template<class T> struct GTypeTraits<cv::GArray<T> >
98132
{
99133
static constexpr const ArgKind kind = ArgKind::GARRAY;
100134
static constexpr const GShape shape = GShape::GARRAY;
135+
static constexpr const ArgSpec spec = GSpecTraits<T>::spec;
101136
using host_type = std::vector<T>;
102137
using strip_type = cv::detail::VectorRef;
103138
static cv::detail::GArrayU wrap_value(const cv::GArray<T> &t) { return t.strip();}
@@ -108,6 +143,7 @@ namespace detail
108143
{
109144
static constexpr const ArgKind kind = ArgKind::GOPAQUE;
110145
static constexpr const GShape shape = GShape::GOPAQUE;
146+
static constexpr const ArgSpec spec = GSpecTraits<T>::spec;
111147
using host_type = T;
112148
using strip_type = cv::detail::OpaqueRef;
113149
static cv::detail::GOpaqueU wrap_value(const cv::GOpaque<T> &t) { return t.strip();}
@@ -140,6 +176,7 @@ namespace detail
140176
template<> struct GTypeOf<cv::Scalar> { using type = cv::GScalar; };
141177
template<typename U> struct GTypeOf<std::vector<U> > { using type = cv::GArray<U>; };
142178
template<typename U> struct GTypeOf { using type = cv::GOpaque<U>;};
179+
143180
// FIXME: This is not quite correct since IStreamSource may produce not only Mat but also Scalar
144181
// and vector data. TODO: Extend the type dispatching on these types too.
145182
template<> struct GTypeOf<cv::gapi::wip::IStreamSource::Ptr> { using type = cv::GMat;};

0 commit comments

Comments
 (0)