caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Extract Features from fc7

Open afshindn opened this issue 12 years ago • 5 comments

Hi

I have two questions.

  1. 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.
  2. 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

afshindn avatar Jul 15 '14 15:07 afshindn

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/

sergeyk avatar Jul 15 '14 16:07 sergeyk

Hi sergey,

I implemented you suggestion, but i get the error:

Traceback (most recent call last): File "2.py", line 44, in main(sys.argv) File "2.py", line 36, in main predictions = out['fc7'].squeeze(axis=(2,3)) ValueError: 'axis' entry 2 is out of bounds [-1, 1)

Do you have a guess why is this the case?

Thank you!

kadarakos avatar Mar 16 '15 10:03 kadarakos

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.

seanbell avatar Mar 16 '15 17:03 seanbell

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 main(sys.argv) File "python/classify.py", line 110, in main channel_swap=channel_swap) File "/home/anurika/Downloads/caffe-master-n/python/caffe/classifier.py", line 34, in init self.transformer.set_mean(in_, mean) File "/home/anurika/Downloads/caffe-master-n/python/caffe/io.py", line 255, in set_mean raise ValueError('Mean shape incompatible with input shape.') ValueError: Mean shape incompatible with input shape.

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 avatar Jul 01 '15 09:07 anurikadisha

@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)

xksteven avatar Jul 27 '15 15:07 xksteven