Saturday, December 20, 2014

Dlib 18.12 released

I just released the next version of dlib.  This time I added tools for computing 2D FFTs, Hough transforms, image skeletonizations, and also a simple and type safe API for calling C++ code from MATLAB.  Readers familiar with writing MATLAB mex functions know how much of a pain it is, but no longer!  Here is an example of a C++ function callable from MATLAB using dlib's new MATLAB binding API.  You can also compile it with CMake so building it is super easy. There is an example CMake file in the dlib/matlab folder showing how to set it up.  I also used this tool to give the MITIE project a simple MATLAB API. So you can see another example of how easy it is to set this up in the MITIE MATLAB example.  

There are also some fun new things in the pipe for the next dlib release (v18.13).  First, Patrick Snape, one of the main developers of the menpo project, is adding a Python interface to dlib's shape prediction tools. You can follow that over on dlib's github repo.  I'm also working on a single object tracker for OpenCV's Vision Challenge which I plan to include in the next version of dlib.


Unknown said...

hi Davis,

Thanks a lot for such a smart and handy lib!

Are you planing to add a more elaborate mexifying example?! In fact, I am trying to mexify the face detector example but I'm getting segmentation fault and then matlab crashes.
for the sake of debugging, I modified example_mex_function.cpp by adding only one line of call to "get_frontal_face_detector()". This compiles and links with no error but when I call the mex file, it crashes matlab! am I overlooking something here?!

Thanks for your help


Davis King said...

Thanks, I'm glad you like it.

There are a few more elaborate examples in MITIE like I wasn't going to add anymore any time soon.

Doing that shouldn't make it crash. What is the exact code you are using and how did you compile it? What compiler and OS?

Unknown said...

Thanks for your reply and sorry for the delay (I forgot to tick the follow_up box and then naively waiting for a notification email!)

I am on Ubuntu 14.04 and using g++ 4.8.2. Here is the example that you have provided, after adding only one line where I create a face detector object:

#include // required header!
void mex_function (
cout << "some_number: "<< some_number << endl;
frontal_face_detector detector = get_frontal_face_detector();// my only modification!

I touched nothing else! This compiles nicely but running example.m causes segmentation violation and crashes Matlab (2014a).

I hope I could state the problem clearly!



Unknown said...

missing part!

#include "dlib/image_processing/frontal_face_detector.h" // required header!
#include "dlib/matrix.h"

Davis King said...

And you compiled it with cmake? Maybe there is something wrong with the cmake setup. So you could try compiling it with the normal matlab mex command and see if that works.

I would debug it myself but I don't have access to matlab at the moment. But it should work and is regularly used on windows and sometimes on centos. It should work on Ubuntu as well. The underlying code (everything but the cmake scripts) works even in GNU Octave.

Unknown said...

Thx Davis. Yes, I used cmake but directly mexifying worked like a charm!

Davis King said...

Huh, you wouldn't happen to have two versions of matlab installed would you? CMake tries to find some matlab libraries that need to be linked into the mex file but maybe it's finding the wrong ones.

Unknown said...

no, only Matlab2014a ...

Davis King said...

Huh. Can you send me the output of the mex command when you compile it with either the -n or -v options? Hopefully -n or -v will show how it's invoking gcc and I can use that to fix the CMake script.

Unknown said...

there you go!the exact output:

Building with 'g++'.
Warning: You are using gcc version '4.8.2'. The version of gcc is not supported.
The version currently supported with MEX is '4.7.x'. For a list of currently
supported compilers see:
Warning: You are using gcc version '4.8.2-19ubuntu1)'. The version of gcc is not
supported. The version currently supported with MEX is '4.7.x'. For a list of
currently supported compilers see:
/usr/bin/g++ -c -DUSE_AVX_INSTRUCTIONS=ON -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT -DMX_COMPAT_32 -D_GNU_SOURCE -DMATLAB_MEX_FILE -I../.. -I../all/ -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/simulink/include" -ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/mosaddegh/Bureau/code/tools/dlib-18.11/dlib/matlab/Vahid_detector2.cpp -o /tmp/mex_1536521156987_4686/Vahid_detector2.o
/usr/bin/g++ -c -DUSE_AVX_INSTRUCTIONS=ON -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT -DMX_COMPAT_32 -D_GNU_SOURCE -DMATLAB_MEX_FILE -I../.. -I../all/ -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/simulink/include" -ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/mosaddegh/Bureau/code/tools/dlib-18.11/dlib/all/source.cpp -o /tmp/mex_1536521156987_4686/source.o
/usr/bin/g++ -pthread -Wl,--no-undefined -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2014a/extern/lib/glnxa64/" /tmp/mex_1536521156987_4686/Vahid_detector2.o /tmp/mex_1536521156987_4686/source.o -lpthread -lX11 -lpng -ljpeg -lz -Wl,-rpath-link,/usr/local/MATLAB/R2014a/bin/glnxa64 -L"/usr/local/MATLAB/R2014a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o Vahid_detector2.mexa64
rm -f /tmp/mex_1536521156987_4686/Vahid_detector2.o
rm -f /tmp/mex_1536521156987_4686/source.o
MEX completed successfully.

As you can see, I borrowed the matlab wrapper from the latest version of dlib but I am using dlib-18.11. this would not be any problem, rihgt?

Davis King said...

Maybe MX_COMPAT_32 needs to be defined. Are you using the 32bit version of ubuntu?

Could you try adding this at line 43 in dlib/matlab/cmake_mex_wrapper?


and then telling me what happens? :)

You might have to delete the previous cmake build folder and rerun cmake for it to take effect.

Unknown said...

No, unfortunately this option did not help. Matlab still crashes.

To make sure that we are on the same track, I'm modifying CMakeList like:
add_mex_function(Vahid_detector2 dlib -lpthread -lX11 -lpng -ljpeg -lz -DUSE_AVX_INSTRUCTIONS=ON -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT)

pls let me know if you want me to run more tests...

Davis King said...

Darn. I suppose it doesn't work if you use only this statement:

add_mex_function(Vahid_detector2 dlib)

Also, maybe adding _GNU_SOURCE to cmake_mex_wrapper is what is needed? So try adding in


to cmake_mex_wrapper. But if that doesn't fix it then I'm out of ideas.

Unknown said...

removing the rest and linking only to dlib does not help with the crash neither I'm afraid!

As for the rest of options, I already tried these two and many others but none of them fixed the crash.

thx anyway. The good news is that I'm not blocked by the cmake wrapper and direct mexification is working flawlessly.


Davis King said...

Ah well, thanks for helping debug it. At least it works with the mex command :)

RHV said...

Is it possible to use and to training with different landmark numbers? Both detect 68 landmarks in a frontal face by default. I would like to detect 39 landmarks only (I can train all right using the train_shape_predictor_ex however face_landmark_detection_ex outputs the attached message:

exception thrown!

Error detected at line 25.
Error detected in file /Users/Vareto/Documents/Dlib/dlib/../dlib/image_processing/render_face_detections.h.
Error detected in function std::vector dlib::render_face_detections(const std::vector &, const dlib::rgb_pixel).

Failing expression was dets[i].num_parts() == 68.
std::vector render_face_detections()
Invalid inputs were given to this function.
dets[0].num_parts(): 39

HChu said...

I keep getting warnings with Visual Studio 2015:
if(i == n)
else if(i >= 2*n)
// Step 4
UIntType z = x[i];
in "mersenne_twister.h" although I am not explicitly using it.

I think the warning is legitimate.