Pages

2015/01/20

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.
 
    caffe-master/examples/images/cat10.png
    caffe-master/examples/images/dog10.png
    caffe-master/examples/images/truck10.png
    caffe-master/examples/prediction_example/file_list.txt
    caffe-master/examples/prediction_example/prediction_example.prototxt
    caffe-master/tools/prediction_example.cpp
 


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

Step4:Prediction
Just run the example.
    ./build/tools/prediction_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


27 comments:

  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

    ReplyDelete
    Replies
    1. this page explain the layers.
      http://caffe.berkeleyvision.org/tutorial/

      Delete
  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’
    Caffe::set_phase(Caffe::TEST);
    ^
    tools/prediction_example.cpp:40:19: error: ‘TEST’ is not a member of ‘caffe::Caffe’
    Caffe::set_phase(Caffe::TEST);
    ^
    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]
    DISABLE_COPY_AND_ASSIGN(Net);
    ^
    ./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&’
    DISABLE_COPY_AND_ASSIGN(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

    ReplyDelete
    Replies
    1. Same thing for me... :(

      Delete
    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);

      Delete
  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?

    ReplyDelete
  4. Anonymous11/7/15 18:11

    This comment has been removed by the author.

    ReplyDelete
  5. Hi,

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

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

      ./data/cifar10/get_cifar10.sh
      ./examples/cifar10/create_cifar10.sh

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

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

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

    ReplyDelete
  8. Can this adapt to HDF5 data type?

    ReplyDelete
  9. How to do that in windows ?

    ReplyDelete
  10. 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

    ReplyDelete
  11. 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

    ReplyDelete
  12. 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

    ReplyDelete
  13. Hey, nice site you have here!We provide world-class Oracle certification and placement training course as i wondered Keep up the excellent work experience!Please visit Greens Technologies located at Chennai.thanks alot.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  14. Thanks for sharing a useful information.. we have learnt so much information from your blog.... oracle training in chennai

    ReplyDelete
  15. Infycle Technologies, the best software training institute in Chennai offers the best Oracle training in Chennai for students, freshers, and tech professionals. In addition to that, other in-demand courses such as Big Data, Java, Python, Power BI, Digital Marketing will be trained with 200% practical classes. Once the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo. Best Oracle Training in Chennai | Infycle Technologies

    ReplyDelete
  16. Regardless of the security break detailed last year, you actually need to search for another VPN that can do precisely exact thing NordVPN does. NordVPN Crack

    ReplyDelete
  17. Connectify Hotspot Pro break is the most recent highlighted application that used to settle the things as indicated by your longing this is most recent form that works! Thums Up Connectify Hotspot Crack

    ReplyDelete