在阅读 Caffe 教程(http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb)时,我遇到了以下语句:
net.blobs['data'].data[...] = transformer.preprocess('data',
caffe.io.load_image
(caffe_root + 'examples/images/cat.jpg'))
它基本上用于将单个图像分配给net.blobs['data'].data
.
net.blobs['data'].data[...]
是一个 4D ndarray 并transformer...
返回一个 3D ndarray,因此省略号用于在第 0 轴上复制 3D 数组。这让我觉得我应该能够重写代码以避免省略号,如下所示:
z3=transformer.preprocess('data',
caffe.io.load_image
(caffe_root + 'examples/images/cat.jpg'))
z4 = z3[np.newaxis,...]
net.blobs['data'].data = z4
但是,当我这样做时,我得到
>> net.blobs['data'].data = z4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
虽然,
net.blobs['data'].data[...] = z3
工作正常。这对任何人都有意义吗?
我已经验证了我的变量的形状和类型,如下所示:
>>> print net.blobs['data'].data.shape, z3.shape, z4.shape
(1, 3, 227, 227) (3, 227, 227) (1, 3, 227, 227)
>>> print type(net.blobs['data'].data),type(z3),type(z4)
<type 'numpy.ndarray'> <type 'numpy.ndarray'> <type 'numpy.ndarray'>
为什么会net.blobs['data'].data = z4
导致问题?