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

时间:2019-01-14 03:28:16

标签: python arrays python-3.x numpy string-formatting

我是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]]])

加上为什么它似乎会更改数据,而不是仅对数据进行排序?

1 个答案:

答案 0 :(得分: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)产生的行相同。

我可以选择索引数组的一个“行”:

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]]