pytorch - pytorch 评分图像集和评估结果
问题描述
在 Pytorch 中通过使用 GPU (cuda) 需要对一组给定训练好的 NN 图像进行评分。以下代码旨在对一组转换后的图像逐个评分。
model.to('cuda')
model.eval()
for ii, (inputs, classes) in enumerate(dataloaders['test']):
inputs, classes = inputs, classes
results = model.forward(inputs)
ps = torch.exp(results)
错误堆栈:
RuntimeError Traceback (most recent call last)
<ipython-input-24-948390e2b25a> in <module>()
5 for ii, (inputs, classes) in enumerate(dataloaders['test']):
6 inputs, classes = inputs, classes
----> 7 results = model(inputs)
8 ps = torch.exp(results)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/vgg.py in forward(self, x)
40
41 def forward(self, x):
---> 42 x = self.features(x)
43 x = x.view(x.size(0), -1)
44 x = self.classifier(x)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
---> 91 input = module(input)
92 return input
93
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
299 def forward(self, input):
300 return F.conv2d(input, self.weight, self.bias, self.stride,
--> 301 self.padding, self.dilation, self.groups)
302
303
RuntimeError:预期为 torch.FloatTensor 类型的对象,但为参数 #2 'weight' 找到了类型 torch.cuda.FloatTensor
在 GPU (cuda) 上制作的模型。
解决方案
这解决了问题并提供了准确率。
model.to('cuda')
model.eval()
accuracy = 0
for ii, (inputs, classes) in enumerate(dataloaders['test']):
inputs, classes = inputs.to('cuda'), classes.to('cuda')
# Forward and backward passes
with torch.no_grad():
output = model.forward(inputs)
ps = torch.exp(output)
equality = (classes.data == ps.max(dim=1)[1])
accuracy += equality.type(torch.FloatTensor).mean()
accuracy = accuracy/len(dataloaders['test'])
print(accuracy)
推荐阅读
- python - 在 Google Cloud Function 中运行 Scrapy 蜘蛛
- firebase - 如何查看 Firebase Cloud Functions 制作的 Cron Job 的日志?
- javascript - Javascript Pipe function - why do all standard implementations return a function which takes an argument
- c# - 尽管重新安装了 Erlang 和 RabbitMQ,但 RabbitMQ 队列没有被删除
- wordpress - WordPress - 如何在不破坏任何其他查询的情况下仅将 tax_query 添加到自定义帖子类型
- android - 限制在 ConstraintLayout 之外的视图不接收触摸事件
- php - optgroup 中选项的 PHP 分组
- mysql - 如何使用 JOIN 和 BETWEEN where 子句加速 MYSQL 查询?
- thymeleaf - 如何将变量传递给 session.attribute.contains() thymeleaf?
- python - How to get the number of values from a database field for the last 10 days