如何解决从训练有素的火炬模型中获取预测
我正在使用转移学习来微调inception_v3模型。训练模型并保存最佳版本后,我尝试使用它为我的测试集生成预测。下面是我尝试一张图片的示例。
img_test=Image.open("img.png")
#Perform same transformations to image that the model used
transform_pipeline = transforms.Compose([
transforms.RandomHorizontalFlip(),transforms.ToTensor(),transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])
img_test = transform_pipeline(img_test)
# I believe this is adding in the batch size of 1,but in looking around online it looked like I needed it
img = img_test.unsqueeze(0)
img = Variable(img)
model_ft(img)
当我执行以上操作时,我会得到
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
这似乎暗示我的模型权重在我的gpu上,而变量在cpu上,我该如何移动一个或另一个以使用它,或者引用位于相反处理器上的一个?
解决方法
正如错误所述,似乎模型的输入(您的img_test)在cpu中。
在通过预先训练的模型发送图像之前,尝试将其移至cuda:
device = torch.device('cuda' if torch.cuda.is_available())
img_test = img_test.to(device)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。