http://blog.csdn.net/guoyilin/article/details/42886365 上文是使用修改deploy.prototxt文件的input_dim为我们想要的图片的张数,因为net.blobs每当forward执行完毕一次batch, 都会更新网络的blobs数据。本文介绍在不修改deploy.prototxt情况下,更方便抽取图片特征。
首先,修改python/caffe/classifier.py, 修改predict方法
out = self.forward_all(blobs=['fc7'], **{self.inputs[0]: caffe_in}) predictions = out[self.outputs[0]].squeeze(axis=(2,3)) fc7 = out['fc7'].squeeze(axis=(2,3)) return fc7重新编译下caffe的python:
cd build
make pycaffe
然后copy 编译好的包到源码路径下:
cp build/python/libpycaffe.so python/caffe/_caffe.so && \ mkdir -p python/caffe/proto && \ touch python/caffe/proto/__init__.py && \ protoc --proto_path=src/caffe/proto --python_out=python/caffe/proto src/caffe/proto/caffe.proto && \ protoc --proto_path=src/caffe/proto --python_out=python/caffe/proto src/caffe/proto/caffe_pretty_print.proto主程序文件调用如下:
features = net.predict(imageList, oversample=False) for feature in features: print feature #feature是4096维度