这是this的后续问题。基本上,我想做的是简单地从平均值中减去每一幅图像。
基于GitHub和this other similar question上的这个问题,以及当我们将图像的裁剪版本提供给网络时的this classification example,我们需要使用如下内容减去平均像素:
mu = mean_file.mean(1).mean(1)但讽刺的是,当我想这么做的时候:
.. = (img[:,0:224,0:224] - mu)我得到了ValueError: operands could not be broadcast together with shapes (3,224,224) (3,)。我不太精通Python和Numpy,也不知道这个错误消息想要传达什么。
目前,我正在裁剪均值文件,这并不理想,但总比没有好。
.. = (img[:,0:224,0:224] - mean_image[:,0:224,0:224])发布于 2017-07-25 23:26:43
替换
mu = mean_file.mean(1).mean(1)使用
mu = mean_file.mean(1).mean(1)[:,None,None]看起来你正在尝试从一个3D数组中减去一个一维向量(shape of (3,)) (shape of (3,224,224))。为此,numpy需要将一维向量broadcast到3D数组的维度中(很像Matlab的bsxfun)。为了帮助numpy了解要广播的维度,需要将单个维度添加到1D向量中:
mu[:,None,None]现在的形状是(3,1,1),这应该使numpy能够正确地执行减法。
发布于 2017-07-26 22:42:40
我注意到,由于我正在读取lmdb上的图像,为了让一切正常工作,我必须执行以下操作:
img = np.array(img.transpose(2,1,0),dtype=np.float32)
img -= mean_image[0].mean(1).mean(1)
#transpose back to the original state
img = img.transpose(2,1,0) https://stackoverflow.com/questions/45307525
复制相似问题