Convert deeplearning model : dlib to tensorflow



    How to convert 
        Dlib's deep learnning weight to tensorflow weight?



CODE ABOUT THIS BLOG - https://github.com/heewinkim/dlib2tensorflow



this tutorial explain specific dlib's model weight, dlib_face_recognition_resnet_model_v1


1. Download Dlib
# download
$ wget http://dlib.net/files/dlib-19.19.tar.bz2

# unzip 
$ tar -xvf dlib-19.19.tar.bz2



2. Download dlib's model weight   

 linux system[ i've done with ubuntu 16.04]
# download
$ wget http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2

# unzip
$ bzip2 dlib_face_recognition_resnet_model_v1.dat.bz2 --decompress



3. write code

# dlib_to_xml.cpp 

#include <dlib/data_io.h>
#include <dlib/dir_nav.h>
#include <dlib/dnn.h>
#include <dlib/image_processing.h>

namespace recognizer {

template <template <int, template <typename> class, int, typename> class block,
          int N, template <typename> class BN, typename SUBNET>
using residual = dlib::add_prev1<block<N, BN, 1, dlib::tag1<SUBNET>>>;

template <template <int, template <typename> class, int, typename> class block,
          int N, template <typename> class BN, typename SUBNET>
using residual_down = dlib::add_prev2<dlib::avg_pool<
    2, 2, 2, 2, dlib::skip1<dlib::tag2<block<N, BN, 2, dlib::tag1<SUBNET>>>>>>;

template <int N, template <typename> class BN, int stride, typename SUBNET>
using block =
    BN<dlib::con<N, 3, 3, 1, 1,
                 dlib::relu<BN<dlib::con<N, 3, 3, stride, stride, SUBNET>>>>>;

template <int N, typename SUBNET>
using ares = dlib::relu<residual<block, N, dlib::affine, SUBNET>>;

template <int N, typename SUBNET>
using ares_down = dlib::relu<residual_down<block, N, dlib::affine, SUBNET>>;

template <typename SUBNET> using alevel0 = ares_down<256, SUBNET>;

template <typename SUBNET>
using alevel1 = ares<256, ares<256, ares_down<256, SUBNET>>>;

template <typename SUBNET>
using alevel2 = ares<128, ares<128, ares_down<128, SUBNET>>>;

template <typename SUBNET>
using alevel3 = ares<64, ares<64, ares<64, ares_down<64, SUBNET>>>>;

template <typename SUBNET> using alevel4 = ares<32, ares<32, ares<32, SUBNET>>>;

using anet_type = dlib::loss_metric<dlib::fc_no_bias<
    128,
    dlib::avg_pool_everything<alevel0<alevel1<alevel2<alevel3<alevel4<
        dlib::max_pool<3, 3, 2, 2,
                       dlib::relu<dlib::affine<dlib::con<
                           32, 7, 7, 2, 2, dlib::input_rgb_image>>>>>>>>>>>>;

} // namespace recognizer
int main(int argc, char **argv) {

  // This tool expects dlib_face_recognition_resnet_model_v1.dat to be in the  // same folder from where you would run it  //  // download the weights from here  // http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2 
  recognizer::anet_type net;

  // deserialize the weights  dlib::deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;

  // serialize them as xml  dlib::net_to_xml(net, "dlib_face_recognition_resnet_model_v1.xml");

  return 0;
}


4. Compile & covert dlib weight to xml file


$ g++ -std=c++11 -O3 -Idlib-19.19 ./dlib-19.19/dlib/all/source.cpp -lpthread -lX11 dlib_to_xml.cpp -o dlib_to_xml


# have to locate "dlib_face_recognition_resnet_model_v1.dat" in same directory with 'dlib_to_xml'
# this covert make a file "dlib_face_recognition_resnet_model_v1.xml"
$ ./dlib_to_xml




5. python environment setting (for covert to tensorflow) (using pyenv)

<if you already set the python environment, skip below>
$ pyenv virtualenv 3.6.1 dlib_to_xml
$ pyenv activate dlib_to_xml

< have to do this part >
$ pip install -r requirements.txt

6. compare between dlib and tensorflow

$ python main.py --xml-path ./dlib_face_recognition_resnet_model_v1.xml


Finally, you can get tensorflow model weight file 'dlib_face_recognition_resnet_model_v1.h5'
and can find saved_model in 'saved_model/'





댓글