首页 > 解决方案 > 如何摆脱此错误:max() 收到无效的参数组合 - 得到 (str, int),但预期其中之一:*(张量输入)

问题描述

我使用 FCN ResNet50 模型对文档图像进行语义分割。我一直在尝试解决这个问题,但到目前为止还没有成功。这是 google colab 上模型的链接:https ://colab.research.google.com/drive/1slJilG1ZBOsk6AqM6AOUaaCxHFSXVMCM?usp=sharing

这是错误:

TypeError: Traceback (most recent call last)
<ipython-input-12-f6d0f244c03c> in <module>()
     13 
     14 model_ft = train_model(final_model, train_dl, criterion, optimizer_ft, exp_lr_scheduler,
---> 15                        num_epochs=25)

<ipython-input-11-683ce68860de> in train_model(model, dataloaders, criterion, optimizer, scheduler, num_epochs)
     31                 with torch.set_grad_enabled(phase == 'train'):
     32                     outputs, aux = model(inputs.float())
---> 33                     _, preds = torch.max(outputs, 1)
     34                     loss = criterion(outputs, labels)
     35 

TypeError: max() received an invalid combination of arguments - got (str, int), but expected one of:
 * (Tensor input)
 * (Tensor input, Tensor other, *, Tensor out)
 * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)
 * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)

标签: pythonmachine-learningcomputer-visionpytorchresnet

解决方案


在 torchvision 中附加预训练的分割模型

model(inputs.float())

返回一个字典,而不是一个数组。因此,您传递给 max 的是该字典中的一组键(因为字典在迭代时被视为键的集合,在这种情况下恰好是字符串)。只需从您的模型中读取正确的密钥。确切的密钥取决于您加载的模型。

outputs_dict = model(inputs.float())
print(outputs_dict.keys())

查看https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/_utils.py

我希望密钥被称为“out”,辅助输出被放置在“aux”中。


推荐阅读