【问题标题】:PyTorch model gives IndexError: index 0 is out of bounds for dimension 0 with size 0PyTorch 模型给出 IndexError: index 0 is out of bounds for dimension 0 with size 0
【发布时间】:2020-03-06 18:49:54
【问题描述】:

我在 pytorch 网站上按照以下教程进行操作: https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html

我已经在眼睛数据库上训练了我的模型来检测瞳孔。我保存训练好的模型并打开它进行评估。 问题是如果图片中没有眼睛,模型就会崩溃。

为了重现,我首先将模型打印在工作图片上:

 [{'boxes': tensor([[126.9488, 112.5852, 141.5698, 127.4215]], device='cuda:0'), 'labels': tensor([1], device='cuda:0'), 'scores': tensor([1.0000], device='cuda:0'), 'masks': tensor([[[[0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          ...,
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.],
          [0., 0., 0.,  ..., 0., 0., 0.]]]], device='cuda:0')}]

然后在闭眼的图片上:(所以什么都没有检测到)

[{'boxes': tensor([], device='cuda:0', size=(0, 4)), 'labels': tensor([], device='cuda:0', dtype=torch.int64), 'scores': tensor([], device='cuda:0'), 'masks': tensor([], device='cuda:0', size=(0, 1, 255, 255))}]

最后一个应该看起来像第一个。

我得到的错误是这样的:

img1 = Image.fromarray(prediction[0]['masks'][0, 0].mul(255).byte().cpu().numpy())
IndexError: index 0 is out of bounds for dimension 0 with size 0

我知道我正在尝试到达列表之外的某个地方,但我无法真正启动预测零点列表,因为它是一个加载模型。模型是一个 .pt 文件

如果你还需要什么,请告诉我

亲切的问候

【问题讨论】:

  • 这不是一个很好的解决方案,但你总是可以尝试除此之外。

标签: tensorflow pytorch bounds


【解决方案1】:

您可以在此处简单地添加一个空masks张量检查(或检查boxeslabels等):

first_mask = prediction[0]['masks']
# check if no of elements in `masks` is zero
if first_mask.nelement() != 0:
    img1 = Image.fromarray(first_mask[0, 0].mul(255).byte().cpu().numpy())

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2019-09-21
    • 2021-12-17
    • 2017-05-20
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多