libtorch c++ 測試程序


在網上找了一個測試程序。使用cmake生成項目時報錯:

Unknown cmake build type:
Call Stack (most recent call first):
  D:/libtorch-gpu/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
  D:/libtorch-gpu/share/cmake/Torch/TorchConfig.cmake:39 (find_package)
  CMakeLists.txt:4 (find_package)
 
-- Configuring incomplete, errors occurred!
See also "E:/simnet-gpu/build/CMakeFiles/CMakeOutput.log".
See also "E:/simnet-gpu/build/CMakeFiles/CMakeError.log".

解決辦法:

點擊cnake-gui的右上角的“Add Entry",然后添加

 

 再一次config則通過。

配置 vs2017 x64 cuda10.0 libtorch1.0.0-cu10

后面再配置opencv就可以使用了。

 測試程序是:

#include <torch/torch.h>
#include <torch/script.h>
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <spdhelper.hpp>
#include <opencv2/opencv.hpp>
#include <BTimer.hpp>
#include "MTCNN.h"

int main(int argc, char* argv[])
{
    ENTER_FUNC;
    BTimer timer;

    std::string pnet_weight_path = std::string(MODEL_PATH) + "pnet.pt";
    std::string rnet_weight_path = std::string(MODEL_PATH) + "rnet.pt";
    std::string onet_weight_path = std::string(MODEL_PATH) + "onet.pt";

    TAlgParam alg_param;
    alg_param.min_face = 40;
    alg_param.scale_factor = 0.79;
    alg_param.cls_thre[0] = 0.6;
    alg_param.cls_thre[1] = 0.7;
    alg_param.cls_thre[2] = 0.7;

    TModelParam modelParam;
    modelParam.alg_param = alg_param;
    modelParam.model_path = {pnet_weight_path, rnet_weight_path, onet_weight_path};
    modelParam.mean_value = {{127.5, 127.5, 127.5}, {127.5, 127.5, 127.5}, {127.5, 127.5, 127.5}};
    modelParam.scale_factor = {1.0f, 1.0f, 1.0f};
    modelParam.gpu_id = 0;
    modelParam.device_type = torch::DeviceType::CUDA;



    MTCNN mt;
    mt.InitDetector(&modelParam);
    std::string img_path = std::string(MODEL_PATH) + "/../img/faces2.jpg";
    cv::Mat src = cv::imread(img_path);
    if(!src.data)
    {
        LOGE("cannot load image!");
        return -1;
    }
    std::vector<cv::Rect> outFaces;
    LOGI("warm up...");
    timer.reset();
    for(int i = 0; i < 5; i++)
        mt.DetectFace(src, outFaces);
    LOGI("warm up over, time cost: {}", timer.elapsed());

    timer.reset();
    //for(;;)
    mt.DetectFace(src, outFaces);
    LOGI(" cost: {}", timer.elapsed());

    for(auto& i : outFaces)
        cv::rectangle(src, i, {0,255,0}, 2);

    cv::imshow("result", src);
    cv::waitKey(0);
//    cv::imwrite("res2.jpg", src);
    LEAVE_FUNC;
    return 0;
}

運行結果是:

 

 只可惜debug版本過不去,只能靠輸出查看中間信息。

參考:https://blog.csdn.net/jacke121/article/details/88709686

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM