C++ extracted features are a lot worse than python and matlab wrapper
Hi,
I am using caffe to extract CNN features from pretrained model by imagenet. Everything goes smooth. But I find a problem with C++ code that, if I use C++ code to do the feature extraction as instructed in http://caffe.berkeleyvision.org/gathered/examples/feature_extraction.html, the features are a lot different than features extracted by python and matlab wrappers( these two wrappers give same results!) and the C++ extracted features have a lot lower accuracy on my test samples...
Do you have any comments or suggestions? many thanks!
How are you doing the comparison? for example in python interface, as in this example (http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb) you can select different crops to examine the features. But the feature_extraction.bin I believe just uses the center crop. To others who may read this, can anyone confirm this is true (e.g. feature_extraction.bin uses center crop)? If it does, then you need to be sure you are comparing center crop to center crop features from the python and C++ interface.
Hi wangboyunze, In addition to MGH1's comment the thing I suggest is: did you observe if tools/extract_features.bin exited with success? I ran into a scenario where it failed due to insuffiient memory. The leveldb databases still got created, but some were empty. I submitted a fix in 1175. It would be easy to miss. I don't know if the empty database would generate some other error or go undetected. Just a thought.
thanks for your reply!
The C++ extracted features exit with success.
What I did is simple, first I run the command line to get the features stored in levelDB format.
Then I wrote a python script to extract the features:
- db = leveldb.LevelDB(dbName)*
# get db iterator
it = db.RangeIter() f = open(filename, 'w'); count = 0; for key,value in it:
-
convert string to datum*
- datum = caffe_pb2.Datum.FromString(db.Get(key))*
-
convert datum to numpy string*
- arr = caffe.io.datum_to_array(datum)[0]*
I think the script is correct. Has anyone used python to extract the features from LevelDB?
On Sun, Sep 28, 2014 at 7:14 PM, jyegerlehner [email protected] wrote:
Hi wangboyunze, In addition to MGH1's comment the thing I suggest is: did you observe if tools/extract_features.bin exited with success? I ran into a scenario where it failed due to insuffiient memory. The leveldb databases still got created, but some were empty. I submitted a fix in 1175 https://github.com/BVLC/caffe/pull/1175. It would be easy to miss. I don't know if the empty database would generate some other error or go undetected. Just a thought.
— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1158#issuecomment-57104138.
I did and my code is similar. On the other hand, I reported a problem in the mailing-list. I forgot to uptade that my problem is related with the dumping multiple layers at once. If you dump the layers independently (running feature_extraction per each layer), you will get the right feature dimension at least. I apologize, if this info is not of your interest ;)
hi, Victor:
thanks for your reply!
Is it possible that you can share your python code if you had
successful experience? I extracted features from matlab, and test it on my database, it got 80% accuracy.If I used python to extract features from leveldb, I only got 10% accuracy. It must be some stupid bug/error but I just couldnot figure it out... Thanks! I think your code would be very helpful to those who have same trouble with me.
On Tue, Sep 30, 2014 at 2:23 AM, Victor Escorcia Castillo < [email protected]> wrote:
I did and my code is similar. On the other hand, I reported a problem in the mailing-list https://groups.google.com/forum/#!topic/caffe-users/UHzU--8khRc. I forgot to uptade that the problem that I mentioned is related with the dumping multiple layers at once. If you dump the layers independently (running feature_extraction per each layer), you will get the right feature dimension at least. I apologize, if this info is not of your interest ;)
— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1158#issuecomment-57271960.
The problem is most likely caused by re-ordering of training/test examples since the db.RangeIter() iterates over keys in alphabetical order while extract_features creates keys from index values without leading zeros (unlike convert_imageset). Hence, you get an order like 0, 1, 10, 100, ...
Parse the key value in python and put the extracted feature vector at that position.
hi, Maskim:
Thanks! I think you are right that the C++ used a different order. I
have fixed this problem used the workaround in python!
Many Thanks!
On Wed, Oct 8, 2014 at 2:37 PM, Maksim Lapin [email protected] wrote:
The problem is most likely caused by re-ordering of training/test examples since the db.RangeIter() iterates over keys in alphabetical order while extract_features creates keys from index values without leading zeros (unlike convert_imageset). Hence, you get an order like 0, 1, 10, 100, ...
Workaround: parse the key value in python and put the extracted feature vector at that position.
Solution: see discussion in PR #1175 https://github.com/BVLC/caffe/pull/1175.
— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/1158#issuecomment-58405497.
sorry for the late reply. For those interested on the python code that I used, I have to say that I made something 90% similar to this guy.
db = leveldb.LevelDB(dirname)
for key, val in db.RangeIter():
datum = caffe.io.caffe_pb2.Datum()
datum.ParseFromString(val)
arr = caffe.io.datum_to_array(datum)
As mlapin said before, take care of the value of key.
Hi @escorciav I tried to run your script to extract the features from lmdb format. However I think I am making some mistake because I dont go into the for loop of your script. What should I give as an input instead of dirname in db = leveldb.LevelDB(dirname). I have two mdb files 'data.mdb' and 'lock.mdb' in 'features' folder. Currently I ran the following code:
import leveldb
import caffe
from caffe.proto import caffe_pb2
db = leveldb.LevelDB('features')
for key, val in db.RangeIter():
datum = caffe.io.caffe_pb2.Datum()
datum.ParseFromString(val)
arr = caffe.io.datum_to_array(datum)
Can someone please help?
features_path = os.path.join(root_path, 'vgg_feats.mat') features_struct = scipy.io.loadmat(features_path) Can you please give me equivalent code for this in leveldb? I do not have vgg features, I have leveldb features. i.e. MANIFEST-000002 and I want to use it in my python code. Please help me as soon as possible.
Please ask usage questions on the mailing list. Reproducing results across C++, pycaffe, and matcaffe can take some care as the different interfaces have different local conventions as expected by the respective communities.
From https://github.com/BVLC/caffe/blob/master/CONTRIBUTING.md:
Please do not post usage, installation, or modeling questions, or other requests for help to Issues. Use the caffe-users list instead. This helps developers maintain a clear, uncluttered, and efficient view of the state of Caffe.
Hello, i have issue in python feature extraction. i extract features with matlab and train my model. now i am testing it in python when i extract features it is different from feature extracted using matlab Need your help