Installing Caffe with Cuda 7.5 on Ubuntu 16.04.1 (Nvidia GeForce GTX 960M GPU)

There are three main steps:

Step 1: Installing the Nvidia graphics driver

Step 2: Installing Cuda 7.5

Step 3: Installing Caffe, pycaffe

Step 1: Installing the Nvidia graphics driver

Ref: http://askubuntu.com/questions/658040/ubuntu-14-04-nvidia-drivers-for-geforce-gtx-960m


sudo apt-get purge nvidia*
sudo apt-get purge bumblebee* primus
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-352 nvidia-prime
sudo add-apt-repository -r ppa:bumblebee/stable

Step 2: Installing Cuda 7.5

Ref: http://askubuntu.com/questions/799184/how-can-i-install-cuda-on-ubuntu-16-04

1. Download cuda_7.5.18_linux.run from https://developer.nvidia.com/cuda-downloads

2.

md5sum cuda_7.5.18_linux.run
sudo apt-get purge nvidia-cuda*
sudo sh cuda_7.5.18_linux.run --override
sudo reboot

Step 3: Installing Caffe, pycaffe
Ref: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide

1. Enter the following commands


sudo apt-get update

sudo apt-get upgrade

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

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

sudo apt-get install -y libatlas-base-dev

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

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

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

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

# (OpenCV 2.4)
sudo apt-get install -y libopencv-dev

2. clone caffe repository

 git clone https://github.com/BVLC/caffe.git 

3. enter caffe folder and copy the Makefile.config

 cp Makefile.config.example Makefile.config 

4. Edit Makefile.config as follows


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

WITH_PYTHON_LAYER := 1

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 /usr/lib/x86_64-linux-gnu/hdf5/serial

5. Apply the following commands (for Ubuntu 16.04)

find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \;

cd /usr/lib/x86_64-linux-gnu

sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so

sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so

6. Installing required python packages

cd python

for req in $(cat requirements.txt); do pip install $req; done

7. Go back to caffe installation folder (~/caffe e.g.), and run the installation commands:

make all

make test

make runtest

make pycaffe

8. In order to make the Python work with Caffe, open the file ~/.bashrc for editing in your favorite text editor. There, add the following line at the end of file:

export PYTHONPATH=/path/to/caffe-master/python:$PYTHONPATH

9. Your binaries will reside in .build_release folder

~/caffe/.build_release/tools/ 

ERROR Resolution: “error — unsupported GNU version! gcc versions later than 5.3 are not supported!”

comment the #error line in file /usr/local/cuda/include/host_config.h

#if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)

//#error — unsupported GNU version! gcc versions later than 5.3 are not supported!

#endif /* __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 1) */

ERROR Resolution: “/usr/include/string.h: In function ‘void* __mempcpy_inline(void*, const void*, size_t)’:
/usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope
return (char *) memcpy (__dest, __src, __n) + __n;
^
Makefile:588: recipe for target ‘.build_release/cuda/src/caffe/util/im2col.o’ failed
make: *** [.build_release/cuda/src/caffe/util/im2col.o] Error 1

If you are compiling with make then edit Makefile and replace the line
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
with
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

Advertisements

Compiling congealReal: Unsupervised joint alignment of complex images

The c++ code provided for the project ‘Unsupervised joint alignment of complex images’ at https://bitbucket.org/gbhuang/congealreal is very useful application for unsupervised image alignment. But the Makefile is now outdated, but we can fix it by modifying it as following,


IFLAGS = `pkg-config --cflags opencv` -O3
LFLAGS = `pkg-config opencv --libs`

all: congealReal funnelReal

clean:
rm congealReal funnelReal

congealReal: congealReal.cpp
g++ $(IFLAGS) -o congealReal congealReal.cpp $(LFLAGS)

funnelReal: funnelReal.cpp
g++ $(IFLAGS) -o funnelReal funnelReal.cpp $(LFLAGS)

How to enable MatCaffe wrapper for Caffe

1. Go to caffe root directory, for me it is :

 $cd /home/naved/Desktop/caffe-master 

2. Make Matcaffe binaries:

 $ make all matcaffe 

3. Test matcaffe installation:

 $make mattest 

4. Now open ~/.bashrc and check if the following lines are there, otherwise find them in your (it can be different for your case) system and modify accordingly:

# CUDA
export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=/usr/local/cuda/lib64/:$LD_LIBRARY_PATH
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6

# Caffe Root
export CAFFE_ROOT=/home/guo/Desktop/caffe-master

5. Now open matlab FROM TERMINAL ,(NOT using launcher icon! This is important!) e.g.:

$/usr/local/bin/matlab

6. Run a test code to makes sure it is running properly. Before running the code make sure to add path to the caffe/matlab directory as shown in the example code below:

clear;clc;

addpath('/home/guo/Desktop/caffe-master/matlab'); % MAKE SURE TO ADD THIS!

img = imread('ak.png');
img = single(img);

averageImage = [129.1863,104.7624,93.5940] ;

img = cat(3,img(:,:,1)-averageImage(1),...
img(:,:,2)-averageImage(2),...
img(:,:,3)-averageImage(3));

img = img(:, :, [3, 2, 1]); % convert from RGB to BGR
img = permute(img, [2, 1, 3]); % permute width and height

model = 'VGG_FACE_16_deploy.prototxt';
weights = 'VGG_FACE.caffemodel';
caffe.set_mode_cpu();
net = caffe.Net(model, weights, 'test'); % create net and load weights

res = net.forward({img});
prob = res{1};

caffe_ft = net.blobs('fc7').get_data();

Installing Caffe and PyCaffe on Ubuntu 14.04

1. Install OpenCV 2.4.11http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

2. Install general dependencies

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev

CUDA: Install via the NVIDIA package instead of apt-get to be certain of the library and driver versions. Install the library and latest driver separately; the driver bundled with the library is usually out-of-date. This can be skipped for CPU-only installation.

BLAS: install ATLAS by sudo apt-get install libatlas-base-dev or install OpenBLAS or MKL for better CPU performance.

Python (optional): if you use the default Python you will need to sudo apt-get install the python-dev package to have the Python headers for building the pycaffe interface.

3. remaining dependencies for 14.04

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

3. clone and install Caffe
git clone https://github.com/BVLC/caffe
cd /path/to/caffe/folder

mkdir build 
cd build 
cmake .. 
make all 
make install 
make runtest

4. Pycaffe installation

sudo apt-get install cython

sudo apt-get install python-skimage
sudo apt-get install python-pip
sudo pip install protobuf

make pycaffe

To import the caffe Python module after completing the installation, add the module directory to your $PYTHONPATH by export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

5. run python terminal and check by typing:
import caffe

OpenCV2 and g++: running your first program

Here’s a sample ‘helloworld’ program which takes an image and displays it:

#include "opencv2/opencv.hpp"

using namespace cv;

int main( int argc, char** argv )
{
// Read the source file
Mat src;
src = imread("fruits.jpg");

// Show the results
namedWindow( " ORIGINAL ", WINDOW_AUTOSIZE );
imshow( " ORIGINAL ", src );

waitKey();
return 0;
}

Save it is ‘helloworld.cpp’ using your favorite editor. Download a ‘fruits.jpg’ file from internet and save it in the same folder.

Comping the program using g++:

1. Use following command to compile:

g++ -Wall helloworld.cpp `pkg-config --libs --cflags opencv` -o helloworld

‘pkg-config –libs –cflags opencv’ adds all the necessary header file path and libraries.

2. run using ‘./helloworld’

OpenCV Installation Issue on Ubuntu: How to fix the ‘include : No such file or directory’ error

To install the latest OpenCV from git we follow the instructions on :

http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation

But if you stumble on the ‘include<Eigen/Eigenvalues>: No such file or directory’ error during make here’s how to fix that:

1. Install package

sudo apt-get install libeigen3-dev

2. Then change the cmake command to:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 ..

3. do ‘make’ and ‘sudo make install’ as instructed.

Installing libsvm-ranksvm-3.20 (Kernel RankSVM) to 64-bit Windows 7

You’ll need CMake or Cygwin to compile and generate the exe files. I have used cygwin for my purpose.

  1. Install Cygwin, make sure to check devel packages during installation for ‘make’ packages to install.
  2. Add cygwin path (eg. C:\cygwin64\bin) to system PATH.
  3. Run cygwin terminal and cd to unzipped libsvm-ranksvm-3.20 directory
  4. Run make. svm-train.exe, svm-predict.exe, and svm-scale.exe will be generated.
  5. You can run from cygwin terminal using ./svm-train or from dos prompt in usual manner.

How to connect Matlab 2013b with OpenCV2.4.5 on 64-bit Windows7 using MS Studio 2010

  • Modify system Path to add following opencv path: C:\OpenCV2.4.5\build
  • Perform mex setup and select MS Studio 2010 as your preferred compiler
  • Open mexopts.bat , find it using the command: >> fullfile(prefdir,’mexopts.bat’)
  • add at the end these lines :

rem rem ************
rem rem OpenCV2.4.5
rem rem ************
set OCVDIR=C:\OpenCV2.4.5\build
set INCLUDE=%OCVDIR%\include; %INCLUDE%
set LIB=%OCVDIR%\x64\vc10\lib; %LIB%
set PATH=%OCVDIR%\x64\vc10\bin; %OCVDIR%\bin; %PATH%

  • In the same file look for the section “Linker parameters”, after that add the different lib files of OpenCV to the end of the set LINKFLAGS= line of the command:

opencv_calib3d245.lib opencv_calib3d245d.lib opencv_contrib245.lib opencv_contrib245d.lib opencv_core245.lib opencv_core245d.lib opencv_features2d245.lib opencv_features2d245d.lib opencv_flann245.lib opencv_flann245d.lib opencv_gpu245.lib opencv_gpu245d.lib opencv_haartraining_engine.lib opencv_haartraining_engined.lib opencv_highgui245.lib opencv_highgui245d.lib opencv_imgproc245.lib opencv_imgproc245d.lib opencv_legacy245.lib opencv_legacy245d.lib opencv_ml245.lib opencv_ml245d.lib opencv_objdetect245.lib opencv_objdetect245d.lib opencv_video245.lib opencv_video245.lib

  • Now, all you have to do is restart Matlab and do not forget to save “MEXOPTS.BAT”

How to Set Adobe Reader as your Default Application in Ubuntu 14.04 when ‘Open with’ doesn’t Work

Usually, going to properties -> Open with and then selecting the desired program does it. But what if the application (here, Adobe Acrobat) isn’t there? Here’s the little trick that solved my problem (in Gnome):

Open the file /etc/gnome/defaults.list (admin mode).

Change the line:
application/pdf=evince.desktop

to:
application/pdf=acroread.desktop

Done.

Adding libSVM 3.2 to MATLAB R2013a

1. Download libsvm 3.2

2. Check available compilers in MATLAB using ‘mex -setup’ command,

3. If MS Visual Studio is not available. Find supported compilers for windows from this link. (I installed MS Visual Studio 2008 SP1 and it worked fine.)

4. go to libsvm-3.20/matlab directory and run the ‘make’ command. If successfull you’ll see svmtrain.mexw64 svmpredict.mexw64 lib libsvmread.mexw64 and libsvmwrite.mexw64 created

5. Run svmtrain or predict from matlab command window to check if they are working correctly.

Here’s some useful screencasts regarding this:

(Unfortunately, in Chinese, but we can learn from the visual instructions.)