计算中的不一致 - Python & numpy



我试图运行的代码是:

import numpy as np
x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]],        dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)
labels = x.copy()
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 1] = ratio[0] * w * (x[0][:, 1] - x[0][:, 3] / 2) + pad[0]  
labels[0][:, 2] = ratio[1] * h * (x[0][:, 2] - x[0][:, 4] / 2) + pad[1]  
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])

输出为:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.6659]
[260.5374]
labels after computation [[0.0000000e+00 4.0355487e+02 2.3036258e+02 2.5829467e+05 9.8256547e+04]]

由2nd&第三种说法是正确的。但是,当我试图将相同计算输出的值分配给标签[0][:,3]&标签[0][:,4],正在分配一些垃圾值。

如果我删除第9&10&运行以下代码,输出正常。

import numpy as np
x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]], dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)
labels = x.copy()
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])

输出:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.6659]
[260.5374]
labels after computation [[0.000000e+00 6.611100e-01 3.250000e-01 4.426659e+02 2.605374e+02]]

有人能解释一下前后矛盾的原因吗?

我的环境:
Python版本:3.6.9
Numpy版本:1.19.4

谢谢!

使用

labels = x.copy()

您只创建了x的浅拷贝,它的元素是数组,因此更改labels[0][:, 1]labels[0][:, 2]会更改x中的相同字段

还要检查copy功能的文档:

请注意,np.copy是一个浅拷贝,不会复制数组中的对象元素。这对于包含Python对象的数组非常重要。新数组将包含相同的对象,如果该对象可以修改(是可变的(,则可能会导致意外:

和解决方案:

要确保复制对象数组中的所有元素,请使用copy.depcopy:

在您的情况下:

import numpy as np
x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]],        dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)
import copy
labels = copy.deepcopy(x)
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 1] = ratio[0] * w * (x[0][:, 1] - x[0][:, 3] / 2) + pad[0]
labels[0][:, 2] = ratio[1] * h * (x[0][:, 2] - x[0][:, 4] / 2) + pad[1]
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])

输出:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.66592]
[260.537429]
labels after computation [[  0.       403.55488  230.362571 442.66592  260.537429]]

最新更新