【问题标题】:Mask RCNN: IndexError: boolean index did not match indexed array掩码 RCNN:IndexError:布尔索引与索引数组不匹配
【发布时间】:2020-01-14 00:36:10
【问题描述】:

我正在做一个项目,我需要针对 SpaceNet 数据集训练 Mask RCNN。

所以,当我尝试训练我的模型时,会出现很多警告和错误。

错误信息是:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-11-a73fb1f7a961> in <module>
      8             learning_rate=config.LEARNING_RATE,
      9             epochs=10,
---> 10             layers='heads')
     11 
     12 # Training - Stage 2

~\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources)
   2372             max_queue_size=100,
   2373             workers=workers,
-> 2374             use_multiprocessing=True,
   2375         )
   2376         self.epoch = max(self.epoch, epochs)

~\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1656             use_multiprocessing=use_multiprocessing,
   1657             shuffle=shuffle,
-> 1658             initial_epoch=initial_epoch)
   1659 
   1660     @interfaces.legacy_generator_methods_support

~\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    179             batch_index = 0
    180             while steps_done < steps_per_epoch:
--> 181                 generator_output = next(output_generator)
    182 
    183                 if not hasattr(generator_output, '__len__'):

~\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py in data_generator(dataset, config, shuffle, augment, augmentation, random_rois, batch_size, detection_targets, no_augmentation_sources)
   1707                     load_image_gt(dataset, config, image_id, augment=augment,
   1708                                 augmentation=augmentation,
-> 1709                                 use_mini_mask=config.USE_MINI_MASK)
   1710 
   1711             # Skip images that have no instances. This can happen in cases

~\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py in load_image_gt(dataset, config, image_id, augment, augmentation, use_mini_mask)
   1263     _idx = np.sum(mask, axis=(0, 1)) > 0
   1264     mask = mask[:, :, _idx]
-> 1265     class_ids = class_ids[_idx]
   1266     # Bounding boxes. Note that some boxes might be all zeros
   1267     # if the corresponding mask got cropped out.

IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 650

警告是:

ERROR:root:Error processing image {'id': 219, 'source': 'yapi', 'path': None, 'width': 650, 'height': 650} 回溯(最近一次通话最后): 文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py”,第 1710 行,在 data_generator use_mini_mask=config.USE_MINI_MASK) 文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py”,第 1266 行,在 load_image_gt class_ids = class_ids[_idx] IndexError:布尔索引与维度 0 上的索引数组不匹配;维度为 1 但对应的布尔维度为 650

--

ERROR:root:Error processing image {'id': 448, 'source': 'yapi', 'path': None, 'width': 650, 'height': 650} 回溯(最近一次通话最后): 文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py”,第 1710 行,在 data_generator use_mini_mask=config.USE_MINI_MASK) 文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet_MaskRCNN\mrcnn\model.py”,第 1266 行,在 load_image_gt class_ids = class_ids[_idx] IndexError:布尔索引与维度 0 上的索引数组不匹配;维度为 1 但对应的布尔维度为 650

--

还有针对 image_id 的警告:219-348-444-448-3986-3023

【问题讨论】:

    标签: python-3.x tensorflow keras deep-learning boolean


    【解决方案1】:

    IndexError: 布尔索引与第 0 维的索引数组不匹配;维度为 1 但对应的布尔维度为 650

    此错误表明您正在尝试将 1 个类 ID 传递到需要 650 个类的地方。

    def load_mask(self, image_id):
        """Load instance masks for the given image.
    
        Different datasets use different ways to store masks. Override this
        method to load instance masks and return them in the form of am
        array of binary masks of shape [height, width, instances].
    
        Returns:
            masks: A bool array of shape [height, width, instance count] with
                a binary mask per instance.
            class_ids: a 1D array of class IDs of the instance masks.
        """
        # Override this function to load a mask from your dataset.
        # Otherwise, it returns an empty mask.
        logging.warning("You are using the default load_mask(), maybe you need to define your own one.")
        mask = np.empty([0, 0, 0])
        class_ids = np.empty([0], np.int32)
        return mask, class_ids
    

    这是 load_mask 函数,它接收一个对象类数组作为 class_ids。您必须以自己的方式实现它,以便每个掩码都有一个相应的对象类。例如,我就是这样做的:

    首先,我从使用 labelimg 创建的 xml 文件中提取边界框及其各自的标签:

    # extract bounding boxes from an annotation file
    def extract_boxes(self, filename):
        # load and parse the file
        tree = ElementTree.parse(filename)
        # get the root of the document
        root = tree.getroot()
        # extract each object
        boxes = list()
        for object in root.findall('.//object'):
            box_class_list = list()
            #find bbox coordinates
            for box in object.findall('.//bndbox'):
                xmin = int(box.find('xmin').text)
                ymin = int(box.find('ymin').text)
                xmax = int(box.find('xmax').text)
                ymax = int(box.find('ymax').text)
                coors = [xmin, ymin, xmax, ymax]
                box_class_list.append(coors)
            #get the name of the object class corresponding to the bbox
            for name in object.findall('.//name'):
                box_class_list.append(name.text)
            #append the box coors and respective name to a list
            boxes.append(box_class_list)
        # extract image dimensions
        width = int(root.find('.//size/width').text)
        height = int(root.find('.//size/height').text)
        return boxes, width, height
    

    然后使用load_mask中提取的数据如下:

    def load_mask(self, image_id):
        # get details of image
        info = self.image_info[image_id]
        # define box file location, here the annotation dir in project dir
        path = info['annotation']
        # load XML
        boxes, w, h = self.extract_boxes(path)
        # create one array for all masks, each on a different channel
        masks = zeros([h, w, len(boxes)], dtype='uint8')
        # create masks
        class_ids = list()
        for i in range(len(boxes)):
            box = boxes[i][0]
            row_s, row_e = box[1], box[3]
            col_s, col_e = box[0], box[2]
            masks[row_s:row_e, col_s:col_e, i] = 1
            class_ids.append(self.class_names.index(boxes[i][1]))
    
        return masks, asarray(class_ids, dtype='int32') 
    

    很抱歉没有写一个功能齐全的例子,但我没有多少时间。希望这可以帮助。此外,这个示例没有提取任何实际的掩码,仅供参考,只有 bbox 和类。

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2016-11-22
      • 2020-11-07
      • 2017-12-25
      • 2020-02-14
      • 1970-01-01
      • 2020-10-03
      • 2019-11-04
      • 2023-02-03
      相关资源
      最近更新 更多