掩码RCNN:IndexError:布尔索引与索引数组不匹配

时间:2019-09-12 07:15:49

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

我正在做一个项目,我需要就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:处理图像{'id':219,'source':'yapi','path':None,'width':650,'height':650}时出错   追溯(最近一次通话):     在data_generator中的文件“ C:\ Users \ MUSTAFAAKTAS \ Desktop \ SpaceNet_MaskRCNN \ mrcnn \ model.py”,行1710       use_mini_mask = config.USE_MINI_MASK)     在load_image_gt的第1266行中,文件“ C:\ Users \ MUSTAFAAKTAS \ Desktop \ SpaceNet_MaskRCNN \ mrcnn \ model.py”       class_ids = class_ids [_idx]   IndexError:布尔索引与维度0上的索引数组不匹配;维度为1,但相应的布尔维度为650

-

  

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

-

  

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

1 个答案:

答案 0 :(得分: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_id。您必须以自己的方式实现它,以便为每个蒙版都有一个对应的对象类。例如,这就是我的操作方式:

首先,我从使用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') 

很抱歉,您没有编写完整的功能示例,但我在atm的时间很少。希望这可以帮助。而且,此示例没有提取任何实际的蒙版,仅用于bboxes和类。