Python 3.x 掩码RCNN:索引器错误:布尔索引与索引数组不匹配
我正在做一个项目,我需要就SpaceNet数据集培训Mask RCNN 因此,当我尝试训练我的模型时,会出现很多警告和错误 错误消息是:Python 3.x 掩码RCNN:索引器错误:布尔索引与索引数组不匹配,python-3.x,tensorflow,keras,deep-learning,boolean,Python 3.x,Tensorflow,Keras,Deep Learning,Boolean,我正在做一个项目,我需要就SpaceNet数据集培训Mask RCNN 因此,当我尝试训练我的模型时,会出现很多警告和错误 错误消息是: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-11-a73fb1f7
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
8学习率=配置学习率,
9个时代=10,
--->10层(头)
11
12#培训-第2阶段
~\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py in train(self、train\u数据集、val\u数据集、学习率、时代、层、增强、自定义回调、无增强源)
2372最大队列大小=100,
2373名工人=工人,
->2374使用多处理=真,
2375 )
2376 self.epoch=max(self.epoch,epoch)
包装中的~\Anaconda3\envs\MaskRCNN\lib\site packages\keras\legacy\interfaces.py(*args,**kwargs)
89 warnings.warn('Update your`'+object\u name+'`调用+
90'Keras 2 API:'+签名,堆栈级别=2)
--->91返回函数(*args,**kwargs)
92包装器._原始函数=func
93返回包装器
~\Anaconda3\envs\MaskRCNN\lib\site packages\keras\engine\training.py in fit\u generator(self、generator、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、验证频率、类权重、最大队列大小、worker、使用多处理、无序、初始历元)
1656使用多处理=使用多处理,
1657洗牌=洗牌,
->1658初始_历元=初始_历元)
1659
1660@interfaces.legacy\u生成器\u方法\u支持
~\Anaconda3\envs\MaskRCNN\lib\site packages\keras\engine\training\u generator.py in-fit\u generator(模型、生成器、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、验证频率、类权重、最大队列大小、工人、使用多处理、无序、初始历元)
179批次指数=0
180当步骤完成时<每个步骤:
-->181发电机输出=下一个(发电机输出)
182
183如果不是HASTATR(发电机输出,'''.'透镜''.'':
数据生成器中的~\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py(数据集、配置、无序、扩充、扩充、随机ROI、批量大小、检测目标、无扩充源)
1707加载图像(数据集,配置,图像id,增强=增强,
1708扩充=扩充,
->1709使用最小掩码=配置。使用最小掩码)
1710
1711#跳过没有实例的图像。在某些情况下可能会发生这种情况
加载图像gt中的~\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py(数据集、配置、图像标识、增强、增强、使用迷你掩码)
1263 _idx=np.sum(掩码,轴=(0,1))>0
1264掩码=掩码[:,:,_idx]
->1265 class_id=class_id[_idx]
1266#边界框。请注意,某些框可能全部为零
1267#如果相应的面具被裁剪掉。
索引器错误:布尔索引与维度0上的索引数组不匹配;维度为1,但相应的布尔维度为650
警告如下:
错误:根:处理图像{'id':219,'source':'yapi','path':None,'width':650,'height':650}时出错
回溯(最近一次呼叫最后一次):
文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py”,第1710行,在数据生成器中
使用\u mini\u mask=config。使用\u mini\u mask)
文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py”,第1266行,在load\u image\u gt
class_id=class_id[_idx]
索引器错误:布尔索引与维度0上的索引数组不匹配;维度为1,但相应的布尔维度为650
--
错误:根:处理图像{'id':448,'source':'yapi','path':None,'width':650,'height':650}时出错
回溯(最近一次呼叫最后一次):
文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py”,第1710行,在数据生成器中
使用\u mini\u mask=config。使用\u mini\u mask)
文件“C:\Users\MUSTAFAAKTAS\Desktop\SpaceNet\u MaskRCNN\mrcnn\model.py”,第1266行,在load\u image\u gt
class_id=class_id[_idx]
索引器错误:布尔索引与维度0上的索引数组不匹配;维度为1,但相应的布尔维度为650
--
此外,还有针对图像识别号219-348-444-448-3986-3023的警告
索引器错误:布尔索引与维度0上的索引数组不匹配;维度为1,但相应的布尔维度为650 此错误表示您正试图将1个类ID传递给需要650个类的place
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函数,它接受一个对象类数组作为类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_掩码中使用提取的数据,如下所示:
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和类