mirror of https://github.com/opencv/opencv.git
Merge pull request #18491 from TolyaTalamanov:at/wrap-inference
[G-API] Wrap cv::gapi::infer<Generic> into python * Introduce generic infer * Move Generic to infer.hpp * Removew num_outs * Fix windows warnings * Fix comments to review * Fix doxygen * Add comment * Fix comments to review * Wrap inference to python * Add default ctor to Params * Add test * Fix clang build * Implement GInferInputs/GInferOutputs as Pimpl * Add checkIEtarget to infer test * Fix path * Supress warning * Use getAvailableDevices insted of checkIETarget * Move PyParams to bindings_ie * Add namespace * Update CMakeLists.txtpull/18662/head
parent
36598677cf
commit
93c3775927
9 changed files with 223 additions and 27 deletions
@ -0,0 +1,56 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
//
|
||||
// Copyright (C) 2020 Intel Corporation
|
||||
|
||||
#ifndef OPENCV_GAPI_INFER_BINDINGS_IE_HPP |
||||
#define OPENCV_GAPI_INFER_BINDINGS_IE_HPP |
||||
|
||||
#include <opencv2/gapi/util/any.hpp> |
||||
#include "opencv2/gapi/own/exports.hpp" // GAPI_EXPORTS |
||||
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage |
||||
#include <opencv2/gapi/infer/ie.hpp> // Params |
||||
|
||||
#include <string> |
||||
|
||||
namespace cv { |
||||
namespace gapi { |
||||
namespace ie { |
||||
|
||||
// NB: Used by python wrapper
|
||||
// This class can be marked as SIMPLE, because it's implemented as pimpl
|
||||
class GAPI_EXPORTS_W_SIMPLE PyParams { |
||||
public: |
||||
PyParams() = default; |
||||
|
||||
PyParams(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &weights, |
||||
const std::string &device); |
||||
|
||||
PyParams(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &device); |
||||
|
||||
GBackend backend() const; |
||||
std::string tag() const; |
||||
cv::util::any params() const; |
||||
|
||||
private: |
||||
std::shared_ptr<Params<cv::gapi::Generic>> m_priv; |
||||
}; |
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &weights, |
||||
const std::string &device); |
||||
|
||||
GAPI_EXPORTS_W PyParams params(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &device); |
||||
} // namespace ie
|
||||
} // namespace gapi
|
||||
} // namespace cv
|
||||
|
||||
#endif // OPENCV_GAPI_INFER_BINDINGS_IE_HPP
|
@ -0,0 +1,62 @@ |
||||
#!/usr/bin/env python |
||||
|
||||
import numpy as np |
||||
import cv2 as cv |
||||
import os |
||||
|
||||
from tests_common import NewOpenCVTests |
||||
|
||||
|
||||
class test_gapi_infer(NewOpenCVTests): |
||||
|
||||
def test_getAvailableTargets(self): |
||||
targets = cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_OPENCV) |
||||
self.assertTrue(cv.dnn.DNN_TARGET_CPU in targets) |
||||
|
||||
|
||||
def test_age_gender_infer(self): |
||||
|
||||
# NB: Check IE |
||||
if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE): |
||||
return |
||||
|
||||
root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013' |
||||
model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) |
||||
weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) |
||||
img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) |
||||
device_id = 'CPU' |
||||
img = cv.resize(cv.imread(img_path), (62,62)) |
||||
|
||||
# OpenCV DNN |
||||
net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path) |
||||
net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE) |
||||
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) |
||||
|
||||
blob = cv.dnn.blobFromImage(img) |
||||
|
||||
net.setInput(blob) |
||||
dnn_age, dnn_gender = net.forward(net.getUnconnectedOutLayersNames()) |
||||
|
||||
# OpenCV G-API |
||||
g_in = cv.GMat() |
||||
inputs = cv.GInferInputs() |
||||
inputs.setInput('data', g_in) |
||||
|
||||
outputs = cv.gapi.infer("net", inputs) |
||||
age_g = outputs.at("age_conv3") |
||||
gender_g = outputs.at("prob") |
||||
|
||||
comp = cv.GComputation(cv.GIn(g_in), cv.GOut(age_g, gender_g)) |
||||
pp = cv.gapi.ie.params("net", model_path, weights_path, device_id) |
||||
|
||||
nets = cv.gapi.networks(pp) |
||||
args = cv.compile_args(nets) |
||||
gapi_age, gapi_gender = comp.apply(cv.gin(img), args=cv.compile_args(cv.gapi.networks(pp))) |
||||
|
||||
# Check |
||||
self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF)) |
||||
self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF)) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
NewOpenCVTests.bootstrap() |
@ -0,0 +1,39 @@ |
||||
#include <opencv2/gapi/infer/bindings_ie.hpp> |
||||
|
||||
cv::gapi::ie::PyParams::PyParams(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &weights, |
||||
const std::string &device) |
||||
: m_priv(std::make_shared<Params<cv::gapi::Generic>>(tag, model, weights, device)) { |
||||
} |
||||
|
||||
cv::gapi::ie::PyParams::PyParams(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &device) |
||||
: m_priv(std::make_shared<Params<cv::gapi::Generic>>(tag, model, device)) { |
||||
} |
||||
|
||||
cv::gapi::GBackend cv::gapi::ie::PyParams::backend() const { |
||||
return m_priv->backend(); |
||||
} |
||||
|
||||
std::string cv::gapi::ie::PyParams::tag() const { |
||||
return m_priv->tag(); |
||||
} |
||||
|
||||
cv::util::any cv::gapi::ie::PyParams::params() const { |
||||
return m_priv->params(); |
||||
} |
||||
|
||||
cv::gapi::ie::PyParams cv::gapi::ie::params(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &weights, |
||||
const std::string &device) { |
||||
return {tag, model, weights, device}; |
||||
} |
||||
|
||||
cv::gapi::ie::PyParams cv::gapi::ie::params(const std::string &tag, |
||||
const std::string &model, |
||||
const std::string &device) { |
||||
return {tag, model, device}; |
||||
} |
Loading…
Reference in new issue