RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor 报错

报错环境

在使用pytorch训练好的模型,进行测试时,报错
部分代码为

model = torch.load("../save_model/mynn_30.pth")
model.eval()
with torch.no_grad(): 
    output = model(image)
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor

分析原因

分析一下报错信息,Input type 和 weight type不同,其中 weight type是cuda.FloatTensor,而 input type是FloatTensor。一个是cuda,一个不是,仔细想了一下,模型使用gpu训练的,而现在使用的input没有加gpu.

修改报错

方法一:对input进行加gpu

model.eval()
with torch.no_grad():
    image = image.cuda()
    output = model(image)

方法二:导入模型时,加入map_location参数,使得device指向cpu

model = torch.load("../save_model/mynn_30.pth", map_location=torch.device('cpu'))

两种方法都可以解决这个问题

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值