Skip to content

Commit 2a3cdba

Browse files
Merge pull request opencv#18701 from TolyaTalamanov:at/introduce-config-for-ie-params
Expand ie::Params to support config * Add config to IE params * Add test * Remove comments from tests * Rename to pluginConfig * Add one more overloads for pluginConfig * Add more tests
1 parent 6df92b3 commit 2a3cdba

File tree

3 files changed

+135
-5
lines changed

3 files changed

+135
-5
lines changed

Diff for: modules/gapi/include/opencv2/gapi/infer/ie.hpp

+30-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212
#include <array>
1313
#include <tuple> // tuple, tuple_size
14+
#include <map>
1415

1516
#include <opencv2/gapi/opencv_includes.hpp>
1617
#include <opencv2/gapi/util/any.hpp>
@@ -42,6 +43,8 @@ enum class TraitAs: int
4243
IMAGE //!< G-API traits an associated cv::Mat as an image so creates an "image" blob (NCHW/NHWC, etc)
4344
};
4445

46+
using IEConfig = std::map<std::string, std::string>;
47+
4548
namespace detail {
4649
struct ParamDesc {
4750
std::string model_path;
@@ -63,6 +66,7 @@ namespace detail {
6366
enum class Kind { Load, Import };
6467
Kind kind;
6568
bool is_generic;
69+
IEConfig config;
6670
};
6771
} // namespace detail
6872

@@ -86,7 +90,8 @@ template<typename Net> class Params {
8690
, std::tuple_size<typename Net::InArgs>::value // num_in
8791
, std::tuple_size<typename Net::OutArgs>::value // num_out
8892
, detail::ParamDesc::Kind::Load
89-
, false} {
93+
, false
94+
, {}} {
9095
};
9196

9297
Params(const std::string &model,
@@ -95,7 +100,8 @@ template<typename Net> class Params {
95100
, std::tuple_size<typename Net::InArgs>::value // num_in
96101
, std::tuple_size<typename Net::OutArgs>::value // num_out
97102
, detail::ParamDesc::Kind::Import
98-
, false} {
103+
, false
104+
, {}} {
99105
};
100106

101107
Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) {
@@ -121,6 +127,16 @@ template<typename Net> class Params {
121127
return *this;
122128
}
123129

130+
Params& pluginConfig(IEConfig&& cfg) {
131+
desc.config = std::move(cfg);
132+
return *this;
133+
}
134+
135+
Params& pluginConfig(const IEConfig& cfg) {
136+
desc.config = cfg;
137+
return *this;
138+
}
139+
124140
// BEGIN(G-API's network parametrization API)
125141
GBackend backend() const { return cv::gapi::ie::backend(); }
126142
std::string tag() const { return Net::tag(); }
@@ -138,15 +154,25 @@ class Params<cv::gapi::Generic> {
138154
const std::string &model,
139155
const std::string &weights,
140156
const std::string &device)
141-
: desc{ model, weights, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Load, true}, m_tag(tag) {
157+
: desc{ model, weights, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Load, true, {}}, m_tag(tag) {
142158
};
143159

144160
Params(const std::string &tag,
145161
const std::string &model,
146162
const std::string &device)
147-
: desc{ model, {}, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Import, true}, m_tag(tag) {
163+
: desc{ model, {}, device, {}, {}, {}, 0u, 0u, detail::ParamDesc::Kind::Import, true, {}}, m_tag(tag) {
148164
};
149165

166+
Params& pluginConfig(IEConfig&& cfg) {
167+
desc.config = std::move(cfg);
168+
return *this;
169+
}
170+
171+
Params& pluginConfig(const IEConfig& cfg) {
172+
desc.config = cfg;
173+
return *this;
174+
}
175+
150176
// BEGIN(G-API's network parametrization API)
151177
GBackend backend() const { return cv::gapi::ie::backend(); }
152178
std::string tag() const { return m_tag; }

Diff for: modules/gapi/src/backends/ie/giebackend.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ struct IEUnit {
185185
inputs = net.getInputsInfo();
186186
outputs = net.getOutputsInfo();
187187
} else if (params.kind == cv::gapi::ie::detail::ParamDesc::Kind::Import) {
188-
this_plugin = cv::gimpl::ie::wrap::getPlugin(params);
188+
this_plugin = cv::gimpl::ie::wrap::getPlugin(params);
189+
this_plugin.SetConfig(params.config);
189190
this_network = cv::gimpl::ie::wrap::importNetwork(this_plugin, params);
190191
// FIXME: ICNNetwork returns InputsDataMap/OutputsDataMap,
191192
// but ExecutableNetwork returns ConstInputsDataMap/ConstOutputsDataMap
@@ -225,6 +226,7 @@ struct IEUnit {
225226
// FIXME: In case importNetwork for fill inputs/outputs need to obtain ExecutableNetwork, but
226227
// for loadNetwork they can be obtained by using readNetwork
227228
non_const_this->this_plugin = cv::gimpl::ie::wrap::getPlugin(params);
229+
non_const_this->this_plugin.SetConfig(params.config);
228230
non_const_this->this_network = cv::gimpl::ie::wrap::loadNetwork(non_const_this->this_plugin, net, params);
229231
}
230232

Diff for: modules/gapi/test/infer/gapi_infer_ie_test.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,108 @@ TEST(TestAgeGenderIE, GenericInfer)
403403
normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
404404
}
405405

406+
TEST(TestAgeGenderIE, InvalidConfigGeneric)
407+
{
408+
initDLDTDataPath();
409+
410+
std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml");
411+
std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin");
412+
std::string device_id = "CPU";
413+
414+
// Configure & run G-API
415+
cv::GMat in;
416+
GInferInputs inputs;
417+
inputs["data"] = in;
418+
419+
auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", inputs);
420+
auto age = outputs.at("age_conv3");
421+
auto gender = outputs.at("prob");
422+
cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
423+
424+
auto pp = cv::gapi::ie::Params<cv::gapi::Generic>{"age-gender-generic",
425+
model_path,
426+
weights_path,
427+
device_id}.pluginConfig({{"unsupported_config", "some_value"}});
428+
429+
EXPECT_ANY_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
430+
cv::compile_args(cv::gapi::networks(pp))));
431+
}
432+
433+
TEST(TestAgeGenderIE, CPUConfigGeneric)
434+
{
435+
initDLDTDataPath();
436+
437+
std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml");
438+
std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin");
439+
std::string device_id = "CPU";
440+
441+
// Configure & run G-API
442+
cv::GMat in;
443+
GInferInputs inputs;
444+
inputs["data"] = in;
445+
446+
auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", inputs);
447+
auto age = outputs.at("age_conv3");
448+
auto gender = outputs.at("prob");
449+
cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
450+
451+
auto pp = cv::gapi::ie::Params<cv::gapi::Generic>{"age-gender-generic",
452+
model_path,
453+
weights_path,
454+
device_id}.pluginConfig({{"ENFORCE_BF16", "NO"}});
455+
456+
EXPECT_NO_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
457+
cv::compile_args(cv::gapi::networks(pp))));
458+
}
459+
460+
TEST(TestAgeGenderIE, InvalidConfig)
461+
{
462+
initDLDTDataPath();
463+
464+
std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml");
465+
std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin");
466+
std::string device_id = "CPU";
467+
468+
using AGInfo = std::tuple<cv::GMat, cv::GMat>;
469+
G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
470+
471+
cv::GMat in;
472+
cv::GMat age, gender;
473+
std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
474+
cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
475+
476+
auto pp = cv::gapi::ie::Params<AgeGender> {
477+
model_path, weights_path, device_id
478+
}.cfgOutputLayers({ "age_conv3", "prob" }).pluginConfig({{"unsupported_config", "some_value"}});
479+
480+
EXPECT_ANY_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
481+
cv::compile_args(cv::gapi::networks(pp))));
482+
}
483+
484+
TEST(TestAgeGenderIE, CPUConfig)
485+
{
486+
initDLDTDataPath();
487+
488+
std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml");
489+
std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin");
490+
std::string device_id = "CPU";
491+
492+
using AGInfo = std::tuple<cv::GMat, cv::GMat>;
493+
G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
494+
495+
cv::GMat in;
496+
cv::GMat age, gender;
497+
std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
498+
cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
499+
500+
auto pp = cv::gapi::ie::Params<AgeGender> {
501+
model_path, weights_path, device_id
502+
}.cfgOutputLayers({ "age_conv3", "prob" }).pluginConfig({{"ENFORCE_BF16", "NO"}});
503+
504+
EXPECT_NO_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
505+
cv::compile_args(cv::gapi::networks(pp))));
506+
}
507+
406508
} // namespace opencv_test
407509

408510
#endif // HAVE_INF_ENGINE

0 commit comments

Comments
 (0)