今天部署模型时遇到一个问题:
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
报错原因:
原因是cpu和cdua 使用的不一致,我这里是因为模型加载时没用将其分配到GPU中,做个记录。
解决办法:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
model = torch.load('best.pt')
model = model.to(device) # 加上这一句
或者
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
model = torch.load('best.pt',map_location=device)
参考:
https://blog.csdn.net/momo_mo520/article/details/105668810