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


14 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. 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