vs2019调用pytorch gpu模型预测踩坑记录

我在电脑上实现的gpu调用版本为,cuda10.2 ,cudnn10.2 ,libtorch1.8.1+cu102 显卡驱动版本为441.22,要注意显卡驱动和cuda匹配,网上很多可以查到。下面说一下我都遇见了那些问题。
1,未加载kernelbase.pdb,c10::Error, 这个问题很可能是你版本没有对应,或者vs没有成功调用cuda。用以下两句话检测是否调用成功:如果没有函数说明你没有包含头文件#include<torch/torch.h>

    std::cout << torch::cuda::cudnn_is_available() << endl;//输出为1,成功
	std::cout << torch::cuda::is_available()<<endl;//输出为1,成功

如果输出为零,在属性页,链接器-命令行-其他选项加入:
/INCLUDE:?warp_size@cuda@at@@YAHXZ
如果加入这句话后,报这句话无法解析,将你libtorch下lib文件中的.lib的项全部加入属性页的附加依赖项中。
2,未加载kernelbase.pdb,std::runtime_error, 我一直以为是库不对应或者模型问题,但实际上我的问题出在了声明前向传播容器的时候。

    std::vector<torch::jit::IValue> inputs;
	inputs.push_back(torch::ones({ 1, 3,512, 512 }, torch::kCUDA));

我之前在这里没有加入torch::KCUDA这个参数导致声明的还是用于cpu的,加上这句话顺利通过。
最后附上我在vs2019,qt工程下调用成功的代码:

#include "QtGuiApplication2.h"
#undef slots
#include <torch/script.h>
#include<torch/torch.h>
#define slots Q_SLOTS
#include<iostream>
using namespace std;
QtGuiApplication2::QtGuiApplication2(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	std::cout << torch::cuda::cudnn_is_available() << endl;//输出为1,成功
	std::cout << torch::cuda::is_available()<<endl;//输出为1,成功
	torch::jit::script::Module module = torch::jit::load("F:/pytorch-gpu.pt",torch::kCUDA);
	module.to(at::kCUDA);
	torch::NoGradGuard on_grad;
	std::vector<torch::jit::IValue> inputs;
	inputs.push_back(torch::ones({ 1, 3,512, 512 }, torch::kCUDA));
	at::Tensor output = module.forward(inputs).toTensor();
	std::cout << output << std::endl;
}

3 在实现了上面gpu参数输出后,在进行图片预测时,报未加载opencv_world331.pdb,这个错误可能不是opencv造成的,我的问题是将模型放到gpu预测获取预测结果后没有放回到cpu在进行处理。

at::Tensor outputs =module.forward(inputs).toTensor();
outputs=outputs.to(torch::KCPU); //这里要将预测结果放回到cpu
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用PyTorch的LSTM进行GPU预测,你需要确保你的计算设备是GPU,并且将模型和数据移动到GPU上。你可以使用`.to('cuda')`方法将模型和张量移动到GPU上。下面是一个示例代码片段,展示了如何进行GPU预测: ```python # 将模型移动到GPU上 model.to('cuda') # 将输入数据移动到GPU上 seq = seq.to('cuda') # 进行预测 with torch.no_grad(): model.hidden = (torch.zeros(1, 1, model.hidden_layer_size).to('cuda'), torch.zeros(1, 1, model.hidden_layer_size).to('cuda')) prediction = model(seq).item() ``` 在这个示例中,我们首先将模型移动到GPU上,然后将输入数据也移动到GPU上。接下来,我们使用`torch.no_grad()`上下文管理器来禁用梯度计算,以提高预测的速度。然后,我们使用模型进行预测,并将结果移回CPU上,以便进一步处理。 请注意,要运行这段代码,你的计算设备必须支持CUDA,并且你必须已经安装了支持CUDA的PyTorch版本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [基于 PyTorch + LSTM 进行时间序列预测(附完整源码)](https://blog.csdn.net/m0_59596937/article/details/129779542)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值