1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge pull request #18419 from TolyaTalamanov:at/generic-inference

[G-API] Introduce generic version for cv::gapi::infer

* 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

* standoalone ifdef in ginfer.cpp

* Fix test
This commit is contained in:
Anatoliy Talamanov
2020-10-09 01:12:25 +03:00
committed by GitHub
parent 6a51e3b39a
commit 76be3529f4
11 changed files with 237 additions and 13 deletions
+7 -2
View File
@@ -56,11 +56,16 @@ public:
Priv& priv();
const Priv& priv() const;
protected:
std::shared_ptr<Priv> m_priv;
// GKernel and params can be modified, it's needed for infer<Generic>,
// because information about output shapes doesn't exist in compile time
GKernel& kernel();
cv::util::any& params();
void setArgs(std::vector<GArg> &&args);
protected:
std::shared_ptr<Priv> m_priv;
// Public versions return a typed array or opaque, those are implementation details
detail::GArrayU yieldArray(int output = 0);
detail::GOpaqueU yieldOpaque(int output = 0);
@@ -121,6 +121,45 @@ struct GInferBase {
}
};
// Struct stores network input/output names.
// Used by infer<Generic>
struct InOutInfo
{
std::vector<std::string> in_names;
std::vector<std::string> out_names;
};
/**
* @{
* @brief G-API object used to collect network inputs
*/
class GAPI_EXPORTS GInferInputs
{
public:
cv::GMat& operator[](const std::string& name);
const std::unordered_map<std::string, cv::GMat>& getBlobs() const;
private:
std::unordered_map<std::string, cv::GMat> in_blobs;
};
/** @} */
/**
* @{
* @brief G-API object used to collect network outputs
*/
struct GAPI_EXPORTS GInferOutputs
{
public:
GInferOutputs(std::shared_ptr<cv::GCall> call);
cv::GMat at(const std::string& name);
private:
std::shared_ptr<cv::GCall> m_call;
InOutInfo* m_info = nullptr;
std::unordered_map<std::string, cv::GMat> out_blobs;
};
/** @} */
// Base "Infer list" kernel.
// All notes from "Infer" kernel apply here as well.
@@ -254,6 +293,45 @@ typename Net::Result infer(Args&&... args) {
return GInfer<Net>::on(std::forward<Args>(args)...);
}
/**
* @brief Special network type
*/
struct Generic { };
/**
* @brief Calculates response for generic network
*
* @param tag a network tag
* @param inputs networks's inputs
* @return a GInferOutputs
*/
template<typename T = Generic> GInferOutputs
infer(const std::string& tag, const GInferInputs& inputs)
{
std::vector<GArg> input_args;
std::vector<std::string> input_names;
const auto& blobs = inputs.getBlobs();
for (auto&& p : blobs)
{
input_names.push_back(p.first);
input_args.emplace_back(p.second);
}
GKinds kinds(blobs.size(), cv::detail::OpaqueKind::CV_MAT);
auto call = std::make_shared<cv::GCall>(GKernel{
GInferBase::id(),
tag,
GInferBase::getOutMeta,
{}, // outShape will be filled later
std::move(kinds)
});
call->setArgs(std::move(input_args));
call->params() = InOutInfo{input_names, {}};
return GInferOutputs{std::move(call)};
}
} // namespace gapi
} // namespace cv
+28 -4
View File
@@ -17,6 +17,7 @@
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
#include <opencv2/gapi/infer.hpp> // Generic
namespace cv {
namespace gapi {
@@ -58,6 +59,8 @@ namespace detail {
// (e.g. topology's partial execution)
std::size_t num_in; // How many inputs are defined in the operation
std::size_t num_out; // How many outputs are defined in the operation
bool is_generic;
};
} // namespace detail
@@ -80,7 +83,7 @@ public:
: desc{ model, weights, device, {}, {}, {}
, std::tuple_size<typename Net::InArgs>::value // num_in
, std::tuple_size<typename Net::OutArgs>::value // num_out
} {
, false} {
};
Params<Net>& cfgInputLayers(const typename PortCfg<Net>::In &ll) {
@@ -107,15 +110,36 @@ public:
}
// BEGIN(G-API's network parametrization API)
GBackend backend() const { return cv::gapi::ie::backend(); }
std::string tag() const { return Net::tag(); }
cv::util::any params() const { return { desc }; }
GBackend backend() const { return cv::gapi::ie::backend(); }
std::string tag() const { return Net::tag(); }
cv::util::any params() const { return { desc }; }
// END(G-API's network parametrization API)
protected:
detail::ParamDesc desc;
};
template<>
class Params<cv::gapi::Generic> {
public:
Params(const std::string& tag,
const std::string &model,
const std::string &weights,
const std::string &device)
: desc{ model, weights, device, {}, {}, {}, 0u, 0u, true}, m_tag(tag) {
};
// BEGIN(G-API's network parametrization API)
GBackend backend() const { return cv::gapi::ie::backend(); }
std::string tag() const { return m_tag; }
cv::util::any params() const { return { desc }; }
// END(G-API's network parametrization API)
protected:
detail::ParamDesc desc;
std::string m_tag;
};
} // namespace ie
} // namespace gapi
} // namespace cv