Add tests

This commit is contained in:
TolyaTalamanov 2022-10-03 10:58:21 +00:00
parent cf5db9b94f
commit aafb7567c1
3 changed files with 86 additions and 14 deletions

View File

@ -52,6 +52,8 @@ enum class TraitAs: int
using IEConfig = std::map<std::string, std::string>;
enum InferMode {Sync, Async};
namespace detail {
struct ParamDesc {
std::string model_path;
@ -89,7 +91,6 @@ struct ParamDesc {
cv::optional<cv::gapi::wip::onevpl::Device> vpl_preproc_device;
cv::optional<cv::gapi::wip::onevpl::Context> vpl_preproc_ctx;
enum InferMode {Sync, Async};
InferMode mode;
};
} // namespace detail
@ -136,7 +137,7 @@ public:
, {}
, {}
, {}
, detail::ParamDesc::InferMode::Async} {
, InferMode::Async} {
};
/** @overload
@ -161,7 +162,7 @@ public:
, {}
, {}
, {}
, detail::ParamDesc::InferMode::Async} {
, InferMode::Async} {
};
/** @brief Specifies sequence of network input layers names for inference.
@ -367,7 +368,7 @@ public:
@param mode Inference mode which will be used.
@return reference to this parameter structure.
*/
Params<Net>& cfgInferMode(detail::ParamDesc::InferMode mode) {
Params<Net>& cfgInferMode(InferMode mode) {
desc.mode = mode;
return *this;
}
@ -407,7 +408,7 @@ public:
: desc{ model, weights, device, {}, {}, {}, 0u, 0u,
detail::ParamDesc::Kind::Load, true, {}, {}, {}, 1u,
{}, {}, {}, {},
detail::ParamDesc::InferMode::Async },
InferMode::Async },
m_tag(tag) {
};
@ -426,7 +427,7 @@ public:
: desc{ model, {}, device, {}, {}, {}, 0u, 0u,
detail::ParamDesc::Kind::Import, true, {}, {}, {}, 1u,
{}, {}, {}, {},
detail::ParamDesc::InferMode::Async },
InferMode::Async },
m_tag(tag) {
};
@ -500,7 +501,7 @@ public:
}
/** @see ie::Params::cfgInferAPI */
Params& cfgInferMode(detail::ParamDesc::InferMode mode) {
Params& cfgInferMode(InferMode mode) {
desc.mode = mode;
return *this;
}

View File

@ -376,10 +376,10 @@ struct IEUnit {
GAPI_LOG_INFO(nullptr, "VPP preproc created successfuly");
}
if (params.mode == cv::gapi::ie::detail::ParamDesc::InferMode::Sync &&
if (params.mode == cv::gapi::ie::InferMode::Sync &&
params.nireq != 1u) {
throw std::logic_error(
"Failed: ParamDesc::InferMode::Sync works only with nireq equal to 1.");
"Failed: cv::gapi::ie::InferMode::Sync works only with nireq equal to 1.");
}
}
@ -917,7 +917,7 @@ void AsyncInferExecutor::callback(IInferExecutor::Task task,
class cv::gimpl::ie::RequestPool {
public:
explicit RequestPool(cv::gapi::ie::detail::ParamDesc::InferMode mode,
explicit RequestPool(cv::gapi::ie::InferMode mode,
std::vector<InferenceEngine::InferRequest>&& requests);
IInferExecutor::Ptr getIdleRequest();
@ -936,21 +936,21 @@ void cv::gimpl::ie::RequestPool::release(const size_t id) {
}
// RequestPool implementation //////////////////////////////////////////////
cv::gimpl::ie::RequestPool::RequestPool(cv::gapi::ie::detail::ParamDesc::InferMode mode,
cv::gimpl::ie::RequestPool::RequestPool(cv::gapi::ie::InferMode mode,
std::vector<InferenceEngine::InferRequest>&& requests) {
for (size_t i = 0; i < requests.size(); ++i) {
IInferExecutor::Ptr iexec = nullptr;
switch (mode) {
case cv::gapi::ie::detail::ParamDesc::InferMode::Async:
case cv::gapi::ie::InferMode::Async:
iexec = std::make_shared<AsyncInferExecutor>(std::move(requests[i]),
std::bind(&RequestPool::release, this, i));
break;
case cv::gapi::ie::detail::ParamDesc::InferMode::Sync:
case cv::gapi::ie::InferMode::Sync:
iexec = std::make_shared<SyncInferExecutor>(std::move(requests[i]),
std::bind(&RequestPool::release, this, i));
break;
default:
GAPI_Assert(false && "Unsupported ParamDesc::InferMode");
GAPI_Assert(false && "Unsupported cv::gapi::ie::InferMode");
}
m_requests.emplace_back(std::move(iexec));
}

View File

@ -2956,6 +2956,77 @@ TEST(TestAgeGender, ThrowBlobAndInputPrecisionMismatchStreaming)
}
}
struct AgeGenderInferTest: public ::testing::Test {
cv::Mat m_in_mat;
cv::Mat m_gapi_age;
cv::Mat m_gapi_gender;
cv::gimpl::ie::wrap::Plugin m_plugin;
IE::CNNNetwork m_net;
cv::gapi::ie::detail::ParamDesc m_params;
using AGInfo = std::tuple<cv::GMat, cv::GMat>;
G_API_NET(AgeGender, <AGInfo(cv::GMat)>, "test-age-gender");
void SetUp() {
initDLDTDataPath();
m_params.model_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.xml");
m_params.weights_path = findDataFile(SUBDIR + "age-gender-recognition-retail-0013.bin");
m_params.device_id = "CPU";
m_plugin = cv::gimpl::ie::wrap::getPlugin(m_params);
m_net = cv::gimpl::ie::wrap::readNetwork(m_params);
setNetParameters(m_net);
m_in_mat = cv::Mat(cv::Size(320, 240), CV_8UC3);
cv::randu(m_in_mat, 0, 255);
}
cv::GComputation buildGraph() {
cv::GMat in, age, gender;
std::tie(age, gender) = cv::gapi::infer<AgeGender>(in);
return cv::GComputation(cv::GIn(in), cv::GOut(age, gender));
}
void validate() {
IE::Blob::Ptr ie_age, ie_gender;
{
auto this_network = cv::gimpl::ie::wrap::loadNetwork(m_plugin, m_net, m_params);
auto infer_request = this_network.CreateInferRequest();
infer_request.SetBlob("data", cv::gapi::ie::util::to_ie(m_in_mat));
infer_request.Infer();
ie_age = infer_request.GetBlob("age_conv3");
ie_gender = infer_request.GetBlob("prob");
}
// Validate with IE itself (avoid DNN module dependency here)
normAssert(cv::gapi::ie::util::to_ocv(ie_age), m_gapi_age, "Test age output" );
normAssert(cv::gapi::ie::util::to_ocv(ie_gender), m_gapi_gender, "Test gender output");
}
};
TEST_F(AgeGenderInferTest, SyncExecution) {
auto pp = cv::gapi::ie::Params<AgeGender> {
m_params.model_path, m_params.weights_path, m_params.device_id
}.cfgOutputLayers({ "age_conv3", "prob" })
.cfgInferMode(cv::gapi::ie::InferMode::Sync);
buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
cv::compile_args(cv::gapi::networks(pp)));
validate();
}
TEST_F(AgeGenderInferTest, ThrowSyncWithNireqNotEqualToOne) {
auto pp = cv::gapi::ie::Params<AgeGender> {
m_params.model_path, m_params.weights_path, m_params.device_id
}.cfgOutputLayers({ "age_conv3", "prob" })
.cfgInferMode(cv::gapi::ie::InferMode::Sync)
.cfgNumRequests(4u);
EXPECT_ANY_THROW(buildGraph().apply(cv::gin(m_in_mat), cv::gout(m_gapi_age, m_gapi_gender),
cv::compile_args(cv::gapi::networks(pp))));
}
} // namespace opencv_test
#endif // HAVE_INF_ENGINE