libtorch windows 使用 error汇总

 

先在python中进行模型转换

读取模型

    model = GetModel(opt)
    parameters_name = 'C:/code/testproject/Seg_prediction/checkpoints/007_LinHuiMin_FocalWDice2000_2_epoch_3.ckpt'
    model_CKPT = torch.load(parameters_name)
    model.load_state_dict(model_CKPT['state_dict'])

模型转成gpu,pt模型

    image_patch=torch.rand(1,1,120,120,120).cuda()
    modelpt=torch.jit.trace(model.module,image_patch)
    modelpt=modelpt.cuda()
    modelpt.eval()
    modelpt.save('weak_segment.pt')

也可以转成CPU,pt模型

    image_patch=torch.rand(1,1,120,120,120)
    modelpt=torch.jit.trace(model.module,image_patch)
    modelpt.eval()
    modelpt.save('weak_segment.pt')
    modelpt=torch.jit.load('weak_segment.pt',map_location='cpu')
    modelpt.eval()
    modelpt.save('weak_segment.pt')

这里按照我这种转换应该是没问题了,

下面是c++报错

错误1: “std”: 不明确的符号

解决办法:项目->属性->c/c++->语言->符合模式->选择否

错误2:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt"); 报错

Could not run 'aten::empty_strided' with arguments from the 'CUDA' backend. 'aten::empty_strided' is only available for these backends: [CPU, BackendSelect, Named, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, Tracer, Autocast, Batched, VmapMode].

解决办法

torch::jit::script::Module module = torch::jit::load 之前加 

LoadLibraryA("ATen_cuda.dll");
LoadLibraryA("c10_cuda.dll");
LoadLibraryA("torch_cuda.dll");
LoadLibraryA("torchvision.dll");

另外这种错误还有一种可能,你使用的dll库和你使用的libtorch版本不对应,我之前把项目考过去,用了另外一个版本的libtorch,dll没有换就有这个问题

错误3:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt");

转pt的版本与libtorch版本不一致造成的

错误4:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt");

THCudaCheck FAIL file=..\..\aten\src\THC\THCGeneral.cpp line=47 error=35 : CUDA driver version is insufficient for CUDA runtime version
cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at ..\..\aten\src\THC\THCGeneral.cpp:47

错误原因:cuda与模型转换pt的版本不一致

错误5:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt");

Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don't directly use any symbols from the CUDA library! One common culprit is a lack of -Wl,--no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don't depend on any of their symbols.  You can check if this has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library. (initCUDA at C:\w\b\windows\pytorch\aten\src\ATen/detail/CUDAHooksInterface.h:63)
(no backtrace available)

解决办法:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt");之前加上

LoadLibraryA("ATen_cuda.dll");
LoadLibraryA("c10_cuda.dll");
LoadLibraryA("torch_cuda.dll");
LoadLibraryA("torchvision.dll");

错误5:torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/weak_segment.pt")

PyTorch is not linked with support for cuda devices (getDeviceGuardImpl at C:\w\b\windows\pytorch\c10/core/impl/DeviceGuardImplInterface.h:216)
(no backtrace available)

解决办法与错误5一样

附上c++代码如下

int main() {


	try
	{
		
		/*try {
			LoadLibraryA("ATen_cuda.dll");
			LoadLibraryA("c10_cuda.dll");
			LoadLibraryA("torch_cuda.dll");
			LoadLibraryA("torchvision.dll");
			std::cout << torch::cuda::is_available() << std::endl;
			torch::Tensor tensor = at::tensor({ -1, 1 }, at::kCUDA);
		}
		catch (std::exception& ex) {
			std::cout << ex.what() << std::endl;
		}*/
		//torch::cuda::is_avaliable();
		std::string path = "C:/liaomingwei/image120.tif";
		int width;

		int height;//获取图片的高
		TIFF *tif = TIFFOpen(path.c_str(), "r");      //使用TIFFOpen函数以只读形式打开图像。
		int depth = TIFFNumberOfDirectories(tif);
		TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
		TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
		TIFFClose(tif);
		uint16 *nImage = new uint16[width*height*depth];
		uint8 *outImage = new uint8[width*height*depth];
		memset(outImage, 0, sizeof(uint8)*width*height*depth);
		memset(nImage, 0, sizeof(uint16)*width*height*depth);
		read_3d_buffer(path.c_str(), nImage, width, height, depth);
		float *cimage= new float[width*height*depth];
		memset(cimage, 0.0, sizeof(float)*width*height*depth);
		double all_mean1 = 168.5;
		double all_std1 = 500;
		for (int i = 0; i < depth; i++)
		{
			for (int j = 0; j < height; j++)
			{
				for (int k = 0; k < width; k++)
				{
					if (nImage[i*height * width + j * width + k] < 50)
						nImage[i*height * width + j * width + k] = 50;
					if (nImage[i*height * width + j * width + k] > 1500)
						nImage[i*height * width + j * width + k] = 1500;
					cimage[i*height * width + j * width + k] = (float(nImage[i*height * width + j * width + k] - all_mean1)) / all_std1;
				}
			}
		}
		//torch::DeviceType device_type1;
		//device_type1 = torch::kCUDA;
		//torch::Device device1(device_type1);
		//torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/data_enchance/weak_segment_1.pt", device1);
		LoadLibraryA("ATen_cuda.dll");
		LoadLibraryA("c10_cuda.dll");
		LoadLibraryA("torch_cuda.dll");
		LoadLibraryA("torchvision.dll");
		torch::jit::script::Module module = torch::jit::load("C:/liaomingwei/data_enchance/weak_segment.pt");
		torch::DeviceType device_type;
		if (torch::cuda::is_available()) {
			std::cout << "CUDA available! Training on GPU" << std::endl;
			device_type = torch::kCUDA;
		}
		else {
			std::cout << "Training on CPU" << std::endl;
			device_type = torch::kCPU;
		}
		torch::Device device(device_type);
		module.to(device);

		std::cout << "ok\n";

		torch::Tensor ten_img = torch::from_blob(cimage, { 1,1,120,120,120}, torch::kFloat).to(device);
		//ten_img = ten_img.permute({ 0, 3, 1, 2 });
		ten_img = ten_img.toType(torch::kFloat);
		//ten_img = ten_img.div(255);
		//int h = ten_img.sizes()[2], w = ten_img.sizes()[3];


		// 建立一个输入,维度为(1,3,224,224),并移动至cuda
		std::vector<torch::jit::IValue> inputs;
		inputs.push_back(ten_img);

		// Execute the model and turn its output into a tensor.
		at::Tensor output = module.forward(inputs).toTensor();
		torch::Tensor output_max = output.argmax(1);
		output_max = output_max.squeeze();
		output_max = output_max.mul(255).to(torch::kU8);
		output_max = output_max.to(torch::kCPU);
		memcpy(outImage,output_max.data_ptr(),sizeof(torch::kU8)*output_max.numel());
		write_3D_buffer("C:/liaomingwei/test.tif", outImage, width, height, depth);
		//std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
		/*unsigned short *image;
		Vec3i image_shape;
		Vec3i patch_size;
		Vec3i overlap;
		Vec3i n_patches;
		n_patches[0] = ceil(image_shape[0] / (patch_size[0] - overlap[0]));
		n_patches[1] = ceil(image_shape[1] / (patch_size[1] - overlap[1]));
		n_patches[2] = ceil(image_shape[2] / (patch_size[2] - overlap[2]));
		int patches_num = n_patches[0] * n_patches[1] * n_patches[2];
		double all_mean1 = 168.5;
		double all_std1 = 500;
		for (int i = 0; i < n_patches[2]; i++)
		{
			for (int j = 0; j < n_patches[1]; j++)
			{
				for (int k = 0; k < n_patches[0]; k++)
				{
					if (image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] < 50)
						image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] = 50;
					if (image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] > 1500)
						image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] = 1500;
					image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] = (image[i*n_patches[1] * n_patches[0] + j * n_patches[0] + k] - all_mean1) / all_std1;
				}
			}
		}
		std::vector<Vec3i> patchindex = compute_patch_indices(image_shape, n_patches, overlap, 0);*/
	}
	catch(std::exception& e){
		std::cout << e.what() << std::endl;
	}
	// Deserialize the ScriptModule from a file using torch::jit::load().
	
}

错误6

解决方法:头文件添加方法

#undef slots
#include <torch/torch.h>
#include <torch/script.h> // One-stop header.
#define slots Q_SLOTS

直接运行,输出: “cuda::is_available(): 0”,显卡未调用起来。

Windows10系统下使用LibTorch 1.5、1.6,使用Visual Studio进行C++开发时,Torch::cuda::is_available()返回值为0(使用cmake来构建工程,是可以正常编译和执行)。

解决方法:

1)使用VS2017及以上版本;

2)windows上装的cuda版本需要与下载的libtorch的cuda版本相对应;

3)在“属性 --> 链接器 --> 命令行 --> 其他选项”中添加:

/INCLUDE:?warp_size@cuda@at@@YAHXZ

 

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值