Showing posts with label caffe. Show all posts
Showing posts with label caffe. Show all posts

Monday, 28 September 2020

Caffe How to Debug?

 文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

这篇文件主要介绍如何使用Linux的gdb调试Caffe的源码,源码调试主要是为了阅读并更好的了解Caffe源码。

1. 准备工作

  1. 首先要在编译Caffe源码时打开debug模式,即将Makefile.config中的DEBUG := 1打开。
  2. 下载mnist数据集,主要是在mnist数据集上进行调试,执行bash data/mnist/get_mnist.sh
  3. 转换mnist数据集为LMDB,bash examples/mnist/create_mnist.sh
  4. 修改examples/mnist/lenet_solver.prototxt,将GPU改为CPU。

2. 调试

1. 激活GDB

使用GDB启动调试,执行gdb --args build/tools/caffe train --solver examples/mnist/lenet_solver.prototxt--args表示我们调试时需要输入的参数,调试的命令为build/tools/caffe,caffe命令的参数为--solver examples/mnist/lenet_solver.prototxt

执行结果:

$ gdb --args build/tools/caffe train --solver examples/mnist/lenet_solver.prototxt
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/irteam/line-brain/deploy/caffe/.build_debug/tools/caffe.bin...done.
 
2. 设置断点

执行b src/caffe/layers/base_conv_layer.cpp:117b表示插入断点(breakpoint),断点的位置是base_conv_layer.cpp文件中的117行。插入断点的命令形式为:

b path/to/code.cpp:#line 
 

118行相关代码:

117 channels_ = bottom[0]->shape(channel_axis_);
118 num_output_ = this->layer_param_.convolution_param().num_output();
119 CHECK_GT(num_output_, 0);
 
执行结果:
(gdb) b src/caffe/layers/base_conv_layer.cpp:117
No source file named src/caffe/layers/base_conv_layer.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 1 (src/caffe/layers/base_conv_layer.cpp:117) pending.

3. 运行程序

运行程序的命令是r

执行结果:

Starting program: /*/caffe/build/tools/caffe train --solver examples/mnist/lenet_solver.prototxt
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
I0718 15:19:19.671941 29986 caffe.cpp:211] Use CPU.

Breakpoint 1, caffe::BaseConvolutionLayer<float>::LayerSetUp (this=0x91edd70,
    bottom=std::vector of length 1, capacity 1 = {...}, top=std::vector of length 1, capacity 1 = {...})
    at src/caffe/layers/base_conv_layer.cpp:117
117       channels_ = bottom[0]->shape(channel_axis_);
Missing separate debuginfos, use: debuginfo-install OpenEXR-libs-1.7.1-7.el7.x86_64 atk-2.14.0-1.el7.x86_64 atlas-3.10.1-10.el7.x86_64 boost-filesystem-1.53.0-26.el7.x86_64 boost-python-1.53.0-26.el7.x86_64 boost-system-1.53.0-26.el7.x86_64 boost-thread-1.53.0-26.el7.x86_64 cairo-1.14.2-1.el7.x86_64 expat-2.1.0-10.el7_3.x86_64 fontconfig-2.10.95-10.el7.x86_64 freetype-2.4.11-12.el7.x86_64 gdk-pixbuf2-2.31.6-3.el7.x86_64 gflags-2.1.1-6.el7.x86_64 glib2-2.46.2-4.el7.x86_64 glibc-2.17-157.el7_3.1.x86_64 glog-0.3.3-8.el7.x86_64 graphite2-1.3.6-1.el7_2.x86_64 gstreamer-0.10.36-7.el7.x86_64 gstreamer-plugins-base-0.10.36-10.el7.x86_64 gtk2-2.24.28-8.el7.x86_64 harfbuzz-0.9.36-1.el7.x86_64 hdf5-1.8.12-8.el7.x86_64 ilmbase-1.0.3-7.el7.x86_64 jasper-libs-1.900.1-29.el7.x86_64 jbigkit-libs-2.0-11.el7.x86_64 leveldb-1.12.0-11.el7.x86_64 libX11-1.6.3-3.el7.x86_64 libXau-1.0.8-2.1.el7.x86_64 libXcomposite-0.4.4-4.1.el7.x86_64 libXcursor-1.1.14-2.1.el7.x86_64 libXdamage-1.1.4-4.1.el7.x86_64 libXext-1.3.3-3.el7.x86_64 libXfixes-5.0.1-2.1.el7.x86_64 libXi-1.7.4-2.el7.x86_64 libXinerama-1.1.3-2.1.el7.x86_64 libXrandr-1.4.2-2.el7.x86_64 libXrender-0.9.8-2.1.el7.x86_64 libffi-3.0.13-18.el7.x86_64 libgcc-4.8.5-11.el7.x86_64 libgfortran-4.8.5-11.el7.x86_64 libjpeg-turbo-1.2.90-5.el7.x86_64 libpng-1.5.13-7.el7_2.x86_64 libquadmath-4.8.5-11.el7.x86_64 libselinux-2.5-6.el7.x86_64 libstdc++-4.8.5-11.el7.x86_64 libtiff-4.0.3-27.el7_3.x86_64 libv4l-0.9.5-4.el7.x86_64 libxcb-1.11-4.el7.x86_64 libxml2-2.9.1-6.el7_2.3.x86_64 lmdb-libs-0.9.18-1.el7.x86_64 opencv-2.4.5-3.el7.x86_64 opencv-core-2.4.5-3.el7.x86_64 orc-0.4.22-5.el7.x86_64 pango-1.36.8-2.el7.x86_64 pcre-8.32-15.el7_2.1.x86_64 pixman-0.34.0-1.el7.x86_64 protobuf-2.5.0-8.el7.x86_64 python-libs-2.7.5-48.el7.x86_64 snappy-1.1.0-3.el7.x86_64 xz-libs-5.2.2-1.el7.x86_64 zlib-1.2.7-17.el7.x86_64

Breakpoint 1之前是正常的程序日志输出,程序在断点处暂停。

查看变量命令为p var,命令与结果如下:

(gdb) p channels_
$1 = 0

(gdb) p channel_axis_
$2 = 1

此时,channels_值为0。下一行命令为n,执行结果如下:

(gdb) n
118       num_output_ = this->layer_param_.convolution_param().num_output();
此时查看channels_值为1,mnist数据是灰度图像,channels_1没问题:
(gdb) p channels_
$3 = 1

命令c是继续执行直到下一个断点。

如果需要调试GPU程序,可以使用cuda-gdb,文档地址为:http://docs.nvidia.com/cuda/cuda-gdb/index.html#axzz4nAAR7ujZ

参考资料

  1. http://zhaok.xyz/blog/post/debug-caffe/

Sunday, 7 July 2019

how to build caffe in ubuntu16, cuda10.0, cudnn7.6, python3.5

This is similar to the previous post where we change the focus to python3.5.

Make sure you have install caffe for python2.7 before continue reading this post.

Here are few steps I have done for successfully compiling the caffe for python 3.5.

1. Clone the default caffe folder into a new folder named as caffe3. Then, delete all files in the build folder,
$ cp ./caffe/* ./caffe3/
$ sudo rm -r ./caffe3/build/*

2. Create a new conda environment and I named it as caffe3 as well. Make sure you use python3.5 as following,
$ conda create -n caffe3 python=3.5

3. Activate the caffe3 env and then install the numpy as following,
$ conda activate caffe3
$ pip install numpy
$ pip install scikit-image

4. Install additional dependencies as following:
$ sudo apt-get install python3-skimage
$ sudo apt-get install python3-protobuf

5. Once completed, cd to the caffe3 root folder and copy the makefile as following:
$ cd ./caffe3
$ cp Makefile.config.example Makefile.config

6. You will see a new file named as Makefile.config under ./caffe folder. Copy and paste the Makefile content as given in the appendix below. Note that please change the path accordingly as my anaconda env was installed at /home/ninja/.conda/envs/*, yours might be different such as /home/ninja/anaconda3/envs/*.

7. Once everything is ready, you are good to compile caffe, make sure you are in the ./caffe3 folder:
$ make clean
$ make all -j32
$ make test
$ make runtest
$ make pycaffe 
$ make distribute
(you should be able to see the distribute folder under caffe3)

8.If you have no error in compilation, congratulation! Next is to set the path in ~/.bashrc and you should able to import caffe.
$ sudo gedit ~/.bashrc

then add the following lines into ~/.bashrc, save it and source it

export PYTHONPATH=/home/ninja/caffe3/distribute/python:$PYTHONPATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/lib/x86_64-linux-gnu/:/home/ninja/caffe3/distribute/lib

$ source ~/.bashrc
$ conda activate caffe3
$ python (version 3.5.*)
>>> import caffe
>>> (pray, no error)

Example1 of Error:
./include/caffe/util/signal_handler.h:4:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory

If you are using conda, you need to include the env lib into the LD_LIBRARY_PATH:
$ export LD_LIBRARY_PATH=/home/ninja/.conda/envs/caffe3/lib


Example2 of Error:
xxx... .nccl.h: No such file or directory

If you are using conda, you need to copy all include and lib files into env:
$ cp nccl_2.4.7-1+cuda10.0_x86_64/include/nccl* /home/ninja/.conda/envs/c3d11/include/
$ cp nccl_2.4.7-1+cuda10.0_x86_64/lib/libnccl* /home/ninja/.conda/envs/c3d11/lib/


Example3 of Error:
xxx... .cudnn.h: No such file or directory

If you are using conda, you need to copy all include and lib files into env:
$ cp cuda/include/cudnn.h /home/ninja/.conda/envs/c3d11/include/
$ cp cuda/lib64/libcudnn* /home/ninja/.conda/envs/c3d11/lib/

I am using cudnn-10.0-linux-x64-v7.6.0.64. 


Example4 of Error:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
$ locate pyconfig.h
>> set the path in Makefile.config > PYTHON_INCLUDE


Example5 of Error:
Solve the problem: "cannot find -lboost_python3" when using Python3 Ubuntu16.04
$ cd /usr/lib/x86_64-linux-gnu
$ sudo ln -s libboost_python-py35.so libboost_python3.so


Example6 of Error:
Solve the problem: "/usr/bin/ld: cannot find -lopencv_imgcodecs" when using Python3 Ubuntu16.04, modify Makefile.config:-

PYTHON_LIBRARIES := boost_python3 python3.5m opencv_imgcodecs
PYTHON_LIB := $(ANACONDA_HOME)/lib /home/ninja/anaconda3/envs/caffe3/lib /home/ninja/workspace/opencv-3.4.4/distribute/lib



Example of Makefile, CUDA 8.0, CUDNN 6.0 used in this post.

## Refer to http://caffe.berkeleyvision.org/installation.html

# Contributions simplifying and improving our build system are welcome!

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib


# cuDNN acceleration switch (uncomment to build with cuDNN).

USE_CUDNN := 1


# CPU-only switch (uncomment to build without GPU support).

# CPU_ONLY := 1


# uncomment to disable IO dependencies and corresponding data layers

# USE_OPENCV := 0

# USE_LEVELDB := 0

# USE_LMDB := 0

# This code is taken from https://github.com/sh1r0/caffe-android-lib

# USE_HDF5 := 0


# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)

#    You should not set this flag if you will be reading LMDBs with any

#    possibility of simultaneous read and write

ALLOW_LMDB_NOLOCK := 1


# Uncomment if you're using OpenCV 3

OPENCV_VERSION := 3


# To customize your choice of compiler, uncomment and set the following.

# N.B. the default for Linux is g++ and the default for OSX is clang++

# CUSTOM_CXX := g++


# CUDA directory contains bin/ and lib/ directories that we need.

CUDA_DIR := /usr/local/cuda-8.0

# On Ubuntu 14.04, if cuda tools are installed via

# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:

# CUDA_DIR := /usr


# CUDA architecture setting: going with all of them.

# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.

# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.

CUDA_ARCH := -gencode arch=compute_60,code=sm_61


# BLAS choice:

# atlas for ATLAS (default)

# mkl for MKL

# open for OpenBlas

BLAS := atlas

# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.

# Leave commented to accept the defaults for your choice of BLAS

# (which should work)!

# BLAS_INCLUDE := /path/to/your/blas

# BLAS_LIB := /path/to/your/blas


# Homebrew puts openblas in a directory that is not on the standard search path

# BLAS_INCLUDE := $(shell brew --prefix openblas)/include

# BLAS_LIB := $(shell brew --prefix openblas)/lib


# This is required only if you will compile the matlab interface.

# MATLAB directory should contain the mex binary in /bin.

# MATLAB_DIR := /usr/local

# MATLAB_DIR := /Applications/MATLAB_R2012b.app


# NOTE: this is required only if you will compile the python interface.

# We need to be able to find Python.h and numpy/arrayobject.h.

# PYTHON_INCLUDE := /usr/include/python2.7 \

        /usr/lib/python2.7/dist-packages/numpy/core/include

# Anaconda Python distribution is quite popular. Include path:

# Verify anaconda location, sometimes it's in root.

ANACONDA_HOME := /home/ninja/.conda/envs/caffe3

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

        $(ANACONDA_HOME)/include/python3.5 \

        $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include


# Uncomment to use Python 3 (default is Python 2)

PYTHON_LIBRARIES := boost_python3 python3.5m

PYTHON_INCLUDE := /home/ninja/.conda/envs/caffe3/include/python3.5m \

                /home/ninja/.conda/envs/caffe3/lib/python3.5/site-packages/numpy/core/include


# We need to be able to find libpythonX.X.so or .dylib.

# PYTHON_LIB := /usr/lib

PYTHON_LIB := $(ANACONDA_HOME)/lib /home/ninja/.conda/envs/caffe3/lib


# Homebrew installs numpy in a non standard path (keg only)

# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include

# PYTHON_LIB += $(shell brew --prefix numpy)/lib


# Uncomment to support layers written in Python (will link against Python libs)

WITH_PYTHON_LAYER := 1


# Whatever else you find you need goes here.

#INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include

#LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /home/ninja/opencv/include/ /home/ninja/.conda/envs/caffe3/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /home/ninja/opencv/build/lib


# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies

# INCLUDE_DIRS += $(shell brew --prefix)/include

# LIBRARY_DIRS += $(shell brew --prefix)/lib


# NCCL acceleration switch (uncomment to build with NCCL)

# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)

USE_NCCL := 1


# Uncomment to use `pkg-config` to specify OpenCV library paths.

# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)

USE_PKG_CONFIG := 1


# N.B. both build and distribute dirs are cleared on `make clean`

BUILD_DIR := build

DISTRIBUTE_DIR := distribute


# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171

# DEBUG := 1


# The ID of the GPU that 'make runtest' will use to run unit tests.

TEST_GPUID := 0


# enable pretty build (comment to see full commands)

Q ?= @






Example of Makefile, CUDA 10.0, CUDNN 7.6 used in this post.

## Refer to http://caffe.berkeleyvision.org/installation.html

# Contributions simplifying and improving our build system are welcome!

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib


# cuDNN acceleration switch (uncomment to build with cuDNN).

USE_CUDNN := 1


# CPU-only switch (uncomment to build without GPU support).

# CPU_ONLY := 1


# uncomment to disable IO dependencies and corresponding data layers

# USE_OPENCV := 0

# USE_LEVELDB := 0

# USE_LMDB := 0

# This code is taken from https://github.com/sh1r0/caffe-android-lib

# USE_HDF5 := 0


# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)

#    You should not set this flag if you will be reading LMDBs with any

#    possibility of simultaneous read and write

ALLOW_LMDB_NOLOCK := 1


# Uncomment if you're using OpenCV 3

OPENCV_VERSION := 3


# To customize your choice of compiler, uncomment and set the following.

# N.B. the default for Linux is g++ and the default for OSX is clang++

# CUSTOM_CXX := g++


# CUDA directory contains bin/ and lib/ directories that we need.

CUDA_DIR := /usr/local/cuda-10.0

# On Ubuntu 14.04, if cuda tools are installed via

# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:

# CUDA_DIR := /usr


# CUDA architecture setting: going with all of them.

# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.

# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.

CUDA_ARCH := -gencode arch=compute_75,code=sm_75


# BLAS choice:

# atlas for ATLAS (default)

# mkl for MKL

# open for OpenBlas

BLAS := atlas

# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.

# Leave commented to accept the defaults for your choice of BLAS

# (which should work)!

# BLAS_INCLUDE := /path/to/your/blas

# BLAS_LIB := /path/to/your/blas


# Homebrew puts openblas in a directory that is not on the standard search path

# BLAS_INCLUDE := $(shell brew --prefix openblas)/include

# BLAS_LIB := $(shell brew --prefix openblas)/lib


# This is required only if you will compile the matlab interface.

# MATLAB directory should contain the mex binary in /bin.

# MATLAB_DIR := /usr/local

# MATLAB_DIR := /Applications/MATLAB_R2012b.app


# NOTE: this is required only if you will compile the python interface.

# We need to be able to find Python.h and numpy/arrayobject.h.

# PYTHON_INCLUDE := /usr/include/python2.7 \

        /usr/lib/python2.7/dist-packages/numpy/core/include

# Anaconda Python distribution is quite popular. Include path:

# Verify anaconda location, sometimes it's in root.

ANACONDA_HOME := /home/ninja/.conda/envs/caffe3

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

        $(ANACONDA_HOME)/include/python3.5 \

        $(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include


# Uncomment to use Python 3 (default is Python 2)

PYTHON_LIBRARIES := boost_python3 python3.5m

PYTHON_INCLUDE := /home/ninja/.conda/envs/caffe3/include/python3.5m \

                /home/ninja/.conda/envs/caffe3/lib/python3.5/site-packages/numpy/core/include


# We need to be able to find libpythonX.X.so or .dylib.

# PYTHON_LIB := /usr/lib

PYTHON_LIB := $(ANACONDA_HOME)/lib /home/ninja/.conda/envs/caffe3/lib


# Homebrew installs numpy in a non standard path (keg only)

# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include

# PYTHON_LIB += $(shell brew --prefix numpy)/lib


# Uncomment to support layers written in Python (will link against Python libs)

WITH_PYTHON_LAYER := 1


# Whatever else you find you need goes here.

#INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include

#LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /home/ninja/opencv/include/ /home/ninja/.conda/envs/caffe3/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /home/ninja/opencv/build/lib


# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies

# INCLUDE_DIRS += $(shell brew --prefix)/include

# LIBRARY_DIRS += $(shell brew --prefix)/lib


# NCCL acceleration switch (uncomment to build with NCCL)

# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)

USE_NCCL := 1


# Uncomment to use `pkg-config` to specify OpenCV library paths.

# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)

USE_PKG_CONFIG := 1


# N.B. both build and distribute dirs are cleared on `make clean`

BUILD_DIR := build

DISTRIBUTE_DIR := distribute


# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171

# DEBUG := 1


# The ID of the GPU that 'make runtest' will use to run unit tests.

TEST_GPUID := 0


# enable pretty build (comment to see full commands)

Q ?= @

Monday, 6 May 2019

how to build caffe in ubuntu16, cuda10.0, cudnn7.6, python2.7

0. install the compatible nvidia driver, cudnn and nccl for ubuntu16. They are
  cuda10.0 (cuda_10.0.130_410.48_linux.run) and cudnn7.6.0 (cudnn-10.0-linux-x64-v7.6.0.64.tgz) and nccl (nccl_2.4.7-1+cuda10.0_x86_64.txz).

0.1 install general dependencies:

sudo apt-get remove x264 libx264-dev

sudo apt update

sudo apt upgrade

sudo apt install python2.7 python-pip
sudo apt install build-essential checkinstall cmake pkg-config yasm git gfortran libjpeg8-dev libjasper-dev libpng12-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libv4l-dev  libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libgtk2.0-dev libtbb-dev libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

sudo apt-get install ffmpeg libgtk-3-dev python-numpy python3-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libv4l-dev libtbb-dev qtbase5-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip liblmdb-dev libleveldb-dev libsnappy-dev libboost-dev

sudo apt install qt5-default

sudo apt install libboost-all-dev



1. install the correct protobuf packages (version 3.6.1) as given:-
# Make sure you grab the latest version
wget https://github.com/google/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip

# Unzip
unzip protoc-3.6.1-linux-x86_64.zip -d protoc3

# Move protoc to /usr/local/bin/
sudo cp -r protoc3/bin/* /usr/local/bin/

# Move protoc3/include to /usr/local/include/
sudo cp -r protoc3/include/* /usr/local/include/

# Optional: change owner
sudo chown $USER /usr/local/bin/protoc
sudo chown -R $USER /usr/local/include/google

sudo ldconfig




1.1 (optional, ignore) install the correct protobuf packages (version 3.1.0) as given in https://ninja5code.blogspot.com/2019/02/protobuf-error.html
 $  sudo add-apt-repository ppa:maarten-fonville/protobuf
 $  sudo apt-get update
 $  sudo apt-get remove libprotobuf-dev libprotoc-dev protobuf-compiler
 $  sudo apt-get install libprotobuf-dev libprotoc-dev protobuf-compiler
 $  protoc --version
 $  dpkg -l | grep protobuf


2. download zip files of opencv 3.4.4 and opencv-contrib 3.4.4 from https://github.com/opencv/opencv/releases/tag/3.4.4, https://github.com/opencv/opencv_contrib/releases/tag/3.4.4. You should unzip it into the folder as /home/ninja/opencv and /home/ninja/opencv-contrib-3.4.4, respectively. Then, build it with 'cmake ..' and 'make -j$(nproc)' and 'sudo make install'
 $  cd opencv
 $  cd build/
 $  rm -r ./*
 $  cmake ..
 $  make -j32
 $  sudo make install


2.1 if there is any wrong reference to xxx.lib, you can list down all the dependencies using:
$ ldd /usr/local/lib/xxx.lib 

2.2 Or you can check the library path to see the additional path within it:-
$ echo $LD_LIBRARY_PATH

Then, manually set it using:
$ export LD_LIBRARY_PATH = xxx:xxx

2.3 If cmake .. failed to build it, check the log printed by cmake, then install the dependencies if necessary, for example, failed to find libssl:-
$ sudo apt search libssl
$ sudo apt install libssl-dev

2.4 If make -j32 failed to build, you may try the following cmake:-
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=/home/ninja/workspace/opencv-3.4.4/distribute \
            -D INSTALL_C_EXAMPLES=ON \
            -D OPENCV_GENERATE_PKGCONFIG=ON \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D WITH_TBB=ON \
            -D WITH_V4L=ON \
            -D OPENCV_PYTHON3_INSTALL_PATH=/home/ninja/workspace/opencv-3.4.4/distribute/python3 \
            -D WITH_QT=ON \
            -D WITH_OPENGL=ON \
            -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.4/modules \
            -D BUILD_NEW_PYTHON_SUPPORT=ON \
            -D BUILD_opencv_python3=ON \
            -D HAVE_opencv_python3=ON \
            -D PYTHON3_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_DEFAULT_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_INCLUDE_PATH=/home/ninja/anaconda3/include/python3.7m \
            -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ninja/anaconda3/lib/python3.7/site-packages/numpy/core/include \
            -D PYTHON3_PACKAGES_PATH=/home/ninja/anaconda3/lib/python3.7/site-packages \
            -D PYTHON3_LIBRARIES=/home/ninja/anaconda3/lib/libpython3.7m.so    \
            -D PYTHON3_LIBRARIES_PATH=/home/ninja/anaconda3/lib    \
            -D BUILD_EXAMPLES=ON ..


(make sure you only have single version of library in LD_LIBRARY_PATH, for example, you cannot have protobuf 3.8.0 in /usr/lib/x86_64-linux-gnu/ and protobuf 3.1.0 in anaconda. You need to uninstall both and install protobuf 3.1.0 under /usr/lib/x86_64-linux-gnu only)

2.5 If you want to build with CUDA 8.0 and CUDNN 6.0 support:-
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=/home/ninja/workspace/opencv-3.4.4/distribute \
            -D INSTALL_C_EXAMPLES=ON \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D WITH_TBB=ON \
            -D WITH_V4L=ON \
            -D WITH_CUDA=ON \
            -D WITH_CUBLAS=ON \
            -D CUDA_FAST_MATH=ON \
            -D CUDA_GENERATION=Fermi \
            -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \

            -D OPENCV_PYTHON3_INSTALL_PATH=/home/ninja/workspace/opencv-3.4.4/distribute/python3 \

            -D WITH_QT=ON \
            -D WITH_OPENGL=ON \
            -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.4/modules \
            -D BUILD_NEW_PYTHON_SUPPORT=ON \
            -D BUILD_opencv_python3=ON \
            -D HAVE_opencv_python3=ON \
            -D PYTHON3_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_DEFAULT_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_INCLUDE_PATH=/home/ninja/anaconda3/include/python3.7m \
            -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ninja/anaconda3/lib/python3.7/site-packages/numpy/core/include \
            -D PYTHON3_PACKAGES_PATH=/home/ninja/anaconda3/lib/python3.7/site-packages \
            -D PYTHON3_LIBRARIES=/home/ninja/anaconda3/lib/libpython3.7m.so    \
            -D PYTHON3_LIBRARIES_PATH=/home/ninja/anaconda3/lib    \
            -D BUILD_EXAMPLES=ON ..



2.6 If you want to build with CUDA 10.0 and CUDNN 7.6 support:-
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=/home/ninja/workspace/opencv-3.4.4/distribute \
            -D INSTALL_C_EXAMPLES=ON \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D WITH_TBB=ON \
            -D WITH_V4L=ON \
            -D WITH_CUDA=ON \
            -D WITH_CUBLAS=ON \
            -D BUILD_opencv_cudacodec=OFF \
            -D CUDA_FAST_MATH=ON \
            -D CUDA_GENERATION=Kepler \
            -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.0 \

            -D OPENCV_PYTHON3_INSTALL_PATH=/home/ninja/workspace/opencv-3.4.4/distribute/python3 \

            -D WITH_QT=ON \
            -D WITH_OPENGL=ON \
            -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.4/modules \
            -D BUILD_NEW_PYTHON_SUPPORT=ON \
            -D BUILD_opencv_python3=ON \
            -D HAVE_opencv_python3=ON \
            -D PYTHON3_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_DEFAULT_EXECUTABLE=/home/ninja/anaconda3/bin/python \
            -D PYTHON3_INCLUDE_PATH=/home/ninja/anaconda3/include/python3.7m \
            -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/ninja/anaconda3/lib/python3.7/site-packages/numpy/core/include \
            -D PYTHON3_PACKAGES_PATH=/home/ninja/anaconda3/lib/python3.7/site-packages \
            -D PYTHON3_LIBRARIES=/home/ninja/anaconda3/lib/libpython3.7m.so    \
            -D PYTHON3_LIBRARIES_PATH=/home/ninja/anaconda3/lib    \
            -D BUILD_EXAMPLES=ON ..


Build with Opencv-4.4.0, Opencv_Contrib-4.4.0, Ubuntu18, Cuda10-2

{
    "cmake.configureArgs": [
        "-DCMAKE_BUILD_TYPE=RELEASE",
        "-DCMAKE_INSTALL_PREFIX=/home/ninja/workspace/opencv-4.4.0/distribute",
        "-DINSTALL_C_EXAMPLES=ON",
        "-DOPENCV_GENERATE_PKGCONFIG=ON",
        "-DINSTALL_PYTHON_EXAMPLES=ON",
        "-DBUILD_SHARED_LIBS=ON"
        "-DWITH_TBB=ON",
        "-DWITH_V4L=ON",
        "-DBUILD_opencv_world=OFF",
        "-DOPENCV_PYTHON3_INSTALL_PATH=/home/ninja/workspace/opencv-4.4.0/distribute/python3",
        "-DWITH_QT=ON",
        "-DWITH_OPENGL=ON",
        "-DWITH_CUDA=ON",
        "-DWITH_CUBLAS=ON",
        "-DBUILD_opencv_cudacodec=ON",
        "-DCUDA_FAST_MATH=ON",
        "-DCUDA_GENERATION=Kepler",
        "-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2",
        "-DOPENCV_EXTRA_MODULES_PATH=/home/ninja/workspace/opencv_contrib-4.4.0/modules",
        "-DBUILD_NEW_PYTHON_SUPPORT=ON",
        "-DBUILD_opencv_python3=ON",
        "-DHAVE_opencv_python3=ON",
        "-DPYTHON3_EXECUTABLE=/home/ninja/anaconda3/bin/python",
        "-DPYTHON3_DEFAULT_EXECUTABLE=/home/ninja/anaconda3/bin/python",
        "-DPYTHON3_INCLUDE_PATH=/home/ninja/anaconda3/include/python3.8",
        "-DPYTHON3_NUMPY_INCLUDE_DIRS=/home/ninja/anaconda3/lib/python3.8/site-packages/numpy/core/include",
        "-DPYTHON3_PACKAGES_PATH=/home/ninja/anaconda3/lib/python3.8/site-packages",
        "-DPYTHON3_LIBRARIES=/home/ninja/anaconda3/lib/libpython3.8.so",
        "-DPYTHON3_LIBRARIES_PATH=/home/ninja/anaconda3/lib",
        "-DBUILD_EXAMPLES=ON"
        ]
}



2.7. if you have this error: The imported target "Qt5::Gui" references the file, "/usr/lib/x86_64-linux-gnu/libEGL.so"
sudo rm /usr/lib/x86_64-linux-gnu/libEGL.so; sudo ln /usr/lib/x86_64-linux-gnu/libEGL.so.1 /usr/lib/x86_64-linux-gnu/libEGL.so

2.8. if you have this error: The imported target "Qt5::Gui" references the file, "/usr/lib/x86_64-linux-gnu/libGL.so" 
sudo rm /usr/lib/x86_64-linux-gnu/libGL.so; sudo ln /usr/lib/x86_64-linux-gnu/libGL.so.1 /usr/lib/x86_64-linux-gnu/libGL.so


2.9. If you have this error: CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: CUDA_nppi_LIBRARY (ADVANCED)
$ cd ~/workspace/opencv-2.4.13.6/cmake
$ cp ../../opencv-3.4.4/cmake/FindCUDA.cmake ./ $ cp ../../opencv-3.4.4/cmake/OpenCVDetectCUDA.cmake ./
$ cd ../build
$ make all -j32


2.10. If you have this error, opencv_dep_CUDA_nppi_LIBRARY is not found:-

replace the root > cmake > FindCUDA.cmake from opencv3.4.4 to 2.4.13.6

2.11. If you have this error, "gflags-2.1.1/src/gflags.cc:90:20: fatal error: config.h: No such file or directory"
cmake version is too new, need to downgrade

3. create a conda virtual env named as caffe2 with python2.7, install:
$ pip install scikit-image
$ pip install protobuf (after completed step 2)

4. download caffe and build it with 'cmake ..' and 'make -j$(nproc)' and 'sudo make install'
 $  cd caffe/
 $  cd build/
 $  sudo rm -r ./*
 $  cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/home/ninja/.conda/envs/caffe

 $  make -j32
 $  sudo make install

 $  make runtest
 $  make pycaffe 

4.1 if sudo make install failed due to undefined reference to `ZSTD_initCStream, run:
$ conda list
$ conda install libtiff (it should install zstd)

or you can just modify the library path by pointing to system libraries only

$ gedit ~/.bashrc
>> # insert into last line
>> export $LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu

$ source ~/.bashrc
$ conda activate caffe2
(compile again, make -j32)

4.2 C++ exception with description "locale::facet::_S_create_c_locale name not valid" thrown in SetUp().
$ export LC_ALL="C"
(rerun make runtest) 
 

4.3 If you received the following error:-

CXX/LD -o .build_release/tools/convert_imageset.bin .build_release/lib/libcaffe.so: undefined reference to `google::protobuf::io::CodedOutputStream::WriteVarint64ToArray(unsigned long long, unsigned char*)' .build_release/lib/libcaffe.so: undefined reference to `google::protobuf::internal::WireFormatLite::WriteInt64(int, long long, google::protobuf::io::CodedOutputStream*)' .build_release/lib/libcaffe.so: undefined reference to `google::protobuf::UnknownFieldSet::AddVarint(int, unsigned long long)' .build_release/lib/libcaffe.so: undefined reference to `google::protobuf::io::CodedOutputStream::VarintSize64(unsigned long long)' .build_release/lib/libcaffe.so: undefined reference to `google::protobuf::io::CodedOutputStream::WriteVarint64(unsigned long long)' collect2: error: ld returned 1 exit status make: *** [.build_release/tools/convert_imageset.bin] Error 1

$ ldd .build_release/lib/libcaffe.so 

(check libcaffe.so is using which library, it should point to libprotobuf.so.11 => /usr/local/lib/libprotobuf.so.11. If it links to library under anaconda, just delete it.) 

 
 

5.If all succeed, you need to set the pythonpath as:-
$ export PYTHONPATH=/home/ninja/.conda/envs/caffe/python:$PYTHONPATH

6. run python and then import caffe, you may need to install protobuf as pip install protobuf==3.1.0 and pip install scikit-image (check using protoc --version)

7. /usr/bin/ld: cannot find -llib64

check the CUDNN_LIBRARY or CUDA_LIBRARY path, should be full path to the specific library, /home/ubuntu/workspace/cuda/lib64/libcudnn.so, not folder name /home/ubuntu/workspace/cuda/lib64/

 

8. /home/ubuntu/workspace/caffe-opencv4.2-cuda10.0/include/caffe/proto/caffe.pb.h:17:2: error: #error This file was generated by an older version of protoc which is

or 

./include/caffe/util/cudnn.hpp:8:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory

$ protoc src/caffe/proto/caffe.proto --cpp_out=.
$ mkdir include/caffe/proto
$ mv src/caffe/proto/caffe.pb.h include/caffe/proto
  

9. how to force cmake to use one particular cuda architecture?

-DCUDA_ARCH_NAME=Manual -DCUDA_ARCH_BIN="75" -DCUDA_ARCH_PTX="75"

 

Example of .bashrc for building opencv
PATH=$PATH:$HOME/anaconda/bin:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/lib/x86_64-linux-gnu/:/home/ninja/anaconda3/lib

Example of .bashrc for building caffe
export PYTHONPATH=/home/ninja/.conda/envs/caffe/python:$PYTHONPATH
PATH=$PATH:$HOME/anaconda/bin:/usr/local/cuda/bin

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/lib/x86_64-linux-gnu/:/home/ninja/.conda/envs/caffe/lib 



10. (-218:No OpenGL support) Library was built without OpenGL support in function 'CvWindow':-

        -D OpenGL_GL_PREFERENCE=GLVND \
        -D WITH_QT=ON \
        -D WITH_OPENGL=ON \
        -D WITH_GTK_2_X=ON \

 


11. How to find the correct path of opencv from a new C++ project:

During opencv compilation, set the install path in cmakelists.txt:

  -D CMAKE_INSTALL_PREFIX=/home/peseyes/Documents/opencv-4.4.0/distribute \

During the new project compilation, set the opencv path in cmakelists.txt:-

SET(OpenCV_DIR /home/peseyes/Documents/opencv-4.4.0/distribute/share/opencv4) find_package(OpenCV 4 REQUIRED)

Wednesday, 27 February 2019

Protobuf Error @@


This error often happen when building opencv or caffe.
I found it is quite annoying as everytime I have to spend a lot of time to debug.
I think I understand the issue now.

First, check the protoc version you are using:-
$ protoc --version
libprotoc 2.6.1
$ dpkg -l | grep protobuf

 
Second, check which protoc you are using, remove those not from /usr/bin/:-
$ which protoc
/home/user/.conda/envs/caffe2/bin/protoc
$ conda uninstall libprotobuf



Third, remove and install again the protoc and protobuf:-

$ sudo add-apt-repository ppa:maarten-fonville/protobuf 
$ sudo apt-get update
$ sudo apt-get remove libprotobuf-dev libprotoc-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev libprotoc-dev protobuf-compiler
 
Finally rerun the make for opencv and caffe.
One thing to take note for opencv as there are some conflict when enabling the dnn module.
If there is an error of protobuf when building opencv, try add on the following to cmake:-
-DBUILD_PROTOBUF=OFF
-DPROTOBUF_UPDATE_FILES=ON
-DBUILD_OPENCV_DNN=OFF

platform: ubuntu16.04, python2.7, anaconda3