读取 Caffe 向量提取的特征,在 python 中重塑向量

Reading Caffe vector extracted features, reshape vector in python

我已经使用 Caffe 的教程成功地从一组图像中提取特征。然后,我使用这段代码将以 leveldb 格式存储在 features 文件中的结果转换为 npy 文件:

input_path='../examples/images/'
path='../examples/test/npy/'
files = os.listdir(input_path)

db = leveldb.LevelDB('../examples/_temp/features')

for k in range(len(files)): 
        datum = caffe_pb2.Datum.FromString(db.Get(str(k)))
        arr = caffe.io.datum_to_array(datum)
        file=files[k]
       out=np.save(path+file[0:len(file)-4],arr)

但是每张图片提取出来的结果用vector arr表示的一定是4096-dim的vector,我的情况不是这样的如下图

>>> arr
array([[[ 0.    ],
        [ 0.    ],
        [ 0.    ],
        ...,
        [ 0.    ],
        [ 0.    ],
        [ 0.199 ]]])
>>> arr.shape
(1, 4096, 1)

我不熟悉python。我需要在 npy 文件中将该数组保存为 4096-dim,以便将其作为一行进一步存储在 csv 文件中,供 matlab 读取。
如何将向量 arr 转换为 4096-dim?

提前感谢您的帮助

如果我没理解错的话,你希望 arrshape 是 4096 dim,而不是 (1, 4096, 1)?
在这种情况下,您可能需要使用 flatten:

arr.flatten()

arr 转换为 1D。