在2D数组上使用滑动窗口分类器对1-in-N分类进行有效多数投票

时间:2018-07-30 21:40:52

标签: python arrays python-3.x numpy matrix-indexing

简短版本:我想使用2D数组中的值来索引较大数组的相应子集的第三维-然后增加这些元素。

我希望能帮助您更快地完成这两种Incorporated_votes算法。实际上,将分类器在数组上滑动并计算最佳步幅并不是重点。

长版: 我有一种算法,将R1xC1 2D数组中的每个元素分类为N个类之一。

我想对尺寸为R2xC2的较大2D数组进行分类。与其将较大的数组细分为多个R1xC1 2D数组,我不希望将分类器滑动到较大的数组上,这样较大数组中的每个元素都会被分类多次。这意味着我将拥有一个R2xC2xN数组来存储结果,并且当窗口在大数组上滑动时,窗口中的每个像素将在三维中增加一个元素(即N个类之一)。

所有滑动完成后,我们可以简单地获取与分类对应的维度中的argmax,以获得每个元素的分类。

我打算按比例放大以对几十万个像素和几十个像素进行分类,因此我担心使用分类结果来增加每个元素的分类维度中的一个值的效率。

下面是我整个晚上一直在Python3中制作的问题的玩具版本。它具有天真的double循环实现,以及通过索引刷新和一些智能索引获得的更好的一种。分类器只是随机的。

import numpy as np

map_rows = 8
map_cols = 10
num_candidates = 3
vote_rows = 6
vote_cols = 5


def display_tally(the_tally):
    print("{:25s}{:25s}{:25s}".format("Class 0", "Class 1", "Class 2"))
    for i in range(map_rows):
        for k in range(num_candidates):
            for j in range(map_cols):
                print("{:<2}".format(the_tally[i, j, k]), end='')
            print("     ", end='')
        print("")


def incorporate_votes(current_tally, this_vote, left, top):
    for i in range(vote_rows):
        for j in range(vote_cols):
            current_tally[top + i, left + j, this_vote[i, j]] += 1
    return current_tally


def incorporate_votes2(current_tally, this_vote, left, top):
    for i in range(num_candidates):
        current_tally[i, top:top + vote_rows, left:left + vote_cols][this_vote == i] += 1
    return current_tally


tally = np.zeros((map_rows, map_cols, num_candidates), dtype=int)
swizzled_tally = np.zeros((num_candidates, map_rows, map_cols), dtype=int)
print("Before voting")
display_tally(tally)

print("\n Votes from classifier A (centered at (2,2))")
votes = np.random.randint(num_candidates, size=vote_rows*vote_cols).reshape((vote_rows, vote_cols))
print(votes)

tally = incorporate_votes(tally, votes, 0, 0)
swizzled_tally = incorporate_votes2(swizzled_tally, votes, 0, 0)

print("\nAfter classifier A voting (centered at (2,2))")
display_tally(tally)

print("\n Votes from classifier B (Centered at (5, 4))")
votes2 = np.random.randint(num_candidates, size=vote_rows*vote_cols).reshape((vote_rows, vote_cols))
print(votes2)

tally = incorporate_votes(tally, votes2, 3, 2)
swizzled_tally = incorporate_votes2(swizzled_tally, votes2, 3, 2)

print("\nAfter classifier B voting (Centered at (5, 4))")
print("Naive vote counting")
display_tally(tally)
print("\nSwizzled vote counting")
display_tally(np.moveaxis(swizzled_tally, [-2, -1], [0, 1]))

new_tally = np.moveaxis(tally, -1, 0)
classifications = np.argmax(swizzled_tally, axis=0)
print("\nNaive classifications")
print(classifications)

print("\nSwizzled classifications")
classifications = np.argmax(tally, axis=2)
print(classifications)

还有一些示例输出:

Before voting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      

 Votes from classifier A (centered at (2,2))
[[1 1 2 2 1]
 [0 2 0 2 1]
 [0 2 2 0 2]
 [1 1 1 2 0]
 [1 0 0 2 1]
 [2 1 1 1 0]]

After classifier A voting (centered at (2,2))
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 0 1 0 0 0 0 0      
0 0 0 0 1 0 0 0 0 0      1 1 1 0 0 0 0 0 0 0      0 0 0 1 0 0 0 0 0 0      
0 1 1 0 0 0 0 0 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 1 0 0 0 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 0 0 0 0 0      1 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 0      

 Votes from classifier B (Centered at (5, 4))
[[2 2 2 0 0]
 [0 1 2 1 2]
 [2 0 0 2 0]
 [2 2 1 1 1]
 [1 2 0 2 1]
 [1 1 1 1 2]]

After classifier B voting (Centered at (5, 4))
Naive vote counting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 1 1 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 1 2 1 0 0 0 0      
0 0 0 1 1 0 0 0 0 0      1 1 1 0 1 0 1 0 0 0      0 0 0 1 0 1 0 1 0 0      
0 1 1 0 1 1 0 1 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 2 0 0 1 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 1 1 1 0 0      1 0 0 1 1 0 0 0 0 0      
0 0 0 0 0 1 0 0 0 0      0 0 0 1 0 0 0 1 0 0      0 0 0 0 1 0 1 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 1 1 1 1 0 0 0      0 0 0 0 0 0 0 1 0 0      

Swizzled vote counting
Class 0                  Class 1                  Class 2                  
0 0 0 0 0 0 0 0 0 0      1 1 0 0 1 0 0 0 0 0      0 0 1 1 0 0 0 0 0 0      
1 0 1 0 0 0 0 0 0 0      0 0 0 0 1 0 0 0 0 0      0 1 0 1 0 0 0 0 0 0      
1 0 0 1 0 0 1 1 0 0      0 0 0 0 0 0 0 0 0 0      0 1 1 1 2 1 0 0 0 0      
0 0 0 1 1 0 0 0 0 0      1 1 1 0 1 0 1 0 0 0      0 0 0 1 0 1 0 1 0 0      
0 1 1 0 1 1 0 1 0 0      1 0 0 0 1 0 0 0 0 0      0 0 0 2 0 0 1 0 0 0      
0 0 0 0 1 0 0 0 0 0      0 1 1 1 0 1 1 1 0 0      1 0 0 1 1 0 0 0 0 0      
0 0 0 0 0 1 0 0 0 0      0 0 0 1 0 0 0 1 0 0      0 0 0 0 1 0 1 0 0 0      
0 0 0 0 0 0 0 0 0 0      0 0 0 1 1 1 1 0 0 0      0 0 0 0 0 0 0 1 0 0      

Naive classifications
[[1 1 2 2 1 0 0 0 0 0]
 [0 2 0 2 1 0 0 0 0 0]
 [0 2 2 0 2 2 0 0 0 0]
 [1 1 1 0 0 2 1 2 0 0]
 [1 0 0 2 0 0 2 0 0 0]
 [2 1 1 1 0 1 1 1 0 0]
 [0 0 0 1 2 0 2 1 0 0]
 [0 0 0 1 1 1 1 2 0 0]]

Swizzled classifications
[[1 1 2 2 1 0 0 0 0 0]
 [0 2 0 2 1 0 0 0 0 0]
 [0 2 2 0 2 2 0 0 0 0]
 [1 1 1 0 0 2 1 2 0 0]
 [1 0 0 2 0 0 2 0 0 0]
 [2 1 1 1 0 1 1 1 0 0]
 [0 0 0 1 2 0 2 1 0 0]
 [0 0 0 1 1 1 1 2 0 0]]

0 个答案:

没有答案