Windows下libtorch C++部署CPU版
文章目录环境准备模型转换VS编译QAWindows PyTorch CPU Debug/ReleaseVS/CMake环境准备libtorch:https://pytorch.org/OpenCVVisual Studio 2017(Visual Studio需要VS2017及以上,因为VS2015不完全支持C++14)模型转换PyTorch1.0后,可以通过TorchScript的方式创建序列化
·
Windows PyTorch CPU Debug/Release VS/CMake
环境准备
- libtorch:https://pytorch.org/
- OpenCV
- Visual Studio 2017
(Visual Studio需要VS2017及以上,因为VS2015不完全支持C++14)
模型转换
- PyTorch1.0后,可以通过TorchScript的方式创建序列化和可优化的模型。可以通过Tracing和Script将一个Python代码转化为TorchScript代码,被C++所调用
def save(net, input, save_path):
net.eval()
traced_script_module = torch.jit.trace(net, input)
traced_script_module.save(save_path)
VS编译
-
环境配置
- C/C++——常规——附加包含目录
-
链接器——常规——附加库目录
-
链接器——输入——附加依赖项
-
main.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <torch/script.h>
#include <torch/torch.h>
int main(int argc, char *argv[]) {
bool running_gpu_mode = false;
torch::DeviceType device_type;
if (torch::cuda::is_available() && running_gpu_mode) {
device_type = torch::kCUDA;
}
else {
device_type = torch::kCPU;
}
torch::Device device(device_type);
std::string model_path = "../model/net_cpu.pt";
torch::jit::script::Module module = torch::jit::load(model_path);
if (torch::cuda::is_available() && running_gpu_mode) {
module.to(at::kCUDA);
}
cv::Mat img, img_trans;
std::string img_path = "../data/1.jpg";
img = cv::imread(img_path, -1);
cv::cvtColor(img, img_trans, cv::COLOR_BGR2GRAY);
cv::resize(img_trans, img_trans, cv::Size(140, 32));
//img_trans.convertTo(img_trans, CV_32F, 1.0 / 255.0);
// Change the Image into Tensor for prediction
torch::Tensor tensor_image = torch::from_blob(img_trans.data, { 32, 140, 1 }, torch::kByte);
tensor_image = tensor_image.permute({ 2,0,1 });
tensor_image = tensor_image.toType(torch::kFloat);
// distribution between 0 and 1
tensor_image = tensor_image.div(255);
// normalize, value between -1 and 1
tensor_image = tensor_image.sub(0.5);
tensor_image = tensor_image.div(0.5);
tensor_image = tensor_image.unsqueeze(0);
//std::cout<<tensor_image<<std::endl;
if (running_gpu_mode && torch::cuda::is_available()) {
torch::Device device(device_type);
tensor_image = tensor_image.to(device);
}
std::vector<torch::jit::IValue> inputs;
inputs.push_back(tensor_image);
//at::Tensor result = module.forward(inputs).toTensor();
at::Tensor result = module.forward({ tensor_image }).toTensor();
//std::cout<<result<<std::endl;
auto res = result.max(2);
auto conf = std::get<0>(res).squeeze(1);
auto pred = std::get<1>(res).squeeze(1);
int size = 0;
int plateNum[10];
float plateConf[10];
for (int i = 0; i < 32; i++) {
if (pred[i].item<int>() != 0 && !(i > 0 && (pred[i - 1].item<int>() == pred[i].item<int>()))) {
//std::cout << pred[i].item<int>() - 1 << std::endl;
//std::cout << size << std::endl;
plateNum[size] = pred[i].item<int>() - 1;
//std::cout << conf[i].item<float>() << std::endl;
plateConf[size] = conf[i].item<float>();
std::cout << alphabet[pred[i].item<int>() - 1];
size++;
}
}
std::cout << std::endl;
system("pause");
return 0;
}
QA
- Q:#include “torch/script.h“ “std“: 不明确符号
A :项目-》属性-》c/c+±》语言-》符合模式 改成否
- Q:“c10::guts::min”:未找到匹配的重载函数(CMakefile可以编译,但是VS编译报错)
A:Visual Studio需要VS2017及以上,因为VS2015不完全支持C++14,见https://github.com/pytorch/pytorch/issues/48165
更多推荐
所有评论(0)