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.

17 comments :

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

    Sahel

    ReplyDelete
  2. Thanks, I'm glad you like it.

    There are a few more elaborate examples in MITIE like https://github.com/mit-nlp/MITIE/blob/master/examples/matlab/extract_entities.cpp. 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?

    ReplyDelete
  3. hi,
    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!
    #include
    ...
    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!

    cheers

    S

    ReplyDelete
  4. missing part!

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

    ReplyDelete
  5. 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.

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

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

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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:
    http://www.mathworks.com/support/compilers/current_release.
    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:
    http://www.mathworks.com/support/compilers/current_release.
    /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/mexFunction.map" /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?

    ReplyDelete
  10. 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?

    ADD_DEFINITIONS(-DMX_COMPAT_32)

    and then telling me what happens? :)

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

    ReplyDelete
  11. 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...


    ReplyDelete
  12. 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

    ADD_DEFINITIONS(-DMX_COMPAT_32)
    ADD_DEFINITIONS(-D_GNU_SOURCE )

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

    ReplyDelete
  13. 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.

    cheers

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

    ReplyDelete
  15. Is it possible to use http://dlib.net/train_shape_predictor_ex.cpp.html and http://dlib.net/face_landmark_detection_ex.cpp.html 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

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

    I think the warning is legitimate.

    ReplyDelete