天天看點

Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA

文章目錄

  • 環境準備
  • 模型轉換
  • VS編譯
  • QA

Windows PyTorch CPU Debug/Release VS/CMake

環境準備

  • libtorch:https://pytorch.org/
Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA
  • 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++——正常——附加包含目錄
    Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA
    • 連結器——正常——附加庫目錄
      Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA
    • 連結器——輸入——附加依賴項
      Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA
  • 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“: 不明确符号
Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA

A :項目-》屬性-》c/c+±》語言-》符合模式 改成否

Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA
  • Q:“c10::guts::min”:未找到比對的重載函數(CMakefile可以編譯,但是VS編譯報錯)
    Windows下libtorch C++部署CPU版環境準備模型轉換VS編譯QA

A:Visual Studio需要VS2017及以上,因為VS2015不完全支援C++14,見https://github.com/pytorch/pytorch/issues/48165

下一篇: troubleshoot

繼續閱讀