【问题标题】:KeyError: 0 in python 3.7KeyError:python 3.7 中的 0
【发布时间】:2019-12-02 08:36:13
【问题描述】:

我正在为 3 个对象类训练 maskRCNN,遵循 git 中的 train_shapes 样本。我使用 VIA 2.0.8 将图像注释提取到 JSON。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-bbed02f10f39> in <module>
      3 for image_id in image_ids:
      4     image = dataset.load_image(image_id)
----> 5     mask, class_ids = dataset.load_mask(image_id)
      6     visualize.display_top_masks(image, mask, class_ids, dataset.class_names)

K:\Python\maskRCNN\samples\dining\dining.py in load_mask(self, image_id)
    186             mask[:, :, i] = mask[:, :, i] * occlusion
    187             occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
--> 188         # Map class names to class IDs.
    189         class_ids = np.array([self.class_names.index(s[0]) for s in polygons])
    190         return mask.astype(np.bool), class_ids.astype(np.int32)

K:\Python\maskRCNN\samples\dining\dining.py in <listcomp>(.0)
    186             mask[:, :, i] = mask[:, :, i] * occlusion
    187             occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
--> 188         # Map class names to class IDs.
    189         class_ids = np.array([self.class_names.index(s[0]) for s in polygons])
    190         return mask.astype(np.bool), class_ids.astype(np.int32)

KeyError: 0

这是它调用的代码:

def load_mask(self, image_id):
    """Generate instance masks for an image.
   Returns:
    masks: A bool array of shape [height, width, instance count] with
        one mask per instance.
    class_ids: a 1D array of class IDs of the instance masks.
    """
    # If not a dining dataset image, delegate to parent class.
    image_info = self.image_info[image_id]
    if image_info["source"] != "dining":
        return super(self.__class__, self).load_mask(image_id)

    # Convert polygons to a bitmap mask of shape
    # [height, width, instance_count]
    info = self.image_info[image_id]
    polygons = info["polygons"]
    count = len(info["polygons"])
    mask = np.zeros([info["height"], info["width"], count],
                    dtype=np.uint8)
    for i, p in enumerate(info["polygons"]):
        # Get indexes of pixels inside the polygon and set them to 1
        rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
        mask[rr, cc, i] = 1

    # Handle occlusions
    occlusion = np.logical_not(mask[:, :, -1]).astype(np.uint8)
    for i in range(count-2, -1, -1):
        mask[:, :, i] = mask[:, :, i] * occlusion
        occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
    # Map class names to class IDs.
    class_ids = np.array([self.class_names.index(s[0]) for s in polygons])
    return mask.astype(np.bool), class_ids.astype(np.int32)

什么是 KeyError?这是我的多边形变量的数据结构错误吗?

【问题讨论】:

    标签: python-3.x keyerror


    【解决方案1】:

    Key errors and how to handle them

    简而言之。无论多边形是什么,它似乎都包含某种字典对象,当您在class_ids = np.array([self.class_names.index(s[0]) for s in polygons]) 行中循环时,这些对象被分配给s,然后您尝试访问键0,除非没有.

    我的建议是通过找出其中的 polygons 和每个 s 到底是什么来进行调试。然后计算出您对s[0] 的期望。在不知道每个s 看起来的情况下,很难给出更具体的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多