gpt4 book ai didi

c++ - 什么是使用 opencv::Mat 优化 c++ 矩阵计算

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:26 25 4
gpt4 key购买 nike

我有以下代码,比 llvm 优化的 numpy/python 版本慢 2 倍:

numpy.sum(numpy.square(desc - desc_2))

如何在 C++ 中改进以下 opencv 矩阵代码:

   cv::Mat broad;
cv::Mat features
broad = features - cmp;
cv::pow(broad,2,broad);
cv::reduce(broad, broad, 1, cv::REDUCE_SUM);

既是 numpy 又是 Mat 是 (512 X float) 矩阵

最佳答案

如果数组是连续的,即 desc.isContinuous() 为真,您可以获得指向矩阵的指针并手动计算差平方和,这在我的笔记本电脑上大约需要 2 毫秒对于两个 512×1024 double 矩阵。

但是,您可以使用 Python 获得大致相同的性能

np.linalg.norm(desc - desc_2)**2

代替

np.sum(np.square(desc - desc_2))

np.linalg.norm 更快,因为与其他方法不同,它在平方后直接对差异求和,而不是先将它们存储到 RAM 中再读回。这在这里很重要,因为计算主要受内存带宽的限制。

如果精度为对您的应用程序不重要,因为它减少了要传输的字节数。如果你走那条路,你甚至可以考虑使用 SSE 或 AVX 指令,以防你不再受到内存带宽的瓶颈。

C++ 示例

#include <opencv2/opencv.hpp>
#include <chrono>
#include <iostream>

int main(){
for (int test = 0; test < 10; test++){
// initialize matrices with random data
cv::Mat desc1(512, 1024, CV_64F);
cv::Mat desc2(512, 1024, CV_64F);
randu(desc1, cv::Scalar(0.0), cv::Scalar(1.0));
randu(desc2, cv::Scalar(0.0), cv::Scalar(1.0));

auto start = std::chrono::high_resolution_clock::now();

// get pointers to first elements of matrices
double *ptr1 = desc1.ptr<double>(0);
double *ptr2 = desc2.ptr<double>(0);

double sum = 0.0;
cv::Size size = desc1.size();
// sum the squared differences between all elements
for (int i = 0; i < size.width * size.height; i++){
double difference = ptr1[i] - ptr2[i];
sum += difference * difference;
}

auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
double milliseconds = nanoseconds * 1e-6;

std::cout << "result C++: " << sum << ", " << milliseconds << " milliseconds" << std::endl;
}
}

C++ 示例的计时结果

result C++: 87452.5, 2.83437 milliseconds
result C++: 87382.4, 1.92824 milliseconds
result C++: 87334.1, 1.93404 milliseconds
result C++: 87409, 1.92608 milliseconds
result C++: 87524, 1.9333 milliseconds
result C++: 87352.1, 2.1178 milliseconds
result C++: 87390.5, 1.95265 milliseconds
result C++: 87325.5, 2.14512 milliseconds
result C++: 87361.8, 1.95677 milliseconds
result C++: 87687.6, 2.10184 milliseconds

Python 示例

import time
import numpy as np

for _ in range(10):
desc1 = np.random.rand(512, 1024)
desc2 = np.random.rand(512, 1024)

t0 = time.perf_counter()

s = np.sum(np.square(desc1 - desc2))

t1 = time.perf_counter()

print("result sum(square(desc1 - desc2)):", s, 1000*(t1 - t0), "milliseconds")

print("")

for _ in range(10):
desc1 = np.random.rand(512, 1024)
desc2 = np.random.rand(512, 1024)

t0 = time.perf_counter()

s = np.linalg.norm(desc1 - desc2)**2

t1 = time.perf_counter()

print("result linalg.norm(desc1 - desc2)):", s, 1000*(t1 - t0), "milliseconds")

Python 示例的计时结果

result  sum(square(desc1 - desc2)): 87074.95194414 25.832481998804724 milliseconds
result sum(square(desc1 - desc2)): 87248.23486227753 4.291343997465447 milliseconds
result sum(square(desc1 - desc2)): 87298.36234439172 4.271910001989454 milliseconds
result sum(square(desc1 - desc2)): 87335.12881267883 4.619887000444578 milliseconds
result sum(square(desc1 - desc2)): 87329.50342643914 5.444231999717886 milliseconds
result sum(square(desc1 - desc2)): 87622.93760898946 4.942010997183388 milliseconds
result sum(square(desc1 - desc2)): 87376.8813873815 5.2427179980441 milliseconds
result sum(square(desc1 - desc2)): 87419.14640286344 4.6821379983157385 milliseconds
result sum(square(desc1 - desc2)): 87193.05495816837 4.524519001279259 milliseconds
result sum(square(desc1 - desc2)): 87327.52989629997 5.168449999473523 milliseconds

result linalg.norm(desc1 - desc2)): 87418.11425419849 2.766734000033466 milliseconds
result linalg.norm(desc1 - desc2)): 87433.25400706155 3.2142550007847603 milliseconds
result linalg.norm(desc1 - desc2)): 87295.75712318903 2.63671799984877 milliseconds
result linalg.norm(desc1 - desc2)): 87300.2682143185 3.1689810020907316 milliseconds
result linalg.norm(desc1 - desc2)): 87430.74565029072 2.64247700033593 milliseconds
result linalg.norm(desc1 - desc2)): 87384.7557858529 2.645990996825276 milliseconds
result linalg.norm(desc1 - desc2)): 87221.95238863592 2.6713590013969224 milliseconds
result linalg.norm(desc1 - desc2)): 87366.24164248169 2.495335997082293 milliseconds
result linalg.norm(desc1 - desc2)): 87183.96607524085 2.6664280012482777 milliseconds
result linalg.norm(desc1 - desc2)): 87441.26642263135 2.5408009969396517 milliseconds

关于c++ - 什么是使用 opencv::Mat 优化 c++ 矩阵计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921948/

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