Skip to content

Instantly share code, notes, and snippets.

@WIStudent
Last active November 16, 2018 08:15
Show Gist options
  • Save WIStudent/08072e8dd41487d2dde7ce75eec3dcbf to your computer and use it in GitHub Desktop.
Save WIStudent/08072e8dd41487d2dde7ce75eec3dcbf to your computer and use it in GitHub Desktop.
Compiling DeepMatching's GPU version on Ubuntu 16.10

Compiling DeepMatching's GPU version on Ubuntu 16.10

DeepMatching is an algorithm that finds corresponding points in two images. Its GPU implementation was written for Fedora 21, which makes things a bit more difficult if you want to run it on an Ubuntu system. This document contains step-by-step instructions on how to get DeepMatching running on Ubuntu 16.10. I only tested it with Ubuntu 16.10, just let me know if it works with previous versions too.

To compile the GPU version you first need to compile the Caffe version that is included that comes with the DeepMatching files. Newer versions of Caffe won't work because Caffe changed the structure of its header files.

Compiling Caffe

Before compiling Caffe we need to make sure all its dependencies are installed. From the installation guide for Ubuntu 16.04/15.10:

sudo apt-get install build-essential cmake git pkg-config

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler

sudo apt-get install libatlas-base-dev 

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

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

# (Python general)
sudo apt-get install python-pip

# (Python 2.7 development files)
sudo apt-get install python-dev
sudo apt-get install python-numpy python-scipy

# (or, Python 3.5 development files)
sudo apt-get install python3-dev
sudo apt-get install python3-numpy python3-scipy

You also need to install Nvidia CUDA. All you need to do is install the packages nvidia-cuda-dev and nvidia-cuda-toolkit

sudo apt-get install nvidia-cuda-dev nvidia-cuda-toolkit

To install all python dependencies, extract the caffe.zip folder, navigate to caffe\python and execute

cd caffe/python
for req in $(cat requirements.txt); do pip install $req; done

To compile Caffe we will use CMake. Navigate back to the parent directory caffe, create a build folder ant then start cmake-gui

cd ..
mkdir build
cmake-gui

If you get the error that cmake-gui is not install, install it with

sudo apt-get install cmake-qt-gui

Enter the path to the caffe directory as path to the source code and the path to caffe/build as directory for the binaries. Then hit Configure. If everything went right, the log will show Configuring done at the end. Then hit Generate. The log should now say Generating done. Now close the CMake window, navigate to the caffe/build directory and execute make all. To speed things up, use the -jX flag, where X specifies the number of jobs that are executed simultaneously.

cd build
make all -j8

During compiling you might run into the issue that your gcc version is not supported.

#error -- unsupported GNU version! gcc versions later than 5 are not supported!

First check, if the packages gcc-5 and g++5 are installed on your system.

gcc-5 -v
g++-5 -v

If not, install them.

sudo apt-get install gcc-5 g++-5

Now delete the build folder and restart at the cmake-gui step, but this time search for the variables CMAKE_CXX_COMPILER and CMAKE_C_COMPILER and set them to /usr/bin/g++-5 and /usr/bin/gcc-5 respectively.

When make all was successful, execute make install. This will create an install folder in your build directory. We will need this folder later to compile DeepMatching. You can save this folder and then delete the whole caffe directory, if you want to.

make install -j8

If you want to test if your compiled caffe library runs correctly, you can call make runtest in the build directory.

make runtest -j8

Now we can start compiling DeepMatching.

Compiling DeepMatching

Before we can compile DeepMatching, we need to install the packages python-matplotlib and swig.

sudo apt-get install python-matplotlib swig

The Makefile that comes with DeepMatching isn't really compatible with Ubuntu, it contains many paths that don't work on Ubuntu. That's why I wrote my own Makefile.

# Path to caffe's install directory that was created by Cmake 
CAFFEDIR=install
CAFFELIB=$(CAFFEDIR)/lib

# 1: Include the location of the libcaffe.so library in the _gpudm.so library. Usefull if the CAFFEDIR is not in a standard location
# for libraries because you don't need to set the location by setting the variable LD_LIBRARY_PATH before using _gpudm.so.
# A disadvantage is, that _gpudm.so won't work anymore if you move the CAFFEDIR folder.
INCLUDE_CAFFE_LOCATION = 1

OPTFLAGS=-g -O2

# Path to python header file
INCLUDES += -I/usr/include/python2.7/
# Path to caffe's header files
INCLUDES += -I$(CAFFEDIR)/include/
# Path to hdf5's header files
INCLUDES += -I/usr/include/hdf5/serial/


#include gpudm/Makefile.config
CUDA_ARCH := \
    -gencode arch=compute_35,code=sm_35 \
    -gencode arch=compute_50,code=sm_50 \
    -gencode arch=compute_60,code=sm_60 \
    -gencode arch=compute_61,code=sm_61

HEADERS := $(shell find . -maxdepth 1 -name '*.hpp')
EXTRA_LAYERS := $(shell find . -maxdepth 1 -name '*.hpp')

all: _gpudm.so

_gpudm.so: gpudm_wrap.o $(EXTRA_LAYERS:.hpp=.o) $(EXTRA_LAYERS:.hpp=.cuo)
ifeq ($(INCLUDE_CAFFE_LOCATION),1)
	g++ $(OPTFLAGS) -fPIC -L$(CAFFELIB) $^ -shared -Xlinker -rpath $(CAFFELIB) -o $@ -lcaffe -lcusparse
else
	g++ $(OPTFLAGS) -fPIC -L$(CAFFELIB) $^ -o $@ -lcaffe -lcusparse
endif

%.cuo: %.cu %.hpp 
	nvcc $(CUDA_ARCH) -Xcompiler -fPIC $(INCLUDES) $(OPTFLAGS) -c $< -o $@

gpudm_wrap.cxx: gpudm.swig $(HEADERS)
	swig -cpperraswarn -python -c++ $(INCLUDES) gpudm.swig

gpudm_wrap.o: gpudm_wrap.cxx 
	g++ $(OPTFLAGS) -c gpudm_wrap.cxx -fPIC $(INCLUDES) -o gpudm_wrap.o

%.o: %.cpp %.hpp
	g++ $(OPTFLAGS) -c $< -fPIC $(INCLUDES) -L$(CAFFELIB) -o $@

clean:
	rm -f *.pyc *~ _gpudm.so gpudm_wrap.o $(EXTRA_LAYERS:.hpp=.o) $(EXTRA_LAYERS:.hpp=.cuo)

cleanswig: clean
	rm -f gpudm.py gpudm_wrap.cxx gpudm_wrap.o

The only thing you need to do is set CAFFE_DIR to the location of the install folder that was created during the compiling of Caffe. To compile, simply run make all. To test if DeepMatching runs correctly, execute

python deep_matching_gpu.py liberty1.png liberty2.png -v -viz corres

For more information about DeepMatching, look into the included README.txt file.

# Path to caffe's install directory that was created by Cmake
CAFFEDIR=caffe/build/install
CAFFELIB=$(CAFFEDIR)/lib
# 1: Include the location of the libcaffe.so library in the _gpudm.so library. Usefull if the CAFFEDIR is not in a standard location
# for libraries because you don't need to set the location by setting the variable LD_LIBRARY_PATH before using _gpudm.so.
# A disadvantage is, that _gpudm.so won't work anymore if you move the CAFFEDIR folder.
INCLUDE_CAFFE_LOCATION = 1
OPTFLAGS=-g -O2
# Path to python header file
INCLUDES += -I/usr/include/python2.7/
# Path to caffe's header files
INCLUDES += -I$(CAFFEDIR)/include/
# Path to hdf5's header files
INCLUDES += -I/usr/include/hdf5/serial/
#include gpudm/Makefile.config
CUDA_ARCH := \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61
HEADERS := $(shell find . -maxdepth 1 -name '*.hpp')
EXTRA_LAYERS := $(shell find . -maxdepth 1 -name '*.hpp')
all: _gpudm.so
_gpudm.so: gpudm_wrap.o $(EXTRA_LAYERS:.hpp=.o) $(EXTRA_LAYERS:.hpp=.cuo)
ifeq ($(INCLUDE_CAFFE_LOCATION),1)
g++ $(OPTFLAGS) -fPIC -L$(CAFFELIB) $^ -shared -Xlinker -rpath $(CAFFELIB) -o $@ -lcaffe -lcusparse
else
g++ $(OPTFLAGS) -fPIC -L$(CAFFELIB) $^ -o $@ -lcaffe -lcusparse
endif
%.cuo: %.cu %.hpp
nvcc $(CUDA_ARCH) -Xcompiler -fPIC $(INCLUDES) $(OPTFLAGS) -c $< -o $@
gpudm_wrap.cxx: gpudm.swig $(HEADERS)
swig -cpperraswarn -python -c++ $(INCLUDES) gpudm.swig
gpudm_wrap.o: gpudm_wrap.cxx
g++ $(OPTFLAGS) -c gpudm_wrap.cxx -fPIC $(INCLUDES) -o gpudm_wrap.o
%.o: %.cpp %.hpp
g++ $(OPTFLAGS) -c $< -fPIC $(INCLUDES) -L$(CAFFELIB) -o $@
clean:
rm -f *.pyc *~ _gpudm.so gpudm_wrap.o $(EXTRA_LAYERS:.hpp=.o) $(EXTRA_LAYERS:.hpp=.cuo)
cleanswig: clean
rm -f gpudm.py gpudm_wrap.cxx gpudm_wrap.o
@sberryman
Copy link

sberryman commented Apr 19, 2018

Thank you so much! This helped me get about 99% of the way there. If anyone else comes across this, I used a cuda docker container nvidia/cuda:8.0-devel and had to add a single include to the make file as I was getting an error compiling Deep Matching.

Error:

g++ -g -O2 -c gpudm_wrap.cxx -fPIC -I/usr/include/python2.7/ -I/root/web_gpudm_1.0/caffe/build/install/include/ -I/usr/include/hdf5/serial/ -o gpudm_wrap.o
In file included from /root/web_gpudm_1.0/caffe/build/install/include/caffe/common.hpp:19:0,
                 from /root/web_gpudm_1.0/caffe/build/install/include/caffe/blob.hpp:8,
                 from /root/web_gpudm_1.0/caffe/build/install/include/caffe/caffe.hpp:7,
                 from gpudm_wrap.cxx:3273:
/root/web_gpudm_1.0/caffe/build/install/include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory

On Ubuntu I just ran find /usr -name cublas_v2.h to find the location of cublas then added it to the build includes. For my specific version of cuda, I added the following on line 18.

INCLUDES += -I/usr/local/cuda-8.0/targets/x86_64-linux/include/
  • Update: I did NOT install nvidia-cuda-dev and nvidia-cuda-toolkit as they are already provided by the docker container.

@JohnnyRisk
Copy link

Can you please comment on which version of cudNN you are using? I am having problems using 5.1 and 6.0. It is hard to tell if this is my problem though or if i have broken something else as well.

@sberryman
Copy link

sberryman commented May 1, 2018

@JohnnyRisk I wasn't able to get it working with cuDNN to be honest and I just ended up giving up on it. It is easy to disable cuDNN as part of the caffe build if you don't need it though.

Update: I take that comment back, I was able to get caffe to compile using cuDNN v3 Download cuDNN v3 (September 8, 2015), for CUDA 7.0 and later.

I used nvidia/cuda:cuda:8.0-cudnn5-devel docker image and then downloaded cuDNN v3 and replaced the existing cuDNN header and library files.

  1. tar xf cudnn-7.0-linux-x64-v3.0.8-prod.tgz
  2. cp ./cuda/include/cudnn.h /usr/include/
  3. cp ./cuda/lib64/* /usr/lib/x86_64-linux-gnu/

Then proceed with the the above instructions

Update 2: Now I'm getting errors while trying to run deep_matching_gpu

python deep_matching_gpu.py liberty1.png liberty2.png -v
layer 0, patch_size = 8x8, 64 channels --> res shape = 64x33x33 
Traceback (most recent call last):
  File "deep_matching_gpu.py", line 455, in <module>
    corres = match_images((img0, img1), params, GPU=args.GPU, viz=viz)[0]
  File "deep_matching_gpu.py", line 336, in match_images
    net, levels = create_matching_net( images.shape, params, viz=viz )
  File "deep_matching_gpu.py", line 309, in create_matching_net
    levels = build_response_pyramid( net, levels[0], imgsize, **params )
  File "deep_matching_gpu.py", line 227, in build_response_pyramid
    child.grid, child.norms, dense_step )
  File "/root/web_gpudm_1.0/gpudm.py", line 13196, in prepare_big_cells
    children = numpy.empty((gty,gtx,nc),numpy.int32) if child_grid!=None else None
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Update 3: Using nvidia/cuda:cuda:8.0-devel and then copying cuDNN v3 header and library to the correct paths resulted in it working again. I can't honestly tell if cuDNN made any difference as I'm still unable to run deep matching against some large images I have using --downscale 3 --use_sparse But I can say it works, compiles and runs correctly on the GPU using smaller images.

@sberryman
Copy link

Anyone else who comes across this, I've put together a simple (not optimized or designed for production use) Dockerfile which should make getting started less painful.

https://gist.github.com/sberryman/7bea400ec2743bbdd93428fbee43cb7a

@uthynauta
Copy link

Hello, I have this error:

g++ -g -O2 -c gpudm_wrap.cxx -fPIC -I/usr/include/python2.7/ -I/home/uthy/Documents/caffe/build/include/ -I/usr/include/hdf5/serial/ -o gpudm_wrap.o
gpudm_wrap.cxx:3273:27: fatal error: caffe/caffe.hpp: No such file or directory
compilation terminated.
Makefile:46: recipe for target 'gpudm_wrap.o' failed
make: *** [gpudm_wrap.o] Error 1

Can anyone tell me what am I doing wrong?

@shensheng27
Copy link

Which version of cudNN is working? I am having problems using cuda9 and cudnn7 when make caffe with error
argument of type "int" is incompatible with parameter of type "cudnnNanPropagation_t"
I think the problem might be the version, so any success version?

@derkbreeze
Copy link

which kind of caffe are you using, I used current version of caffe, I followed your instruction, but I was not able to compile the deepmatching gpu version, although compiling caffe works well.
I am using Ubuntu16.04 LTS, python2.7 system default version, and CUDA8.0. cudnn6.0

@BananaNeil
Copy link

When I ran cmake-gui, I got the error:

QXcbConnection: Could not connect to display
Aborted (core dumped)

Is there any chance you can just upload the compiled GPU Deepmatching binary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment