Extract Features from fc7
Hi
I have two questions.
- The tutorial provided (http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb) to extract features from different layers does not seem to be an efficient way of doing it. It takes around 0.5 second to extract features. Is there a way to speed it up or process images in batch mode? I assume it should take only a couple ms on a K40.
- I tried to use the binary provided in builds/tools/extract_features.bin which seems to have batch model built in. But the features are save in leveldb format and I don't know how to read that in python?
Thanks
Hi Afshin,
Take a look at python/classify.py, which can process a whole directory of images (python python/classify.py examples/images temp will process all images in examples/images/*.jpg and output predictions to temp.npy.
You'll want to output fc7 instead of (or in additional to) probabilities. Change L78 in classifier.py to out = self.forward_all(**{self.inputs[0]: caffe_in, 'blobs': ['fc7']}). Now you can get the fc7 feature vector via out['fc7'].squeeze(axis=(2,3)). Adjust the return values as appropriate in classifier.py and save what you need in classify.py. Looking at what oversample does will help you get up to speed with the Python wrapper. It is kind of confusing, and if you develop a simpler system or script, we'll be happy to look at the PR!
Reading LMDB data from Python is very easy: http://lmdb.readthedocs.org/en/release/
Hi sergey,
I implemented you suggestion, but i get the error:
Traceback (most recent call last):
File "2.py", line 44, in
Do you have a guess why is this the case?
Thank you!
Blobs are now general N-D and not 4D. So the "squeeze" is no longer necessary. See #1970.
You can try debugging with print out['fc7'].shape to see the new dimensions.
Hi,
I ran python python/classify.py examples/images temp from the caffe root directory but get the following error:
Traceback (most recent call last):
File "python/classify.py", line 138, in
My images folder has the default images that come when caffe is downloaded and I downloaded the mean islrvc12 mean file from net. Can anyone please help?
@anurikadisha As shown here #2075 This can be fixed by changing: mean=np.load(mean_file) to this: mean=np.load(mean_file).mean(1).mean(1)