Getting Started with Training a Caffe Object Detection Inference Network

Applicable products


Application note description

This application note describes how to install SSD-Caffe on Ubuntu and how to train and test the files needed to create a compatible network inference file for Firefly-DL.

Preparing for use

Before you use your camera, we recommend that you are aware of the following resources available from our website:

  • Camera Reference for the camera—HTML document containing specifications, EMVA imaging, installation guide, and technical reference for the camera model. Replace <PART-NUMBER> with your model's part number:<PART-NUMBER>/latest/Model/Readme.html
    For example:
  • Getting Started Manual for the camera—provides information on installing components and software needed to run the camera.
  • Technical Reference for the camera—provides information on the camera’s specifications, features and operations, as well as imaging and acquisition controls.
  • Firmware updates—ensure you are using the most up-to-date firmware for the camera to take advantage of improvements and fixes.
  • Tech Insights—Subscribe to our bi-monthly email updates containing information on new knowledge base articles, new firmware and software releases, and Product Change Notices (PCN).

Installing SSD-Caffe on Ubuntu 18.04/16.04

We strongly recommend you start with a fresh Ubuntu 18.04/16.04 system.

1. Install NCSDK (

a. To download this version, in terminal use the following:

git clone -b v2.05.00.02 --single-branch

b. To install, navigate to the ncsdk- folder and use the following:

$ make install

Note: If using Ubuntu 18.04, open file, on Line 32 replace 1604 with 1804.

c. By default, caffe is installed under the directory “/opt/movidius/caffe”. Set this as the $CAFFE_ROOT:

$ echo "export CAFFE_ROOT=/opt/movidius/caffe/" >> ~/.profile
$ source ~/.profile
  1. d. Set PYTHONPATH: 
$ export PYTHONPATH="${PYTHONPATH}:/opt/movidius/caffe/python"

2. Install Boost:

$ sudo apt-get install -y --no-install-recommends libboost-all-dev

3. Install all the necessary packages for Caffe:

$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libopenblas-dev \
libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

4. Install all the necessary Python packages:

$ pip3 install scikit-image protobuf
$ cd $CAFFE_ROOT/python
$ pip3 install -r requirements.txt

5. Make a copy of the file Makefile.config.example:

$ cp Makefile.config.example Makefile.config
$ sudo gedit Makefile.config

Note: You can also use vim or Text Editor to edit.

6. Modify Makefile.config as below:

a. Uncomment Line 8: CPU_ONLY := 1

b. Uncomment Line 21: OPENCV_VERSION := 3

c. On our test setup, we used python 3.6. To ensure that the makefile is configurated to use python 3.6, uncomment and modify Line 76 - 78 to below:

PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m \

d. Save and Exit.

7. Check the name of the hdf5 library and hdf5_hl library. It may vary depending on the Ubuntu version.

$ cd /usr/lib/x86_64-linux-gnu/
$ ls -al | grep libhdf5_serial

8. Note the libraries that are present on your system. Note that the postfixes of hdf5 and hdf5_hl are not always the same. Then make a link to them:

$ sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/
$ sudo ln -s /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/

9. In MakeFile.config, modify Line 92 and 93 to the following:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial/

10. Navigate to $CAFFE_ROOT folder and make:

$ make all -j8
$ make test -j8
$ make runtest -j8

Each make command might take a few minutes to finish.

11. Open /opt/movidius/caffe/src/caffe/util/math_functions.cpp

$ sudo gedit $CAFFE_ROOT/src/caffe/util/math_functions.cpp

12. Replace the function caffe_rng_uniform() at Line 247 to:

void caffe_rng_uniform(const int n, Dtype a, Dtype b, Dtype* r) {
CHECK_GE(n, 0);
if(a > b) {
Dtype c = a;
a = b;
b = c;
CHECK_LE(a, b);
boost::uniform_real<Dtype> random_distribution(a, caffe_nextafter<Dtype>(b));
boost::variate_generator<caffe::rng_t*, boost::uniform_real<Dtype> >
variate_generator(caffe_rng(), random_distribution);
for (int i = 0; i < n; ++i) {
r[i] = variate_generator();


13. Run the command below:

$ make pycaffe

14. To confirm the installation, import caffe in python3.6:

$ python3.6
>>> import caffe

Training and testing

Once ssd-caffe is properly set up, you can train your data to generate the .caffemodel and .prototxt files necessary to create a compatible network inference file for Firefly-DL.

Preparing data

1. Follow Preparation - Step 2 and 3 at to generate the LMDB files at:

$HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb (Size: around 1.8GB)
$HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb (Size: around 446MB)

2. Since we are using python3, in /opt/movidius/caffe/data/VOC0712/

  • On Line 24 change “python” to “python3”

3. If you get the error “python3 can’t open file No such file or directory”:

a. Make the following changes to

  • Change line 1 to:
  • Change line 2 to:

b. Run the script again from the $CAFFE_ROOT directory.

The labelmap_voc.prototxt file is generated in the directory “/opt/movidius/caffe/data/VOC0712”. The labelmap_voc.prototxt file lists the objects to be detected, and can be used to create the label file to be uploaded to the FFY_DL camera.

The file voc0712_detection_labels.txt for this example can downloaded from:

Preparing training scripts and other files

1. Download training scripts (for MobileNet-SSD model):

$ cd $CAFFE_ROOT/examples
$ git clone

A 'MobileNet-SSD' folder is created in '/opt/movidius/caffe/examples' with the code from the original MobileNet-SSD repo for retraining and testing.

2. Generate your own training (train/test/deploy) prototxt files:

$ cd MobileNet-SSD

CLASSNUM is the number of classes in your dataset. It is also reflected in labelmap_voc.prototxt file. In this example for VOC0712 dataset, CLASSNUM = 21.

Three files are generated:

  • MobileNetSSD_train.prototxt
  • MobileNetSSD_test.prototxt
  • MobileNetSSD_deploy.prototxt

These are saved in the "/opt/movidius/caffe/examples/MobileNet-SSD/example/" directory.

3. Create symlinks to lmdb data:

$ cd $CAFFE_ROOT/examples/MobileNet-SSD
$ ln -s PATH_TO_YOUR_TRAIN_LMDB trainval_lmdb
$ ln -s PATH_TO_YOUR_TEST_LMDB test_lmdb

In this example:

PATH_TO_YOUR_TRAIN_LMDB is: $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb

PATH_TO_YOUR_TEST_LMDB is: $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb

Under the “/opt/movidius/caffe/examples/MobileNet-SSD" directory, there are two symlink folders named “trainval_lmdb” and “test_lmdb” that point to your training data folder and test data folder, respectively.


Note: You can also use vim or Text Editor to edit.

1. Modify training scripts for CPU-ONLY training:

$ sudo gedit solver_train.prototxt

a. Change line 13 to:

solver_mode: CPU

b. Save.

2. Edit

$ sudo gedit

a. Comment out the last line:

#-gpu 0

b. Save.

3. Run to train your own model:

$ ./

Training can take a long time, but you can temporarily pause the training by pressing “Ctrl+C”.

Two output files are generated:

  • mobilenet_iter_*.caffemodel
  • mobilenet_iter_*.solverstate

These are saved in the "/opt/movidius/caffe/examples/MobileNet-SSD/snapshot" directory.

The more iterations you train for, the higher accuracy can be achieved. We have tested mobilenet_iter_1000 (1000 iterations), and have achieved good results.

The mobilenet_iter_*.caffemodel and MobileNetSSD_deploy.prototxt files are needed for conversion. These are in the "/opt/movidius/caffe/examples/MobileNet-SSD/example/" directory.For conversion, follow the steps in Example 2 in Getting Started with Firefly-DL in Linux.

Testing (optional)

1. Modify testing scripts for CPU-ONLY testing:

$ sudo gedit solver_test.prototxt

a. Change line 1 to:

train_net: "example/MobileNetSSD_train.prototxt"

b. Change line 2 to:

test_net: "example/MobileNetSSD_test.prototxt"

c. Change line 13 to:

solver_mode: CPU

d. Save.

2. Edit

$ sudo gedit

a. Comment out the last line:

#-gpu 0

b. Save.

3. Run to test your own model, which can take a long time as well:

$ ./


Related Articles