我如何正确处理多维 numpy 数组

how do i correctly handle a multi dimensional numpy array

我是一个 Python 新手,在 for 循环中使用多维数组有点吃力。我有的是:

CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
    "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
    "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
    "sofa", "train", "tvmonitor"]
...
...
idxs = np.argsort(preds[0])[::-1][:5]
    print(idxs)
    #loop over top 5 predictions & display them
    for (i, idx) in enumerate(idxs):
        # draw the top prediction on the input image
        print (idx)
        if i == 0:
            print (preds)
            text = "Label: {}, {:.2f}%".format(CLASSES[idx], preds[0][idx] * 100)

            cv2.putText(frame, text, (5, 25),  cv2.FONT_HERSHEY_SIMPLEX,
                0.7, (0, 0, 255), 2)

        # display the predicted label + associated probability to the
        # console   
        print("[INFO] {}. label: {}, probability: {:.5}".format(i + 1,CLASSES[idx], preds[0][idx]))

我得到类似的东西:

[[[ 0.          7.          0.3361728   0.2269333   0.6589312
    0.70067763  0.8960621 ]
  [ 0.         15.          0.44955394  0.5509065   0.4315516
    0.6530549   0.7223625 ]]]
[[[0 3 2 4 5 6 1]
  [0 4 2 3 5 6 1]]]
[[0 3 2 4 5 6 1]
 [0 4 2 3 5 6 1]]
[[[[ 0.          7.          0.3361728   0.2269333   0.6589312
     0.70067763  0.8960621 ]
   [ 0.         15.          0.44955394  0.5509065   0.4315516
     0.6530549   0.7223625 ]]]]
Traceback (most recent call last):
  File "real_time_object_detection.py", line 80, in <module>
    text = "Label: {}, {:.2f}%".format(CLASSES[idx], preds[0][idx] * 100)
TypeError: only integer scalar arrays can be converted to a scalar index

我从 https://www.pyimagesearch.com/2017/08/21/deep-learning-with-opencv/ 复制了这段代码,但看起来我做错了什么,因为 idx 应该是一个 int 而不是一个数组

更新:

我试图弄清楚这里发生了什么,但我遇到了以下问题:为什么所有 argsort 调用都给出相同的结果? :o

>>> preds[0] = [[[ 0.,          7.,          0.3361728,   0.2269333,   0.6589312,0.70067763,  0.8960621 ],[ 0.,         15.,          0.44955394,  0.5509065,   0.4315516,0.6530549,   0.7223625 ]]]
>>> print(preds[0])
[[[0.0, 7.0, 0.3361728, 0.2269333, 0.6589312, 0.70067763, 0.8960621], [0.0, 15.0, 0.44955394, 0.5509065, 0.4315516, 0.6530549, 0.7223625]]]
>>> import numpy as np
>>> np.argsort(preds[0])
array([[[0, 3, 2, 4, 5, 6, 1],
        [0, 4, 2, 3, 5, 6, 1]]])
>>> np.argsort(preds[0])[::-1]
array([[[0, 3, 2, 4, 5, 6, 1],
        [0, 4, 2, 3, 5, 6, 1]]])
>>> np.argsort(preds[0])[::-1][:5]
array([[[0, 3, 2, 4, 5, 6, 1],
        [0, 4, 2, 3, 5, 6, 1]]])

另外为什么它似乎改变了数据,它不应该只是排序吗?

你的preds[0],赋值给变量名的是一个3d数组:

In [449]: preds0 = np.array([[[ 0.,          7.,          0.3361728,   0.2269333
     ...: ,   0.6589312,0.70067763,  0.8960621 ],[ 0.,         15.,          0.4
     ...: 4955394,  0.5509065,   0.4315516,0.6530549,   0.7223625 ]]])
In [450]: preds0.shape
Out[450]: (1, 2, 7)

argsort 应用于那个是相同形状的数组:

In [451]: np.argsort(preds0)
Out[451]: 
array([[[0, 3, 2, 4, 5, 6, 1],
        [0, 4, 2, 3, 5, 6, 1]]])
In [452]: _.shape
Out[452]: (1, 2, 7)

对于大小为 1 的初始维度,该维度上的反转或切片量不会产生影响。我怀疑你想反转并切片最后一个维度,大小为 7 的维度。但是,要小心。多维数组的 argsort,即使应用于一维(默认的最后一个)也是一件很难理解和使用的事情。

形状与数组匹配,但取值范围为0-6,即最后一维。 numpy 1.15 添加了几个函数,以便更容易地使用 argsort(和其他一些函数)的结果:

In [455]: np.take_along_axis(preds0, Out[451], axis=-1)
Out[455]: 
array([[[ 0.        ,  0.2269333 ,  0.3361728 ,  0.6589312 ,
          0.70067763,  0.8960621 ,  7.        ],
        [ 0.        ,  0.4315516 ,  0.44955394,  0.5509065 ,
          0.6530549 ,  0.7223625 , 15.        ]]])

请注意,行现在已排序,与 np.sort(preds0, axis=-1) 生成的相同。

我可以选择一个 'row' 索引数组:

In [459]: idxs = Out[451]
In [461]: idx = idxs[0,0]
In [462]: idx
Out[462]: array([0, 3, 2, 4, 5, 6, 1])
In [463]: idx[::-1]               # reverse
Out[463]: array([1, 6, 5, 4, 2, 3, 0])
In [464]: idx[::-1][:5]           # select
Out[464]: array([1, 6, 5, 4, 2])
In [465]: preds0[0,0,Out[464]]
Out[465]: array([7.        , 0.8960621 , 0.70067763, 0.6589312 , 0.3361728 ])

现在我有 preds0[0,0,:] 的五个最大值,倒序排列。

并对整个 preds0 数组执行此操作:

np.take_along_axis(preds0, idxs[:,:,::-1][:,:,:5], axis=-1)

或早期版本:

preds0[[0], [[0],[1]], idxs[:,:,::-1][:,:,:5]]