Caffe, at its core, is written in C++. It is possible to use the C++
API of Caffe to implement an image classification application similar
to the Python code presented in one of the Notebook examples. To look
at a more general-purpose example of the Caffe C++ API, you should
study the source code of the command line tool
A simple C++ code is proposed in
examples/cpp_classification/classification.cpp. For the sake of
simplicity, this example does not support oversampling of a single
sample nor batching of multiple independent samples. This example is
not trying to reach the maximum possible classification throughput on
a system, but special care was given to avoid unnecessary
pessimization while keeping the code readable.
The C++ example is built automatically when compiling Caffe. To
compile Caffe you should follow the documented instructions. The
classification example will be built as
in your build directory.
To use the pre-trained CaffeNet model with the classification example,
you need to download it from the “Model Zoo” using the following
The ImageNet labels file (also called the synset file) is also
required in order to map a prediction to the name of the class:
Using the files that were downloaded, we can classify the provided cat
examples/images/cat.jpg) using this command:
The output should look like this:
---------- Prediction for examples/images/cat.jpg ----------
0.3134 - "n02123045 tabby, tabby cat"
0.2380 - "n02123159 tiger cat"
0.1235 - "n02124075 Egyptian cat"
0.1003 - "n02119022 red fox, Vulpes vulpes"
0.0715 - "n02127052 lynx, catamount"
To further improve performance, you will need to leverage the GPU more, here are some guidelines: