许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  人脸检测器模型训练方法(从数据准备到模型部署)

人脸检测器模型训练方法(从数据准备到模型部署)

阅读数 6
点赞 0
article_banner

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

68个关键点的训练 数据集  (1.7GB):http://dlib.net/files/data/ibug_300W_large_face_landmark_dataset.tar.gz

   194个关键点的数据集(需要翻墙):http://stackoverflow.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. using namespace dlib;
  6. using namespace std;
  7. std::vector<std::vector<double> > get_interocular_distances (
  8. const std::vector<std::vector<full_object_detection> >& objects
  9. );
  10. int main(int argc, char** argv)
  11. {
  12. try
  13. {
  14. if (argc != 2)
  15. {
  16. cout << "Give the path to the examples/faces directory as the argument to this" << endl;
  17. cout << "program. For example, if you are in the examples folder then execute " << endl;
  18. cout << "this program by running: " << endl;
  19. cout << " ./train_shape_predictor_ex faces" << endl;
  20. cout << endl;
  21. return 0;
  22. }
  23. const std::string faces_directory = argv[1];
  24. dlib::array<array2d<unsigned char> > images_train, images_test;
  25. std::vector<std::vector<full_object_detection> > faces_train, faces_test;
  26. // 1. 载入训练集,测试集
  27. // load_image_dataset(images_train, faces_train, faces_directory+"/training_with_face_landmarks.xml");
  28. // load_image_dataset(images_test, faces_test, faces_directory+"/testing_with_face_landmarks.xml");
  29. // 68个点的训练数据集:http://dlib.net/files/data/ibug_300W_large_face_landmark_dataset.tar.gz
  30. load_image_dataset(images_train, faces_train, faces_directory + "/labels_ibug_300W_train.xml");
  31. load_image_dataset(images_test, faces_test, faces_directory + "/labels_ibug_300W_test.xml");
  32. shape_predictor_trainer trainer;
  33. // 测试中调节了 tree_depth参数:2,4, 5, 10
  34. // 测试机器为8核,set_num_threads使用8,训练时cpu:70%
  35. trainer.set_oversampling_amount(300);
  36. trainer.set_nu(0.05);
  37. trainer.set_tree_depth(5);
  38. trainer.set_num_threads(8);
  39. trainer.be_verbose();
  40. // 训练
  41. shape_predictor sp = trainer.train(images_train, faces_train);
  42. cout << "mean training error: "<<
  43. test_shape_predictor(sp, images_train, faces_train, get_interocular_distances(faces_train)) << endl;
  44. cout << "mean testing error: "<<
  45. test_shape_predictor(sp, images_test, faces_test, get_interocular_distances(faces_test)) << endl;
  46. // 保存模型
  47. serialize("sp.dat") << sp;
  48. std::string str;
  49. std::cin >> str;
  50. }
  51. catch (exception& e)
  52. {
  53. cout << "\nexception thrown!" << endl;
  54. cout << e.what() << endl;
  55. }
  56. }
  57. double interocular_distance (
  58. const full_object_detection& det
  59. )
  60. {
  61. dlib::vector<double,2> l, r;
  62. double cnt = 0;
  63. for (unsigned long i = 36; i <= 41; ++i)
  64. {
  65. l += det.part(i);
  66. ++cnt;
  67. }
  68. l /= cnt;
  69. cnt = 0;
  70. for (unsigned long i = 42; i <= 47; ++i)
  71. {
  72. r += det.part(i);
  73. ++cnt;
  74. }
  75. r /= cnt;
  76. return length(l-r);
  77. }
  78. std::vector<std::vector<double> > get_interocular_distances (
  79. const std::vector<std::vector<full_object_detection> >& objects
  80. )
  81. {
  82. std::vector<std::vector<double> > temp(objects.size());
  83. for (unsigned long i = 0; i < objects.size(); ++i)
  84. {
  85. for (unsigned long j = 0; j < objects[i].size(); ++j)
  86. {
  87. temp[i].push_back(interocular_distance(objects[i][j]));
  88. }
  89. }
  90. return temp;
  91. }

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


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删



相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空