首页 > 解决方案 > 使用 Pytorch Mask RCNN 时的零除错误

问题描述

我在包含多个视频的数据集上使用预训练的 torchvision MaskRCNN 模型,将视频逐帧传递给模型。然而,在 MaskRCNN 模型的前向传递中,我得到以下错误语句:

RuntimeError                              Traceback (most recent call last)

----> 1 output = model(images,targets)

4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/generalized_rcnn.py in forward(self, images, targets)
     64             original_image_sizes.append((val[0], val[1]))
     65 
---> 66         images, targets = self.transform(images, targets)
     67         features = self.backbone(images.tensors)
     68         if isinstance(features, torch.Tensor):

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/transform.py in forward(self, images, targets)
     42                 raise ValueError("images is expected to be a list of 3d tensors "
     43                                  "of shape [C, H, W], got {}".format(image.shape))
---> 44             image = self.normalize(image)
     45             image, target_index = self.resize(image, target_index)
     46             images[i] = image

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/transform.py in normalize(self, image)
     62         mean = torch.as_tensor(self.image_mean, dtype=dtype, device=device)
     63         std = torch.as_tensor(self.image_std, dtype=dtype, device=device)
---> 64         return (image - mean[:, None, None]) / std[:, None, None]
     65 
     66     def torch_choice(self, l):

RuntimeError: ZeroDivisionError

images在这段代码中是一个大小为 431 的列表,表示视频中的帧数,列表中的每个项目都是形状为 3 x 256 x 320 的火炬张量,分别表示通道 x 高度 x 宽度。Targets这里包含列表字典形式的标签、掩码和边界框坐标。列表的长度targets也是 431,每个项目都包含有关每个单独帧的信息。

我使用的代码与本教程中的代码相似除了该教程处理静态图像,而且我正在处理视频,因此我只对该代码进行了一些小的更改。

标签: pythoncomputer-visionpytorch

解决方案


推荐阅读