How to build Caffe

Building Caffe from source is easy. There are two options provided: using a Makefile or using CMake. I found that using Makefile was easier, since CMake was erroneously complaining about -lpthreads (which is a Clang library, not a GCC library).

  • Download or clone the source code of Caffe from here.

  • Copy the Makefile config file, open it in an editor and check whether all the paths are set correctly:

$ cp Makefile.config.example Makefile.config
$ vim Makefile.config
  • To build Caffe:
$ make all

MatCaffe

Building support for MATLAB to use Caffe takes a few more steps:

  • In Makefile.config, set the MATLAB_DIR to the path that contains bin/mex. On my computer, this was the path /usr/local/MATLAB/R2014a.

  • Compiling the MATLAB wrapper gave an error about C++11 on my Ubuntu 14.04 due to the older GCC compilers. So, I had to add -std=c++11 to the CXXFLAGS variable in Makefile as shown here.

  • Finally, compiling MatCaffe is easy:

$ make all matcaffe
Advertisements

Visual Studio Code extensions that I use

  • CPP Tools: The official extension for working with C++ code. Automatically indexes all code in the currently open directory, offers auto-completion and syntax highlighting.

  • Python by Don Jayamanne: There are many Python extensions, but this seems to be the most popular one. Syntax highlighting, indexing and code completion.

  • Vim: There are many Vim extensions, but this seems to be the most popular one. It has entire universes to traverse before it can be as good as Vrapper, the Vim extension for Eclipse. This VSCode extension offers very basic navigation and editing commands.

  • Git Blame: This extension does one little thing that I need everyday to work with code from other people: know who modified a line of code. This extension shows that for the current line in the status bar.

  • Matlab: I need to regularly browse through some MATLAB files. This extension offers syntax highlighting of Matlab files.

Tried with: Visual Studio Code 1.4 and Ubuntu 16.04

Reading text in the wild

This project helps in localizing and recognizing text in natural images.

To try it:

  • Download their NIPS DLW 2014 models and unzip it.
  • Their MEX code is compiled for a Mac, so we need to recompile it for Linux.
  • Open matconvnet/Makefile. Set the CUDAROOT and MATLABROOT variables to the paths on your system.
  • Run make in matconvnet directory. This builds the MEX files for your system.
  • Now run the runmodels.m script in MATLAB and it should work.

How to use MATLAB without GUI

MATLAB has a GUI that is great for editing code, inspecting runtime values and debugging. However, there are some situations when you may want to run MATLAB without the GUI. For example, you are running Matlab from over SSH and X cannot be used or is too slow over the network. Thankfully, MATLAB provides a decent console version of itself.

  • It can be invoked as:
$ matlab -nodisplay
  • If your EDITOR environment variable is set, you can edit files using edit foo.m at the MATLAB command prompt.
  • The full power of debugging is available by using the debug commands as described here.
  • Some people suggest using the -nojvm option. This is not recommended. Many MATLAB features, like the parallel cluster require the JVM to work.

Tried with: MATLAB R2014a and Ubuntu 14.04

How to debug at command window in MATLAB

One of the killer features of MATLAB is its excellent debugging features in GUI mode. Anyone who has used the Visual Studio debugger will be right at home in MATLAB, since the keyboard shortcuts and mouse operations for debugging are the same. However, sometimes you may be using MATLAB in its -nodesktop mode where you only have access to its command window.

In these situations, you can still debug MATLAB scripts using the debugging commands:

  • dbstop in foo.m at 600: Set breakpoint in foo.m file at line 600.
  • dbstop if error: Indicate MATLAB that you want debugging to be stopped at the instructions that caused an error.
  • dbstatus: List all the breakpoints that are currently set.
  • dbclear all: Remove all breakpoints.
  • dbclear in foo.m: Remove all breakpoints in foo.m.
  • dbclear in foo.m at 600: Remove breakpoint set in foo.m at line 600.
  • dbcont: Continue execution until next breakpoint.
  • dbquit: Exit debug mode. Execution will be terminated.
  • dbstack: List the function call stack.
  • dbstep: Execute next instruction. Same as F10 in Visual Studio.
  • dbstep in: Step into next instruction. Same as F11 in Visual Studio.
  • dbstep out: Step out of next instruction. Same as Shift-F11 in Visual Studio.
  • To print the value of any variable, just type its name.
  • size: Get the size of any variable.
  • whos: Get the list of variables at current scope.

Reference: Debugging documentation of MATLAB

How to expand matrix in MATLAB

Sometimes, you may want to increase the size of a matrix and pad the new locations with a value, like 0 or 1. Doing this is super easy in MATLAB, but the syntax will raise some eyebrows:

% m already exists, say of size 4x10
m(6,12) = 0;
% m is now of size 6x12
% Only the new locations are intialized with 1
% The old cells have their old values

How to view call stack in MATLAB

Unlike most IDEs, MATLAB does not show the function call stack in a separate window.

  • To view the call stack, go to the Editor tab (at the top) and look for the Function Call Stack dropdown. It shows the names of the functions, but no line numbers. But choosing a call from the dropdown goes to the relevant line in the Editor window directly.

  • To view the call stack in the Command Window, use the command dbstack. This shows the stack with line numbers. The function call can be clicked to view the line in the Editor window.

Tried with: MATLAB R2014a and Ubuntu 14.04

How to switch between windows in MATLAB

MATLAB has keyboard shortcuts to switch to any of the different types of windows it displays. These shortcuts are hard-coded and cannot be configured or changed from the Preferences.

Some of the shortcuts I find useful are:

  • Command window: Ctrl + 0
  • Editor: Ctrl + Shift + 0
  • Workspace: Ctrl + 3
  • Variables editor: Ctrl + Shift + 3

The full list can be seen at the bottom of this page.

Tried with: MATLAB R2014a and Ubuntu 14.04

MATLAB parallel pool error

Problem

I ran a MATLAB script that uses a parallel pool of workers. It failed with this error:

The client lost connection to worker 4. This might be due to network problems, or the interactive communicating job might have errored

The corresponding matlab_crash_dump file had this stack trace starting from mkl.so:

[  0] 0x00007f995128a1c5        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+16769477 mkl_blas_avx_sgemm_mscale+00001253
[  1] 0x00007f995115ba1c        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+15530524 mkl_blas_avx_xsgemm+00000204
[  2] 0x00007f99505a1a5c        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+03234396 mkl_blas_xsgemm+00000316
[  3] 0x00007f9950529720        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+02742048
[  4] 0x00007f9950525d5a        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+02727258 mkl_blas_sgemm+00001386
[  5] 0x00007f99503b8ba5        /usr/local/MATLAB/R2014a/bin/glnxa64/mkl.so+01231781 sgemm+00000377
[  6] 0x00007f991b803154    /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so+01315156 cblas_sgemm+00000372

Solution

To run a parallel pool of workers, we need to use the libmkl_rt.so provided by Intel. I added the path of this library path, which was /opt/intel/mkl/lib/intel64 in my case, to my LD_LIBRARY_PATH. I also set the BLAS_VERSION environment variable to libmkl_rt.so.

After this I checked if everything worked fine by going to Parallel -> Manage cluster profiles -> Local -> Validation profiles -> Validate. All the parallel test tasks were validated successfully. My script too worked fine after this.

Tried with: MATLAB R2014a and Ubuntu 14.04

Unable to parse command history line in MATLAB

Problem

Everytime I start MATLAB it reports an error in my command history with the error message: Unable to parse command history line

Solution

Open the file History.xml in the MATLAB Preferences directory. You can find this directory using the command prefdir. Either find the offending entry in this XML file and delete it or just delete the entire file itself.

Tried with: MATLAB R2014a and Ubuntu 14.04