如何以有效的方式调整图像处理算法的参数? [英] How can I adjust parameters for image processing algorithm in an efficient way?

查看:209
本文介绍了如何以有效的方式调整图像处理算法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始解决我的问题的解决方案之前,我只想确定我是否会重新发明轮子,如果我可以重复使用以前曾做过的工作。所以我的问题是:

Before starting implementation of solution for my problem I just want to be sure if I will not "reinvent wheel" and if I can reuse work that someone have done before. So my problem is:

我使用OpenCV库制作了图像匹配器。此匹配器接收一组图像文件,并尝试在数据库中查找类似的图像。最后,它根据 ROC曲线定义返回统计结果(真阳性,真阴性,假阳性)和False负数匹配)。由于OpenCV的库算法参数值大约为10,因此这些结果可能会有所不同。这意味着参数调整将带来更多的True Positive匹配和更少的False Positive匹配。由于我必须调整或多或少10个参数,强力调节器将非常缓慢。蛮力我的意思是:

I have made image matcher using OpenCV library. This matcher receives a set of image files and trying to find similar images in database. At the end it returns statistical results according to ROC Curves definition (True Positive, True Negative, False Positive and False Negative number of matches). These results can vary because of OpenCV’s library algorithm parameters values, which are about 10. This means that parameters adjustment will bring to more True Positive matches and to less False Positive matches. As I have to adjust more or less 10 parameters, brute force adjuster will be very slowly. By brute force I mean:

While(param1 < 50){
  While(param2 < 50){
    While(param3 < 50){
      …
      PerformMatching();
      param3 +=2;
    }
    param2++;
  }
  pram1 +=5;
}

我想做的是随机选择参数然后分析是否有统计结果越来越好了。然后这个分析将有助于改变随机生成参数的方法,以便选择更好的参数。

What I want to do is to choose parameters randomly and then analyze if statistical results are becoming better. Then this analysis will help to alter method which is randomly generates parameters in order to choose better parameters.

所以我的问题是,如果Java中有库,或者是否有任何AI算法,它会根据True Positive和False Positive值的评估返回更好的参数集?

So my question is if there is library in Java or if there is any AI algorithm, which will return better parameter set based on evaluation of True Positive and False Positive values?

可以感谢一些帮助,谢谢。

Could appreciate some help, thanks.

推荐答案

爬坡



您可以尝试一些随机优化算法,例如 Hill Climbing ,您可以从随机解决方案(如@Don Reba指出)开始,并查看相邻解决方案,以获得与成本函数相关的更好的解决方案。我将使用一些示例python代码来解释这个想法。

Hill Climbing

You could try some stochastic optimization algorithms, e.g., Hill Climbing, in which you start with a random solution (like @Don Reba pointed out) and look at the set of neighboring solutions for those that are better with respective to the cost function. I will use some sample python code to explain the idea.

对于你的邻居,你可以使用一个简单的函数,如:

For neighbors in your case, you can use a simple function like:

n_params = 5  # number of parameters
upper_bound = 5  # upper limit of your parameters
lower_bound = 0  # lower limit of your parameters

def get_neighbors(solution):
    neighbors = []
    for i in range(n_params):
        x = copy.deepcopy(solution)
        if x[i] < upper_bound:
            x[i] += 1 # increment one of the components
            neighbors.append(x)
        x = copy.deepcopy(solution)
        if x[i] > lower_bound:
            x[i] -= 1 # decrement one of the components
            neighbors.append(x)
    return neighbors 






如果你有[1,3,4,2,2]的当前解,通过递增或递减任何一个组件,您将获得10个不同的邻居,如下所示:


If you have a current solution of [1,3,4,2,2], by incrementing or decrementing any of the components, you get 10 different neighbors as below:

 [2, 3, 4, 2, 2],
 [0, 3, 4, 2, 2],
 [1, 4, 4, 2, 2],
 [1, 2, 4, 2, 2],
 [1, 3, 5, 2, 2],
 [1, 3, 3, 2, 2],
 [1, 3, 4, 3, 2],
 [1, 3, 4, 1, 2],
 [1, 3, 4, 2, 3],
 [1, 3, 4, 2, 1]

这里我们假设每个参数都是一个整数。您可以通过调整步长(例如,0.05)来获得更多粒度。

Here we assume every parameter is an integer. You could achieve more granularity by adjusting the step size (e.g., 0.05).

def hill_climb():
    initial_solution = np.random.randint(lower_bound, upper_bound, n_params)
    current_solution = initial_solution
    print 'initial solution', initial_solution
    current_cost = get_cost(initial_solution)
    step = 1
    while True:
        #try to replace each single component w/ its neighbors
        lowest_cost = current_cost
        lowest_solution = current_solution
        print 'hill-climbing cost at step %6d: %d' % (step, lowest_cost)
        neighbors = get_neighbors(current_solution)
        for new_solution in neighbors:
            neighbor_cost = get_cost(new_solution)
            if neighbor_cost < lowest_cost:
                lowest_cost = neighbor_cost
                lowest_solution = new_solution

        if lowest_cost >= current_cost:
            break
        else: 
            current_solution= lowest_solution
            current_cost = lowest_cost
            step += 1
    return current_solution



成本函数



为了完整性,我将使用自己的成本函数(仅用于演示目的),这是

Cost function

For completeness, I will use my own cost function (just for a demo purpose), which is

f(x) = x1^1 - x2^2 + x3^3 - x4^4 + x5^5

即:

def get_cost(solution):
    cost = 0
    for i,param in enumerate(solution):
        cost += (-1.)**i * param**(i+1)  
    return cost



优化结果:



结果如下。我们使用[4,0,1,3,1]的随机初始猜测。在14个步骤(14 * 10 = 140个邻居的评估)之后,我们找到[0,5,0,5,0]的最佳答案,其使成本最小化。对于蛮力,您必须评估6 ^ 6 = 46656解决方案。当您拥有高维解决方案时,可以节省更多的运行时间。

Optimization result:

Here is the result. We use a random initial guess of [4, 0, 1, 3, 1]. After 14 steps (evaluation of 14*10 = 140 neighbors), we find the optimal answer of [0, 5, 0, 5, 0] which minimize the cost. For brute force, you have to evaluate 6^6 = 46656 solutions. Much more running time could be saved while you have a high dimensional solution.

注意,因为这是一个随机方法,所以找到局部最小值作为最终结果(尽管有时它与全局最小值相同,但不能保证)。但实际上它已经足够好了。

Note since this is a stochastic method, local minimum is found as the final result (though sometimes it's identical to the global minimum, it's not guaranteed). However practically it's good enough.

initial solution:               [4 0 1 3 1]
hill-climbing cost at step      1: -75
hill-climbing cost at step      2: -250
hill-climbing cost at step      3: -619
hill-climbing cost at step      4: -620
hill-climbing cost at step      5: -621
hill-climbing cost at step      6: -622
hill-climbing cost at step      7: -623
hill-climbing cost at step      8: -624
hill-climbing cost at step      9: -627
hill-climbing cost at step     10: -632
hill-climbing cost at step     11: -639
hill-climbing cost at step     12: -648
hill-climbing cost at step     13: -649
hill-climbing cost at step     14: -650
Final solution:                 [0 5 0 5 0]



相关文章



相关但更多复杂的问题在这里:用于从一组中选择n个向量同时最小化成本的算法

上面的所有代码都可以找到此处

All codes above can be found here.

这篇关于如何以有效的方式调整图像处理算法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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