ubuntu 18 编译libtorch 文件部署demo

编译好opencv, 下载好对应的libtorch 到pytorch官网上并解压到任意文件夹

编译所需文件: 建议文件夹:test_two , mkdir build , 将以下2个文件放入build 

main.cpp

makelists.txt

一张图像 7.jpg

main.cpp内容:

#include <torch/script.h> // One-stop header.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <memory>

//https://pytorch.org/tutorials/advanced/cpp_export.html

using namespace std;

std::string image_path = "/home/xiaou/ubuntu_libtorch/test_two/img";


int main(int argc, const char* argv[]) {
    // char image_path = "/home/xiaou/ubuntu_libtorch/test_two/img";
    // Deserialize the ScriptModule from a file using torch::jit::load().
    torch::jit::script::Module module = torch::jit::load("/home/xiaou/ubuntu_libtorch/test_two/model_cc.pt");
    //torch::jit::script::Module module = torch::jit::load("/home/xiaou/ubuntu_libtorch/test_two/squeezenet.pt");
    //std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("/home/weipenghui/Project-dev/Cpp/testLibTorch2/model/model_squeezenet_utkface.pt");

    //assert(module != nullptr);
    std::cout << "ok\n";


    //输入图像
    auto image = cv::imread(image_path +"/"+ "7.jpg",cv::ImreadModes::IMREAD_COLOR);
    cv::Mat image_transfomed;
    cv::resize(image, image_transfomed, cv::Size(112,112));
    cv::cvtColor(image_transfomed, image_transfomed, cv::COLOR_BGR2RGB);

    // 图像转换为Tensor
    torch::Tensor tensor_image = torch::from_blob(image_transfomed.data, {image_transfomed.rows,         image_transfomed.cols,3},torch::kByte);
    tensor_image = tensor_image.permute({2,0,1});
    tensor_image = tensor_image.toType(torch::kFloat);
    tensor_image = tensor_image.div(255);

    tensor_image = tensor_image.unsqueeze(0);


    // 网络前向计算
    // Execute the model and turn its output into a tensor.
    at::Tensor output = module.forward({tensor_image}).toTensor();
    std::cout<<"out\n";
    std::cout<<output;
    auto max_result = output.max(1,true);
    auto max_index = std::get<1>(max_result).item<float>();

    if (max_index == 0){
        cv::putText(image, "male", cv::Point(50, 50), 1, 1,cv::Scalar(0, 255, 255));
    }else{
        cv::putText(image, "female", cv::Point(50, 50), 1, 1,cv::Scalar(0, 255, 255));
    }

    cv::imwrite("/home/xiaou/ubuntu_libtorch/test_two/build/result7.jpg", image);
    cv::imshow("image", image);
    //cv::waitkey()
}

 

makelists.txt 如下:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test_two)

#include_directories(/home/weipenghui/Lib-dev/opencv_3.4.3_contrib/opencv-3.4.3/build/install_cv/include)
# set(CMAKE_PREFIX_PATH "/home/weipenghui/Lib-dev/opencv_3.4.3_contrib/opencv-3.4.3/build/install_cv")
find_package(OpenCV REQUIRED)

set(CMAKE_PREFIX_PATH
        /home/xiaou/Downloads/libtorch-shared-with-deps-1.4.0+cpu/libtorch
        /home/xiaou/Documents/opencv-3.4.3/include)

find_package(Torch REQUIRED)

add_executable(test_two main.cpp)
target_link_libraries(test_two ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET test_two PROPERTY CXX_STANDARD 11)

  

命令行cd 到build 目录 执行: 

cmake -DCMAKE_PREFIX_PATH=/home/xiaou/Downloads/libtorch-shared-with-deps-1.4.0+cpu/libtorch

make

执行:./test_two

 


 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值