Simple C++ interface to Caffe

1,119 views
Skip to first unread message

Thomas Wood

unread,
Feb 2, 2016, 6:30:21 AM2/2/16
to Caffe Users
Hi

I am running this Python code:

import caffe
net = caffe.Classifier('mymodelfile.prototxt', 'mypretrainedfile.caffemodel', image_dims=(224, 224), raw_scale=255)
img = caffe.io.load_image('myface.png', color=False) - [0.504633984375, 0.409228125, 0.3656015625] # subtract mean image
score = net.predict([img], oversample=False)
print score

this works perfectly. So all I'm doing is loading a model and executing it on 1 image.

Now I would like to run the same code in C++. Is there a simple snippet of C++ that calls the equivalent high level Caffe methods? I have looked in the repository and I can only find quite complex C++ examples which go very low level. For example, no equivalent of net.predict.
I would be grateful for any advice!
Thanks a lot!

Jan C Peters

unread,
Feb 2, 2016, 7:20:20 AM2/2/16
to Caffe Users
Well, there is the https://github.com/BVLC/caffe/tree/master/examples/cpp_classification example. You probably won't find anything higher level, because that is all the caffe library gives you. And the "high-level" is usually heavily dependent on what you actually want to do, so this is a good thing. But it is not that hard. Basically you just need to understand how to handle the caffe::Net class, which is actually quite similar to the usage of caffe.Net in python.

If everything in C++ would be as nice and simple and high-level as it is in python, there would be no need/use for python :-). That is why I only use C++ for production code or things that really need to be _fast_. Everything else in python.

Jan

P.S.: I doubt that doing what you do (feedforward of individual samples) is significantly faster in C++ than it is in python, since pycaffe is just a binding to the same C++ caffe library, which does the actual work in this case.

Thomas Wood

unread,
Feb 2, 2016, 7:52:41 AM2/2/16
to Caffe Users
Hi Jan,
Thanks for this. OK, I will work from the example you posted.
Tom

Felix Abecassis

unread,
Feb 2, 2016, 4:30:31 PM2/2/16
to Caffe Users
Actually, it should be possible to have a slightly simpler C++ example if you use MemoryDataLayer. You won't have to do the image preprocessing.
But when writing the cpp_classification example I decided to be a bit more low level to show how to directly manipulate the input layer of the network.

Andrew Tulloch

unread,
Feb 4, 2016, 6:05:58 PM2/4/16
to Caffe Users
You might find https://github.com/facebook/fb-caffe-exts/#predictor useful, your demonstration there is something like:
predictor_ = folly::make_unique<caffe::fb::Predictor>(FLAGS_prototxt_path,
                                                      FLAGS_weights_path);
const auto& output_blobs = predictor_->forward({&input_blob});
return output_blobs[FLAGS_output_layer_name];

Reply all
Reply to author
Forward
0 new messages