This is an interesting implementation of a G-API-aware pipeline executor. Here's my analysis with suggestions for improvement:
Issues and Improvements
1. G-API Graph Construction Bug
The current implementation has issues with G-API graph construction:
cpp
// Current problematic code:
g_out = cv::gapi::input(current_mat); // ❌ Incorrect usage
Fixed version:
cpp
template <typename DataT>
class GapiPipeline : public Pipeline<DataT> {
using DataType = DataT;
public:
DataType execute_pipeline(std::vector<std::unique_ptr<PipelineNodeBase>>& nodes,
std::any& current_data) const override {
if (!current_data.has_value()) {
throw Exception("Pipeline has no input data", ErrorCode::INVALID_DATA);
}
DataType current_value = std::any_cast<DataType>(current_data);
// Group consecutive G-API nodes
auto groups = group_nodes(nodes);
for (const auto& group : groups) {
if (group.is_gapi) {
current_value = execute_gapi_group(group.nodes, current_value);
} else {
current_value = execute_native_group(group.nodes, current_data, current_value);
}
}
return current_value;
}
private:
struct NodeGroup {
bool is_gapi;
std::vector<PipelineNodeBase*> nodes;
};
std::vector<NodeGroup> group_nodes(
const std::vector<std::unique_ptr<PipelineNodeBase>>& nodes) const {
std::vector<NodeGroup> groups;
for (const auto& node : nodes) {
bool is_gapi = dynamic_cast<GapiNodeInterface*>(node.get()) != nullptr;
if (groups.empty() || groups.back().is_gapi != is_gapi) {
groups.push_back({is_gapi, {node.get()}});
} else {
groups.back().nodes.push_back(node.get());
}
}
return groups;
}
DataType execute_gapi_group(const std::vector<PipelineNodeBase*>& nodes,
const DataType& input) const {
cv::Mat input_mat = convert_to_mat(input);
// Properly construct G-API graph
cv::GMat g_in;
cv::GMat g_current = g_in;
for (auto* node : nodes) {
if (auto* gapi_node = dynamic_cast<GapiNodeInterface*>(node)) {
g_current = gapi_node->gapi_operation(g_current);
}
}
// Compile and execute
cv::GComputation comp(g_in, g_current);
cv::Mat output_mat;
comp.apply(cv::gin(input_mat), cv::gout(output_mat));
return convert_from_mat(output_mat);
}
DataType execute_native_group(const std::vector<PipelineNodeBase*>& nodes,
std::any& current_data,
DataType current_value) const {
for (auto* node : nodes) {
current_data = std::make_any<DataType>(current_value);
current_value = std::any_cast<DataType>(node->execute(current_data));
}
return current_value;
}
2. Improved Conversion Functions with Type Traits
cpp
private:
// Type trait to detect if type has mat() method
template<typename T, typename = void>
struct has_mat_method : std::false_type {};
template<typename T>
struct has_mat_method<T,
std::void_t<decltype(std::declval<T&>().mat())>> : std::true_type {};
// Generic conversion with SFINAE
template<typename T = DataType>
static typename std::enable_if_t<has_mat_method<T>::value, cv::Mat>
convert_to_mat(const T& data) {
return data.mat().clone();
}
template<typename T = DataType>
static typename std::enable_if_t<std::is_same_v<T, cv::Mat>, cv::Mat>
convert_to_mat(const T& data) {
return data.clone();
}
template<typename T = DataType>
static typename std::enable_if_t<has_mat_method<T>::value, T>
convert_from_mat(const cv::Mat& mat) {
T result;
mat.copyTo(result.mat());
return result;
}
template<typename T = DataType>
static typename std::enable_if_t<std::is_same_v<T, cv::Mat>, T>
convert_from_mat(const cv::Mat& mat) {
return mat.clone();
}
3. Add Performance Optimization with Caching
cpp
class GapiPipeline : public Pipeline<DataT> {
private:
mutable std::unordered_map<size_t, cv::GComputation> compilation_cache_;
size_t compute_graph_hash(const std::vector<PipelineNodeBase*>& nodes) const {
size_t hash = 0;
for (const auto* node : nodes) {
hash ^= std::hash<std::type_index>{}(typeid(*node)) + 0x9e3779b9 +
(hash << 6) + (hash >> 2);
}
return hash;
}
DataType execute_gapi_group_cached(const std::vector<PipelineNodeBase*>& nodes,
const DataType& input) const {
size_t hash = compute_graph_hash(nodes);
// Check cache
auto it = compilation_cache_.find(hash);
if (it != compilation_cache_.end()) {
cv::Mat input_mat = convert_to_mat(input);
cv::Mat output_mat;
it->second.apply(cv::gin(input_mat), cv::gout(output_mat));
return convert_from_mat(output_mat);
}
// Build and cache new computation
cv::GMat g_in;
cv::GMat g_current = g_in;
for (auto* node : nodes) {
if (auto* gapi_node = dynamic_cast<GapiNodeInterface*>(node)) {
g_current = gapi_node->gapi_operation(g_current);
}
}
cv::GComputation comp(g_in, g_current);
compilation_cache_[hash] = comp;
cv::Mat input_mat = convert_to_mat(input);
cv::Mat output_mat;
comp.apply(cv::gin(input_mat), cv::gout(output_mat));
return convert_from_mat(output_mat);
}
};
4. Add Better Error Handling
cpp
DataType execute_pipeline(std::vector<std::unique_ptr<PipelineNodeBase>>& nodes,
std::any& current_data) const override {
try {
if (!current_data.has_value()) {
throw Exception("Pipeline has no input data", ErrorCode::INVALID_DATA);
}
DataType current_value;
try {
current_value = std::any_cast<DataType>(current_data);
} catch (const std::bad_any_cast& e) {
throw Exception(
fmt::format("Input data type mismatch: expected {}, error: {}",
typeid(DataType).name(), e.what()),
ErrorCode::TYPE_MISMATCH
);
}
// ... rest of the implementation
} catch (const cv::Exception& e) {
throw Exception(
fmt::format("G-API execution failed: {}", e.what()),
ErrorCode::GAPI_ERROR
);
}
}
5. Add Profiling Support
cpp
class GapiPipeline : public Pipeline<DataT> {
private:
struct ProfilingData {
std::chrono::nanoseconds gapi_time{0};
std::chrono::nanoseconds native_time{0};
size_t gapi_nodes_executed{0};
size_t native_nodes_executed{0};
};
mutable ProfilingData profiling_data_;
DataType execute_with_profiling(/* ... */) const {
auto start = std::chrono::high_resolution_clock::now();
// Execute nodes...
auto end = std::chrono::high_resolution_clock::now();
if (is_gapi) {
profiling_data_.gapi_time += end - start;
profiling_data_.gapi_nodes_executed += nodes.size();
} else {
profiling_data_.native_time += end - start;
profiling_data_.native_nodes_executed += nodes.size();
}
}
public:
ProfilingData get_profiling_data() const { return profiling_data_; }
void reset_profiling() { profiling_data_ = {}; }
};
These improvements provide:
- Correct G-API graph construction
- Better node grouping for optimal performance
- Type-safe conversion functions
- Compilation caching for repeated executions
- Comprehensive error handling
- Performance profiling capabilities