caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Extract features - having trouble with input specification

Open swamiviv opened this issue 11 years ago • 13 comments

For extracting features, I am using the C++ utility as shown here:

http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html

For my purpose, I have a million images whose features I want to extract. I want to do them in batches of 1000. Now, in the network prototxt file, I have another batch_size parameter, what should I set this to ? What I am observing when I run the code is, even though I have a million images and the corresponding leveldb files, when I set the num_mini_batch parameter in the extract_features input to 500, I get features extracted only for 500 images. This doesn't make sense to me. How do I then extract features for 1M images ?

swamiviv avatar Jun 20 '15 15:06 swamiviv

The batch_size in the net prototxt is how many images are in each mini-batch. The num_mini_batch parameter given to extract_features specifies how many minibatches. So if you are getting features for 500 images out when you specify num_mini_batch to be 500, it sounds like your batch_size is set to 1. Set them both to 1000 and you should get features for 1M images.

jyegerlehner avatar Jun 21 '15 16:06 jyegerlehner

That is what one would expect. But when I deploy the network with different batch sizes for the test case, the data getting written as lmdb files does not seem to depend on the batch size specified in the net prototxt file at all. For eg, this is the command I run for extracting features:

$TOOLS/extract_features.bin <model> <net_prototxt> <blob_name> <output_dir> 1000 lmdb

Given this I vary the batchsizes from 10-100 in the <net_prototxt> file, but the number of outputs getting written is 1000 everytime. Why should this be so ?

swamiviv avatar Jun 21 '15 18:06 swamiviv

Hmm. I'm not seeing that behavior, at least with a recent master branch build. I ran a script of mine that extracts features with batch_size set to 100, and num_mini_batches to 97, and got the expected 9700 images extracted:

E0621 17:20:14.207777  4268 extract_features.cpp:72] Using GPU
E0621 17:20:14.207990  4268 extract_features.cpp:73] Using Device_id=1
E0621 17:20:15.140331  4268 extract_features.cpp:148] Extacting Features
E0621 17:20:23.098330  4268 extract_features.cpp:183] Extracted features of 1000 query images for feature blob encode1_conv
E0621 17:20:24.880564  4268 extract_features.cpp:183] Extracted features of 1000 query images for feature blob middle1_deconv
E0621 17:20:32.786949  4268 extract_features.cpp:183] Extracted features of 2000 query images for feature blob encode1_conv
E0621 17:20:34.539641  4268 extract_features.cpp:183] Extracted features of 2000 query images for feature blob middle1_deconv
E0621 17:20:42.453361  4268 extract_features.cpp:183] Extracted features of 3000 query images for feature blob encode1_conv
E0621 17:20:44.246687  4268 extract_features.cpp:183] Extracted features of 3000 query images for feature blob middle1_deconv
E0621 17:20:52.123885  4268 extract_features.cpp:183] Extracted features of 4000 query images for feature blob encode1_conv
E0621 17:20:53.890521  4268 extract_features.cpp:183] Extracted features of 4000 query images for feature blob middle1_deconv
E0621 17:21:01.890260  4268 extract_features.cpp:183] Extracted features of 5000 query images for feature blob encode1_conv
E0621 17:21:03.755271  4268 extract_features.cpp:183] Extracted features of 5000 query images for feature blob middle1_deconv
E0621 17:21:11.532836  4268 extract_features.cpp:183] Extracted features of 6000 query images for feature blob encode1_conv
E0621 17:21:13.410534  4268 extract_features.cpp:183] Extracted features of 6000 query images for feature blob middle1_deconv
E0621 17:21:21.254788  4268 extract_features.cpp:183] Extracted features of 7000 query images for feature blob encode1_conv
E0621 17:21:23.065832  4268 extract_features.cpp:183] Extracted features of 7000 query images for feature blob middle1_deconv
E0621 17:21:30.876734  4268 extract_features.cpp:183] Extracted features of 8000 query images for feature blob encode1_conv
E0621 17:21:32.744978  4268 extract_features.cpp:183] Extracted features of 8000 query images for feature blob middle1_deconv
E0621 17:21:40.578064  4268 extract_features.cpp:183] Extracted features of 9000 query images for feature blob encode1_conv
E0621 17:21:42.443068  4268 extract_features.cpp:183] Extracted features of 9000 query images for feature blob middle1_deconv
E0621 17:21:47.898468  4268 extract_features.cpp:194] Extracted features of 9700 query images for feature blob encode1_conv
E0621 17:21:49.376199  4268 extract_features.cpp:194] Extracted features of 9700 query images for feature blob middle1_deconv
E0621 17:21:49.535563  4268 extract_features.cpp:199] Successfully extracted the features!

It is hard-wired to print out in increments of 1000. Could it be you're just not waiting for it to finish? Especially if you're running on CPU it could take a while.

jyegerlehner avatar Jun 21 '15 22:06 jyegerlehner

I will do a recent checkout and verify it. I am running on a tesla K-40 and I am of course waiting for it to finish. To be more sure, I am also using a utility which reads the lmdb file externally to verify what I had said earlier.

On Sun, Jun 21, 2015 at 6:29 PM, jyegerlehner [email protected] wrote:

Hmm. I'm not seeing that behavior, at least with a recent master branch. I ran a script of mine that extracts features with batch_size set to 100, and num_mini_batches to 97, and got the expected 9700 images extracted:

E0621 17:20:14.207777 4268 extract_features.cpp:72] Using GPU E0621 17:20:14.207990 4268 extract_features.cpp:73] Using Device_id=1 E0621 17:20:15.140331 4268 extract_features.cpp:148] Extacting Features E0621 17:20:23.098330 4268 extract_features.cpp:183] Extracted features of 1000 query images for feature blob encode1_conv E0621 17:20:24.880564 4268 extract_features.cpp:183] Extracted features of 1000 query images for feature blob middle1_deconv E0621 17:20:32.786949 4268 extract_features.cpp:183] Extracted features of 2000 query images for feature blob encode1_conv E0621 17:20:34.539641 4268 extract_features.cpp:183] Extracted features of 2000 query images for feature blob middle1_deconv E0621 17:20:42.453361 4268 extract_features.cpp:183] Extracted features of 3000 query images for feature blob encode1_conv E0621 17:20:44.246687 4268 extract_features.cpp:183] Extracted features of 3000 query images for feature blob middle1_deconv E0621 17:20:52.123885 4268 extract_features.cpp:183] Extracted features of 4000 query images for feature blob encode1_conv E0621 17:20:53.890521 4268 extract_features.cpp:183] Extracted features of 4000 query images for feature blob middle1_deconv E0621 17:21:01.890260 4268 extract_features.cpp:183] Extracted features of 5000 query images for feature blob encode1_conv E0621 17:21:03.755271 4268 extract_features.cpp:183] Extracted features of 5000 query images for feature blob middle1_deconv E0621 17:21:11.532836 4268 extract_features.cpp:183] Extracted features of 6000 query images for feature blob encode1_conv E0621 17:21:13.410534 4268 extract_features.cpp:183] Extracted features of 6000 query images for feature blob middle1_deconv E0621 17:21:21.254788 4268 extract_features.cpp:183] Extracted features of 7000 query images for feature blob encode1_conv E0621 17:21:23.065832 4268 extract_features.cpp:183] Extracted features of 7000 query images for feature blob middle1_deconv E0621 17:21:30.876734 4268 extract_features.cpp:183] Extracted features of 8000 query images for feature blob encode1_conv E0621 17:21:32.744978 4268 extract_features.cpp:183] Extracted features of 8000 query images for feature blob middle1_deconv E0621 17:21:40.578064 4268 extract_features.cpp:183] Extracted features of 9000 query images for feature blob encode1_conv E0621 17:21:42.443068 4268 extract_features.cpp:183] Extracted features of 9000 query images for feature blob middle1_deconv E0621 17:21:47.898468 4268 extract_features.cpp:194] Extracted features of 9700 query images for feature blob encode1_conv E0621 17:21:49.376199 4268 extract_features.cpp:194] Extracted features of 9700 query images for feature blob middle1_deconv E0621 17:21:49.535563 4268 extract_features.cpp:199] Successfully extracted the features!

It is hard-wired to print out in increments of 1000. Could it be you're just not waiting for it to finish. Especially if you're running on CPU it could take a while.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/2630#issuecomment-113964998.

--Swami

swamiviv avatar Jun 22 '15 01:06 swamiviv

Closing as this looks like a usage issue/request for help.

This tracker is reserved for specific Caffe development issues and bugs; please ask usage questions on the caffe-users list.

For more information, see our contributing guide.

Thanks!

longjon avatar Aug 02 '15 22:08 longjon

@jyegerlehner @shelhamer Hi. If I executed the same code, and the number of images were originally 900k, where does 100k ( = 1M - 900k) come from?

hyojinie avatar Oct 19 '15 22:10 hyojinie

@hyojinie Hi. I'm afraid you'll have to be more explicit about what you are doing and what you observe happening. From what you wrote, all I can say is: 100K sounds like a number you made up.

jyegerlehner avatar Oct 20 '15 01:10 jyegerlehner

@jyegerlehner Yes I made up that number because I don't think it will make much difference. Here is what I did. I had 803 image file list. My batch_size was 50 and num_mini_batches was 17. There was 850 entries in the output feature file (50*17=850). I wonder which image files correspond to these additional 47 features.

hyojinie avatar Oct 20 '15 01:10 hyojinie

@hyojinie

Sorry to reply so late to this. I was missing my notifications and didn't realize you ever replied.

I see now. Well you will always batch_size * num_mini_batches out when you extract features. It doesn't stop when it gets to the end of input database. It just wraps back to the start. So 47 or so of the images in the input were repeated at the end to get you to 850. Caveat: I didn't check the code that my statement is correct; just going from memory here.

jyegerlehner avatar Nov 10 '15 21:11 jyegerlehner

In folder examples/_temp/features, I am having following files (which are generated during the extracting features process) - 000003.log , current , LOCK , LOG , MANIFEST-000002 Out of these files, which file contains extracted features? Kindly reply as soon as possible.

amangupta199334 avatar Apr 20 '16 12:04 amangupta199334

When you extract features with extract_features, you give it the path to the database where it puts the leveldb or lmdb database. Those files sound like a level_db database. When you open a leveldb database, you don't worry about what's in each of the files unless you care about the internals of leveldb. You just tell it the directory containing the database.

jyegerlehner avatar Apr 21 '16 08:04 jyegerlehner

Can I convert this features file into vgg_feats.mat format?

amangupta199334 avatar Apr 21 '16 12:04 amangupta199334

@jyegerlehner @HanuManuOm I use the extract_features.bin to extract feature,I got the result in a leveldb file,how can I convert leveldb file into .mat format? I have tried to convert it into numpy array,but I got the wrong dimension,my network is AlexNet,I extact the features in FC7 layer,it should be 4096,but I got the result in 20487.
Thank you for your help in advance.

CHENCHAOQI avatar Dec 05 '17 06:12 CHENCHAOQI