gpt4 book ai didi

c++ - OpenCV GAPI 性能不如预期

转载 作者:行者123 更新时间:2023-12-02 16:13:56 25 4
gpt4 key购买 nike

我做了下面的测试,但是结果不是很好,因为我希望GAPI能有很大的提高。不知道是不是我做错了,希望大家帮我指正,万分感谢!

My test environment are OpenCV4.2 official build, Windows 10 x64, VS2019 Release x64,i7-8700K.

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/imgproc.hpp>

std::string image_path = "1.png";
cv::Mat GAPITEST(const cv::Mat& input_frame)
{
cv::Mat output_frame;

cv::GMat in;
cv::GMat vga = cv::gapi::resize(in, cv::Size(), 0.5, 0.5);
cv::GMat gray = cv::gapi::BGR2Gray(vga);
cv::GMat blurred = cv::gapi::blur(gray, cv::Size(5, 5));
cv::GMat out = cv::gapi::Canny(blurred, 32, 128, 3);
cv::GComputation ac(in, out);

int64 t0 = cv::getTickCount();
for(int i=0;i<200;i++)
ac.apply(input_frame, output_frame);
int64 t1 = cv::getTickCount();
std::cout <<__func__<< "\t seconds:" << (t1 - t0) / cv::getTickFrequency()<<std::endl;

return output_frame;
}

cv::Mat TraditionalTEST(const cv::Mat& input_frame)
{
cv::Mat output_frame;
cv::Mat vga;
cv::Mat gray;
cv::Mat blurred;

int64 t0 = cv::getTickCount();
for (int i = 0; i < 200; i++)
{
cv::resize(input_frame,vga, cv::Size(), 0.5, 0.5);
cv::cvtColor(vga, gray, cv::COLOR_BGR2GRAY);
cv::blur(gray, blurred,cv::Size(5,5));
cv::Canny(blurred,output_frame,32,128,3);
}
int64 t1 = cv::getTickCount();
std::cout << __func__ << "\t seconds:" << (t1 - t0) / cv::getTickFrequency()<<std::endl;
return output_frame;
}
int main()
{
cv::Mat input_frame = cv::imread(image_path);
cv::imshow("input_frame",input_frame);
cv::waitKey(100);
auto result1 = GAPITEST(input_frame);
auto result2 = TraditionalTEST(input_frame);
//check result whether identical or not.
bool eq = cv::countNonZero(result1 != result2) == 0;
std::cout << "result equal "<< eq;
return 0;
}

输出

GAPITEST         seconds:4.92153
TraditionalTEST seconds:4.68761
result equal 1

最佳答案

GAPI 仍处于开发初期,其在单机上的性能非常糟糕。 GAPI 本身并非主要设计用于直接计算算法,因此它使用后端库来执行计算。默认是 OpenCV 的默认后端,这有点糟糕。您可以将其替换为 Fluid 后端,据说它在执行算法时具有更好的缓存局部性,但在我的一些测试中它仍然很糟糕。

这些后端非常缺乏基本 OpenCV 函数的实现(例如,Fluid 仅支持盒式过滤器的 3x3 内核),并且当您使用不受支持的操作时,GComputation::apply 会不正常地崩溃,并且经常没有任何有用的错误消息。

GAPI 的优点在于它实现的图形模型与硬件无关。您可以将它生成的图形放到具有多个 GPU、CPU 等的云或分布式计算系统中,它会自动充分利用可用的资源。

如果你想在单机上获得快速性能,我推荐使用 cv::cuda::GpuMat。我自己经常使用它,它对许多操作来说都非常快。它省去了编写自定义 CUDA 内核的麻烦。

我不能保证 UMat 或其他 GPU 实现的质量,因为我只使用过带有 Nvidia 卡的 OpenCV。

您还可以考虑使用 OpenMP 支持编译 OpenCV 以提高性能。

总之,这是一种随口一说的回答。转到此处了解有关 GAPI 的更多详细信息和比较多个后端的更完整的测试程序:https://docs.opencv.org/master/d3/d7a/tutorial_gapi_anisotropic_segmentation.html

关于c++ - OpenCV GAPI 性能不如预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60629331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com