简体   繁体   English

使用 2d arrays 更快地迭代 for 循环

[英]Faster iteration on for loop with 2d arrays

I have a problem with optimization to compute errors for disparity map estimation.我在优化计算差异 map 估计的误差时遇到问题。

To compute errors I create a class with called methods for each error.为了计算错误,我创建了一个 class 并为每个错误调用了方法。 I need to iterate for every pixel to get an error.我需要对每个像素进行迭代以获得错误。 This arrays are big cause I'm iterating in size of 1937 x 1217 images.这个 arrays 很重要,因为我正在迭代 1937 x 1217 图像的大小。 Do you know how to optimize it?你知道怎么优化吗?

Here is code of my method:这是我的方法的代码:

EDIT:编辑:

def mreError(self):
    s_gt = self.ref_disp_norm
    s_all = self.disp_bin
    s_r = self.disp_norm 

    s_gt = s_gt.astype(np.float32)
    s_r = s_r.astype(np.float32)
    n, m = s_gt.shape
    all_arr = []

    for i in range(0, n):
        for j in range(0, m):

            if s_all[i,j] == 255:
                if s_gt[i,j] == 0:
                    sub_mre = 0
                else:   
                    sub_mre = np.abs(s_gt[i,j] - s_r[i,j]) / s_gt[i,j]
                all_arr.append(sub_mre)

    mre_all = np.mean(all_arr)
    return mre_all

You could simply use array operators instead applying them to every element inside a for loop:您可以简单地使用数组运算符,而不是将它们应用于 for 循环中的每个元素:

import numpy as np

# Creating 2000x2000 Test-Data
s_gt = np.random.randint(0,2,(2000,2000)).astype(np.float32)
s_r = np.random.randint(0,2,(2000,2000)).astype(np.float32)
s_all = np.random.randint(0,256,(2000,2000)).astype(np.float32)


def calc(s_gt, s_r, s_all):
    n, m = s_gt.shape
    all_arr = []
    for i in range(0, n):
        for j in range(0, m):
            if s_gt[i,j] == 0:
                sub_mre = 0
            else:   
                sub_mre = np.abs(s_gt[i,j] - s_r[i,j]) / s_gt[i,j]
    
            if s_all[i,j] == 255:
                all_arr.append(sub_mre)
    
    mre_all = np.mean(all_arr)
    return mre_all

def calc_optimized(s_gt, s_r, s_all):
    sub_mre = np.abs((s_gt-s_r)/s_gt)
    sub_mre[s_gt==0] = 0
    return np.mean(sub_mre[s_all == 255])

When I test the speed of the two different approaches:当我测试两种不同方法的速度时:

%time calc(s_gt, s_r, s_all)
Wall time: 27.6 s
Out[53]: 0.24686379928315413

%time calc_optimized(s_gt, s_r, s_all)
Wall time: 63.3 ms
__main__:34: RuntimeWarning: divide by zero encountered in true_divide
__main__:34: RuntimeWarning: invalid value encountered in true_divide
Out[54]: 0.2468638

A straight up vectorisation of your method would be您的方法的直接矢量化将是

def method_1(self):
    # get s_gt, s_all, s_r
    sub_mre = np.zeros((s_gt.shape), dtype=np.float32)
    idx = s_gt != 0
    sub_mre[idx] = np.abs((s_gt[idx] - s_r[idx]) / s_gt[idx])
    return np.mean(sub_mre[s_all == 255])

But since you're doing your averaging only for pixels where s_all is 255, you could also filter for those first and then do the rest但由于您只对s_all为 255 的像素进行平均,您也可以先过滤这些像素,然后再执行 rest

def method_2(self):
    idx = s_all == 255
    s_gt = s_gt[idx].astype(np.float32)
    s_r = s_r[idx].astype(np.float32)
    sub_mre = np.zeros_like(s_gt)
    idx = s_gt != 0
    sub_mre[idx] = np.abs((s_gt[idx] - s_r[idx]) / s_gt[idx])
    return np.mean(sub_mre)

Personally, I would favour the first method unless the second one results in a much faster result.就个人而言,我会赞成第一种方法,除非第二种方法产生更快的结果。 Calling the function only once and spending, for example, 40 ms vs 5 ms is not noticeable and the readability of the function matters more.仅调用一次 function 并花费,例如,40 ms 与 5 ms 并不明显,function 的可读性更重要。

You can just make an image grey (this will speed up calculations substantially) Go check this link how you can do it.您可以将图像设为灰色(这将大大加快计算速度) Go 检查此链接如何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM