dlib初识 c++代码

网站 http://dlib.net/ 这是一个提供机器学习的算法库,比如它提供的深度学习的算法包就特小,速度快。

神经网络:

这是我见过的比较简洁的网络模型写法、实现,所有的包编译完不到80M。用于人脸检测的模型参数一共十几kb。比如说人脸检测的模型是这么写的:

template <long num_filters, typename SUBNET> using con5d = dlib::con<num_filters,5,5,2,2,SUBNET>;
template <long num_filters, typename SUBNET> using con5  = dlib::con<num_filters,5,5,1,1,SUBNET>;

template <typename SUBNET> using downsampler  = dlib::relu<dlib::affine<con5d<32, dlib::relu<
                dlib::affine<con5d<32, dlib::relu<dlib::affine<con5d<16,SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5  = dlib::relu<dlib::affine<con5<45,SUBNET>>>;

using net_type = dlib::loss_mmod<dlib::con<1,9,9,1,1,rcon5<rcon5<rcon5<
        downsampler<dlib::input_rgb_image_pyramid<dlib::pyramid_down<6>>>>>>>>;

里面可直接看得到的有卷积convolution、激活层relu;affine是不是仿射变换,一种归一化层没去细究。这代码可以在其官网的示例中看到(http://dlib.net/dnn_mmod_face_detection_ex.cpp.html),其中示例中列车了网络参数的下载地址。

网络模型加载:

string path_to_detector = "mmod_human_face_detector.dat";
net_type net;
dlib::deserialize(path_to_detector) >> net; 

结果的输出和读取:
 

vector<Rect> faces;  
auto dets = net(cimg_small);
cout<<dets.size();
for (auto det : dets){//type(rt):mmod_rect
   dlib::rectangle rt=det.rect;
   long tl_x, tl_y;
   unsigned long h, w;
   tl_x, tl_yrt.left(), rt.top();
   h ,w = rt.height(),rt.width();
   cout<<"rt ->left():"<<rt.left();
   faces.push_back(Rect(tl_x,tl_y,w,h));
}

 输出的dets类型,可以通过查看dlib/dnn/loss来查看,类型vector<mmod_rect>;而相应的mmod_rect是在dlib/image_processing/full_object_detection.h:132:    struct mmod_rect。

数据格式的转换:

dlib::cv_image<dlib::bgr_pixel> cimg_small(image);

这个可以参考这个链接。相关的文件在dlib/opencv/cv_image.h中,也是因为这,dlib和cv的namespace不能同时使用。

 

或者下面这个链接的做法更直接一点,链接

matrix<rgb_pixel> img;
cv::Mat image = cv::imread(path);
array2d< bgr_pixel> arrimg(image.rows, image.cols);
dlib::assign_image(img, cv_image<rgb_pixel>(image));

 更多的转换方法,参考这里:Dlib格式与Opencv之间的转化

更多说明:一份好的教程能说明一切,dlib有一份非常好的教程,在tools/python/src/cnn_face_detector.cpp:18:class cnn_face_detection_model_v1中,里面有一整套的处理方式

编译:

undefined reference to `dlib::tt::add(float, dlib::tensor&, float, dlib::tensor const&)'
//在dlib/cudnn/tensor_tool下面没有找到add。在http://dlib.net/最新版的里面却又相应的文件。所以重新编译安装。
//其中,搜寻namesapce tt可以定位到上面说的文件

 编译时,需要用到cmake,在dlib有个例子,https://github.com/davisking/dlib#compiling-your-own-c-programs-that-use-dlib。里面有用的只有三行。

add_subdirectory(../dlib dlib_build)
#重点是这儿,第一个是头文件的位置,比如相对于dlib/example下的cmake来说,其像对位置是父文件夹下的dlib,所以在自己工程里面写上绝对路径;
#第二个文件是在build下的编译文件,比如在dlib中的文件路径是example/build/dlib_build


add_executable(assignment_learning_ex assignment_learning_ex.cpp)
target_link_libraries(assignment_learning_ex dlib::dlib)
#这俩是常规的编译文件,不过注意target那个第二个变量

一份Demo:

//tools/python/src/cnn_face_detector.cpp:18:class cnn_face_detection_model_v1
vector<Rect> detect (Mat img, const int upsample_num_times ){
    vector<Rect> faces;
    dlib::matrix<dlib::rgb_pixel>  image;
    dlib::assign_image(image, dlib::cv_image<dlib::rgb_pixel>(img));

    //matrix<dlib::rgb_pixel> ;
    //dlib::load_image(cimg_small, inputName);
    string path_to_detector = "mmod_human_face_detector.dat";
    net_type net;
    dlib::deserialize(path_to_detector) >> net; 
    // Upsampling the image will allow us to detect smaller faces but will cause the
    // program to use more RAM and run longer.
    unsigned int levels = upsample_num_times;
    dlib::pyramid_down<2> pyr;
    while (levels > 0)
    {
        levels--;
        pyramid_up(image, pyr);
    }
    
    auto dets = net(image);
    
    cout<<"dets.size()"<<dets.size()<<"img.size():"<<image.size()<<
    "img.nr():"<<image.nr()<<"img.nc():"<<image.nc()<<endl;

    for (auto det : dets){//type(rt):mmod_rect
        dlib::rectangle rt = pyr.rect_down(det.rect, upsample_num_times);
        long tl_x, tl_y;
        unsigned long h, w;
        tl_x = rt.left()>0? rt.left(): 0;
        tl_y = rt.top()>0? rt.top() : 0;
        h = rt.height();
        w = rt.width();
        cout<<"rt ->left():"<<tl_x<<"y "<<tl_y<<"h "<< h <<"w "<< w <<endl;
        faces.push_back(Rect(tl_x,tl_y,w,h));
    }
    return faces;
}

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
里面包括贝叶斯网络学习,SVM向量机学习等优秀的数学学习算法。 Dlib 18.1 发布了,除了一些 bug 修复外,该版本还包含值得关注的改进: 更精确的 SURF 特征提取器 更快的切削平面解算器 对非常大的矩阵计算奇异值分解的程序 对大数据集进行相关分析的工具 简单的工具用来编写并行循环 Dlib是一个使用现代C++技术编写的跨平台的通用库,遵守Boost Software licence. 主要特点如下: 1.完善的文档:每个类每个函数都有详细的文档,并且提供了大量的示例代码,如果你发现文档描述不清晰或者没有文档,告诉作者,作者会立刻添加。 2.可移植代码代码符合ISO C++标准,不需要第三方库支持,支持win32、Linux、Mac OS X、Solaris、HPUX、BSDs 和 POSIX 系统 3.线程支持:提供简单的可移植的线程API 4.网络支持:提供简单的可移植的Socket API和一个简单的Http服务器 5.图形用户界面:提供线程安全的GUI API 6.数值算法:矩阵、大整数、随机数运算等 7.机器学习算法: 8.图形模型算法: 9.图像处理:支持读写Windows BMP文件,不同类型色彩转换 10.数据压缩和完整性算法:CRC32、Md5、不同形式的PPM算法 11.测试:线程安全的日志类和模块化的单元测试框架以及各种测试assert支持 12.一般工具:XML解析、内存管理、类型安全的big/little endian转换、序列化支持和容器类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值