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“: 不明确符号

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6iR2amlA-1625969975158)(C:\Users\Voilencer\AppData\Roaming\Typora\typora-user-images\image-20210706234358713.png)]

A :项目-》属性-》c/c+±》语言-》符合模式 改成否

  • Q:“c10::guts::min”:未找到匹配的重载函数(CMakefile可以编译,但是VS编译报错)
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jy7PbOYS-1625969975159)(C:\Users\Voilencer\AppData\Roaming\Typora\typora-user-images\image-20210711101538572.png)]

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

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐