深度学习项目部署:Libtorch使用torch::jit::load加载模型时报错(已解决)

torch::jit::load加载模型时报错

源码

#include <torch/script.h>
#include <iostream>
#include <memory>
#include <torch/torch.h>

int main() {
	auto device = torch::kCPU;
	// Deserialize the ScriptModule from a file using torch::jit::load().
	torch::jit::script::Module module = torch::jit::load(FileName, device);

	assert(module != nullptr);
	std::cout << "ok\n";
	// Create a vector of inputs.
	std::vector<torch::jit::IValue> inputs;
	inputs.push_back(torch::ones({ 1,3,320,192 }));

	// Execute the model and turn its output into a tensor.
	auto o = module.forward(std::move(inputs));
	at::Tensor output = o.toTensor();

	std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
	while (1);
}

报错

0x00007FFA38539329处(位于XXX.exe中)引发的异常: Microsoft C++ 异常: c10::Error,位于内存位置 0x00000031321EF5F0 处。

环境

torch、torchvision版本:
在这里插入图片描述
Libtorch版本:

libtorch-win-shared-with-deps-1.10.0+cu113

Libtorch版本和Pytorch版本对应;
TorchScript导出是用的官方的export.py(device=cpu)导出的文件;
VS中该配的属性都配了。


解决方法

使用类和指针,只需要把具体方法实现都放在类里就可以了:

#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>


class Detector {
public:
	/***
	 * @brief constructor
	 * @param model_path - path of the TorchScript weight file
	 * @param device_type - inference with CPU/GPU
	 */
	Detector(const std::string& model_path, bool use_gpu);
	
	// other methond
	...
	
private:
	torch::jit::script::Module module_;
	torch::Device device_;
	bool half_;
};

Detector::Detector(const std::string& model_path, bool use_gpu) :device_(torch::kCPU)
{
	if (torch::cuda::is_available() && use_gpu)
	{
		//std::cout << "use cuda...\n";
		device_ = torch::kCUDA;
	}
	else
	{
		//std::cout << "use cpu...\n";
		device_ = torch::kCPU;
	}

	try {
		// Deserialize the ScriptModule from a file using torch::jit::load().
		module_ = torch::jit::load(model_path);
	}
	catch (const c10::Error& e) {
		std::cerr << "Error loading the model!\n";
		std::exit(EXIT_FAILURE);
	}

	half_ = (device_ != torch::kCPU);
	module_.to(device_);

	if (half_) {
		module_.to(torch::kHalf);
	}

	module_.eval();
}


int main() 
{
	std::shared_ptr<Detector> detector = std::make_shared<Detector>(yourpath, true);
}
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 36
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值