GPU上的OpenCV FAST检测器 [英] OpenCV FAST detector on GPU

查看:153
本文介绍了GPU上的OpenCV FAST检测器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下代码:

cv::Ptr<cv::FastFeatureDetector> fastDetector = cv::FastFeatureDetector::create(100, true, 2);
cv::Ptr<cv::cuda::FastFeatureDetector> gpuFastDetector = cv::cuda::FastFeatureDetector::create(100, true, 2);

std::vector<cv::KeyPoint> keypoints;
std::vector<cv::KeyPoint> gpuKeypoints;

cv::Mat frame;
cv::cuda::GpuMat gFrame;

frame = cv::imread("image1.jpg"); // 4608 x 3456
cv::cvtColor(frame, frame, CV_BGR2GRAY);
gFrame.upload(frame);

gpuFastDetector->detect(gFrame, gpuKeypoints);
std::cout << "FAST GPU " << gpuKeypoints.size() << std::endl;
fastDetector->detect(frame, keypoints);
std::cout << "FAST " << keypoints.size() << std::endl;

输出为:

FAST GPU 2210
FAST 3209

问题1

为什么将相同算法应用于具有相同参数的同一图像会导致检测到不同数量的关键点?

问题2

我正在Windows中的Visual Studio中运行它.使用 Debug (调试)配置时,GPU检测速度更快.

I am running this on Windows in Visual Studio. When using Debug configuration, the GPU detection performs faster.

但是使用 Release 时,常规(CPU)快速检测器的执行速度更快.此外,无论使用哪种配置类型,检测器在GPU上的性能均保持不变.但是,与调试"配置相比,在发行版"下执行检测时,CPU的性能急剧提高.

But when using Release, the normal (CPU) fast detector performs faster. Moreover, The detector's performance on GPU remains the same regardless of the configuration type used. But the performance sharply increases on CPU when detection is performed under Release compared to Debug config.

(我没有在这里介绍的代码上运行测量.我知道,由于上下文初始化,对某些OpenCV函数的第一次调用可能需要更长的时间才能执行.)

(I am not running measurements on the code I presented here. I am aware that the first call to some OpenCV functions can take longer to execute because of context initialization.)

这很可能与我以前的有关FAST检测器的问题有关. BHawk对CPU上的SIMD优化给出了合理的解释.

This is very likely related to my old question about the FAST detector. A plausible explanation was given by BHawk about SIMD optimizations on CPU.

第二个问题是:

SIMD优化的CPU是否可以比GPU更快地执行FAST功能检测?这似乎不太可能.

推荐答案

初始化冗长的答案:)

问题1:

调试编译不使用发行版使用的代码优化.调试版本将执行诸如保留临时变量数据之类的操作,以便您可以在调试器中读取数据.这通常意味着通常会暂时存在于CPU寄存器中的数据将溢出,并以调试版本复制到RAM中.如果在优化的发行版中不再需要相同的数据,则将其丢弃.如果在编译设置中禁用代码优化,则这种差异可能会消失.我不确定我之前从未尝试过不进行优化.

A debug compilation doesn't use the code optimizations used by a release version. The debug version will do things like retain temporary variable data so that you can read the data in the debugger. This often means that data that would normally exist temporarily in CPU registers will overflow and be copied into RAM in a debug version. The same data would be discarded when it is no longer needed in an optimized Release version. This difference might go away if you disable code optimization in your compile settings; I'm not sure I've never tried to compile without optimization before.

问题2:

确定在GPU或CPU上执行图像处理的效果时,有几个因素在起作用.

There are a few factors at play when determining whether an image process will perform better on a GPU or CPU.

1:内存管理

GPU处理的主要瓶颈是将数据加载到GPU并从GPU检索数据.如果图像非常大(您的情况下为16个MegaPixels),则此瓶颈可能会成为一个重大障碍.当您将图像加载到GPU上,然后通过OpenGL上下文将图像放置在那里进行处理和显示时,GPU的工作效果最佳(就像您在3D游戏引擎中看到的那样).

The major bottleneck with GPU processing is loading data onto the GPU and retrieving it from the GPU. In the case of very large images (16 MegaPixels in your case) this bottleneck can become a significant impediment. GPUs work best when you load images onto them and then leave the images there to be manipulated and displayed via OpenGL context (as you would see in a 3D gaming engine).

2:串行与并行

GPU由数千个并行运行的小型处理核心组成.因此,他们能够同时执行许多小任务.另一方面,对CPU进行了优化,可以串行执行复杂的任务.这意味着某些任务(大图像上下文,复杂的计算,多步处理)可能会在CPU上比在GPU上执行得更好.另一方面,使用较小图像上下文且不需要多个处理步骤的简单任务将在GPU上更快地执行.更复杂的是,可以根据可用计算内核的数量使CPU线程化以并行运行.最重要的是,SIMD优化的CPU可以进一步并行化其处理.因此,具有4个内核和8个SIMD ALU的单个CPU可以同时处理32个数据.与GPU中存在的数千个内核相比,这仍然相去甚远,但是CPU内核通常处理得更快,因此具有8个SIMD的4个内核在某些任务上的执行速度可能更快.当然,如果您使用具有更多核心或更多ALU的系统,CPU速度也会提高;如果减少内核数量,则CPU速度也会降低.

GPUs are made up of thousands of small processing cores that run in parallel. As such, they are able to perform lots of small tasks simultaneously. CPUs on the other hand are optimized to perform complex tasks in serial. This means some tasks (large image context, complex calculation, multi-step process) will likely perform better on a CPU than on a GPU. On the other hand, simpler tasks that use small image contexts, and don't require multiple processing steps will perform much faster on a GPU. To further complicate matters, CPUs can be threaded to run in parallel depending on the number of compute cores available. On top of that, SIMD optimized CPUs can further parallelize their processing. So a single CPU with 4 cores and an 8 SIMD ALUs can process 32 pieces of data simultaneously. This is still a far cry from the 1000s of cores present in a GPU but CPU cores usually process much faster so 4 cores with 8 SIMDs may perform faster on certain tasks. Of course the CPU speed will also scale if you go to a system with more cores or more ALUs, and decrease if you reduce the number.

结论

由于内存瓶颈,有些图像处理任务不太适合GPU.数据IO抵消了大规模并行化带来的任何速度提升.在您拥有高度优化的并行SIMD CPU算法的情况下,由于算法的性质和/或流入和流出GPU的数据IO,当然,CPU版本的性能可能会比GPU更快.您可能还会发现,在小图像上,GPU版本仍然略快.

Because of the memory bottleneck, there are some image processing tasks that aren't well suited to the GPU. The data IO negates any speed gain from the massive parallelization. In the case where you have a highly optimized, parallelized SIMD CPU algorithm, it is certainly possible that the CPU version will perform faster than the GPU due either to the nature of the algorithm and/or the data IO onto and off of the GPU. You might also find that on small images the GPU version is still slightly faster.

我必须通读源代码,以确切地了解此特定功能如何以及为什么在CPU上比GPU上运行得更快,但我对此并不感到惊讶.关于为什么一个实现与另一个实现具有不同数量的功能,这也需要通读,但这可能是为了内存分配或优化目的而不同地更改每个实现的功能.

I would have to read through the source to see exactly how and why this specific function runs faster on CPU than GPU but I am not surprised that it does. Regarding why you get a different number of features with one implementation versus the other, that would also require a read through, but it is probably a function of altering the implementation of each differently for memory allocation or optimization purposes.

很抱歉,答案很长,但这是一个复杂的讨论主题.

Sorry for the long answer, but it is a complicated topic of discussion.

这篇关于GPU上的OpenCV FAST检测器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆