windows下libtorch配置指南

libtorch配置指南

1. 安装cuda toolkit
   https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
   下载自己对应的版本安装

2. 安装CuDNN
   https://developer.nvidia.com/rdp/cudnn-download,解压后将lib/include/bin三个文件夹内的文件复制到cuda相应目录下,
   安装参照 https://blog.csdn.net/sinat_23619409/article/details/84202651

## cuda和cudnn版本必须严格和libtorch的cuda版本一致!!!

3. 安装 cmake
    https://cmake.org/

4. 安装make(可略)
    参照:https://blog.csdn.net/Nicholas_Liu2017/article/details/78323391

5. 下载libtorch Release版本
   https://pytorch.org/get-started, 下载压缩文件后解压到本地某位置。


 6. 参照教程:https://blog.csdn.net/gulingfengze/article/details/92013360
     要修改的地方:
     1. example-app.cpp
         修改: torch::jit::script::Module module = torch::jit::load("D:/project/WDD/model.pt");
         见: https://github.com/pytorch/pytorch/issues/23450
     2. 构建应用程序
         命令中根据自己的vs版本修改,我的是vs2019
         cmake -DCMAKE_PREFIX_PATH=D:\opencv\build\x64\vc15\lib;D:\libtorchR -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 16" ..

7. 将libtorch里lib文件夹下所有文件拷贝到项目下build/Release文件夹下


测试:

#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#include  // One-stop header.
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

int main() {
    std::string model_path = "D:\\project\\WDD/model_cpu.pt";
    std::string image_path = "D:\\data/glass_crack\\converted\\84\\img.png";

    torch::DeviceType device_type;
    device_type = torch::kCPU;
    device_type = torch::kCUDA;
    std::cout << device_type << endl;

    torch::jit::script::Module model = torch::jit::load(model_path); //Dice-se_resnext50_32x4d, , torch::kCUDA
    model.to(device_type);
    assert(module != nullptr);
    std::cout << "load model sucessfully.\n";

    // Create a vector of inputs.
    //std::vector inputs;
    //inputs.push_back(torch::ones({ 1, 3, 224, 224 }).to(torch::kCUDA));

    //load img and normalize
    Mat img = imread(image_path, 1); //[650 x 450]
    cv::cvtColor(img, img, CV_BGR2RGB);
    //std::cout << img.size() << '\n';
    if (img.empty())
    {
        printf("could not show image...");
        return -1;
    }
    //imshow("test", img);
    //waitKey(0);

    cv::Mat img_float;
    img.convertTo(img_float, CV_32FC3, 1.0f / 255.0f);   //[650 x 450]
    //std::cout << img_float.size() << '\n';

    auto tensor_image = torch::from_blob(img_float.data, { 1, img.rows, img.cols, 3 }).permute({ 0, 3, 1, 2 });// .to(torch::kCUDA);
    //std::cout << tensor_image.sizes() << '\n';

    //normalize
    //https://github.com/pytorch/pytorch/issues/14219
    tensor_image[0][0] = tensor_image[0][0].sub(0.485).div(0.229);  //[450, 650]
    tensor_image[0][1] = tensor_image[0][1].sub(0.456).div(0.224);
    tensor_image[0][2] = tensor_image[0][2].sub(0.406).div(0.225);
    tensor_image = tensor_image.to(device_type);
    std::cout << tensor_image.sizes() << '\n'; //[1, 3, 450, 650]

    std::vector inputs;
    inputs.emplace_back(tensor_image);
    //std::cout << tensor_image << '\n';

    // Execute the model and turn its output into a tensor.
    torch::Tensor out_tensor = model.forward(inputs).toTensor();

    // convert result to CV mat and save
    //std::cout << out_tensor.sizes() << '\n'; //[1, 1, 448, 648]
    out_tensor = out_tensor.squeeze(0).detach().cpu().permute({ 1,2,0 });
    out_tensor = out_tensor.mul(255).clamp(0, 255).to(torch::kU8);
    //std::cout << out_tensor.sizes()[0] << '\n';
    cv::Mat resultImg(out_tensor.sizes()[0], out_tensor.sizes()[1], CV_8UC1);
    std::memcpy((void*)resultImg.data, out_tensor.data_ptr(), sizeof(torch::kU8) * out_tensor.numel());
    //std::cout << resultImg.size() << '\n';
    imshow("result", resultImg);
    waitKey(0);
    imwrite("landscape_output.jpg", resultImg);
    //at::Tensor output = model.forward(img).toTensor();
    //std::cout << out_tensor << '\n';

    std::cout << "Done!\n";
    while (1);
}

cpu/gpu下pass

你可能感兴趣的:(深度学习,pytorch,caffe)