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.)

Gradle configurations for running your Java program using NetBeans 8

What is Gradle?

Gradle is a project automation tool that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the more traditional XML form of declaring the project configuration.” (Wikipedia)

Setting up Gradle project on NetBeans

Go to ‘Files’

Select Gradle from Categories, then select ‘Single Gradle Project’

Provide project name and click on finish

Project skeleton

Project_Folder/build/ – Holds the compiled class and jars

Project_Folder/build/jar – Will hold the jar file of your project

Project_Folder/src/  – Sources and resources goes here

Project_Folder/src/main – Your source codes that’ll be ran from jar

Project_Folder/src/main/java – main source code

Project_Folder/src/main/resources – resources accessed by main source code

Project_Folder/src/test/java – unit tests code (eg. JUnit)

Project_Folder/src/test/resource – unit test code accessed resources

build.gradle – Gradle configurations goes here (This is where most gradle configs go)

settings.gradle – Holds project name and some other stuff

build.gradle Configurations

1. External dependency resolution

Most of the time we will have some external jar libraries that we’ll need import in our code. We tell gradle about them here. There are two ways we can access them 1) From a maven repository 2) from local drive.

a. From maven repository

Sample code : 

dependencies {
compile ‘org.json:json:20140107’
compile ‘net.sf.opencsv:opencsv:2.3’
compile ‘net.sf.supercsv:super-csv:2.2.0’

}

This tells gradle to download the external jars from a maven repository, store them in a predefined folder set by the IDE and use them for our code. The syntax is

compile ‘group:name:version’

For example, if we want to add the org.jason library to our project we go to http://mvnrepository.com/. Type in ‘org.json’ in the search box and it will give us the link to the repo. Then we chose our selected version of the jar and click on the gradle tab. Which will give us the code that we add in the above script. 

mvnrepo

Fig. picture of the org.json repo page

b. From a local directory

Sample script:

dependencies {
 compile fileTree(dir: ‘lib’, include: ‘**/*.jar’)

}

This will access the jar files from Project_Folder/lib  directory. 

2. Runtime inclusion of external libraries

Even though we’ll able to build the project using the build script so far. But we need to embed the external jars inside our project jar to make it run. We need to use the following snippet for that – 

jar {
    from {
       configurations.compile.collect {
           it.isDirectory() ? it : zipTree(it)
       }
       configurations.runtime.collect {
           it.isDirectory() ? it : zipTree(it)
       }
    }

}

This will unzip the external jars and include them inside the project jar. 

3. Unsuppressing console output

Gradle will suppress the System.out and System.err functions which is a hassle during bug hunt. To enable add the following snippet to the build script – 

test {
//makes the standard streams (err and out) visible at console when running tests
testLogging.showStandardStreams = true
}

How to create a Jar file in Netbeans 8.0

To do this, follow these steps

1 Click on project name. Right-click and select Properties

2 Click on Packaging

3 Check Compress JAR File

4 Accept changes, click ok

5 Click the button in the ribbon tab with the hammer (clean and build project)

6 Go to the directory where your project is stored

7 Look in the dist folder

It should be located there if everything went alright. Make sure you aren’t excluding any files when the jar file is being set up in the dist folder.