gapi_infer_ie_test.cpp 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2019-2023 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #if defined HAVE_INF_ENGINE && INF_ENGINE_RELEASE < 2023010000
  8. #include <stdexcept>
  9. #include <mutex>
  10. #include <condition_variable>
  11. #include <inference_engine.hpp>
  12. #include <ade/util/iota_range.hpp>
  13. #include <opencv2/gapi/infer/ie.hpp>
  14. #include <opencv2/gapi/streaming/cap.hpp>
  15. #include "backends/ie/util.hpp"
  16. #include "backends/ie/giebackend/giewrapper.hpp"
  17. #ifdef HAVE_NGRAPH
  18. #if defined(__clang__) // clang or MSVC clang
  19. #pragma clang diagnostic push
  20. #pragma clang diagnostic ignored "-Wunused-parameter"
  21. #elif defined(_MSC_VER)
  22. #pragma warning(push)
  23. #pragma warning(disable : 4100)
  24. # if _MSC_VER < 1910
  25. # pragma warning(disable:4268) // Disable warnings of ngraph. OpenVINO recommends to use MSVS 2019.
  26. # pragma warning(disable:4800)
  27. # endif
  28. #elif defined(__GNUC__)
  29. #pragma GCC diagnostic push
  30. #pragma GCC diagnostic ignored "-Wunused-parameter"
  31. #endif
  32. #include <ngraph/ngraph.hpp>
  33. #endif
  34. namespace opencv_test
  35. {
  36. namespace {
  37. class TestMediaBGR final: public cv::MediaFrame::IAdapter {
  38. cv::Mat m_mat;
  39. using Cb = cv::MediaFrame::View::Callback;
  40. Cb m_cb;
  41. public:
  42. explicit TestMediaBGR(cv::Mat m, Cb cb = [](){})
  43. : m_mat(m), m_cb(cb) {
  44. }
  45. cv::GFrameDesc meta() const override {
  46. return cv::GFrameDesc{cv::MediaFormat::BGR, cv::Size(m_mat.cols, m_mat.rows)};
  47. }
  48. cv::MediaFrame::View access(cv::MediaFrame::Access) override {
  49. cv::MediaFrame::View::Ptrs pp = { m_mat.ptr(), nullptr, nullptr, nullptr };
  50. cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
  51. return cv::MediaFrame::View(std::move(pp), std::move(ss), Cb{m_cb});
  52. }
  53. cv::util::any blobParams() const override {
  54. #if INF_ENGINE_RELEASE > 2023000000
  55. // NB: blobParams() shouldn't be used in tests
  56. // if OpenVINO versions is higher than 2023.0
  57. GAPI_Assert(false && "NV12 feature has been deprecated in OpenVINO 1.0 API.");
  58. #else
  59. return std::make_pair<InferenceEngine::TensorDesc,
  60. InferenceEngine::ParamMap>({IE::Precision::U8,
  61. {1, 3, 300, 300},
  62. IE::Layout::NCHW},
  63. {{"HELLO", 42},
  64. {"COLOR_FORMAT",
  65. InferenceEngine::ColorFormat::NV12}});
  66. #endif // INF_ENGINE_RELEASE > 2023000000
  67. }
  68. };
  69. class TestMediaNV12 final: public cv::MediaFrame::IAdapter {
  70. cv::Mat m_y;
  71. cv::Mat m_uv;
  72. public:
  73. TestMediaNV12(cv::Mat y, cv::Mat uv) : m_y(y), m_uv(uv) {
  74. }
  75. cv::GFrameDesc meta() const override {
  76. return cv::GFrameDesc{cv::MediaFormat::NV12, cv::Size(m_y.cols, m_y.rows)};
  77. }
  78. cv::MediaFrame::View access(cv::MediaFrame::Access) override {
  79. cv::MediaFrame::View::Ptrs pp = {
  80. m_y.ptr(), m_uv.ptr(), nullptr, nullptr
  81. };
  82. cv::MediaFrame::View::Strides ss = {
  83. m_y.step, m_uv.step, 0u, 0u
  84. };
  85. return cv::MediaFrame::View(std::move(pp), std::move(ss));
  86. }
  87. };
  88. // FIXME: taken from DNN module
  89. static void initDLDTDataPath()
  90. {
  91. static bool initialized = false;
  92. if (!initialized)
  93. {
  94. cvtest::addDataSearchEnv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH");
  95. const std::string dnnDataPath = cv::utils::getConfigurationParameterString("OPENCV_DNN_TEST_DATA_PATH");
  96. if (!dnnDataPath.empty()) {
  97. // Add the dnnDataPath itself - G-API is using some images there directly
  98. cvtest::addDataSearchPath(dnnDataPath);
  99. cvtest::addDataSearchPath(dnnDataPath + std::string("/omz_intel_models"));
  100. }
  101. initialized = true;
  102. }
  103. }
  104. #if INF_ENGINE_RELEASE >= 2020010000
  105. static const std::string SUBDIR = "intel/age-gender-recognition-retail-0013/FP32/";
  106. #else
  107. static const std::string SUBDIR = "Retail/object_attributes/age_gender/dldt/";
  108. #endif
  109. // FIXME: taken from the DNN module
  110. void normAssert(cv::InputArray ref, cv::InputArray test,
  111. const char *comment /*= ""*/,
  112. double l1 = 0.00001, double lInf = 0.0001)
  113. {
  114. double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
  115. EXPECT_LE(normL1, l1) << comment;
  116. double normInf = cvtest::norm(ref, test, cv::NORM_INF);
  117. EXPECT_LE(normInf, lInf) << comment;
  118. }
  119. namespace IE = InferenceEngine;
  120. void setNetParameters(IE::CNNNetwork& net, bool is_nv12 = false) {
  121. auto ii = net.getInputsInfo().at("data");
  122. ii->setPrecision(IE::Precision::U8);
  123. ii->getPreProcess().setResizeAlgorithm(IE::RESIZE_BILINEAR);
  124. if (is_nv12) {
  125. #if INF_ENGINE_RELEASE > 2023000000
  126. // NB: NV12 feature shouldn't be used in tests
  127. // if OpenVINO versions is higher than 2023.0
  128. GAPI_Assert(false && "NV12 feature has been deprecated in OpenVINO 1.0 API.");
  129. #else
  130. ii->getPreProcess().setColorFormat(IE::ColorFormat::NV12);
  131. #endif // INF_ENGINE_RELEASE > 2023000000
  132. }
  133. }
  134. bool checkDeviceIsAvailable(const std::string& device) {
  135. const static auto available_devices = [&](){
  136. auto devices = cv::gimpl::ie::wrap::getCore().GetAvailableDevices();
  137. return std::unordered_set<std::string>{devices.begin(), devices.end()};
  138. }();
  139. return available_devices.find(device) != available_devices.end();
  140. }
  141. void skipIfDeviceNotAvailable(const std::string& device) {
  142. if (!checkDeviceIsAvailable(device)) {
  143. throw SkipTestException("Device: " + device + " isn't available!");
  144. }
  145. }
  146. void compileBlob(const cv::gapi::ie::detail::ParamDesc& params,
  147. const std::string& output,
  148. const IE::Precision& ip) {
  149. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  150. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  151. for (auto&& ii : net.getInputsInfo()) {
  152. ii.second->setPrecision(ip);
  153. }
  154. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  155. std::ofstream out_file{output, std::ios::out | std::ios::binary};
  156. GAPI_Assert(out_file.is_open());
  157. this_network.Export(out_file);
  158. }
  159. std::string compileAgeGenderBlob(const std::string& device) {
  160. const static std::string blob_path = [&](){
  161. cv::gapi::ie::detail::ParamDesc params;
  162. const std::string model_name = "age-gender-recognition-retail-0013";
  163. const std::string output = model_name + ".blob";
  164. params.model_path = findDataFile(SUBDIR + model_name + ".xml", false);
  165. params.weights_path = findDataFile(SUBDIR + model_name + ".bin", false);
  166. params.device_id = device;
  167. compileBlob(params, output, IE::Precision::U8);
  168. return output;
  169. }();
  170. return blob_path;
  171. }
  172. } // anonymous namespace
  173. // TODO: Probably DNN/IE part can be further parametrized with a template
  174. // NOTE: here ".." is used to leave the default "gapi/" search scope
  175. TEST(TestAgeGenderIE, InferBasicTensor)
  176. {
  177. initDLDTDataPath();
  178. cv::gapi::ie::detail::ParamDesc params;
  179. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  180. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  181. params.device_id = "CPU";
  182. // Load IE network, initialize input data using that.
  183. cv::Mat in_mat;
  184. cv::Mat gapi_age, gapi_gender;
  185. IE::Blob::Ptr ie_age, ie_gender;
  186. {
  187. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  188. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  189. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  190. auto infer_request = this_network.CreateInferRequest();
  191. const auto &iedims = net.getInputsInfo().begin()->second->getTensorDesc().getDims();
  192. auto cvdims = cv::gapi::ie::util::to_ocv(iedims);
  193. in_mat.create(cvdims, CV_32F);
  194. cv::randu(in_mat, -1, 1);
  195. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  196. infer_request.Infer();
  197. ie_age = infer_request.GetBlob("age_conv3");
  198. ie_gender = infer_request.GetBlob("prob");
  199. }
  200. // Configure & run G-API
  201. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  202. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  203. cv::GMat in;
  204. cv::GMat age, gender;
  205. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  206. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  207. auto pp = cv::gapi::ie::Params<AgeGender> {
  208. params.model_path, params.weights_path, params.device_id
  209. }.cfgOutputLayers({ "age_conv3", "prob" });
  210. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  211. cv::compile_args(cv::gapi::networks(pp)));
  212. // Validate with IE itself (avoid DNN module dependency here)
  213. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  214. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  215. }
  216. TEST(TestAgeGenderIE, InferBasicImage)
  217. {
  218. initDLDTDataPath();
  219. cv::gapi::ie::detail::ParamDesc params;
  220. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  221. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  222. params.device_id = "CPU";
  223. // FIXME: Ideally it should be an image from disk
  224. // cv::Mat in_mat = cv::imread(findDataFile("grace_hopper_227.png"));
  225. cv::Mat in_mat(cv::Size(320, 240), CV_8UC3);
  226. cv::randu(in_mat, 0, 255);
  227. cv::Mat gapi_age, gapi_gender;
  228. // Load & run IE network
  229. IE::Blob::Ptr ie_age, ie_gender;
  230. {
  231. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  232. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  233. setNetParameters(net);
  234. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  235. auto infer_request = this_network.CreateInferRequest();
  236. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  237. infer_request.Infer();
  238. ie_age = infer_request.GetBlob("age_conv3");
  239. ie_gender = infer_request.GetBlob("prob");
  240. }
  241. // Configure & run G-API
  242. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  243. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  244. cv::GMat in;
  245. cv::GMat age, gender;
  246. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  247. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  248. auto pp = cv::gapi::ie::Params<AgeGender> {
  249. params.model_path, params.weights_path, params.device_id
  250. }.cfgOutputLayers({ "age_conv3", "prob" });
  251. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  252. cv::compile_args(cv::gapi::networks(pp)));
  253. // Validate with IE itself (avoid DNN module dependency here)
  254. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  255. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  256. }
  257. struct InferWithReshape: public ::testing::Test {
  258. cv::gapi::ie::detail::ParamDesc params;
  259. cv::Mat m_in_mat;
  260. std::vector<cv::Rect> m_roi_list;
  261. std::vector<size_t> reshape_dims;
  262. std::vector<cv::Mat> m_out_ie_ages;
  263. std::vector<cv::Mat> m_out_ie_genders;
  264. std::vector<cv::Mat> m_out_gapi_ages;
  265. std::vector<cv::Mat> m_out_gapi_genders;
  266. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  267. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  268. InferenceEngine::CNNNetwork net;
  269. InferenceEngine::Core plugin;
  270. void SetUp() {
  271. // FIXME: it must be cv::imread(findDataFile("../dnn/grace_hopper_227.png", false));
  272. m_in_mat = cv::Mat(cv::Size(320, 240), CV_8UC3);
  273. cv::randu(m_in_mat, 0, 255);
  274. m_out_gapi_ages.resize(1);
  275. m_out_gapi_genders.resize(1);
  276. // both ROIs point to the same face, with a slightly changed geometry
  277. m_roi_list = {
  278. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  279. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  280. };
  281. // New dimensions for "data" input
  282. reshape_dims = {1, 3, 70, 70};
  283. initDLDTDataPath();
  284. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  285. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  286. params.device_id = "CPU";
  287. plugin = cv::gimpl::ie::wrap::getPlugin(params);
  288. net = cv::gimpl::ie::wrap::readNetwork(params);
  289. setNetParameters(net);
  290. net.reshape({{"data", reshape_dims}});
  291. }
  292. void inferROIs(IE::Blob::Ptr blob) {
  293. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  294. auto infer_request = this_network.CreateInferRequest();
  295. for (auto &&rc : m_roi_list) {
  296. const auto ie_rc = IE::ROI {
  297. 0u
  298. , static_cast<std::size_t>(rc.x)
  299. , static_cast<std::size_t>(rc.y)
  300. , static_cast<std::size_t>(rc.width)
  301. , static_cast<std::size_t>(rc.height)
  302. };
  303. infer_request.SetBlob("data", IE::make_shared_blob(blob, ie_rc));
  304. infer_request.Infer();
  305. using namespace cv::gapi::ie::util;
  306. m_out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  307. m_out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  308. }
  309. }
  310. void infer(cv::Mat& in, const bool with_roi = false) {
  311. if (!with_roi) {
  312. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  313. auto infer_request = this_network.CreateInferRequest();
  314. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in));
  315. infer_request.Infer();
  316. using namespace cv::gapi::ie::util;
  317. m_out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  318. m_out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  319. } else {
  320. auto frame_blob = cv::gapi::ie::util::to_ie(in);
  321. inferROIs(frame_blob);
  322. }
  323. }
  324. void validate() {
  325. // Validate with IE itself (avoid DNN module dependency here)
  326. GAPI_Assert(!m_out_gapi_ages.empty());
  327. ASSERT_EQ(m_out_gapi_genders.size(), m_out_gapi_ages.size());
  328. ASSERT_EQ(m_out_gapi_ages.size(), m_out_ie_ages.size());
  329. ASSERT_EQ(m_out_gapi_genders.size(), m_out_ie_genders.size());
  330. const size_t size = m_out_gapi_ages.size();
  331. for (size_t i = 0; i < size; ++i) {
  332. normAssert(m_out_ie_ages [i], m_out_gapi_ages [i], "Test age output");
  333. normAssert(m_out_ie_genders[i], m_out_gapi_genders[i], "Test gender output");
  334. }
  335. }
  336. }; // InferWithReshape
  337. struct InferWithReshapeNV12: public InferWithReshape {
  338. cv::Mat m_in_uv;
  339. cv::Mat m_in_y;
  340. void SetUp() {
  341. InferWithReshape::SetUp();
  342. cv::Size sz{320, 240};
  343. m_in_y = cv::Mat{sz, CV_8UC1};
  344. cv::randu(m_in_y, 0, 255);
  345. m_in_uv = cv::Mat{sz / 2, CV_8UC2};
  346. cv::randu(m_in_uv, 0, 255);
  347. // NB: NV12 feature shouldn't be used in tests
  348. // if OpenVINO versions is higher than 2023.0
  349. #if INF_ENGINE_RELEASE <= 2023000000
  350. setNetParameters(net, true);
  351. net.reshape({{"data", reshape_dims}});
  352. auto frame_blob = cv::gapi::ie::util::to_ie(m_in_y, m_in_uv);
  353. inferROIs(frame_blob);
  354. #endif // INF_ENGINE_RELEASE <= 2023000000
  355. }
  356. };
  357. struct ROIList: public ::testing::Test {
  358. cv::gapi::ie::detail::ParamDesc params;
  359. cv::Mat m_in_mat;
  360. std::vector<cv::Rect> m_roi_list;
  361. std::vector<cv::Mat> m_out_ie_ages;
  362. std::vector<cv::Mat> m_out_ie_genders;
  363. std::vector<cv::Mat> m_out_gapi_ages;
  364. std::vector<cv::Mat> m_out_gapi_genders;
  365. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  366. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  367. void SetUp() {
  368. initDLDTDataPath();
  369. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  370. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  371. params.device_id = "CPU";
  372. // FIXME: it must be cv::imread(findDataFile("../dnn/grace_hopper_227.png", false));
  373. m_in_mat = cv::Mat(cv::Size(320, 240), CV_8UC3);
  374. cv::randu(m_in_mat, 0, 255);
  375. // both ROIs point to the same face, with a slightly changed geometry
  376. m_roi_list = {
  377. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  378. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  379. };
  380. // Load & run IE network
  381. {
  382. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  383. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  384. setNetParameters(net);
  385. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  386. auto infer_request = this_network.CreateInferRequest();
  387. auto frame_blob = cv::gapi::ie::util::to_ie(m_in_mat);
  388. for (auto &&rc : m_roi_list) {
  389. const auto ie_rc = IE::ROI {
  390. 0u
  391. , static_cast<std::size_t>(rc.x)
  392. , static_cast<std::size_t>(rc.y)
  393. , static_cast<std::size_t>(rc.width)
  394. , static_cast<std::size_t>(rc.height)
  395. };
  396. infer_request.SetBlob("data", IE::make_shared_blob(frame_blob, ie_rc));
  397. infer_request.Infer();
  398. using namespace cv::gapi::ie::util;
  399. m_out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  400. m_out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  401. }
  402. } // namespace IE = ..
  403. } // ROIList()
  404. void validate() {
  405. // Validate with IE itself (avoid DNN module dependency here)
  406. ASSERT_EQ(2u, m_out_ie_ages.size());
  407. ASSERT_EQ(2u, m_out_ie_genders.size());
  408. ASSERT_EQ(2u, m_out_gapi_ages.size());
  409. ASSERT_EQ(2u, m_out_gapi_genders.size());
  410. normAssert(m_out_ie_ages [0], m_out_gapi_ages [0], "0: Test age output");
  411. normAssert(m_out_ie_genders[0], m_out_gapi_genders[0], "0: Test gender output");
  412. normAssert(m_out_ie_ages [1], m_out_gapi_ages [1], "1: Test age output");
  413. normAssert(m_out_ie_genders[1], m_out_gapi_genders[1], "1: Test gender output");
  414. }
  415. }; // ROIList
  416. struct ROIListNV12: public ::testing::Test {
  417. cv::gapi::ie::detail::ParamDesc params;
  418. cv::Mat m_in_uv;
  419. cv::Mat m_in_y;
  420. std::vector<cv::Rect> m_roi_list;
  421. std::vector<cv::Mat> m_out_ie_ages;
  422. std::vector<cv::Mat> m_out_ie_genders;
  423. std::vector<cv::Mat> m_out_gapi_ages;
  424. std::vector<cv::Mat> m_out_gapi_genders;
  425. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  426. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  427. void SetUp() {
  428. initDLDTDataPath();
  429. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  430. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  431. params.device_id = "CPU";
  432. cv::Size sz{320, 240};
  433. m_in_y = cv::Mat{sz, CV_8UC1};
  434. cv::randu(m_in_y, 0, 255);
  435. m_in_uv = cv::Mat{sz / 2, CV_8UC2};
  436. cv::randu(m_in_uv, 0, 255);
  437. // both ROIs point to the same face, with a slightly changed geometry
  438. m_roi_list = {
  439. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  440. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  441. };
  442. // NB: NV12 feature shouldn't be used in tests
  443. // if OpenVINO versions is higher than 2023.0
  444. #if INF_ENGINE_RELEASE <= 2023000000
  445. {
  446. // Load & run IE network
  447. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  448. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  449. setNetParameters(net, true);
  450. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  451. auto infer_request = this_network.CreateInferRequest();
  452. auto frame_blob = cv::gapi::ie::util::to_ie(m_in_y, m_in_uv);
  453. for (auto &&rc : m_roi_list) {
  454. const auto ie_rc = IE::ROI {
  455. 0u
  456. , static_cast<std::size_t>(rc.x)
  457. , static_cast<std::size_t>(rc.y)
  458. , static_cast<std::size_t>(rc.width)
  459. , static_cast<std::size_t>(rc.height)
  460. };
  461. infer_request.SetBlob("data", IE::make_shared_blob(frame_blob, ie_rc));
  462. infer_request.Infer();
  463. using namespace cv::gapi::ie::util;
  464. m_out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  465. m_out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  466. }
  467. } // namespace IE = ..
  468. #endif // INF_ENGINE_RELEASE <= 2023000000
  469. } // ROIList()
  470. void validate() {
  471. #if INF_ENGINE_RELEASE <= 2023000000
  472. // Validate with IE itself (avoid DNN module dependency here)
  473. ASSERT_EQ(2u, m_out_ie_ages.size());
  474. ASSERT_EQ(2u, m_out_ie_genders.size());
  475. ASSERT_EQ(2u, m_out_gapi_ages.size());
  476. ASSERT_EQ(2u, m_out_gapi_genders.size());
  477. normAssert(m_out_ie_ages [0], m_out_gapi_ages [0], "0: Test age output");
  478. normAssert(m_out_ie_genders[0], m_out_gapi_genders[0], "0: Test gender output");
  479. normAssert(m_out_ie_ages [1], m_out_gapi_ages [1], "1: Test age output");
  480. normAssert(m_out_ie_genders[1], m_out_gapi_genders[1], "1: Test gender output");
  481. #else
  482. GAPI_Assert(false && "Reference hasn't been calculated because"
  483. " NV12 feature has been deprecated.");
  484. #endif // INF_ENGINE_RELEASE <= 2023000000
  485. }
  486. };
  487. struct SingleROI: public ::testing::Test {
  488. cv::gapi::ie::detail::ParamDesc params;
  489. cv::Mat m_in_mat;
  490. cv::Rect m_roi;
  491. cv::Mat m_out_gapi_age;
  492. cv::Mat m_out_gapi_gender;
  493. cv::Mat m_out_ie_age;
  494. cv::Mat m_out_ie_gender;
  495. void SetUp() {
  496. initDLDTDataPath();
  497. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  498. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  499. params.device_id = "CPU";
  500. // FIXME: it must be cv::imread(findDataFile("../dnn/grace_hopper_227.png", false));
  501. m_in_mat = cv::Mat(cv::Size(320, 240), CV_8UC3);
  502. cv::randu(m_in_mat, 0, 255);
  503. m_roi = cv::Rect(cv::Point{64, 60}, cv::Size{96, 96});
  504. // Load & run IE network
  505. IE::Blob::Ptr ie_age, ie_gender;
  506. {
  507. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  508. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  509. setNetParameters(net);
  510. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  511. auto infer_request = this_network.CreateInferRequest();
  512. const auto ie_rc = IE::ROI {
  513. 0u
  514. , static_cast<std::size_t>(m_roi.x)
  515. , static_cast<std::size_t>(m_roi.y)
  516. , static_cast<std::size_t>(m_roi.width)
  517. , static_cast<std::size_t>(m_roi.height)
  518. };
  519. IE::Blob::Ptr roi_blob = IE::make_shared_blob(cv::gapi::ie::util::to_ie(m_in_mat), ie_rc);
  520. infer_request.SetBlob("data", roi_blob);
  521. infer_request.Infer();
  522. using namespace cv::gapi::ie::util;
  523. m_out_ie_age = to_ocv(infer_request.GetBlob("age_conv3")).clone();
  524. m_out_ie_gender = to_ocv(infer_request.GetBlob("prob")).clone();
  525. }
  526. }
  527. void validate() {
  528. // Validate with IE itself (avoid DNN module dependency here)
  529. normAssert(m_out_ie_age , m_out_gapi_age , "Test age output");
  530. normAssert(m_out_ie_gender, m_out_gapi_gender, "Test gender output");
  531. }
  532. };
  533. struct SingleROINV12: public ::testing::Test {
  534. cv::gapi::ie::detail::ParamDesc params;
  535. cv::Mat m_in_y;
  536. cv::Mat m_in_uv;
  537. cv::Rect m_roi;
  538. cv::Mat m_out_gapi_age;
  539. cv::Mat m_out_gapi_gender;
  540. cv::Mat m_out_ie_age;
  541. cv::Mat m_out_ie_gender;
  542. void SetUp() {
  543. initDLDTDataPath();
  544. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  545. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  546. params.device_id = "CPU";
  547. cv::Size sz{320, 240};
  548. m_in_y = cv::Mat{sz, CV_8UC1};
  549. cv::randu(m_in_y, 0, 255);
  550. m_in_uv = cv::Mat{sz / 2, CV_8UC2};
  551. cv::randu(m_in_uv, 0, 255);
  552. m_roi = cv::Rect(cv::Point{64, 60}, cv::Size{96, 96});
  553. // NB: NV12 feature shouldn't be used in tests
  554. // if OpenVINO versions is higher than 2023.0
  555. #if INF_ENGINE_RELEASE <= 2023000000
  556. // Load & run IE network
  557. IE::Blob::Ptr ie_age, ie_gender;
  558. {
  559. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  560. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  561. setNetParameters(net, /* NV12 */ true);
  562. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  563. auto infer_request = this_network.CreateInferRequest();
  564. auto blob = cv::gapi::ie::util::to_ie(m_in_y, m_in_uv);
  565. const auto ie_rc = IE::ROI {
  566. 0u
  567. , static_cast<std::size_t>(m_roi.x)
  568. , static_cast<std::size_t>(m_roi.y)
  569. , static_cast<std::size_t>(m_roi.width)
  570. , static_cast<std::size_t>(m_roi.height)
  571. };
  572. IE::Blob::Ptr roi_blob = IE::make_shared_blob(blob, ie_rc);
  573. infer_request.SetBlob("data", roi_blob);
  574. infer_request.Infer();
  575. using namespace cv::gapi::ie::util;
  576. m_out_ie_age = to_ocv(infer_request.GetBlob("age_conv3")).clone();
  577. m_out_ie_gender = to_ocv(infer_request.GetBlob("prob")).clone();
  578. }
  579. #endif // INF_ENGINE_RELEASE <= 2023000000
  580. }
  581. void validate() {
  582. #if INF_ENGINE_RELEASE <= 2023000000
  583. // Validate with IE itself (avoid DNN module dependency here)
  584. normAssert(m_out_ie_age , m_out_gapi_age , "Test age output");
  585. normAssert(m_out_ie_gender, m_out_gapi_gender, "Test gender output");
  586. #else
  587. GAPI_Assert(false && "Reference hasn't been calculated because"
  588. " NV12 feature has been deprecated.");
  589. #endif
  590. }
  591. };
  592. TEST_F(ROIList, TestInfer)
  593. {
  594. cv::GArray<cv::Rect> rr;
  595. cv::GMat in;
  596. cv::GArray<cv::GMat> age, gender;
  597. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  598. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  599. auto pp = cv::gapi::ie::Params<AgeGender> {
  600. params.model_path, params.weights_path, params.device_id
  601. }.cfgOutputLayers({ "age_conv3", "prob" });
  602. comp.apply(cv::gin(m_in_mat, m_roi_list),
  603. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  604. cv::compile_args(cv::gapi::networks(pp)));
  605. validate();
  606. }
  607. TEST_F(ROIList, TestInfer2)
  608. {
  609. cv::GArray<cv::Rect> rr;
  610. cv::GMat in;
  611. cv::GArray<cv::GMat> age, gender;
  612. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  613. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  614. auto pp = cv::gapi::ie::Params<AgeGender> {
  615. params.model_path, params.weights_path, params.device_id
  616. }.cfgOutputLayers({ "age_conv3", "prob" });
  617. comp.apply(cv::gin(m_in_mat, m_roi_list),
  618. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  619. cv::compile_args(cv::gapi::networks(pp)));
  620. validate();
  621. }
  622. TEST(DISABLED_TestTwoIENNPipeline, InferBasicImage)
  623. {
  624. initDLDTDataPath();
  625. cv::gapi::ie::detail::ParamDesc AGparams;
  626. AGparams.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  627. AGparams.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  628. AGparams.device_id = "MYRIAD";
  629. // FIXME: Ideally it should be an image from disk
  630. // cv::Mat in_mat = cv::imread(findDataFile("grace_hopper_227.png"));
  631. cv::Mat in_mat(cv::Size(320, 240), CV_8UC3);
  632. cv::randu(in_mat, 0, 255);
  633. cv::Mat gapi_age1, gapi_gender1, gapi_age2, gapi_gender2;
  634. // Load & run IE network
  635. IE::Blob::Ptr ie_age1, ie_gender1, ie_age2, ie_gender2;
  636. {
  637. auto AGplugin1 = cv::gimpl::ie::wrap::getPlugin(AGparams);
  638. auto AGnet1 = cv::gimpl::ie::wrap::readNetwork(AGparams);
  639. setNetParameters(AGnet1);
  640. auto AGplugin_network1 = cv::gimpl::ie::wrap::loadNetwork(AGplugin1, AGnet1, AGparams);
  641. auto AGinfer_request1 = AGplugin_network1.CreateInferRequest();
  642. AGinfer_request1.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  643. AGinfer_request1.Infer();
  644. ie_age1 = AGinfer_request1.GetBlob("age_conv3");
  645. ie_gender1 = AGinfer_request1.GetBlob("prob");
  646. auto AGplugin2 = cv::gimpl::ie::wrap::getPlugin(AGparams);
  647. auto AGnet2 = cv::gimpl::ie::wrap::readNetwork(AGparams);
  648. setNetParameters(AGnet2);
  649. auto AGplugin_network2 = cv::gimpl::ie::wrap::loadNetwork(AGplugin2, AGnet2, AGparams);
  650. auto AGinfer_request2 = AGplugin_network2.CreateInferRequest();
  651. AGinfer_request2.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  652. AGinfer_request2.Infer();
  653. ie_age2 = AGinfer_request2.GetBlob("age_conv3");
  654. ie_gender2 = AGinfer_request2.GetBlob("prob");
  655. }
  656. // Configure & run G-API
  657. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  658. G_API_NET(AgeGender1, <AGInfo(cv::GMat)>, "test-age-gender1");
  659. G_API_NET(AgeGender2, <AGInfo(cv::GMat)>, "test-age-gender2");
  660. cv::GMat in;
  661. cv::GMat age1, gender1;
  662. std::tie(age1, gender1) = cv::gapi::infer<AgeGender1>(in);
  663. cv::GMat age2, gender2;
  664. // FIXME: "Multi-node inference is not supported!", workarounded 'till enabling proper tools
  665. std::tie(age2, gender2) = cv::gapi::infer<AgeGender2>(cv::gapi::copy(in));
  666. cv::GComputation comp(cv::GIn(in), cv::GOut(age1, gender1, age2, gender2));
  667. auto age_net1 = cv::gapi::ie::Params<AgeGender1> {
  668. AGparams.model_path, AGparams.weights_path, AGparams.device_id
  669. }.cfgOutputLayers({ "age_conv3", "prob" });
  670. auto age_net2 = cv::gapi::ie::Params<AgeGender2> {
  671. AGparams.model_path, AGparams.weights_path, AGparams.device_id
  672. }.cfgOutputLayers({ "age_conv3", "prob" });
  673. comp.apply(cv::gin(in_mat), cv::gout(gapi_age1, gapi_gender1, gapi_age2, gapi_gender2),
  674. cv::compile_args(cv::gapi::networks(age_net1, age_net2)));
  675. // Validate with IE itself (avoid DNN module dependency here)
  676. normAssert(cv::gapi::ie::util::to_ocv(ie_age1), gapi_age1, "Test age output 1");
  677. normAssert(cv::gapi::ie::util::to_ocv(ie_gender1), gapi_gender1, "Test gender output 1");
  678. normAssert(cv::gapi::ie::util::to_ocv(ie_age2), gapi_age2, "Test age output 2");
  679. normAssert(cv::gapi::ie::util::to_ocv(ie_gender2), gapi_gender2, "Test gender output 2");
  680. }
  681. TEST(TestAgeGenderIE, GenericInfer)
  682. {
  683. initDLDTDataPath();
  684. cv::gapi::ie::detail::ParamDesc params;
  685. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  686. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  687. params.device_id = "CPU";
  688. cv::Mat in_mat(cv::Size(320, 240), CV_8UC3);
  689. cv::randu(in_mat, 0, 255);
  690. cv::Mat gapi_age, gapi_gender;
  691. // Load & run IE network
  692. IE::Blob::Ptr ie_age, ie_gender;
  693. {
  694. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  695. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  696. setNetParameters(net);
  697. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  698. auto infer_request = this_network.CreateInferRequest();
  699. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  700. infer_request.Infer();
  701. ie_age = infer_request.GetBlob("age_conv3");
  702. ie_gender = infer_request.GetBlob("prob");
  703. }
  704. // Configure & run G-API
  705. cv::GMat in;
  706. GInferInputs inputs;
  707. inputs["data"] = in;
  708. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", inputs);
  709. auto age = outputs.at("age_conv3");
  710. auto gender = outputs.at("prob");
  711. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  712. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  713. "age-gender-generic", params.model_path, params.weights_path, params.device_id};
  714. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  715. cv::compile_args(cv::gapi::networks(pp)));
  716. // Validate with IE itself (avoid DNN module dependency here)
  717. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  718. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  719. }
  720. TEST(TestAgeGenderIE, InvalidConfigGeneric)
  721. {
  722. initDLDTDataPath();
  723. std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  724. std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  725. std::string device_id = "CPU";
  726. // Configure & run G-API
  727. cv::GMat in;
  728. GInferInputs inputs;
  729. inputs["data"] = in;
  730. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", inputs);
  731. auto age = outputs.at("age_conv3");
  732. auto gender = outputs.at("prob");
  733. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  734. auto pp = cv::gapi::ie::Params<cv::gapi::Generic>{
  735. "age-gender-generic", model_path, weights_path, device_id
  736. }.pluginConfig({{"unsupported_config", "some_value"}});
  737. EXPECT_ANY_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
  738. cv::compile_args(cv::gapi::networks(pp))));
  739. }
  740. TEST(TestAgeGenderIE, CPUConfigGeneric)
  741. {
  742. initDLDTDataPath();
  743. std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  744. std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  745. std::string device_id = "CPU";
  746. // Configure & run G-API
  747. cv::GMat in;
  748. GInferInputs inputs;
  749. inputs["data"] = in;
  750. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", inputs);
  751. auto age = outputs.at("age_conv3");
  752. auto gender = outputs.at("prob");
  753. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  754. auto pp = cv::gapi::ie::Params<cv::gapi::Generic> {
  755. "age-gender-generic", model_path, weights_path, device_id
  756. }.pluginConfig({{IE::PluginConfigParams::KEY_CPU_THROUGHPUT_STREAMS,
  757. IE::PluginConfigParams::CPU_THROUGHPUT_NUMA}});
  758. EXPECT_NO_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
  759. cv::compile_args(cv::gapi::networks(pp))));
  760. }
  761. TEST(TestAgeGenderIE, InvalidConfig)
  762. {
  763. initDLDTDataPath();
  764. std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  765. std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  766. std::string device_id = "CPU";
  767. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  768. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  769. cv::GMat in;
  770. cv::GMat age, gender;
  771. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  772. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  773. auto pp = cv::gapi::ie::Params<AgeGender> {
  774. model_path, weights_path, device_id
  775. }.cfgOutputLayers({ "age_conv3", "prob" })
  776. .pluginConfig({{"unsupported_config", "some_value"}});
  777. EXPECT_ANY_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
  778. cv::compile_args(cv::gapi::networks(pp))));
  779. }
  780. TEST(TestAgeGenderIE, CPUConfig)
  781. {
  782. initDLDTDataPath();
  783. std::string model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  784. std::string weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  785. std::string device_id = "CPU";
  786. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  787. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  788. cv::GMat in;
  789. cv::GMat age, gender;
  790. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  791. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  792. auto pp = cv::gapi::ie::Params<AgeGender> {
  793. model_path, weights_path, device_id
  794. }.cfgOutputLayers({ "age_conv3", "prob" })
  795. .pluginConfig({{IE::PluginConfigParams::KEY_CPU_THROUGHPUT_STREAMS,
  796. IE::PluginConfigParams::CPU_THROUGHPUT_NUMA}});
  797. EXPECT_NO_THROW(comp.compile(cv::GMatDesc{CV_8U,3,cv::Size{320, 240}},
  798. cv::compile_args(cv::gapi::networks(pp))));
  799. }
  800. TEST_F(ROIList, MediaInputBGR)
  801. {
  802. initDLDTDataPath();
  803. cv::GFrame in;
  804. cv::GArray<cv::Rect> rr;
  805. cv::GArray<cv::GMat> age, gender;
  806. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  807. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  808. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  809. auto pp = cv::gapi::ie::Params<AgeGender> {
  810. params.model_path, params.weights_path, params.device_id
  811. }.cfgOutputLayers({ "age_conv3", "prob" });
  812. comp.apply(cv::gin(frame, m_roi_list),
  813. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  814. cv::compile_args(cv::gapi::networks(pp)));
  815. validate();
  816. }
  817. TEST_F(ROIListNV12, MediaInputNV12)
  818. {
  819. initDLDTDataPath();
  820. cv::GFrame in;
  821. cv::GArray<cv::Rect> rr;
  822. cv::GArray<cv::GMat> age, gender;
  823. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  824. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  825. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  826. auto pp = cv::gapi::ie::Params<AgeGender> {
  827. params.model_path, params.weights_path, params.device_id
  828. }.cfgOutputLayers({ "age_conv3", "prob" });
  829. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  830. // than 2023.0 so G-API must throw error in that case.
  831. #if INF_ENGINE_RELEASE <= 2023000000
  832. comp.apply(cv::gin(frame, m_roi_list),
  833. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  834. cv::compile_args(cv::gapi::networks(pp)));
  835. validate();
  836. #else
  837. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi_list),
  838. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  839. cv::compile_args(cv::gapi::networks(pp))));
  840. #endif
  841. }
  842. TEST(TestAgeGenderIE, MediaInputNV12)
  843. {
  844. initDLDTDataPath();
  845. cv::gapi::ie::detail::ParamDesc params;
  846. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  847. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  848. params.device_id = "CPU";
  849. cv::Size sz{320, 240};
  850. cv::Mat in_y_mat(sz, CV_8UC1);
  851. cv::randu(in_y_mat, 0, 255);
  852. cv::Mat in_uv_mat(sz / 2, CV_8UC2);
  853. cv::randu(in_uv_mat, 0, 255);
  854. cv::Mat gapi_age, gapi_gender;
  855. // NB: NV12 feature shouldn't be used in tests
  856. // if OpenVINO versions is higher than 2023.0
  857. #if INF_ENGINE_RELEASE <= 2023000000
  858. // Load & run IE network
  859. IE::Blob::Ptr ie_age, ie_gender;
  860. {
  861. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  862. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  863. setNetParameters(net, true);
  864. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  865. auto infer_request = this_network.CreateInferRequest();
  866. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat));
  867. infer_request.Infer();
  868. ie_age = infer_request.GetBlob("age_conv3");
  869. ie_gender = infer_request.GetBlob("prob");
  870. }
  871. #endif
  872. // Configure & run G-API
  873. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  874. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  875. cv::GFrame in;
  876. cv::GMat age, gender;
  877. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  878. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  879. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  880. auto pp = cv::gapi::ie::Params<AgeGender> {
  881. params.model_path, params.weights_path, params.device_id
  882. }.cfgOutputLayers({ "age_conv3", "prob" });
  883. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  884. // than 2023.0 so G-API must throw error in that case.
  885. #if INF_ENGINE_RELEASE <= 2023000000
  886. comp.apply(cv::gin(frame), cv::gout(gapi_age, gapi_gender),
  887. cv::compile_args(cv::gapi::networks(pp)));
  888. // Validate with IE itself (avoid DNN module dependency here)
  889. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  890. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  891. #else
  892. EXPECT_ANY_THROW(comp.apply(cv::gin(frame), cv::gout(gapi_age, gapi_gender),
  893. cv::compile_args(cv::gapi::networks(pp))));
  894. #endif
  895. }
  896. TEST(TestAgeGenderIE, MediaInputBGR)
  897. {
  898. initDLDTDataPath();
  899. cv::gapi::ie::detail::ParamDesc params;
  900. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  901. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  902. params.device_id = "CPU";
  903. cv::Size sz{320, 240};
  904. cv::Mat in_mat(sz, CV_8UC3);
  905. cv::randu(in_mat, 0, 255);
  906. cv::Mat gapi_age, gapi_gender;
  907. // Load & run IE network
  908. IE::Blob::Ptr ie_age, ie_gender;
  909. {
  910. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  911. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  912. setNetParameters(net);
  913. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  914. auto infer_request = this_network.CreateInferRequest();
  915. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  916. infer_request.Infer();
  917. ie_age = infer_request.GetBlob("age_conv3");
  918. ie_gender = infer_request.GetBlob("prob");
  919. }
  920. // Configure & run G-API
  921. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  922. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  923. cv::GFrame in;
  924. cv::GMat age, gender;
  925. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  926. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  927. auto frame = MediaFrame::Create<TestMediaBGR>(in_mat);
  928. auto pp = cv::gapi::ie::Params<AgeGender> {
  929. params.model_path, params.weights_path, params.device_id
  930. }.cfgOutputLayers({ "age_conv3", "prob" });
  931. comp.apply(cv::gin(frame), cv::gout(gapi_age, gapi_gender),
  932. cv::compile_args(cv::gapi::networks(pp)));
  933. // Validate with IE itself (avoid DNN module dependency here)
  934. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  935. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  936. }
  937. TEST(InferROI, MediaInputBGR)
  938. {
  939. initDLDTDataPath();
  940. cv::gapi::ie::detail::ParamDesc params;
  941. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  942. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  943. params.device_id = "CPU";
  944. cv::Size sz{320, 240};
  945. cv::Mat in_mat(sz, CV_8UC3);
  946. cv::randu(in_mat, 0, 255);
  947. cv::Mat gapi_age, gapi_gender;
  948. cv::Rect rect(cv::Point{64, 60}, cv::Size{96, 96});
  949. // Load & run IE network
  950. IE::Blob::Ptr ie_age, ie_gender;
  951. {
  952. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  953. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  954. setNetParameters(net);
  955. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  956. auto infer_request = this_network.CreateInferRequest();
  957. const auto ie_rc = IE::ROI {
  958. 0u
  959. , static_cast<std::size_t>(rect.x)
  960. , static_cast<std::size_t>(rect.y)
  961. , static_cast<std::size_t>(rect.width)
  962. , static_cast<std::size_t>(rect.height)
  963. };
  964. IE::Blob::Ptr roi_blob = IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_mat), ie_rc);
  965. infer_request.SetBlob("data", roi_blob);
  966. infer_request.Infer();
  967. ie_age = infer_request.GetBlob("age_conv3");
  968. ie_gender = infer_request.GetBlob("prob");
  969. }
  970. // Configure & run G-API
  971. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  972. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  973. cv::GFrame in;
  974. cv::GOpaque<cv::Rect> roi;
  975. cv::GMat age, gender;
  976. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  977. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  978. auto frame = MediaFrame::Create<TestMediaBGR>(in_mat);
  979. auto pp = cv::gapi::ie::Params<AgeGender> {
  980. params.model_path, params.weights_path, params.device_id
  981. }.cfgOutputLayers({ "age_conv3", "prob" });
  982. comp.apply(cv::gin(frame, rect), cv::gout(gapi_age, gapi_gender),
  983. cv::compile_args(cv::gapi::networks(pp)));
  984. // Validate with IE itself (avoid DNN module dependency here)
  985. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  986. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  987. }
  988. TEST(InferROI, MediaInputNV12)
  989. {
  990. initDLDTDataPath();
  991. cv::gapi::ie::detail::ParamDesc params;
  992. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  993. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  994. params.device_id = "CPU";
  995. cv::Size sz{320, 240};
  996. auto in_y_mat = cv::Mat{sz, CV_8UC1};
  997. cv::randu(in_y_mat, 0, 255);
  998. auto in_uv_mat = cv::Mat{sz / 2, CV_8UC2};
  999. cv::randu(in_uv_mat, 0, 255);
  1000. cv::Mat gapi_age, gapi_gender;
  1001. cv::Rect rect(cv::Point{64, 60}, cv::Size{96, 96});
  1002. // NB: NV12 feature shouldn't be used in tests
  1003. // if OpenVINO versions is higher than 2023.0
  1004. #if INF_ENGINE_RELEASE <= 2023000000
  1005. // Load & run IE network
  1006. IE::Blob::Ptr ie_age, ie_gender;
  1007. {
  1008. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1009. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1010. setNetParameters(net, true);
  1011. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1012. auto infer_request = this_network.CreateInferRequest();
  1013. const auto ie_rc = IE::ROI {
  1014. 0u
  1015. , static_cast<std::size_t>(rect.x)
  1016. , static_cast<std::size_t>(rect.y)
  1017. , static_cast<std::size_t>(rect.width)
  1018. , static_cast<std::size_t>(rect.height)
  1019. };
  1020. IE::Blob::Ptr roi_blob = IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat), ie_rc);
  1021. infer_request.SetBlob("data", roi_blob);
  1022. infer_request.Infer();
  1023. ie_age = infer_request.GetBlob("age_conv3");
  1024. ie_gender = infer_request.GetBlob("prob");
  1025. }
  1026. #endif
  1027. // Configure & run G-API
  1028. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1029. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1030. cv::GFrame in;
  1031. cv::GOpaque<cv::Rect> roi;
  1032. cv::GMat age, gender;
  1033. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  1034. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1035. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  1036. auto pp = cv::gapi::ie::Params<AgeGender> {
  1037. params.model_path, params.weights_path, params.device_id
  1038. }.cfgOutputLayers({ "age_conv3", "prob" });
  1039. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1040. // than 2023.0 so G-API must throw error in that case.
  1041. #if INF_ENGINE_RELEASE <= 2023000000
  1042. comp.apply(cv::gin(frame, rect), cv::gout(gapi_age, gapi_gender),
  1043. cv::compile_args(cv::gapi::networks(pp)));
  1044. // Validate with IE itself (avoid DNN module dependency here)
  1045. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  1046. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  1047. #else
  1048. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, rect), cv::gout(gapi_age, gapi_gender),
  1049. cv::compile_args(cv::gapi::networks(pp))));
  1050. #endif
  1051. }
  1052. TEST_F(ROIList, Infer2MediaInputBGR)
  1053. {
  1054. cv::GArray<cv::Rect> rr;
  1055. cv::GFrame in;
  1056. cv::GArray<cv::GMat> age, gender;
  1057. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  1058. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1059. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  1060. auto pp = cv::gapi::ie::Params<AgeGender> {
  1061. params.model_path, params.weights_path, params.device_id
  1062. }.cfgOutputLayers({ "age_conv3", "prob" });
  1063. comp.apply(cv::gin(frame, m_roi_list),
  1064. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1065. cv::compile_args(cv::gapi::networks(pp)));
  1066. validate();
  1067. }
  1068. TEST_F(ROIListNV12, Infer2MediaInputNV12)
  1069. {
  1070. cv::GArray<cv::Rect> rr;
  1071. cv::GFrame in;
  1072. cv::GArray<cv::GMat> age, gender;
  1073. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  1074. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1075. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  1076. auto pp = cv::gapi::ie::Params<AgeGender> {
  1077. params.model_path, params.weights_path, params.device_id
  1078. }.cfgOutputLayers({ "age_conv3", "prob" });
  1079. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1080. // than 2023.0 so G-API must throw error in that case.
  1081. #if INF_ENGINE_RELEASE <= 2023000000
  1082. comp.apply(cv::gin(frame, m_roi_list),
  1083. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1084. cv::compile_args(cv::gapi::networks(pp)));
  1085. validate();
  1086. #else
  1087. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi_list),
  1088. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1089. cv::compile_args(cv::gapi::networks(pp))));
  1090. #endif
  1091. }
  1092. TEST_F(SingleROI, GenericInfer)
  1093. {
  1094. // Configure & run G-API
  1095. cv::GMat in;
  1096. cv::GOpaque<cv::Rect> roi;
  1097. cv::GInferInputs inputs;
  1098. inputs["data"] = in;
  1099. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", roi, inputs);
  1100. auto age = outputs.at("age_conv3");
  1101. auto gender = outputs.at("prob");
  1102. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1103. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1104. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1105. };
  1106. pp.cfgNumRequests(2u);
  1107. comp.apply(cv::gin(m_in_mat, m_roi), cv::gout(m_out_gapi_age, m_out_gapi_gender),
  1108. cv::compile_args(cv::gapi::networks(pp)));
  1109. validate();
  1110. }
  1111. TEST_F(SingleROI, GenericInferMediaBGR)
  1112. {
  1113. // Configure & run G-API
  1114. cv::GFrame in;
  1115. cv::GOpaque<cv::Rect> roi;
  1116. cv::GInferInputs inputs;
  1117. inputs["data"] = in;
  1118. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", roi, inputs);
  1119. auto age = outputs.at("age_conv3");
  1120. auto gender = outputs.at("prob");
  1121. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1122. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1123. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1124. };
  1125. pp.cfgNumRequests(2u);
  1126. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  1127. comp.apply(cv::gin(frame, m_roi), cv::gout(m_out_gapi_age, m_out_gapi_gender),
  1128. cv::compile_args(cv::gapi::networks(pp)));
  1129. validate();
  1130. }
  1131. TEST_F(SingleROINV12, GenericInferMediaNV12)
  1132. {
  1133. // Configure & run G-API
  1134. cv::GFrame in;
  1135. cv::GOpaque<cv::Rect> roi;
  1136. cv::GInferInputs inputs;
  1137. inputs["data"] = in;
  1138. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", roi, inputs);
  1139. auto age = outputs.at("age_conv3");
  1140. auto gender = outputs.at("prob");
  1141. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1142. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1143. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1144. };
  1145. pp.cfgNumRequests(2u);
  1146. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  1147. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1148. // than 2023.0 so G-API must throw error in that case.
  1149. #if INF_ENGINE_RELEASE <= 2023000000
  1150. comp.apply(cv::gin(frame, m_roi), cv::gout(m_out_gapi_age, m_out_gapi_gender),
  1151. cv::compile_args(cv::gapi::networks(pp)));
  1152. validate();
  1153. #else
  1154. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi),
  1155. cv::gout(m_out_gapi_age, m_out_gapi_gender),
  1156. cv::compile_args(cv::gapi::networks(pp))));
  1157. #endif
  1158. }
  1159. TEST_F(ROIList, GenericInfer)
  1160. {
  1161. cv::GMat in;
  1162. cv::GArray<cv::Rect> rr;
  1163. cv::GInferInputs inputs;
  1164. inputs["data"] = in;
  1165. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", rr, inputs);
  1166. auto age = outputs.at("age_conv3");
  1167. auto gender = outputs.at("prob");
  1168. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1169. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1170. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1171. };
  1172. pp.cfgNumRequests(2u);
  1173. comp.apply(cv::gin(m_in_mat, m_roi_list),
  1174. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1175. cv::compile_args(cv::gapi::networks(pp)));
  1176. validate();
  1177. }
  1178. TEST_F(ROIList, GenericInferMediaBGR)
  1179. {
  1180. cv::GFrame in;
  1181. cv::GArray<cv::Rect> rr;
  1182. cv::GInferInputs inputs;
  1183. inputs["data"] = in;
  1184. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", rr, inputs);
  1185. auto age = outputs.at("age_conv3");
  1186. auto gender = outputs.at("prob");
  1187. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1188. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1189. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1190. };
  1191. pp.cfgNumRequests(2u);
  1192. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  1193. comp.apply(cv::gin(frame, m_roi_list),
  1194. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1195. cv::compile_args(cv::gapi::networks(pp)));
  1196. validate();
  1197. }
  1198. TEST_F(ROIListNV12, GenericInferMediaNV12)
  1199. {
  1200. cv::GFrame in;
  1201. cv::GArray<cv::Rect> rr;
  1202. cv::GInferInputs inputs;
  1203. inputs["data"] = in;
  1204. auto outputs = cv::gapi::infer<cv::gapi::Generic>("age-gender-generic", rr, inputs);
  1205. auto age = outputs.at("age_conv3");
  1206. auto gender = outputs.at("prob");
  1207. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1208. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1209. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1210. };
  1211. pp.cfgNumRequests(2u);
  1212. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  1213. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1214. // than 2023.0 so G-API must throw error in that case.
  1215. #if INF_ENGINE_RELEASE <= 2023000000
  1216. comp.apply(cv::gin(frame, m_roi_list),
  1217. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1218. cv::compile_args(cv::gapi::networks(pp)));
  1219. validate();
  1220. #else
  1221. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi_list),
  1222. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1223. cv::compile_args(cv::gapi::networks(pp))));
  1224. #endif
  1225. }
  1226. TEST_F(ROIList, GenericInfer2)
  1227. {
  1228. cv::GArray<cv::Rect> rr;
  1229. cv::GMat in;
  1230. GInferListInputs list;
  1231. list["data"] = rr;
  1232. auto outputs = cv::gapi::infer2<cv::gapi::Generic>("age-gender-generic", in, list);
  1233. auto age = outputs.at("age_conv3");
  1234. auto gender = outputs.at("prob");
  1235. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1236. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1237. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1238. };
  1239. pp.cfgNumRequests(2u);
  1240. comp.apply(cv::gin(m_in_mat, m_roi_list),
  1241. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1242. cv::compile_args(cv::gapi::networks(pp)));
  1243. validate();
  1244. }
  1245. TEST_F(ROIList, GenericInfer2MediaInputBGR)
  1246. {
  1247. cv::GArray<cv::Rect> rr;
  1248. cv::GFrame in;
  1249. GInferListInputs inputs;
  1250. inputs["data"] = rr;
  1251. auto outputs = cv::gapi::infer2<cv::gapi::Generic>("age-gender-generic", in, inputs);
  1252. auto age = outputs.at("age_conv3");
  1253. auto gender = outputs.at("prob");
  1254. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1255. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1256. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1257. };
  1258. pp.cfgNumRequests(2u);
  1259. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  1260. comp.apply(cv::gin(frame, m_roi_list),
  1261. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1262. cv::compile_args(cv::gapi::networks(pp)));
  1263. validate();
  1264. }
  1265. TEST_F(ROIListNV12, GenericInfer2MediaInputNV12)
  1266. {
  1267. cv::GArray<cv::Rect> rr;
  1268. cv::GFrame in;
  1269. GInferListInputs inputs;
  1270. inputs["data"] = rr;
  1271. auto outputs = cv::gapi::infer2<cv::gapi::Generic>("age-gender-generic", in, inputs);
  1272. auto age = outputs.at("age_conv3");
  1273. auto gender = outputs.at("prob");
  1274. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1275. cv::gapi::ie::Params<cv::gapi::Generic> pp{
  1276. "age-gender-generic", params.model_path, params.weights_path, params.device_id
  1277. };
  1278. pp.cfgNumRequests(2u);
  1279. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  1280. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1281. // than 2023.0 so G-API must throw error in that case.
  1282. #if INF_ENGINE_RELEASE <= 2023000000
  1283. comp.apply(cv::gin(frame, m_roi_list),
  1284. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1285. cv::compile_args(cv::gapi::networks(pp)));
  1286. validate();
  1287. #else
  1288. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi_list),
  1289. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1290. cv::compile_args(cv::gapi::networks(pp))));
  1291. #endif
  1292. }
  1293. TEST(Infer, SetInvalidNumberOfRequests)
  1294. {
  1295. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1296. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1297. cv::gapi::ie::Params<AgeGender> pp{"model", "weights", "device"};
  1298. EXPECT_ANY_THROW(pp.cfgNumRequests(0u));
  1299. }
  1300. TEST(Infer, TestStreamingInfer)
  1301. {
  1302. if (cvtest::skipUnstableTests)
  1303. throw SkipTestException("Skip InferROI.TestStreamingInfer as it hangs sporadically");
  1304. initDLDTDataPath();
  1305. std::string filepath = findDataFile("cv/video/768x576.avi");
  1306. cv::gapi::ie::detail::ParamDesc params;
  1307. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1308. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1309. params.device_id = "CPU";
  1310. // Load IE network, initialize input data using that.
  1311. cv::Mat in_mat;
  1312. cv::Mat gapi_age, gapi_gender;
  1313. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1314. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1315. cv::GMat in;
  1316. cv::GMat age, gender;
  1317. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1318. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  1319. auto pp = cv::gapi::ie::Params<AgeGender> {
  1320. params.model_path, params.weights_path, params.device_id
  1321. }.cfgOutputLayers({ "age_conv3", "prob" })
  1322. .cfgNumRequests(4u);
  1323. std::size_t num_frames = 0u;
  1324. std::size_t max_frames = 10u;
  1325. cv::VideoCapture cap;
  1326. cap.open(filepath);
  1327. if (!cap.isOpened())
  1328. throw SkipTestException("Video file can not be opened");
  1329. cap >> in_mat;
  1330. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1331. pipeline.setSource<cv::gapi::wip::GCaptureSource>(filepath);
  1332. pipeline.start();
  1333. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_age, gapi_gender)))
  1334. {
  1335. IE::Blob::Ptr ie_age, ie_gender;
  1336. {
  1337. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1338. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1339. setNetParameters(net);
  1340. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1341. auto infer_request = this_network.CreateInferRequest();
  1342. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  1343. infer_request.Infer();
  1344. ie_age = infer_request.GetBlob("age_conv3");
  1345. ie_gender = infer_request.GetBlob("prob");
  1346. }
  1347. // Validate with IE itself (avoid DNN module dependency here)
  1348. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  1349. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  1350. ++num_frames;
  1351. cap >> in_mat;
  1352. }
  1353. pipeline.stop();
  1354. }
  1355. TEST(InferROI, TestStreamingInfer)
  1356. {
  1357. if (cvtest::skipUnstableTests)
  1358. throw SkipTestException("Skip InferROI.TestStreamingInfer as it hangs sporadically");
  1359. initDLDTDataPath();
  1360. std::string filepath = findDataFile("cv/video/768x576.avi");
  1361. cv::gapi::ie::detail::ParamDesc params;
  1362. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1363. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1364. params.device_id = "CPU";
  1365. // Load IE network, initialize input data using that.
  1366. cv::Mat in_mat;
  1367. cv::Mat gapi_age, gapi_gender;
  1368. cv::Rect rect(cv::Point{64, 60}, cv::Size{96, 96});
  1369. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1370. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1371. cv::GMat in;
  1372. cv::GOpaque<cv::Rect> roi;
  1373. cv::GMat age, gender;
  1374. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  1375. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1376. auto pp = cv::gapi::ie::Params<AgeGender> {
  1377. params.model_path, params.weights_path, params.device_id
  1378. }.cfgOutputLayers({ "age_conv3", "prob" })
  1379. .cfgNumRequests(4u);
  1380. std::size_t num_frames = 0u;
  1381. std::size_t max_frames = 10u;
  1382. cv::VideoCapture cap;
  1383. cap.open(filepath);
  1384. if (!cap.isOpened())
  1385. throw SkipTestException("Video file can not be opened");
  1386. cap >> in_mat;
  1387. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1388. pipeline.setSource(
  1389. cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(filepath), rect));
  1390. pipeline.start();
  1391. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_age, gapi_gender)))
  1392. {
  1393. // Load & run IE network
  1394. IE::Blob::Ptr ie_age, ie_gender;
  1395. {
  1396. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1397. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1398. setNetParameters(net);
  1399. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1400. auto infer_request = this_network.CreateInferRequest();
  1401. const auto ie_rc = IE::ROI {
  1402. 0u
  1403. , static_cast<std::size_t>(rect.x)
  1404. , static_cast<std::size_t>(rect.y)
  1405. , static_cast<std::size_t>(rect.width)
  1406. , static_cast<std::size_t>(rect.height)
  1407. };
  1408. IE::Blob::Ptr roi_blob = IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_mat), ie_rc);
  1409. infer_request.SetBlob("data", roi_blob);
  1410. infer_request.Infer();
  1411. ie_age = infer_request.GetBlob("age_conv3");
  1412. ie_gender = infer_request.GetBlob("prob");
  1413. }
  1414. // Validate with IE itself (avoid DNN module dependency here)
  1415. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  1416. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  1417. ++num_frames;
  1418. cap >> in_mat;
  1419. }
  1420. pipeline.stop();
  1421. }
  1422. TEST(InferList, TestStreamingInfer)
  1423. {
  1424. if (cvtest::skipUnstableTests)
  1425. throw SkipTestException("Skip InferList.TestStreamingInfer as it hangs sporadically");
  1426. initDLDTDataPath();
  1427. std::string filepath = findDataFile("cv/video/768x576.avi");
  1428. cv::gapi::ie::detail::ParamDesc params;
  1429. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1430. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1431. params.device_id = "CPU";
  1432. // Load IE network, initialize input data using that.
  1433. cv::Mat in_mat;
  1434. std::vector<cv::Mat> ie_ages, ie_genders, gapi_ages, gapi_genders;
  1435. std::vector<cv::Rect> roi_list = {
  1436. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  1437. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  1438. };
  1439. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1440. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1441. cv::GMat in;
  1442. cv::GArray<cv::Rect> roi;
  1443. cv::GArray<GMat> age, gender;
  1444. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  1445. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1446. auto pp = cv::gapi::ie::Params<AgeGender> {
  1447. params.model_path, params.weights_path, params.device_id
  1448. }.cfgOutputLayers({ "age_conv3", "prob" })
  1449. .cfgNumRequests(4u);
  1450. std::size_t num_frames = 0u;
  1451. std::size_t max_frames = 10u;
  1452. cv::VideoCapture cap;
  1453. cap.open(filepath);
  1454. if (!cap.isOpened())
  1455. throw SkipTestException("Video file can not be opened");
  1456. cap >> in_mat;
  1457. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1458. pipeline.setSource(
  1459. cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(filepath), roi_list));
  1460. pipeline.start();
  1461. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_ages, gapi_genders)))
  1462. {
  1463. {
  1464. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1465. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1466. setNetParameters(net);
  1467. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1468. auto infer_request = this_network.CreateInferRequest();
  1469. auto frame_blob = cv::gapi::ie::util::to_ie(in_mat);
  1470. for (auto &&rc : roi_list) {
  1471. const auto ie_rc = IE::ROI {
  1472. 0u
  1473. , static_cast<std::size_t>(rc.x)
  1474. , static_cast<std::size_t>(rc.y)
  1475. , static_cast<std::size_t>(rc.width)
  1476. , static_cast<std::size_t>(rc.height)
  1477. };
  1478. infer_request.SetBlob("data", IE::make_shared_blob(frame_blob, ie_rc));
  1479. infer_request.Infer();
  1480. using namespace cv::gapi::ie::util;
  1481. ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  1482. ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  1483. }
  1484. } // namespace IE = ..
  1485. // Validate with IE itself (avoid DNN module dependency here)
  1486. normAssert(ie_ages [0], gapi_ages [0], "0: Test age output");
  1487. normAssert(ie_genders[0], gapi_genders[0], "0: Test gender output");
  1488. normAssert(ie_ages [1], gapi_ages [1], "1: Test age output");
  1489. normAssert(ie_genders[1], gapi_genders[1], "1: Test gender output");
  1490. ie_ages.clear();
  1491. ie_genders.clear();
  1492. ++num_frames;
  1493. cap >> in_mat;
  1494. }
  1495. }
  1496. TEST(Infer2, TestStreamingInfer)
  1497. {
  1498. if (cvtest::skipUnstableTests)
  1499. throw SkipTestException("Skip InferROI.TestStreamingInfer as it hangs sporadically");
  1500. initDLDTDataPath();
  1501. std::string filepath = findDataFile("cv/video/768x576.avi");
  1502. cv::gapi::ie::detail::ParamDesc params;
  1503. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1504. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1505. params.device_id = "CPU";
  1506. // Load IE network, initialize input data using that.
  1507. cv::Mat in_mat;
  1508. std::vector<cv::Mat> ie_ages, ie_genders, gapi_ages, gapi_genders;
  1509. std::vector<cv::Rect> roi_list = {
  1510. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  1511. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  1512. };
  1513. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1514. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1515. cv::GArray<cv::Rect> rr;
  1516. cv::GMat in;
  1517. cv::GArray<cv::GMat> age, gender;
  1518. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  1519. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1520. auto pp = cv::gapi::ie::Params<AgeGender> {
  1521. params.model_path, params.weights_path, params.device_id
  1522. }.cfgOutputLayers({ "age_conv3", "prob" })
  1523. .cfgNumRequests(4u);
  1524. std::size_t num_frames = 0u;
  1525. std::size_t max_frames = 10u;
  1526. cv::VideoCapture cap;
  1527. cap.open(filepath);
  1528. if (!cap.isOpened())
  1529. throw SkipTestException("Video file can not be opened");
  1530. cap >> in_mat;
  1531. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1532. pipeline.setSource(
  1533. cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(filepath), roi_list));
  1534. pipeline.start();
  1535. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_ages, gapi_genders)))
  1536. {
  1537. {
  1538. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1539. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1540. setNetParameters(net);
  1541. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1542. auto infer_request = this_network.CreateInferRequest();
  1543. auto frame_blob = cv::gapi::ie::util::to_ie(in_mat);
  1544. for (auto &&rc : roi_list) {
  1545. const auto ie_rc = IE::ROI {
  1546. 0u
  1547. , static_cast<std::size_t>(rc.x)
  1548. , static_cast<std::size_t>(rc.y)
  1549. , static_cast<std::size_t>(rc.width)
  1550. , static_cast<std::size_t>(rc.height)
  1551. };
  1552. infer_request.SetBlob("data", IE::make_shared_blob(frame_blob, ie_rc));
  1553. infer_request.Infer();
  1554. using namespace cv::gapi::ie::util;
  1555. ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  1556. ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  1557. }
  1558. } // namespace IE = ..
  1559. // Validate with IE itself (avoid DNN module dependency here)
  1560. normAssert(ie_ages [0], gapi_ages [0], "0: Test age output");
  1561. normAssert(ie_genders[0], gapi_genders[0], "0: Test gender output");
  1562. normAssert(ie_ages [1], gapi_ages [1], "1: Test age output");
  1563. normAssert(ie_genders[1], gapi_genders[1], "1: Test gender output");
  1564. ie_ages.clear();
  1565. ie_genders.clear();
  1566. ++num_frames;
  1567. cap >> in_mat;
  1568. }
  1569. pipeline.stop();
  1570. }
  1571. TEST(InferEmptyList, TestStreamingInfer)
  1572. {
  1573. initDLDTDataPath();
  1574. std::string filepath = findDataFile("cv/video/768x576.avi");
  1575. cv::gapi::ie::detail::ParamDesc params;
  1576. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1577. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1578. params.device_id = "CPU";
  1579. // Load IE network, initialize input data using that.
  1580. cv::Mat in_mat;
  1581. std::vector<cv::Mat> ie_ages, ie_genders, gapi_ages, gapi_genders;
  1582. // NB: Empty list of roi
  1583. std::vector<cv::Rect> roi_list;
  1584. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1585. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1586. cv::GMat in;
  1587. cv::GArray<cv::Rect> roi;
  1588. cv::GArray<GMat> age, gender;
  1589. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  1590. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  1591. auto pp = cv::gapi::ie::Params<AgeGender> {
  1592. params.model_path, params.weights_path, params.device_id
  1593. }.cfgOutputLayers({ "age_conv3", "prob" })
  1594. .cfgNumRequests(4u);
  1595. std::size_t num_frames = 0u;
  1596. std::size_t max_frames = 1u;
  1597. cv::VideoCapture cap;
  1598. cap.open(filepath);
  1599. if (!cap.isOpened())
  1600. throw SkipTestException("Video file can not be opened");
  1601. cap >> in_mat;
  1602. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1603. pipeline.setSource(
  1604. cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(filepath), roi_list));
  1605. pipeline.start();
  1606. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_ages, gapi_genders)))
  1607. {
  1608. EXPECT_TRUE(gapi_ages.empty());
  1609. EXPECT_TRUE(gapi_genders.empty());
  1610. }
  1611. }
  1612. TEST(Infer2EmptyList, TestStreamingInfer)
  1613. {
  1614. initDLDTDataPath();
  1615. std::string filepath = findDataFile("cv/video/768x576.avi");
  1616. cv::gapi::ie::detail::ParamDesc params;
  1617. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1618. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1619. params.device_id = "CPU";
  1620. // Load IE network, initialize input data using that.
  1621. cv::Mat in_mat;
  1622. std::vector<cv::Mat> ie_ages, ie_genders, gapi_ages, gapi_genders;
  1623. // NB: Empty list of roi
  1624. std::vector<cv::Rect> roi_list;
  1625. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1626. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1627. cv::GArray<cv::Rect> rr;
  1628. cv::GMat in;
  1629. cv::GArray<cv::GMat> age, gender;
  1630. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  1631. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1632. auto pp = cv::gapi::ie::Params<AgeGender> {
  1633. params.model_path, params.weights_path, params.device_id
  1634. }.cfgOutputLayers({ "age_conv3", "prob" })
  1635. .cfgNumRequests(4u);
  1636. std::size_t num_frames = 0u;
  1637. std::size_t max_frames = 1u;
  1638. cv::VideoCapture cap;
  1639. cap.open(filepath);
  1640. if (!cap.isOpened())
  1641. throw SkipTestException("Video file can not be opened");
  1642. cap >> in_mat;
  1643. auto pipeline = comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1644. pipeline.setSource(
  1645. cv::gin(cv::gapi::wip::make_src<cv::gapi::wip::GCaptureSource>(filepath), roi_list));
  1646. pipeline.start();
  1647. while (num_frames < max_frames && pipeline.pull(cv::gout(gapi_ages, gapi_genders)))
  1648. {
  1649. EXPECT_TRUE(gapi_ages.empty());
  1650. EXPECT_TRUE(gapi_genders.empty());
  1651. }
  1652. }
  1653. TEST_F(InferWithReshape, TestInfer)
  1654. {
  1655. // IE code
  1656. infer(m_in_mat);
  1657. // G-API code
  1658. cv::GMat in;
  1659. cv::GMat age, gender;
  1660. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1661. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  1662. auto pp = cv::gapi::ie::Params<AgeGender> {
  1663. params.model_path, params.weights_path, params.device_id
  1664. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({{"data", reshape_dims}});
  1665. comp.apply(cv::gin(m_in_mat), cv::gout(m_out_gapi_ages.front(), m_out_gapi_genders.front()),
  1666. cv::compile_args(cv::gapi::networks(pp)));
  1667. // Validate
  1668. validate();
  1669. }
  1670. TEST_F(InferWithReshape, TestInferInImage)
  1671. {
  1672. // Input image already has 70x70 size
  1673. cv::Mat rsz;
  1674. cv::resize(m_in_mat, rsz, cv::Size(70, 70));
  1675. // IE code
  1676. infer(rsz);
  1677. // G-API code
  1678. cv::GMat in;
  1679. cv::GMat age, gender;
  1680. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1681. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  1682. auto pp = cv::gapi::ie::Params<AgeGender> {
  1683. params.model_path, params.weights_path, params.device_id
  1684. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({"data"});
  1685. // Reshape CNN input by input image size
  1686. comp.apply(cv::gin(rsz), cv::gout(m_out_gapi_ages.front(), m_out_gapi_genders.front()),
  1687. cv::compile_args(cv::gapi::networks(pp)));
  1688. // Validate
  1689. validate();
  1690. }
  1691. TEST_F(InferWithReshape, TestInferForSingleLayer)
  1692. {
  1693. // IE code
  1694. infer(m_in_mat);
  1695. // G-API code
  1696. cv::GMat in;
  1697. cv::GMat age, gender;
  1698. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1699. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  1700. auto pp = cv::gapi::ie::Params<AgeGender> {
  1701. params.model_path, params.weights_path, params.device_id
  1702. }.cfgOutputLayers({ "age_conv3", "prob" })
  1703. .cfgInputReshape("data", reshape_dims);
  1704. comp.apply(cv::gin(m_in_mat), cv::gout(m_out_gapi_ages.front(), m_out_gapi_genders.front()),
  1705. cv::compile_args(cv::gapi::networks(pp)));
  1706. // Validate
  1707. validate();
  1708. }
  1709. TEST_F(InferWithReshape, TestInferList)
  1710. {
  1711. // IE code
  1712. infer(m_in_mat, true);
  1713. // G-API code
  1714. cv::GArray<cv::Rect> rr;
  1715. cv::GMat in;
  1716. cv::GArray<cv::GMat> age, gender;
  1717. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  1718. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1719. auto pp = cv::gapi::ie::Params<AgeGender> {
  1720. params.model_path, params.weights_path, params.device_id
  1721. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({{"data", reshape_dims}});
  1722. comp.apply(cv::gin(m_in_mat, m_roi_list),
  1723. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1724. cv::compile_args(cv::gapi::networks(pp)));
  1725. // Validate
  1726. validate();
  1727. }
  1728. TEST_F(InferWithReshape, TestInferList2)
  1729. {
  1730. // IE code
  1731. infer(m_in_mat, true);
  1732. // G-API code
  1733. cv::GArray<cv::Rect> rr;
  1734. cv::GMat in;
  1735. cv::GArray<cv::GMat> age, gender;
  1736. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  1737. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1738. auto pp = cv::gapi::ie::Params<AgeGender> {
  1739. params.model_path, params.weights_path, params.device_id
  1740. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({{"data", reshape_dims}});
  1741. comp.apply(cv::gin(m_in_mat, m_roi_list),
  1742. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1743. cv::compile_args(cv::gapi::networks(pp)));
  1744. // Validate
  1745. validate();
  1746. }
  1747. TEST_F(InferWithReshape, TestInferListBGR)
  1748. {
  1749. // IE code
  1750. infer(m_in_mat, true);
  1751. // G-API code
  1752. cv::GArray<cv::Rect> rr;
  1753. cv::GFrame in;
  1754. cv::GArray<cv::GMat> age, gender;
  1755. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  1756. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1757. auto frame = MediaFrame::Create<TestMediaBGR>(m_in_mat);
  1758. auto pp = cv::gapi::ie::Params<AgeGender> {
  1759. params.model_path, params.weights_path, params.device_id
  1760. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({{"data", reshape_dims}});
  1761. comp.apply(cv::gin(frame, m_roi_list),
  1762. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1763. cv::compile_args(cv::gapi::networks(pp)));
  1764. // Validate
  1765. validate();
  1766. }
  1767. TEST_F(InferWithReshapeNV12, TestInferListYUV)
  1768. {
  1769. // G-API code
  1770. cv::GFrame in;
  1771. cv::GArray<cv::Rect> rr;
  1772. cv::GArray<cv::GMat> age, gender;
  1773. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  1774. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1775. auto frame = MediaFrame::Create<TestMediaNV12>(m_in_y, m_in_uv);
  1776. auto pp = cv::gapi::ie::Params<AgeGender> {
  1777. params.model_path, params.weights_path, params.device_id
  1778. }.cfgOutputLayers({ "age_conv3", "prob" }).cfgInputReshape({{"data", reshape_dims}});
  1779. // NB: NV12 feature has been deprecated in OpenVINO versions higher
  1780. // than 2023.0 so G-API must throw error in that case.
  1781. #if INF_ENGINE_RELEASE <= 2023000000
  1782. comp.apply(cv::gin(frame, m_roi_list),
  1783. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1784. cv::compile_args(cv::gapi::networks(pp)));
  1785. // Validate
  1786. validate();
  1787. #else
  1788. EXPECT_ANY_THROW(comp.apply(cv::gin(frame, m_roi_list),
  1789. cv::gout(m_out_gapi_ages, m_out_gapi_genders),
  1790. cv::compile_args(cv::gapi::networks(pp))));
  1791. #endif
  1792. }
  1793. TEST_F(ROIList, CallInferMultipleTimes)
  1794. {
  1795. cv::GArray<cv::Rect> rr;
  1796. cv::GMat in;
  1797. cv::GArray<cv::GMat> age, gender;
  1798. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  1799. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  1800. auto pp = cv::gapi::ie::Params<AgeGender> {
  1801. params.model_path, params.weights_path, params.device_id
  1802. }.cfgOutputLayers({ "age_conv3", "prob" });
  1803. auto cc = comp.compile(cv::descr_of(cv::gin(m_in_mat, m_roi_list)),
  1804. cv::compile_args(cv::gapi::networks(pp)));
  1805. for (int i = 0; i < 10; ++i) {
  1806. cc(cv::gin(m_in_mat, m_roi_list), cv::gout(m_out_gapi_ages, m_out_gapi_genders));
  1807. }
  1808. validate();
  1809. }
  1810. #if INF_ENGINE_RELEASE <= 2023000000
  1811. TEST(IEFrameAdapter, blobParams)
  1812. {
  1813. cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
  1814. cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr);
  1815. auto expected = std::make_pair(IE::TensorDesc{IE::Precision::U8, {1, 3, 300, 300},
  1816. IE::Layout::NCHW},
  1817. IE::ParamMap{{"HELLO", 42}, {"COLOR_FORMAT",
  1818. IE::ColorFormat::NV12}});
  1819. auto actual = cv::util::any_cast<decltype(expected)>(frame.blobParams());
  1820. EXPECT_EQ(expected, actual);
  1821. }
  1822. #endif
  1823. namespace
  1824. {
  1825. struct Sync {
  1826. std::mutex m;
  1827. std::condition_variable cv;
  1828. int counter = 0;
  1829. };
  1830. class GMockMediaAdapter final: public cv::MediaFrame::IAdapter {
  1831. public:
  1832. explicit GMockMediaAdapter(cv::Mat m, std::shared_ptr<Sync> sync)
  1833. : m_mat(m), m_sync(sync) {
  1834. }
  1835. cv::GFrameDesc meta() const override {
  1836. return cv::GFrameDesc{cv::MediaFormat::BGR, m_mat.size()};
  1837. }
  1838. cv::MediaFrame::View access(cv::MediaFrame::Access) override {
  1839. cv::MediaFrame::View::Ptrs pp = { m_mat.ptr(), nullptr, nullptr, nullptr };
  1840. cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
  1841. return cv::MediaFrame::View(std::move(pp), std::move(ss));
  1842. }
  1843. ~GMockMediaAdapter() {
  1844. {
  1845. std::lock_guard<std::mutex> lk{m_sync->m};
  1846. m_sync->counter--;
  1847. }
  1848. m_sync->cv.notify_one();
  1849. }
  1850. private:
  1851. cv::Mat m_mat;
  1852. std::shared_ptr<Sync> m_sync;
  1853. };
  1854. // NB: This source is needed to simulate real
  1855. // cases where the memory resources are limited.
  1856. // GMockSource(int limit) - accept the number of MediaFrames that
  1857. // the source can produce until resources are over.
  1858. class GMockSource : public cv::gapi::wip::IStreamSource {
  1859. public:
  1860. explicit GMockSource(int limit)
  1861. : m_limit(limit), m_mat(cv::Size(1920, 1080), CV_8UC3),
  1862. m_sync(new Sync{}) {
  1863. cv::randu(m_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  1864. }
  1865. bool pull(cv::gapi::wip::Data& data) {
  1866. std::unique_lock<std::mutex> lk(m_sync->m);
  1867. m_sync->counter++;
  1868. // NB: Can't produce new frames until old ones are released.
  1869. m_sync->cv.wait(lk, [this]{return m_sync->counter <= m_limit;});
  1870. data = cv::MediaFrame::Create<GMockMediaAdapter>(m_mat, m_sync);
  1871. return true;
  1872. }
  1873. GMetaArg descr_of() const override {
  1874. return GMetaArg{cv::GFrameDesc{cv::MediaFormat::BGR, m_mat.size()}};
  1875. }
  1876. private:
  1877. int m_limit;
  1878. cv::Mat m_mat;
  1879. std::shared_ptr<Sync> m_sync;
  1880. };
  1881. struct LimitedSourceInfer: public ::testing::Test {
  1882. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1883. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1884. LimitedSourceInfer()
  1885. : comp([](){
  1886. cv::GFrame in;
  1887. cv::GMat age, gender;
  1888. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1889. return cv::GComputation(cv::GIn(in), cv::GOut(age, gender));
  1890. }) {
  1891. initDLDTDataPath();
  1892. }
  1893. GStreamingCompiled compileStreaming(int nireq) {
  1894. cv::gapi::ie::detail::ParamDesc params;
  1895. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1896. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1897. params.device_id = "CPU";
  1898. auto pp = cv::gapi::ie::Params<AgeGender> {
  1899. params.model_path, params.weights_path, params.device_id }
  1900. .cfgOutputLayers({ "age_conv3", "prob" })
  1901. .cfgNumRequests(nireq);
  1902. return comp.compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  1903. }
  1904. void run(const int max_frames, const int limit, const int nireq) {
  1905. auto pipeline = compileStreaming(nireq);
  1906. pipeline.setSource<GMockSource>(limit);
  1907. pipeline.start();
  1908. int num_frames = 0;
  1909. while (num_frames != max_frames &&
  1910. pipeline.pull(cv::gout(out_age, out_gender))) {
  1911. ++num_frames;
  1912. }
  1913. }
  1914. cv::GComputation comp;
  1915. cv::Mat out_age, out_gender;
  1916. };
  1917. } // anonymous namespace
  1918. TEST_F(LimitedSourceInfer, ReleaseFrame)
  1919. {
  1920. constexpr int max_frames = 50;
  1921. constexpr int resources_limit = 1;
  1922. constexpr int nireq = 1;
  1923. run(max_frames, resources_limit, nireq);
  1924. }
  1925. TEST_F(LimitedSourceInfer, ReleaseFrameAsync)
  1926. {
  1927. if (cvtest::skipUnstableTests)
  1928. throw SkipTestException("Skip LimitedSourceInfer.ReleaseFrameAsync as it hangs sporadically");
  1929. constexpr int max_frames = 50;
  1930. constexpr int resources_limit = 4;
  1931. constexpr int nireq = 8;
  1932. run(max_frames, resources_limit, nireq);
  1933. }
  1934. TEST(TestAgeGenderIE, InferWithBatch)
  1935. {
  1936. initDLDTDataPath();
  1937. constexpr int batch_size = 4;
  1938. cv::gapi::ie::detail::ParamDesc params;
  1939. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  1940. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  1941. params.device_id = "CPU";
  1942. cv::Mat in_mat({batch_size, 3, 62, 62}, CV_8U);
  1943. cv::randu(in_mat, 0, 255);
  1944. cv::Mat gapi_age, gapi_gender;
  1945. // Load & run IE network
  1946. IE::Blob::Ptr ie_age, ie_gender;
  1947. {
  1948. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1949. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  1950. auto ii = net.getInputsInfo().at("data");
  1951. ii->setPrecision(IE::Precision::U8);
  1952. net.setBatchSize(batch_size);
  1953. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  1954. auto infer_request = this_network.CreateInferRequest();
  1955. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat));
  1956. infer_request.Infer();
  1957. ie_age = infer_request.GetBlob("age_conv3");
  1958. ie_gender = infer_request.GetBlob("prob");
  1959. }
  1960. // Configure & run G-API
  1961. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  1962. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  1963. cv::GMat in;
  1964. cv::GMat age, gender;
  1965. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  1966. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  1967. auto pp = cv::gapi::ie::Params<AgeGender> {
  1968. params.model_path, params.weights_path, params.device_id
  1969. }.cfgOutputLayers({ "age_conv3", "prob" })
  1970. .cfgBatchSize(batch_size);
  1971. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  1972. cv::compile_args(cv::gapi::networks(pp)));
  1973. // Validate with IE itself (avoid DNN module dependency here)
  1974. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  1975. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  1976. }
  1977. // NB: All tests below use preprocessing for "Import" networks
  1978. // passed as the last argument to SetBLob. This overload has
  1979. // been deprecated in OpenVINO 1.0 API.
  1980. #if INF_ENGINE_RELEASE <= 2023000000
  1981. TEST(ImportNetwork, Infer)
  1982. {
  1983. const std::string device = "MYRIAD";
  1984. skipIfDeviceNotAvailable(device);
  1985. initDLDTDataPath();
  1986. cv::gapi::ie::detail::ParamDesc params;
  1987. params.model_path = compileAgeGenderBlob(device);
  1988. params.device_id = device;
  1989. cv::Mat in_mat(320, 240, CV_8UC3);
  1990. cv::randu(in_mat, 0, 255);
  1991. cv::Mat gapi_age, gapi_gender;
  1992. // Load & run IE network
  1993. IE::Blob::Ptr ie_age, ie_gender;
  1994. {
  1995. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  1996. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  1997. auto infer_request = this_network.CreateInferRequest();
  1998. IE::PreProcessInfo info;
  1999. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2000. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_mat), info);
  2001. infer_request.Infer();
  2002. ie_age = infer_request.GetBlob("age_conv3");
  2003. ie_gender = infer_request.GetBlob("prob");
  2004. }
  2005. // Configure & run G-API
  2006. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2007. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2008. cv::GMat in;
  2009. cv::GMat age, gender;
  2010. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2011. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  2012. auto pp = cv::gapi::ie::Params<AgeGender> {
  2013. params.model_path, params.device_id
  2014. }.cfgOutputLayers({ "age_conv3", "prob" });
  2015. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  2016. cv::compile_args(cv::gapi::networks(pp)));
  2017. // Validate with IE itself (avoid DNN module dependency here)
  2018. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  2019. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  2020. }
  2021. TEST(ImportNetwork, InferNV12)
  2022. {
  2023. const std::string device = "MYRIAD";
  2024. skipIfDeviceNotAvailable(device);
  2025. initDLDTDataPath();
  2026. cv::gapi::ie::detail::ParamDesc params;
  2027. params.model_path= compileAgeGenderBlob(device);
  2028. params.device_id = device;
  2029. cv::Size sz{320, 240};
  2030. cv::Mat in_y_mat(sz, CV_8UC1);
  2031. cv::randu(in_y_mat, 0, 255);
  2032. cv::Mat in_uv_mat(sz / 2, CV_8UC2);
  2033. cv::randu(in_uv_mat, 0, 255);
  2034. cv::Mat gapi_age, gapi_gender;
  2035. // Load & run IE network
  2036. IE::Blob::Ptr ie_age, ie_gender;
  2037. {
  2038. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2039. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2040. auto infer_request = this_network.CreateInferRequest();
  2041. IE::PreProcessInfo info;
  2042. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2043. info.setColorFormat(IE::ColorFormat::NV12);
  2044. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat), info);
  2045. infer_request.Infer();
  2046. ie_age = infer_request.GetBlob("age_conv3");
  2047. ie_gender = infer_request.GetBlob("prob");
  2048. }
  2049. // Configure & run G-API
  2050. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2051. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2052. cv::GFrame in;
  2053. cv::GMat age, gender;
  2054. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2055. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  2056. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  2057. auto pp = cv::gapi::ie::Params<AgeGender> {
  2058. params.model_path, params.device_id
  2059. }.cfgOutputLayers({ "age_conv3", "prob" });
  2060. comp.apply(cv::gin(frame), cv::gout(gapi_age, gapi_gender),
  2061. cv::compile_args(cv::gapi::networks(pp)));
  2062. // Validate with IE itself (avoid DNN module dependency here)
  2063. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  2064. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  2065. }
  2066. TEST(ImportNetwork, InferROI)
  2067. {
  2068. const std::string device = "MYRIAD";
  2069. skipIfDeviceNotAvailable(device);
  2070. initDLDTDataPath();
  2071. cv::gapi::ie::detail::ParamDesc params;
  2072. params.model_path = compileAgeGenderBlob(device);
  2073. params.device_id = device;
  2074. cv::Mat in_mat(320, 240, CV_8UC3);
  2075. cv::randu(in_mat, 0, 255);
  2076. cv::Mat gapi_age, gapi_gender;
  2077. cv::Rect rect(cv::Point{64, 60}, cv::Size{96, 96});
  2078. // Load & run IE network
  2079. IE::Blob::Ptr ie_age, ie_gender;
  2080. {
  2081. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2082. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2083. auto infer_request = this_network.CreateInferRequest();
  2084. const auto ie_rc = IE::ROI {
  2085. 0u
  2086. , static_cast<std::size_t>(rect.x)
  2087. , static_cast<std::size_t>(rect.y)
  2088. , static_cast<std::size_t>(rect.width)
  2089. , static_cast<std::size_t>(rect.height)
  2090. };
  2091. IE::Blob::Ptr roi_blob = IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_mat), ie_rc);
  2092. IE::PreProcessInfo info;
  2093. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2094. infer_request.SetBlob("data", roi_blob, info);
  2095. infer_request.Infer();
  2096. ie_age = infer_request.GetBlob("age_conv3");
  2097. ie_gender = infer_request.GetBlob("prob");
  2098. }
  2099. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2100. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2101. cv::GMat in;
  2102. cv::GOpaque<cv::Rect> roi;
  2103. cv::GMat age, gender;
  2104. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  2105. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  2106. auto pp = cv::gapi::ie::Params<AgeGender> {
  2107. params.model_path, params.device_id
  2108. }.cfgOutputLayers({ "age_conv3", "prob" });
  2109. comp.apply(cv::gin(in_mat, rect), cv::gout(gapi_age, gapi_gender),
  2110. cv::compile_args(cv::gapi::networks(pp)));
  2111. // Validate with IE itself (avoid DNN module dependency here)
  2112. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  2113. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  2114. }
  2115. TEST(ImportNetwork, InferROINV12)
  2116. {
  2117. const std::string device = "MYRIAD";
  2118. skipIfDeviceNotAvailable(device);
  2119. initDLDTDataPath();
  2120. cv::gapi::ie::detail::ParamDesc params;
  2121. params.model_path = compileAgeGenderBlob(device);
  2122. params.device_id = device;
  2123. cv::Size sz{320, 240};
  2124. cv::Mat in_y_mat(sz, CV_8UC1);
  2125. cv::randu(in_y_mat, 0, 255);
  2126. cv::Mat in_uv_mat(sz / 2, CV_8UC2);
  2127. cv::randu(in_uv_mat, 0, 255);
  2128. cv::Rect rect(cv::Point{64, 60}, cv::Size{96, 96});
  2129. cv::Mat gapi_age, gapi_gender;
  2130. // Load & run IE network
  2131. IE::Blob::Ptr ie_age, ie_gender;
  2132. {
  2133. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2134. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2135. auto infer_request = this_network.CreateInferRequest();
  2136. const auto ie_rc = IE::ROI {
  2137. 0u
  2138. , static_cast<std::size_t>(rect.x)
  2139. , static_cast<std::size_t>(rect.y)
  2140. , static_cast<std::size_t>(rect.width)
  2141. , static_cast<std::size_t>(rect.height)
  2142. };
  2143. IE::Blob::Ptr roi_blob =
  2144. IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat), ie_rc);
  2145. IE::PreProcessInfo info;
  2146. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2147. info.setColorFormat(IE::ColorFormat::NV12);
  2148. infer_request.SetBlob("data", roi_blob, info);
  2149. infer_request.Infer();
  2150. ie_age = infer_request.GetBlob("age_conv3");
  2151. ie_gender = infer_request.GetBlob("prob");
  2152. }
  2153. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2154. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2155. cv::GFrame in;
  2156. cv::GOpaque<cv::Rect> roi;
  2157. cv::GMat age, gender;
  2158. std::tie(age, gender) = cv::gapi::infer<AgeGender>(roi, in);
  2159. cv::GComputation comp(cv::GIn(in, roi), cv::GOut(age, gender));
  2160. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  2161. auto pp = cv::gapi::ie::Params<AgeGender> {
  2162. params.model_path, params.device_id
  2163. }.cfgOutputLayers({ "age_conv3", "prob" });
  2164. comp.apply(cv::gin(frame, rect), cv::gout(gapi_age, gapi_gender),
  2165. cv::compile_args(cv::gapi::networks(pp)));
  2166. // Validate with IE itself (avoid DNN module dependency here)
  2167. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  2168. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  2169. }
  2170. TEST(ImportNetwork, InferList)
  2171. {
  2172. const std::string device = "MYRIAD";
  2173. skipIfDeviceNotAvailable(device);
  2174. initDLDTDataPath();
  2175. cv::gapi::ie::detail::ParamDesc params;
  2176. params.model_path = compileAgeGenderBlob(device);
  2177. params.device_id = device;
  2178. cv::Mat in_mat(320, 240, CV_8UC3);
  2179. cv::randu(in_mat, 0, 255);
  2180. std::vector<cv::Rect> roi_list = {
  2181. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  2182. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  2183. };
  2184. std::vector<cv::Mat> out_ie_ages, out_ie_genders, out_gapi_ages, out_gapi_genders;
  2185. // Load & run IE network
  2186. {
  2187. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2188. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2189. auto infer_request = this_network.CreateInferRequest();
  2190. for (auto &&rc : roi_list) {
  2191. const auto ie_rc = IE::ROI {
  2192. 0u
  2193. , static_cast<std::size_t>(rc.x)
  2194. , static_cast<std::size_t>(rc.y)
  2195. , static_cast<std::size_t>(rc.width)
  2196. , static_cast<std::size_t>(rc.height)
  2197. };
  2198. IE::Blob::Ptr roi_blob =
  2199. IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_mat), ie_rc);
  2200. IE::PreProcessInfo info;
  2201. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2202. infer_request.SetBlob("data", roi_blob, info);
  2203. infer_request.Infer();
  2204. using namespace cv::gapi::ie::util;
  2205. out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  2206. out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  2207. }
  2208. }
  2209. // Configure & run G-API
  2210. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2211. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2212. cv::GArray<cv::Rect> rr;
  2213. cv::GMat in;
  2214. cv::GArray<cv::GMat> age, gender;
  2215. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  2216. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  2217. auto pp = cv::gapi::ie::Params<AgeGender> {
  2218. params.model_path, params.device_id
  2219. }.cfgOutputLayers({ "age_conv3", "prob" });
  2220. comp.apply(cv::gin(in_mat, roi_list), cv::gout(out_gapi_ages, out_gapi_genders),
  2221. cv::compile_args(cv::gapi::networks(pp)));
  2222. // Validate with IE itself (avoid DNN module dependency here)
  2223. GAPI_Assert(!out_gapi_ages.empty());
  2224. ASSERT_EQ(out_gapi_genders.size(), out_gapi_ages.size());
  2225. ASSERT_EQ(out_gapi_ages.size(), out_ie_ages.size());
  2226. ASSERT_EQ(out_gapi_genders.size(), out_ie_genders.size());
  2227. const size_t size = out_gapi_ages.size();
  2228. for (size_t i = 0; i < size; ++i) {
  2229. normAssert(out_ie_ages [i], out_gapi_ages [i], "Test age output");
  2230. normAssert(out_ie_genders[i], out_gapi_genders[i], "Test gender output");
  2231. }
  2232. }
  2233. TEST(ImportNetwork, InferListNV12)
  2234. {
  2235. const std::string device = "MYRIAD";
  2236. skipIfDeviceNotAvailable(device);
  2237. initDLDTDataPath();
  2238. cv::gapi::ie::detail::ParamDesc params;
  2239. params.model_path = compileAgeGenderBlob(device);
  2240. params.device_id = device;
  2241. cv::Size sz{320, 240};
  2242. cv::Mat in_y_mat(sz, CV_8UC1);
  2243. cv::randu(in_y_mat, 0, 255);
  2244. cv::Mat in_uv_mat(sz / 2, CV_8UC2);
  2245. cv::randu(in_uv_mat, 0, 255);
  2246. std::vector<cv::Rect> roi_list = {
  2247. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  2248. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  2249. };
  2250. std::vector<cv::Mat> out_ie_ages, out_ie_genders, out_gapi_ages, out_gapi_genders;
  2251. // Load & run IE network
  2252. {
  2253. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2254. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2255. auto infer_request = this_network.CreateInferRequest();
  2256. for (auto &&rc : roi_list) {
  2257. const auto ie_rc = IE::ROI {
  2258. 0u
  2259. , static_cast<std::size_t>(rc.x)
  2260. , static_cast<std::size_t>(rc.y)
  2261. , static_cast<std::size_t>(rc.width)
  2262. , static_cast<std::size_t>(rc.height)
  2263. };
  2264. IE::Blob::Ptr roi_blob =
  2265. IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat), ie_rc);
  2266. IE::PreProcessInfo info;
  2267. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2268. info.setColorFormat(IE::ColorFormat::NV12);
  2269. infer_request.SetBlob("data", roi_blob, info);
  2270. infer_request.Infer();
  2271. using namespace cv::gapi::ie::util;
  2272. out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  2273. out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  2274. }
  2275. }
  2276. // Configure & run G-API
  2277. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2278. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2279. cv::GArray<cv::Rect> rr;
  2280. cv::GFrame in;
  2281. cv::GArray<cv::GMat> age, gender;
  2282. std::tie(age, gender) = cv::gapi::infer<AgeGender>(rr, in);
  2283. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  2284. auto pp = cv::gapi::ie::Params<AgeGender> {
  2285. params.model_path, params.device_id
  2286. }.cfgOutputLayers({ "age_conv3", "prob" });
  2287. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  2288. comp.apply(cv::gin(frame, roi_list), cv::gout(out_gapi_ages, out_gapi_genders),
  2289. cv::compile_args(cv::gapi::networks(pp)));
  2290. // Validate with IE itself (avoid DNN module dependency here)
  2291. GAPI_Assert(!out_gapi_ages.empty());
  2292. ASSERT_EQ(out_gapi_genders.size(), out_gapi_ages.size());
  2293. ASSERT_EQ(out_gapi_ages.size(), out_ie_ages.size());
  2294. ASSERT_EQ(out_gapi_genders.size(), out_ie_genders.size());
  2295. const size_t size = out_gapi_ages.size();
  2296. for (size_t i = 0; i < size; ++i) {
  2297. normAssert(out_ie_ages [i], out_gapi_ages [i], "Test age output");
  2298. normAssert(out_ie_genders[i], out_gapi_genders[i], "Test gender output");
  2299. }
  2300. }
  2301. TEST(ImportNetwork, InferList2)
  2302. {
  2303. const std::string device = "MYRIAD";
  2304. skipIfDeviceNotAvailable(device);
  2305. initDLDTDataPath();
  2306. cv::gapi::ie::detail::ParamDesc params;
  2307. params.model_path = compileAgeGenderBlob(device);
  2308. params.device_id = device;
  2309. cv::Mat in_mat(320, 240, CV_8UC3);
  2310. cv::randu(in_mat, 0, 255);
  2311. std::vector<cv::Rect> roi_list = {
  2312. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  2313. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  2314. };
  2315. std::vector<cv::Mat> out_ie_ages, out_ie_genders, out_gapi_ages, out_gapi_genders;
  2316. // Load & run IE network
  2317. {
  2318. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2319. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2320. auto infer_request = this_network.CreateInferRequest();
  2321. for (auto &&rc : roi_list) {
  2322. const auto ie_rc = IE::ROI {
  2323. 0u
  2324. , static_cast<std::size_t>(rc.x)
  2325. , static_cast<std::size_t>(rc.y)
  2326. , static_cast<std::size_t>(rc.width)
  2327. , static_cast<std::size_t>(rc.height)
  2328. };
  2329. IE::Blob::Ptr roi_blob =
  2330. IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_mat), ie_rc);
  2331. IE::PreProcessInfo info;
  2332. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2333. infer_request.SetBlob("data", roi_blob, info);
  2334. infer_request.Infer();
  2335. using namespace cv::gapi::ie::util;
  2336. out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  2337. out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  2338. }
  2339. }
  2340. // Configure & run G-API
  2341. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2342. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2343. cv::GArray<cv::Rect> rr;
  2344. cv::GMat in;
  2345. cv::GArray<cv::GMat> age, gender;
  2346. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  2347. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  2348. auto pp = cv::gapi::ie::Params<AgeGender> {
  2349. params.model_path, params.device_id
  2350. }.cfgOutputLayers({ "age_conv3", "prob" });
  2351. comp.apply(cv::gin(in_mat, roi_list), cv::gout(out_gapi_ages, out_gapi_genders),
  2352. cv::compile_args(cv::gapi::networks(pp)));
  2353. // Validate with IE itself (avoid DNN module dependency here)
  2354. GAPI_Assert(!out_gapi_ages.empty());
  2355. ASSERT_EQ(out_gapi_genders.size(), out_gapi_ages.size());
  2356. ASSERT_EQ(out_gapi_ages.size(), out_ie_ages.size());
  2357. ASSERT_EQ(out_gapi_genders.size(), out_ie_genders.size());
  2358. const size_t size = out_gapi_ages.size();
  2359. for (size_t i = 0; i < size; ++i) {
  2360. normAssert(out_ie_ages [i], out_gapi_ages [i], "Test age output");
  2361. normAssert(out_ie_genders[i], out_gapi_genders[i], "Test gender output");
  2362. }
  2363. }
  2364. TEST(ImportNetwork, InferList2NV12)
  2365. {
  2366. const std::string device = "MYRIAD";
  2367. skipIfDeviceNotAvailable(device);
  2368. initDLDTDataPath();
  2369. cv::gapi::ie::detail::ParamDesc params;
  2370. params.model_path = compileAgeGenderBlob(device);
  2371. params.device_id = device;
  2372. cv::Size sz{320, 240};
  2373. cv::Mat in_y_mat(sz, CV_8UC1);
  2374. cv::randu(in_y_mat, 0, 255);
  2375. cv::Mat in_uv_mat(sz / 2, CV_8UC2);
  2376. cv::randu(in_uv_mat, 0, 255);
  2377. std::vector<cv::Rect> roi_list = {
  2378. cv::Rect(cv::Point{64, 60}, cv::Size{ 96, 96}),
  2379. cv::Rect(cv::Point{50, 32}, cv::Size{128, 160}),
  2380. };
  2381. std::vector<cv::Mat> out_ie_ages, out_ie_genders, out_gapi_ages, out_gapi_genders;
  2382. // Load & run IE network
  2383. {
  2384. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2385. auto this_network = cv::gimpl::ie::wrap::importNetwork(plugin, params);
  2386. auto infer_request = this_network.CreateInferRequest();
  2387. for (auto &&rc : roi_list) {
  2388. const auto ie_rc = IE::ROI {
  2389. 0u
  2390. , static_cast<std::size_t>(rc.x)
  2391. , static_cast<std::size_t>(rc.y)
  2392. , static_cast<std::size_t>(rc.width)
  2393. , static_cast<std::size_t>(rc.height)
  2394. };
  2395. IE::Blob::Ptr roi_blob =
  2396. IE::make_shared_blob(cv::gapi::ie::util::to_ie(in_y_mat, in_uv_mat), ie_rc);
  2397. IE::PreProcessInfo info;
  2398. info.setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2399. info.setColorFormat(IE::ColorFormat::NV12);
  2400. infer_request.SetBlob("data", roi_blob, info);
  2401. infer_request.Infer();
  2402. using namespace cv::gapi::ie::util;
  2403. out_ie_ages.push_back(to_ocv(infer_request.GetBlob("age_conv3")).clone());
  2404. out_ie_genders.push_back(to_ocv(infer_request.GetBlob("prob")).clone());
  2405. }
  2406. }
  2407. // Configure & run G-API
  2408. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2409. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2410. cv::GArray<cv::Rect> rr;
  2411. cv::GFrame in;
  2412. cv::GArray<cv::GMat> age, gender;
  2413. std::tie(age, gender) = cv::gapi::infer2<AgeGender>(in, rr);
  2414. cv::GComputation comp(cv::GIn(in, rr), cv::GOut(age, gender));
  2415. auto pp = cv::gapi::ie::Params<AgeGender> {
  2416. params.model_path, params.device_id
  2417. }.cfgOutputLayers({ "age_conv3", "prob" });
  2418. auto frame = MediaFrame::Create<TestMediaNV12>(in_y_mat, in_uv_mat);
  2419. comp.apply(cv::gin(frame, roi_list), cv::gout(out_gapi_ages, out_gapi_genders),
  2420. cv::compile_args(cv::gapi::networks(pp)));
  2421. // Validate with IE itself (avoid DNN module dependency here)
  2422. GAPI_Assert(!out_gapi_ages.empty());
  2423. ASSERT_EQ(out_gapi_genders.size(), out_gapi_ages.size());
  2424. ASSERT_EQ(out_gapi_ages.size(), out_ie_ages.size());
  2425. ASSERT_EQ(out_gapi_genders.size(), out_ie_genders.size());
  2426. const size_t size = out_gapi_ages.size();
  2427. for (size_t i = 0; i < size; ++i) {
  2428. normAssert(out_ie_ages [i], out_gapi_ages [i], "Test age output");
  2429. normAssert(out_ie_genders[i], out_gapi_genders[i], "Test gender output");
  2430. }
  2431. }
  2432. #endif
  2433. TEST(TestAgeGender, ThrowBlobAndInputPrecisionMismatch)
  2434. {
  2435. const std::string device = "MYRIAD";
  2436. skipIfDeviceNotAvailable(device);
  2437. initDLDTDataPath();
  2438. cv::gapi::ie::detail::ParamDesc params;
  2439. // NB: Precision for inputs is U8.
  2440. params.model_path = compileAgeGenderBlob(device);
  2441. params.device_id = device;
  2442. // Configure & run G-API
  2443. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2444. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2445. cv::GMat in, age, gender;
  2446. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2447. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  2448. auto pp = cv::gapi::ie::Params<AgeGender> {
  2449. params.model_path, params.device_id
  2450. }.cfgOutputLayers({ "age_conv3", "prob" });
  2451. cv::Mat in_mat(320, 240, CV_32FC3);
  2452. cv::randu(in_mat, 0, 1);
  2453. cv::Mat gapi_age, gapi_gender;
  2454. // NB: Blob precision is U8, but user pass FP32 data, so exception will be thrown.
  2455. // Now exception comes directly from IE, but since G-API has information
  2456. // about data precision at the compile stage, consider the possibility of
  2457. // throwing exception from there.
  2458. EXPECT_ANY_THROW(comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  2459. cv::compile_args(cv::gapi::networks(pp))));
  2460. }
  2461. #ifdef HAVE_NGRAPH
  2462. TEST(Infer, ModelWith2DInputs)
  2463. {
  2464. const std::string model_name = "ModelWith2DInputs";
  2465. const std::string model_path = model_name + ".xml";
  2466. const std::string weights_path = model_name + ".bin";
  2467. const std::string device_id = "CPU";
  2468. const int W = 10;
  2469. const int H = 5;
  2470. // NB: Define model with 2D inputs.
  2471. auto in1 = std::make_shared<ngraph::op::Parameter>(
  2472. ngraph::element::Type_t::u8,
  2473. ngraph::Shape(std::vector<size_t>{(size_t)H, (size_t)W})
  2474. );
  2475. auto in2 = std::make_shared<ngraph::op::Parameter>(
  2476. ngraph::element::Type_t::u8,
  2477. ngraph::Shape(std::vector<size_t>{(size_t)H, (size_t)W})
  2478. );
  2479. auto result = std::make_shared<ngraph::op::v1::Add>(in1, in2);
  2480. auto func = std::make_shared<ngraph::Function>(
  2481. ngraph::OutputVector{result},
  2482. ngraph::ParameterVector{in1, in2}
  2483. );
  2484. cv::Mat in_mat1(std::vector<int>{H, W}, CV_8U),
  2485. in_mat2(std::vector<int>{H, W}, CV_8U),
  2486. gapi_mat, ref_mat;
  2487. cv::randu(in_mat1, 0, 100);
  2488. cv::randu(in_mat2, 0, 100);
  2489. cv::add(in_mat1, in_mat2, ref_mat, cv::noArray(), CV_32F);
  2490. // Compile xml file
  2491. IE::CNNNetwork(func).serialize(model_path);
  2492. // Configure & run G-API
  2493. cv::GMat g_in1, g_in2;
  2494. cv::GInferInputs inputs;
  2495. inputs[in1->get_name()] = g_in1;
  2496. inputs[in2->get_name()] = g_in2;
  2497. auto outputs = cv::gapi::infer<cv::gapi::Generic>(model_name, inputs);
  2498. auto out = outputs.at(result->get_name());
  2499. cv::GComputation comp(cv::GIn(g_in1, g_in2), cv::GOut(out));
  2500. auto pp = cv::gapi::ie::Params<cv::gapi::Generic>(model_name,
  2501. model_path,
  2502. weights_path,
  2503. device_id);
  2504. comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(gapi_mat),
  2505. cv::compile_args(cv::gapi::networks(pp)));
  2506. normAssert(ref_mat, gapi_mat, "Test model output");
  2507. }
  2508. #endif // HAVE_NGRAPH
  2509. TEST(TestAgeGender, ThrowBlobAndInputPrecisionMismatchStreaming)
  2510. {
  2511. const std::string device = "MYRIAD";
  2512. skipIfDeviceNotAvailable(device);
  2513. initDLDTDataPath();
  2514. cv::gapi::ie::detail::ParamDesc params;
  2515. // NB: Precision for inputs is U8.
  2516. params.model_path = compileAgeGenderBlob(device);
  2517. params.device_id = device;
  2518. // Configure & run G-API
  2519. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2520. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2521. auto pp = cv::gapi::ie::Params<AgeGender> {
  2522. params.model_path, params.device_id
  2523. }.cfgOutputLayers({ "age_conv3", "prob" });
  2524. cv::GMat in, age, gender;
  2525. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2526. auto pipeline = cv::GComputation(cv::GIn(in), cv::GOut(age, gender))
  2527. .compileStreaming(cv::compile_args(cv::gapi::networks(pp)));
  2528. cv::Mat in_mat(320, 240, CV_32FC3);
  2529. cv::randu(in_mat, 0, 1);
  2530. cv::Mat gapi_age, gapi_gender;
  2531. pipeline.setSource(cv::gin(in_mat));
  2532. pipeline.start();
  2533. // NB: Blob precision is U8, but user pass FP32 data, so exception will be thrown.
  2534. // Now exception comes directly from IE, but since G-API has information
  2535. // about data precision at the compile stage, consider the possibility of
  2536. // throwing exception from there.
  2537. for (int i = 0; i < 10; ++i) {
  2538. EXPECT_ANY_THROW(pipeline.pull(cv::gout(gapi_age, gapi_gender)));
  2539. }
  2540. }
  2541. struct AgeGenderInferTest: public ::testing::Test {
  2542. cv::Mat m_in_mat;
  2543. cv::Mat m_gapi_age;
  2544. cv::Mat m_gapi_gender;
  2545. cv::gimpl::ie::wrap::Plugin m_plugin;
  2546. IE::CNNNetwork m_net;
  2547. cv::gapi::ie::detail::ParamDesc m_params;
  2548. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2549. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2550. void SetUp() {
  2551. initDLDTDataPath();
  2552. m_params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  2553. m_params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  2554. m_params.device_id = "CPU";
  2555. m_plugin = cv::gimpl::ie::wrap::getPlugin(m_params);
  2556. m_net = cv::gimpl::ie::wrap::readNetwork(m_params);
  2557. setNetParameters(m_net);
  2558. m_in_mat = cv::Mat(cv::Size(320, 240), CV_8UC3);
  2559. cv::randu(m_in_mat, 0, 255);
  2560. }
  2561. cv::GComputation buildGraph() {
  2562. cv::GMat in, age, gender;
  2563. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2564. return cv::GComputation(cv::GIn(in), cv::GOut(age, gender));
  2565. }
  2566. void validate() {
  2567. IE::Blob::Ptr ie_age, ie_gender;
  2568. {
  2569. auto this_network = cv::gimpl::ie::wrap::loadNetwork(m_plugin, m_net, m_params);
  2570. auto infer_request = this_network.CreateInferRequest();
  2571. infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(m_in_mat));
  2572. infer_request.Infer();
  2573. ie_age = infer_request.GetBlob("age_conv3");
  2574. ie_gender = infer_request.GetBlob("prob");
  2575. }
  2576. // Validate with IE itself (avoid DNN module dependency here)
  2577. normAssert(cv::gapi::ie::util::to_ocv(ie_age), m_gapi_age, "Test age output" );
  2578. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), m_gapi_gender, "Test gender output");
  2579. }
  2580. };
  2581. TEST_F(AgeGenderInferTest, SyncExecution) {
  2582. auto pp = cv::gapi::ie::Params<AgeGender> {
  2583. m_params.model_path, m_params.weights_path, m_params.device_id
  2584. }.cfgOutputLayers({ "age_conv3", "prob" })
  2585. .cfgInferMode(cv::gapi::ie::InferMode::Sync);
  2586. buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
  2587. cv::compile_args(cv::gapi::networks(pp)));
  2588. validate();
  2589. }
  2590. TEST_F(AgeGenderInferTest, ThrowSyncWithNireqNotEqualToOne) {
  2591. auto pp = cv::gapi::ie::Params<AgeGender> {
  2592. m_params.model_path, m_params.weights_path, m_params.device_id
  2593. }.cfgOutputLayers({ "age_conv3", "prob" })
  2594. .cfgInferMode(cv::gapi::ie::InferMode::Sync)
  2595. .cfgNumRequests(4u);
  2596. EXPECT_ANY_THROW(buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
  2597. cv::compile_args(cv::gapi::networks(pp))));
  2598. }
  2599. TEST_F(AgeGenderInferTest, ChangeOutputPrecision) {
  2600. auto pp = cv::gapi::ie::Params<AgeGender> {
  2601. m_params.model_path, m_params.weights_path, m_params.device_id
  2602. }.cfgOutputLayers({ "age_conv3", "prob" })
  2603. .cfgOutputPrecision(CV_8U);
  2604. for (auto it : m_net.getOutputsInfo()) {
  2605. it.second->setPrecision(IE::Precision::U8);
  2606. }
  2607. buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
  2608. cv::compile_args(cv::gapi::networks(pp)));
  2609. validate();
  2610. }
  2611. TEST_F(AgeGenderInferTest, ChangeSpecificOutputPrecison) {
  2612. auto pp = cv::gapi::ie::Params<AgeGender> {
  2613. m_params.model_path, m_params.weights_path, m_params.device_id
  2614. }.cfgOutputLayers({ "age_conv3", "prob" })
  2615. .cfgOutputPrecision({{"prob", CV_8U}});
  2616. m_net.getOutputsInfo().at("prob")->setPrecision(IE::Precision::U8);
  2617. buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
  2618. cv::compile_args(cv::gapi::networks(pp)));
  2619. validate();
  2620. }
  2621. TEST_F(AgeGenderInferTest, ThrowIfSetLayoutForImage) {
  2622. auto pp = cv::gapi::ie::Params<AgeGender> {
  2623. m_params.model_path, m_params.weights_path, m_params.device_id
  2624. }.cfgOutputLayers({ "age_conv3", "prob" })
  2625. .cfgOutputPrecision({{"prob", CV_8U}})
  2626. .cfgInputLayout("NHWC");
  2627. EXPECT_ANY_THROW(buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
  2628. cv::compile_args(cv::gapi::networks(pp))));
  2629. }
  2630. TEST(TestAgeGenderIE, InferTensorWithPreproc) {
  2631. initDLDTDataPath();
  2632. cv::gapi::ie::detail::ParamDesc params;
  2633. params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml", false);
  2634. params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin", false);
  2635. params.device_id = "CPU";
  2636. // Load IE network, initialize input data using that.
  2637. cv::Mat in_mat({1, 240, 320, 3}, CV_8U);
  2638. cv::randu(in_mat, 0, 255);
  2639. cv::Mat gapi_age, gapi_gender;
  2640. IE::Blob::Ptr ie_age, ie_gender;
  2641. {
  2642. auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
  2643. auto net = cv::gimpl::ie::wrap::readNetwork(params);
  2644. auto ii = net.getInputsInfo().at("data");
  2645. ii->setPrecision(IE::Precision::U8);
  2646. ii->getPreProcess().setResizeAlgorithm(IE::RESIZE_BILINEAR);
  2647. ii->setLayout(IE::Layout::NHWC);
  2648. auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
  2649. auto infer_request = this_network.CreateInferRequest();
  2650. IE::TensorDesc desc{IE::Precision::U8, {1, 3, 240, 320}, IE::Layout::NHWC};
  2651. auto blob = IE::make_shared_blob<uint8_t>(desc, const_cast<uint8_t*>(in_mat.ptr<uint8_t>()));
  2652. infer_request.SetBlob("data", blob);
  2653. infer_request.Infer();
  2654. ie_age = infer_request.GetBlob("age_conv3");
  2655. ie_gender = infer_request.GetBlob("prob");
  2656. }
  2657. // Configure & run G-API
  2658. using AGInfo = std::tuple<cv::GMat, cv::GMat>;
  2659. G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
  2660. cv::GMat in;
  2661. cv::GMat age, gender;
  2662. std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
  2663. cv::GComputation comp(cv::GIn(in), cv::GOut(age, gender));
  2664. auto pp = cv::gapi::ie::Params<AgeGender> {
  2665. params.model_path, params.weights_path, params.device_id
  2666. }.cfgOutputLayers({ "age_conv3", "prob" })
  2667. .cfgResize(cv::INTER_LINEAR)
  2668. .cfgInputLayout("NHWC");
  2669. comp.apply(cv::gin(in_mat), cv::gout(gapi_age, gapi_gender),
  2670. cv::compile_args(cv::gapi::networks(pp)));
  2671. // Validate with IE itself (avoid DNN module dependency here)
  2672. normAssert(cv::gapi::ie::util::to_ocv(ie_age), gapi_age, "Test age output" );
  2673. normAssert(cv::gapi::ie::util::to_ocv(ie_gender), gapi_gender, "Test gender output");
  2674. }
  2675. } // namespace opencv_test
  2676. #endif // HAVE_INF_ENGINE