【问题标题】:How can I adjust parameters for image processing algorithm in an efficient way?如何有效地调整图像处理算法的参数?
【发布时间】:2012-11-18 14:27:39
【问题描述】:

在开始为我的问题实施解决方案之前,我只想确定我是否不会“重新发明轮子”,以及我是否可以重用某人以前做过的工作。所以我的问题是:

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

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

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

所以我的问题是,Java 中是否有库或者是否有任何 AI 算法,它将根据真阳性和假阳性值的评估返回更好的参数集?

不胜感激,谢谢。

【问题讨论】:

    标签: java statistics artificial-intelligence mathematical-optimization


    【解决方案1】:

    有一系列算法优化技术,从您示例中的简单网格搜索到各种自适应算法。如果您想了解更高级的技术,我建议您先查看Frank Hutter 的研究。他的PhD thesis 包含对该领域的精彩概述。

    如果您不想花费太多时间,您可以做的一项重大改进是随机生成参数,而不是使用常规网格。这样,如果某些参数变得不重要,您就不会浪费时间来保持其他参数不变。你想要这样的东西:

    param1 = random.Next(50);
    param2 = random.Next(50);
    ...
    PerformMatching();
    

    这种方法的另一个优点是您可以根据需要收集样本点,而不必等到探索整个网格。使用quasi-random 参数序列可以做得更好,这将保持点均匀分布。有些库会为您生成这些。

    生成点后,您可以简单地选择最佳组合,或使用绘图工具或模式查找算法(例如 MeanShift)对其进行分析。

    【讨论】:

      【解决方案2】:

      爬山

      您可以尝试一些随机优化算法,例如Hill Climbing,在其中您从一个随机解决方案开始(就像@Don Reba 指出的那样),然后查看一组相邻解决方案,找出那些相对于成本函数。我将使用一些示例 python 代码来解释这个想法。

      获取邻居解决方案

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

      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 个不同的邻居,如下所示:

       [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)来获得更多粒度。

      爬山迭代

      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
      

      成本函数

      为了完整起见,我将使用我自己的成本函数(仅用于演示目的),即

      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 个解决方案。当您拥有高维解决方案时,可以节省更多的运行时间。

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

      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]
      

      相关帖子

      这里有一个相关但更复杂的问题:Algorithm for selecting n vectors out of a set while minimizing cost

      以上所有代码都可以在here找到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多