mask rcnn训练 keyerror(0)

一个问题 坑了两天 解决不了

训练数据:1280*1856

# Train the head branches
# Passing layers="heads" freezes all layers except the head
# layers. You can also pass a regular expression to select
# which layers to train by name pattern.
model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE, 
            epochs=1, 
            layers='heads')

image=skimage.color.gray2rgb(image)

 

Starting at epoch 0. LR=0.001

Checkpoint Path: E:\Mask_RCNN-master-try\logs\shapes20181118T2016\mask_rcnn_shapes_{epoch:04d}.h5
Selecting layers to train
fpn_c5p5               (Conv2D)
fpn_c4p4               (Conv2D)
fpn_c3p3               (Conv2D)
fpn_c2p2               (Conv2D)
fpn_p5                 (Conv2D)
fpn_p2                 (Conv2D)
fpn_p3                 (Conv2D)
fpn_p4                 (Conv2D)
In model:  rpn_model
    rpn_conv_shared        (Conv2D)
    rpn_class_raw          (Conv2D)
    rpn_bbox_pred          (Conv2D)
mrcnn_mask_conv1       (TimeDistributed)
mrcnn_mask_bn1         (TimeDistributed)
mrcnn_mask_conv2       (TimeDistributed)
mrcnn_mask_bn2         (TimeDistributed)
mrcnn_class_conv1      (TimeDistributed)
mrcnn_class_bn1        (TimeDistributed)
mrcnn_mask_conv3       (TimeDistributed)
mrcnn_mask_bn3         (TimeDistributed)
mrcnn_class_conv2      (TimeDistributed)
mrcnn_class_bn2        (TimeDistributed)
mrcnn_mask_conv4       (TimeDistributed)
mrcnn_mask_bn4         (TimeDistributed)
mrcnn_bbox_fc          (TimeDistributed)
mrcnn_mask_deconv      (TimeDistributed)
mrcnn_class_logits     (TimeDistributed)
mrcnn_mask             (TimeDistributed)
C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\ops\gradients_impl.py:108: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
Epoch 1/1
image_id 14
ERROR:root:Error processing image {'mask_path': 'train_data/cv2_mask/7.png', 'height': 1857, 'width': 1280, 'yaml_path': 'train_data/labelme_json/7_json/info.yaml', 'path': 'train_data/pic/7.jpg', 'source': 'shapes', 'id': 14}
Traceback (most recent call last):
  File "E:\Mask_RCNN-master-try\mrcnn\model.py", line 1710, in data_generator
    use_mini_mask=config.USE_MINI_MASK)
  File "E:\Mask_RCNN-master-try\mrcnn\model.py", line 1213, in load_image_gt
    mask, class_ids = dataset.load_mask(image_id)
  File "", line 80, in load_mask
    labels = self.from_yaml_get_class(image_id)
  File "", line 13, in from_yaml_get_class
    del labels[0]
KeyError: 0
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
 in ()
      6             learning_rate=config.LEARNING_RATE,
      7             epochs=1,
----> 8             layers='heads')
      9 
     10 image=skimage.color.gray2rgb(image)

E:\Mask_RCNN-master-try\mrcnn\model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources)
   2373             max_queue_size=100,
   2374             workers=workers,
-> 2375             use_multiprocessing=True,
   2376         )
   2377         self.epoch = max(self.epoch, epochs)

C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

C:\Users\Administrator\AppData\Roaming\Python\Python35\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2143                 batch_index = 0
   2144                 while steps_done < steps_per_epoch:
-> 2145                     generator_output = next(output_generator)
   2146 
   2147                     if not hasattr(generator_output, '__len__'):

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

E:\Mask_RCNN-master-try\mrcnn\model.py in load_image_gt(dataset, config, image_id, augment, augmentation, use_mini_mask)
   1211     # Load image and mask
   1212     image = dataset.load_image(image_id)
-> 1213     mask, class_ids = dataset.load_mask(image_id)
   1214     original_shape = image.shape
   1215     image, window, scale, padding, crop = utils.resize_image(

 in load_mask(self, image_id)
     78             occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
     79         labels = []
---> 80         labels = self.from_yaml_get_class(image_id)
     81         labels_form = []
     82         for i in range(len(labels)):

 in from_yaml_get_class(self, image_id)
     11             temp = yaml.load(f.read())
     12             labels = temp['label_names']
---> 13             del labels[0]
     14         return labels
     15 

KeyError: 0

 

救命啊!

找到的原因1:json的y值大于720https://github.com/matterport/Mask_RCNN/issues/636 【IndexError: index 720 is out of bounds for axis 0 with size 720】

缩小图像 重新标注 

{
  "flags": {},
  "shapes": [
    {
      "label": "bread",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          130,
          0
        ],
      ]
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "imagePath": "0.jpg",
  "imageData": 

没用!

原因2:要标注成多边形 重新标注 没用!https://github.com/matterport/Mask_RCNN/issues/608 【KeyError: 'all_points_y'】

解决:

经过两位大佬提醒,是标签出了问题,对比可以运行的\train_data\labelme_json\0_json\info.yaml数据:

我:    跑通:

证明之前用的json to dataset不合适,导致标签读入错误,删除之前的labelme,安装labelme3.2

批量json to dataset:for /r 路径 %i in (*.json) do labelme_json_to_dataset %i【参考自:https://blog.csdn.net/qq_15969343/article/details/79840517】

感谢大佬!感谢qq网友!感谢实验室小伙伴!

你可能感兴趣的:(mask rcnn训练 keyerror(0))