我正在做一个项目,我需要训练关于 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
警告是:
错误:根:处理图像时出错{'id':219,'源':'yapi','路径':无,'宽度':650,'高度':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 类 ID = 类 ID[_idx] IndexError:布尔索引与维度 0 上的索引数组不匹配;维度为 1 但对应的布尔维度为 650
--
错误:根:处理图像时出错{'id':448,'源':'yapi','路径':无,'宽度':650,'高度':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 类 ID = 类 ID[_idx] IndexError:布尔索引与维度 0 上的索引数组不匹配;维度为 1 但对应的布尔维度为 650
--
还有针对 image_id 的警告:219-348-444-448-3986-3023
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')
抱歉没有写一个功能齐全的示例,但我没有时间 atm。希望这可以帮助。另外,这个示例没有提取任何实际的掩码,仅供参考,只是 bbox 和类。
谢谢您,有什么解决办法吗?