人脸检测器模型训练方法

01 dlib人脸检测器原始数据获取

68个关键点的训练数据集(1.7GB):http://dlib.net/files/data/ibug_300W_large_face_landmark_dataset.tar.gz 
194个关键点的数据集(需要*):http://*.com/questions/36711905/dlib-train-shape-predictor-ex-cpp?answertab=votes#tab-top

02 训练代码

dlib中examples中的代码。 
dlib/examples/train_shape_predictor_ex.cpp

 
  1. // dlib/examples/train_shape_predictor_ex.cpp

  2. #include <dlib/image_processing.h>

  3. #include <dlib/data_io.h>

  4. #include <iostream>

  5.  
  6. using namespace dlib;

  7. using namespace std;

  8.  
  9. std::vector<std::vector<double> > get_interocular_distances (

  10. const std::vector<std::vector<full_object_detection> >& objects

  11. );

  12.  
  13. int main(int argc, char** argv)

  14. {

  15. try

  16. {

  17. if (argc != 2)

  18. {

  19. cout << "Give the path to the examples/faces directory as the argument to this" << endl;

  20. cout << "program. For example, if you are in the examples folder then execute " << endl;

  21. cout << "this program by running: " << endl;

  22. cout << " ./train_shape_predictor_ex faces" << endl;

  23. cout << endl;

  24. return 0;

  25. }

  26. const std::string faces_directory = argv[1];

  27. dlib::array<array2d<unsigned char> > images_train, images_test;

  28. std::vector<std::vector<full_object_detection> > faces_train, faces_test;

  29. // 1. 载入训练集,测试集

  30. // load_image_dataset(images_train, faces_train, faces_directory+"/training_with_face_landmarks.xml");

  31. // load_image_dataset(images_test, faces_test, faces_directory+"/testing_with_face_landmarks.xml");

  32. // 68个点的训练数据集:http://dlib.net/files/data/ibug_300W_large_face_landmark_dataset.tar.gz

  33. load_image_dataset(images_train, faces_train, faces_directory + "/labels_ibug_300W_train.xml");

  34. load_image_dataset(images_test, faces_test, faces_directory + "/labels_ibug_300W_test.xml");

  35.  
  36. shape_predictor_trainer trainer;

  37.  
  38. // 测试中调节了 tree_depth参数:2,4, 5, 10

  39. // 测试机器为8核,set_num_threads使用8,训练时cpu:70%

  40. trainer.set_oversampling_amount(300);

  41. trainer.set_nu(0.05);

  42. trainer.set_tree_depth(5);

  43. trainer.set_num_threads(8);

  44. trainer.be_verbose();

  45. // 训练

  46. shape_predictor sp = trainer.train(images_train, faces_train);

  47.  
  48. cout << "mean training error: "<<

  49. test_shape_predictor(sp, images_train, faces_train, get_interocular_distances(faces_train)) << endl;

  50.  
  51. cout << "mean testing error: "<<

  52. test_shape_predictor(sp, images_test, faces_test, get_interocular_distances(faces_test)) << endl;

  53. // 保存模型

  54. serialize("sp.dat") << sp;

  55. std::string str;

  56. std::cin >> str;

  57. }

  58. catch (exception& e)

  59. {

  60. cout << "\nexception thrown!" << endl;

  61. cout << e.what() << endl;

  62. }

  63. }

  64.  
  65. double interocular_distance (

  66. const full_object_detection& det

  67. )

  68. {

  69. dlib::vector<double,2> l, r;

  70. double cnt = 0;

  71.  
  72. for (unsigned long i = 36; i <= 41; ++i)

  73. {

  74. l += det.part(i);

  75. ++cnt;

  76. }

  77. l /= cnt;

  78.  
  79. cnt = 0;

  80. for (unsigned long i = 42; i <= 47; ++i)

  81. {

  82. r += det.part(i);

  83. ++cnt;

  84. }

  85. r /= cnt;

  86.  
  87. return length(l-r);

  88. }

  89.  
  90. std::vector<std::vector<double> > get_interocular_distances (

  91. const std::vector<std::vector<full_object_detection> >& objects

  92. )

  93. {

  94. std::vector<std::vector<double> > temp(objects.size());

  95. for (unsigned long i = 0; i < objects.size(); ++i)

  96. {

  97. for (unsigned long j = 0; j < objects[i].size(); ++j)

  98. {

  99. temp[i].push_back(interocular_distance(objects[i][j]));

  100. }

  101. }

  102. return temp;

  103. }

03 训练结果

194关键点训练情况: 
tree_depth=2,num_threads=2, 
Release版本训练时间 5+小时 
Debug版本训练时间 148+小时(训练一定要使用Release版本) 
tree_depth=2,sp.dat=44.6MB,占用内存最大11GB 
tree_depth=10,sp.data=11GB

68关键点训练结果: 
tree_depth=2,num_threads=8,CPU:70% 内存:20+GB 6+小时 sp.data=15.8MB 
tree_depth=4,num_threads=8,CPU:70% 内存:20+GB 12+小时 sp.data=63.3MB 
tree_depth=5,num_threads=8,CPU:70% 内存:20+GB 16+小时 sp.data=126MB

 
  1. tree_depth=5,num_threads=8

  2. mean training error: 0.0479476

  3. mean testing error: 0.0586204

04 测试效果

使用dlib中examples中的代码测试。 
dlib/examples/face_landmark_detection_ex.cpp 
也可食用dlib提供的训练模型: 
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

原图: 
人脸检测器模型训练方法 
检测效果图: 
人脸检测器模型训练方法

05 参考

http://blog.csdn.net/jcx1314/article/details/65937839 
http://blog.csdn.net/elaine_bao/article/details/53054533