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

RuntimeError:输入类型(torch.FloatTensor)和权重类型(torch.cuda.FloatTensor)应该相同,或者输入应该是一个MKLDNN张量,而权重是一个密集张量

经过检查发现:输入类型(torch.FloatTensor)和权重类型(torch.cuda.FloatTensor)应该相同。

报错:我们的权重是cuda类型(GPU训练得到),而输入(要测试的数据)不是cuda类型。 

 采用GPU训练的模型,不能直接在CPU上使用,也要放到GPU中预测

用的模型参数是在cuda上(gpu)训练的,在使用其进行测试时,需要将要测试的数据也放到GPU上,即:img.cuda()

model.eval()        # 模型转化为测试类型
with torch.no_grad(): # 节约内存性能,with是自动处理对文件的关闭操作
    output = model(img.cuda())

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐