C++ Prediction Example With Caffe Deep Learning

In this example, we will using cifar10_quick_iter_5000.caffemodel as pre-trained model. And modified cifar10_quick_train_test.prototxt to get argmax for prediction.
Step1:Download the example from Github
Step2:Copy files to caffe master directory.
Copy files to your caffe master directory. keep the same directories and subdirectories.

Step3:Compile Example.
We assume you are ine the caffe master directory, just use make to compile our example.
The caffe makefile will automatic find the new example and copy the result to build directory.
    caffe-master ikki$ ls ./build/tools/prediction_example*

Just run the example.
    I0120 11:02:45.297869 2008834832 prediction_example.cpp:52] Result size: 3
    I0120 11:02:45.297899 2008834832 prediction_example.cpp:55] Blob size: 0
    I0120 11:02:45.297905 2008834832 prediction_example.cpp:58] -------------
    I0120 11:02:45.297910 2008834832 prediction_example.cpp:59]  prediction :  
    I0120 11:02:45.297919 2008834832 prediction_example.cpp:69] ------------------------------------------------------
    I0120 11:02:45.297924 2008834832 prediction_example.cpp:73] Pattern:0 class:5 Prob=2.69744e-06
    I0120 11:02:45.297940 2008834832 prediction_example.cpp:73] Pattern:1 class:3 Prob=4.0121e-05
    I0120 11:02:45.297948 2008834832 prediction_example.cpp:73] Pattern:2 class:9 Prob=0.00531694
    I0120 11:02:45.297956 2008834832 prediction_example.cpp:75] -------------

For run on GPU device.
    ./build/tools/prediction_example GPU 0

Here are the classes in the CIFAR-10 dataset:
    class:0 airplane
    class:1 automobile
    class:2 bird
    class:3 cat
    class:4 deer
    class:5 dog
    class:6 frog
    class:7 horse
    class:8 ship
    class:9 truck


  1. hi it worked pretty good with me can you tell a little bit about the prediction_example.prototxt
    how to get it for a another example letsay imagenet

    1. this page explain the layers.

  2. I tried to compile with no luck, I'm not sure what is wrong?

    /bin/sh: 1: bc: not found
    CXX tools/prediction_example.cpp
    tools/prediction_example.cpp: In function ‘int main(int, char**)’:
    tools/prediction_example.cpp:40:2: error: ‘set_phase’ is not a member of ‘caffe::Caffe’
    tools/prediction_example.cpp:40:19: error: ‘TEST’ is not a member of ‘caffe::Caffe’
    tools/prediction_example.cpp:44:76: error: no matching function for call to ‘caffe::Net::Net(const char [58])’
    Net net("./examples/prediction_example/prediction_example.prototxt");
    tools/prediction_example.cpp:44:76: note: candidates are:
    In file included from ./include/caffe/blob.hpp:4:0,
    from ./include/caffe/caffe.hpp:7,
    from tools/prediction_example.cpp:8:
    ./include/caffe/net.hpp:259:27: note: caffe::Net::Net(const caffe::Net&) [with Dtype = float]
    ./include/caffe/common.hpp:32:3: note: in definition of macro ‘DISABLE_COPY_AND_ASSIGN’
    classname(const classname&);\
    ./include/caffe/net.hpp:259:27: note: no known conversion for argument 1 from ‘const char [58]’ to ‘const caffe::Net&’
    ./include/caffe/common.hpp:32:3: note: in definition of macro ‘DISABLE_COPY_AND_ASSIGN’
    classname(const classname&);\
    In file included from ./include/caffe/caffe.hpp:12:0,
    from tools/prediction_example.cpp:8:
    ./include/caffe/net.hpp:27:12: note: caffe::Net::Net(const string&, caffe::Phase) [with Dtype = float; std::string = std::basic_string]
    explicit Net(const string& param_file, Phase phase);
    ./include/caffe/net.hpp:27:12: note: candidate expects 2 arguments, 1 provided
    ./include/caffe/net.hpp:26:12: note: caffe::Net::Net(const caffe::NetParameter&) [with Dtype = float]
    explicit Net(const NetParameter& param);
    ./include/caffe/net.hpp:26:12: note: no known conversion for argument 1 from ‘const char [58]’ to ‘const caffe::NetParameter&’
    make: *** [.build_release/tools/prediction_example.o] Error 1

    1. Same thing for me... :(

    2. "Caffe::set_phase(Caffe::TEST);" set_phase() is no longer member of Caffe. You have to specify TEST phase in Net constructor like this Net("... .prototxt", Caffe::TEST);

  3. I had the same problem, but fixed it by:

    1. Commenting out Caffe::set_phase(Caffe::TEST); in line 40 of prediction_example.cpp
    2. Adding the test phase argument to the creation of the net, so Net net("./examples/prediction_example/prediction_example.prototxt"); became et net("./examples/prediction_example/prediction_example.prototxt", caffe::test); in line 44.

    The stated probabilities seem really small. Can anyone make sense of those? Is there a mising normalization somewhere?

  4. This comment has been removed by the author.

  5. Hi,

    I can't find the mean file at "./examples/cifar10/mean.binaryproto" in Git repo.

    1. You need to first down and convert cifar10 dataset. Use the following command:


      And then you will get the ./examples/cifar10/mean.binaryproto file

  6. Please note that test phase argument is written with capital letters. So, replace "caffe::test" with "caffe::TEST"

  7. there is no make file in downloaded folder, new to caffe, please help.

  8. Can this adapt to HDF5 data type?

  9. How to do that in windows ?

  10. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

  11. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.

    angularjs-Training in velachery

    angularjs-Training in annanagar

    angularjs Training in chennai

    angularjs Training in chennai

  12. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
    Python training in marathahalli | Python training institute in pune

  13. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    advanced excel training in bangalore

  14. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

  15. From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.

    Java training in Tambaram | Java training in Velachery

    Java training in Omr | Oracle training in Chennai

  16. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

    DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.

    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

  17. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    machine learning training in chennai
    best training insitute for machine learning
    machine learning course in Chennai

  18. Thank you for sharing your article. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.

    best openstack training in chennai | openstack course fees in chennai | openstack certification in chennai | openstack training in chennai velachery